Skip to content

Commit

Permalink
Throw exception NotSupported Exception for UuidGenerator with doctr…
Browse files Browse the repository at this point in the history
…ine/dbal:3.x.

Generating `getGuidExpression` has been removed in doctrine/dbal:3.x.

Partially fixes #8884
  • Loading branch information
scyzoryck committed Aug 15, 2021
1 parent c4456a2 commit 6920251
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 4 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ jobs:

- name: "Run PHPUnit"
continue-on-error: true
run: "vendor/bin/phpunit -c ci/github/phpunit/sqlite.xml --coverage-clover=coverage-no-cache.xml"
run: "vendor/bin/phpunit -c ci/github/phpunit/sqlite.xml --coverage-clover=coverage-no-cache.xml --exclude-group performance,locking_functional,dbal2"
env:
ENABLE_SECOND_LEVEL_CACHE: 0

- name: "Run PHPUnit with Second Level Cache"
id: "phpunit-run-slc"
continue-on-error: true
run: "vendor/bin/phpunit -c ci/github/phpunit/sqlite.xml --exclude-group performance,non-cacheable,locking_functional --coverage-clover=coverage-cache.xml"
run: "vendor/bin/phpunit -c ci/github/phpunit/sqlite.xml --exclude-group performance,non-cacheable,locking_functional,dbal2 --coverage-clover=coverage-cache.xml"
env:
ENABLE_SECOND_LEVEL_CACHE: 1

Expand Down Expand Up @@ -93,7 +93,7 @@ jobs:
ENABLE_SECOND_LEVEL_CACHE: 0

- name: "Run PHPUnit with Second Level Cache"
run: "vendor/bin/phpunit -c ci/github/phpunit/sqlite.xml --exclude-group performance,non-cacheable,locking_functional --coverage-clover=coverage-cache.xml"
run: "vendor/bin/phpunit -c ci/github/phpunit/sqlite.xml --exclude-group performance,non-cacheable,locking_functional,dbal3 --coverage-clover=coverage-cache.xml"
env:
ENABLE_SECOND_LEVEL_CACHE: 1

Expand Down Expand Up @@ -261,7 +261,7 @@ jobs:
ENABLE_SECOND_LEVEL_CACHE: 0

- name: "Run PHPUnit with Second Level Cache"
run: "vendor/bin/phpunit -c ci/github/phpunit/${{ matrix.extension }}.xml --exclude-group performance,non-cacheable,locking_functional --coverage-clover=coverage-no-cache.xml"
run: "vendor/bin/phpunit -c ci/github/phpunit/${{ matrix.extension }}.xml --exclude-group performance,non-cacheable,locking_functional,dbal3 --coverage-clover=coverage-no-cache.xml"
env:
ENABLE_SECOND_LEVEL_CACHE: 1

Expand Down
1 change: 1 addition & 0 deletions ci/github/phpunit/mysqli.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<exclude>
<group>performance</group>
<group>locking_functional</group>
<group>dbal3</group>
</exclude>
</groups>
</phpunit>
1 change: 1 addition & 0 deletions ci/github/phpunit/pdo_mysql.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<exclude>
<group>performance</group>
<group>locking_functional</group>
<group>dbal3</group>
</exclude>
</groups>
</phpunit>
1 change: 1 addition & 0 deletions ci/github/phpunit/pdo_pgsql.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
<exclude>
<group>performance</group>
<group>locking_functional</group>
<group>dbal3</group>
</exclude>
</groups>
</phpunit>
1 change: 1 addition & 0 deletions ci/github/phpunit/sqlite.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<exclude>
<group>performance</group>
<group>locking_functional</group>
<group>dbal3</group>
</exclude>
</groups>
</phpunit>
5 changes: 5 additions & 0 deletions lib/Doctrine/ORM/Exception/NotSupported.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ public static function create(): self
{
return new self('This behaviour is (currently) not supported by Doctrine 2');
}

public static function createForDbal3(): self
{
return new self('Feature was deprecated in doctrine/dbal 2.x and is not supported by installed doctrine/dbal:3.x, please see the doctrine/deprecations logs for new alternative approaches.');
}
}
10 changes: 10 additions & 0 deletions lib/Doctrine/ORM/Id/UuidGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

namespace Doctrine\ORM\Id;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\Deprecations\Deprecation;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Exception\NotSupported;

use function method_exists;

/**
* Represents an ID generator that uses the database UUID expression
Expand All @@ -22,10 +26,16 @@ public function __construct()
'%s is deprecated with no replacement, use an application-side generator instead',
self::class
);

if (! method_exists(AbstractPlatform::class, 'getGuidExpression')) {
throw NotSupported::createForDbal3();
}
}

/**
* {@inheritDoc}
*
* @throws NotSupported
*/
public function generate(EntityManager $em, $entity)
{
Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ parameters:
ignoreErrors:
# The class was added in PHP 8.1
- '/^Attribute class ReturnTypeWillChange does not exist.$/'
#method removed in doctrine/dbal3.x
- '/^Call to an undefined method Doctrine\\DBAL\\Platforms\\AbstractPlatform::getGuidExpression\(\).$/'
1 change: 1 addition & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
<exclude>
<group>performance</group>
<group>locking_functional</group>
<group>dbal3</group>
</exclude>
</groups>

Expand Down
6 changes: 6 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,11 @@
<referencedClass name="Doctrine\Common\Cache\XcacheCache"/>
</errorLevel>
</UndefinedClass>
<UndefinedMethod>
<errorLevel type="suppress">
<!-- See https://github.com/doctrine/orm/issues/8884 -->
<referencedMethod name="Doctrine\DBAL\Platforms\AbstractPlatform::getGuidExpression"/>
</errorLevel>
</UndefinedMethod>
</issueHandlers>
</psalm>
22 changes: 22 additions & 0 deletions tests/Doctrine/Tests/ORM/Functional/UUIDGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\DBAL\Connection;
use Doctrine\Deprecations\PHPUnit\VerifyDeprecations;
use Doctrine\ORM\Exception\NotSupported;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\Tests\OrmFunctionalTestCase;

use function method_exists;
use function strlen;

/**
Expand All @@ -20,18 +23,28 @@ class UUIDGeneratorTest extends OrmFunctionalTestCase
{
use VerifyDeprecations;

/**
* @group dbal2
*/
public function testItIsDeprecated(): void
{
$this->expectDeprecationWithIdentifier('https://github.com/doctrine/orm/issues/7312');
$this->_em->getClassMetadata(UUIDEntity::class);
}

/**
* @group dbal2
*/
public function testGenerateUUID(): void
{
if ($this->_em->getConnection()->getDatabasePlatform()->getName() !== 'mysql') {
self::markTestSkipped('Currently restricted to MySQL platform.');
}

if (! method_exists(Connection::class, 'getGuidExpression')) {
self::markTestSkipped('Test valid for doctrine/dbal:2.x only.');
}

$this->_schemaTool->createSchema([
$this->_em->getClassMetadata(UUIDEntity::class),
]);
Expand All @@ -41,6 +54,15 @@ public function testGenerateUUID(): void
self::assertNotNull($entity->getId());
self::assertTrue(strlen($entity->getId()) > 0);
}

/**
* @group dbal3
*/
public function testItCannotBeInitialised(): void
{
$this->expectException(NotSupported::class);
$this->_em->getClassMetadata(UUIDEntity::class);
}
}

/**
Expand Down

0 comments on commit 6920251

Please sign in to comment.