From 77356b954ffde3b9bf6e920aaef165fcad7f74d7 Mon Sep 17 00:00:00 2001 From: acoulton Date: Thu, 11 Mar 2021 13:07:01 +0000 Subject: [PATCH] Add test coverage for passing optimistic lock version as string As discussed in #8527, when using optimistic locking with integer version columns, Doctrine has always supported passing the lock version as a string. For example when passing in a version received in POST / GET. Technically speaking this does not comply with the docs and phdoc (which show the app explicitly casting to int before passing). Nonetheless the maintainers decided it should continue to be valid for now and reinstated the old soft-equals logic with #8531. This modified test just avoids accidental changes in future. --- .../Tests/ORM/Functional/Locking/LockTest.php | 24 +++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/tests/Doctrine/Tests/ORM/Functional/Locking/LockTest.php b/tests/Doctrine/Tests/ORM/Functional/Locking/LockTest.php index 1b56794a842..f2ca0b2cc70 100644 --- a/tests/Doctrine/Tests/ORM/Functional/Locking/LockTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/Locking/LockTest.php @@ -32,8 +32,10 @@ protected function setUp(): void /** * @group DDC-178 * @group locking + * @testWith [false] + * [true] */ - public function testLockVersionedEntity(): void + public function testLockVersionedEntity(bool $useStringVersion): void { $article = new CmsArticle(); $article->text = 'my article'; @@ -42,7 +44,15 @@ public function testLockVersionedEntity(): void $this->_em->persist($article); $this->_em->flush(); - $this->_em->lock($article, LockMode::OPTIMISTIC, $article->version); + $lockVersion = $article->version; + if ($useStringVersion) { + // NOTE: Officially, the lock method (and callers) do not accept a string argument. Calling code should + // cast the version to (int) as per the docs. However, this is not currently enforced. This may change in + // a future release. + $lockVersion = (string) $lockVersion; + } + + $this->_em->lock($article, LockMode::OPTIMISTIC, $lockVersion); $this->addToAssertionCount(1); } @@ -50,8 +60,10 @@ public function testLockVersionedEntity(): void /** * @group DDC-178 * @group locking + * @testWith [false] + * [true] */ - public function testLockVersionedEntityMismatchThrowsException(): void + public function testLockVersionedEntityMismatchThrowsException(bool $useStringVersion): void { $article = new CmsArticle(); $article->text = 'my article'; @@ -61,8 +73,12 @@ public function testLockVersionedEntityMismatchThrowsException(): void $this->_em->flush(); $this->expectException(OptimisticLockException::class); + $lockVersion = $article->version + 1; + if ($useStringVersion) { + $lockVersion = (string) $lockVersion; + } - $this->_em->lock($article, LockMode::OPTIMISTIC, $article->version + 1); + $this->_em->lock($article, LockMode::OPTIMISTIC, $lockVersion); } /**