LockTest.php 4.45 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Doctrine\Tests\ORM\Functional\Locking;

use Doctrine\Tests\Models\CMS\CmsArticle,
    Doctrine\Tests\Models\CMS\CmsUser,
7
    Doctrine\DBAL\LockMode,
8
    Doctrine\ORM\EntityManager;
9 10 11

require_once __DIR__ . '/../../../TestInit.php';

12 13
class LockTest extends \Doctrine\Tests\OrmFunctionalTestCase {
    protected function setUp() {
14 15
        $this->useModelSet('cms');
        parent::setUp();
16
        $this->handles = array();
17 18 19 20 21 22
    }

    /**
     * @group DDC-178
     * @group locking
     */
23
    public function testLockVersionedEntity() {
24 25 26 27 28 29 30 31 32 33 34 35 36 37
        $article = new CmsArticle();
        $article->text = "my article";
        $article->topic = "Hello";

        $this->_em->persist($article);
        $this->_em->flush();

        $this->_em->lock($article, LockMode::OPTIMISTIC, $article->version);
    }

    /**
     * @group DDC-178
     * @group locking
     */
38
    public function testLockVersionedEntity_MissmatchThrowsException() {
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
        $article = new CmsArticle();
        $article->text = "my article";
        $article->topic = "Hello";

        $this->_em->persist($article);
        $this->_em->flush();

        $this->setExpectedException('Doctrine\ORM\OptimisticLockException');
        $this->_em->lock($article, LockMode::OPTIMISTIC, $article->version + 1);
    }

    /**
     * @group DDC-178
     * @group locking
     */
54
    public function testLockUnversionedEntity_ThrowsException() {
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
        $user = new CmsUser();
        $user->name = "foo";
        $user->status = "active";
        $user->username = "foo";

        $this->_em->persist($user);
        $this->_em->flush();

        $this->setExpectedException('Doctrine\ORM\OptimisticLockException');
        $this->_em->lock($user, LockMode::OPTIMISTIC);
    }

    /**
     * @group DDC-178
     * @group locking
     */
71
    public function testLockUnmanagedEntity_ThrowsException() {
72 73 74 75 76 77 78 79 80 81
        $article = new CmsArticle();

        $this->setExpectedException('InvalidArgumentException', 'Entity is not MANAGED.');
        $this->_em->lock($article, LockMode::OPTIMISTIC, $article->version + 1);
    }

    /**
     * @group DDC-178
     * @group locking
     */
82
    public function testLockPessimisticRead_NoTransaction_ThrowsException() {
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
        $article = new CmsArticle();
        $article->text = "my article";
        $article->topic = "Hello";

        $this->_em->persist($article);
        $this->_em->flush();

        $this->setExpectedException('Doctrine\ORM\TransactionRequiredException');
        $this->_em->lock($article, LockMode::PESSIMISTIC_READ);
    }

    /**
     * @group DDC-178
     * @group locking
     */
98
    public function testLockPessimisticWrite_NoTransaction_ThrowsException() {
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
        $article = new CmsArticle();
        $article->text = "my article";
        $article->topic = "Hello";

        $this->_em->persist($article);
        $this->_em->flush();

        $this->setExpectedException('Doctrine\ORM\TransactionRequiredException');
        $this->_em->lock($article, LockMode::PESSIMISTIC_WRITE);
    }

    /**
     * @group DDC-178
     * @group locking
     */
114
    public function testLockPessimisticWrite() {
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
        $writeLockSql = $this->_em->getConnection()->getDatabasePlatform()->getWriteLockSql();
        if (strlen($writeLockSql) == 0) {
            $this->markTestSkipped('Database Driver has no Write Lock support.');
        }

        $article = new CmsArticle();
        $article->text = "my article";
        $article->topic = "Hello";

        $this->_em->persist($article);
        $this->_em->flush();

        $this->_em->beginTransaction();
        $this->_em->lock($article, LockMode::PESSIMISTIC_WRITE);

        $query = array_pop( $this->_sqlLoggerStack->queries );
        $this->assertContains($writeLockSql, $query['sql']);
    }

    /**
     * @group DDC-178
     * @group locking
     */
138
    public function testLockPessimisticRead() {
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
        $readLockSql = $this->_em->getConnection()->getDatabasePlatform()->getReadLockSql();
        if (strlen($readLockSql) == 0) {
            $this->markTestSkipped('Database Driver has no Write Lock support.');
        }

        $article = new CmsArticle();
        $article->text = "my article";
        $article->topic = "Hello";

        $this->_em->persist($article);
        $this->_em->flush();

        $this->_em->beginTransaction();
        $this->_em->lock($article, LockMode::PESSIMISTIC_READ);

        $query = array_pop( $this->_sqlLoggerStack->queries );
        $this->assertContains($readLockSql, $query['sql']);
    }
}