From 386d4ba34341b6332b4696885e9be1ded2e0d728 Mon Sep 17 00:00:00 2001 From: Vincent Chalamon Date: Mon, 19 Apr 2021 15:30:08 +0200 Subject: [PATCH] Add tests for Symfony Uuid --- .github/workflows/ci.yml | 9 ++- CHANGELOG.md | 1 + composer.json | 8 +-- features/main/uuid.feature | 11 ++++ tests/Behat/DoctrineContext.php | 13 ++++ .../TestBundle/Entity/SymfonyUuidDummy.php | 65 +++++++++++++++++++ tests/Fixtures/app/AppKernel.php | 7 ++ .../app/config/config_symfony_uid.yml | 6 ++ 8 files changed, 114 insertions(+), 6 deletions(-) create mode 100644 tests/Fixtures/TestBundle/Entity/SymfonyUuidDummy.php create mode 100644 tests/Fixtures/app/config/config_symfony_uid.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d339b3301f8..b2be7812e4f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -252,7 +252,11 @@ jobs: if [ "$COVERAGE" = '1' ]; then vendor/bin/behat --out=std --format=progress --format=junit --out=build/logs/behat/junit --profile=default-coverage --no-interaction else - vendor/bin/behat --out=std --format=progress --format=junit --out=build/logs/behat/junit --profile=default --no-interaction + if [ "${{ matrix.php }}" = '7.1' ]; then + vendor/bin/behat --out=std --format=progress --format=junit --out=build/logs/behat/junit --profile=default --no-interaction --tags='~@symfony/uid' + else + vendor/bin/behat --out=std --format=progress --format=junit --out=build/logs/behat/junit --profile=default --no-interaction + fi fi - name: Merge code coverage reports if: matrix.coverage @@ -384,7 +388,8 @@ jobs: - name: Clear test app cache run: tests/Fixtures/app/console cache:clear --ansi - name: Run Behat tests - run: vendor/bin/behat --out=std --format=progress --profile=default --no-interaction + # @TODO remove the tag "@symfony/uid" in 3.0 + run: vendor/bin/behat --out=std --format=progress --profile=default --no-interaction --tags='~@symfony/uid' postgresql: name: Behat (PHP ${{ matrix.php }}) (PostgreSQL) diff --git a/CHANGELOG.md b/CHANGELOG.md index 647cbd39a1c..9f9d6e27b47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ * Filter validation: Fix issue in Required filter validator with dot notation (#4221) * OpenAPI: Fix notice/warning for `response` without `content` in the `openapi_context` (#4210) * Serializer: Convert internal error to HTTP 400 in Ramsey uuid denormalization from invalid body string (#4200) +* Symfony: Add tests with Symfony Uuid (#4230) ## 2.6.4 diff --git a/composer.json b/composer.json index 522df6edea5..97df41a39bf 100644 --- a/composer.json +++ b/composer.json @@ -29,10 +29,6 @@ "require-dev": { "behat/behat": "^3.1", "behat/mink": "^1.7", - "friends-of-behat/mink-browserkit-driver": "^1.3.1", - "friends-of-behat/mink-extension": "^2.2", - "friends-of-behat/symfony-extension": "^2.1", - "soyuka/contexts": "^3.3.1", "doctrine/annotations": "^1.7", "doctrine/common": "^2.11 || ^3.0", "doctrine/data-fixtures": "^1.2.2", @@ -41,6 +37,9 @@ "doctrine/mongodb-odm-bundle": "^4.0", "doctrine/orm": "^2.6.4 || ^3.0", "elasticsearch/elasticsearch": "^6.0 || ^7.0", + "friends-of-behat/mink-browserkit-driver": "^1.3.1", + "friends-of-behat/mink-extension": "^2.2", + "friends-of-behat/symfony-extension": "^2.1", "guzzlehttp/guzzle": "^6.0 || ^7.0", "jangregor/phpstan-prophecy": "^0.8", "justinrainbow/json-schema": "^5.2.1", @@ -54,6 +53,7 @@ "psr/log": "^1.0", "ramsey/uuid": "^3.7 || ^4.0", "ramsey/uuid-doctrine": "^1.4", + "soyuka/contexts": "^3.3.1", "soyuka/stubs-mongodb": "^1.0", "symfony/asset": "^3.4 || ^4.4 || ^5.1", "symfony/browser-kit": "^4.4 || ^5.1", diff --git a/features/main/uuid.feature b/features/main/uuid.feature index a2b0b3024db..ada016ad9a9 100644 --- a/features/main/uuid.feature +++ b/features/main/uuid.feature @@ -194,3 +194,14 @@ Feature: Using uuid identifier on resource Then the response status code should be 400 And the response should be in JSON And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8" + + # @TODO remove the tag "@symfony/uid" in 3.0 + @symfony/uid + @!mongodb + @createSchema + Scenario: Retrieve a resource identified by Symfony\Component\Uid\Uuid + Given there is a Symfony dummy identified resource with uuid "cdf8f706-ebe3-4fb6-b0bd-ae7b48028f24" + When I send a "GET" request to "/symfony_uuid_dummies/cdf8f706-ebe3-4fb6-b0bd-ae7b48028f24" + Then the response status code should be 200 + And the response should be in JSON + And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8" diff --git a/tests/Behat/DoctrineContext.php b/tests/Behat/DoctrineContext.php index 0f03df48c7f..4625095f016 100644 --- a/tests/Behat/DoctrineContext.php +++ b/tests/Behat/DoctrineContext.php @@ -154,6 +154,7 @@ use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\SecuredDummy; use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Site; use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\SoMany; +use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\SymfonyUuidDummy; use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Taxon; use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\ThirdLevel; use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\UrlEncodedId; @@ -169,6 +170,7 @@ use Doctrine\Persistence\ObjectManager; use Ramsey\Uuid\Uuid; use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; +use Symfony\Component\Uid\Uuid as SymfonyUuid; /** * Defines application features from the specific context. @@ -1372,6 +1374,17 @@ public function thereIsARamseyIdentifiedResource(string $uuid) $this->manager->flush(); } + /** + * @Given there is a Symfony dummy identified resource with uuid :uuid + */ + public function thereIsASymfonyDummyIdentifiedResource(string $uuid) + { + $dummy = new SymfonyUuidDummy(SymfonyUuid::fromString($uuid)); + + $this->manager->persist($dummy); + $this->manager->flush(); + } + /** * @Given there is a dummy object with a fourth level relation */ diff --git a/tests/Fixtures/TestBundle/Entity/SymfonyUuidDummy.php b/tests/Fixtures/TestBundle/Entity/SymfonyUuidDummy.php new file mode 100644 index 00000000000..98b5f0da7dd --- /dev/null +++ b/tests/Fixtures/TestBundle/Entity/SymfonyUuidDummy.php @@ -0,0 +1,65 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity; + +use ApiPlatform\Core\Annotation\ApiResource; +use Doctrine\ORM\Mapping as ORM; +use Symfony\Bridge\Doctrine\Types\UuidType; +use Symfony\Component\Uid\Uuid; + +/* @TODO remove this check in 3.0 */ +if (\PHP_VERSION_ID >= 70200 && class_exists(Uuid::class) && class_exists(UuidType::class)) { + /** + * @ORM\Entity + * @ApiResource + * + * @author Vincent Chalamon + */ + class SymfonyUuidDummy + { + /** + * @ORM\Id + * @ORM\Column(type="symfony_uuid", unique=true) + * @ORM\GeneratedValue(strategy="NONE") + */ + private $id; + + /** + * @ORM\Column(nullable=true) + */ + private $number; + + public function __construct($id = null) + { + $this->id = $id ?? Uuid::v4(); + } + + public function getId(): Uuid + { + return $this->id; + } + + public function getNumber(): ?string + { + return $this->number; + } + + public function setNumber(?string $number): self + { + $this->number = $number; + + return $this; + } + } +} diff --git a/tests/Fixtures/app/AppKernel.php b/tests/Fixtures/app/AppKernel.php index 128d952fd6a..1fe07564e33 100644 --- a/tests/Fixtures/app/AppKernel.php +++ b/tests/Fixtures/app/AppKernel.php @@ -20,6 +20,7 @@ use Doctrine\Common\Inflector\Inflector; use FriendsOfBehat\SymfonyExtension\Bundle\FriendsOfBehatSymfonyExtensionBundle; use Nelmio\ApiDocBundle\NelmioApiDocBundle; +use Symfony\Bridge\Doctrine\Types\UuidType; use Symfony\Bundle\FrameworkBundle\FrameworkBundle; use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; use Symfony\Bundle\MercureBundle\MercureBundle; @@ -35,6 +36,7 @@ use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator; use Symfony\Component\Routing\RouteCollectionBuilder; use Symfony\Component\Security\Core\User\UserInterface; +use Symfony\Component\Uid\Uuid; /** * AppKernel for tests. @@ -110,6 +112,11 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load $loader->load(__DIR__."/config/config_{$this->getEnvironment()}.yml"); + /* @TODO remove this check in 3.0 */ + if (\PHP_VERSION_ID >= 70200 && class_exists(Uuid::class) && class_exists(UuidType::class)) { + $loader->load(__DIR__.'/config/config_symfony_uid.yml'); + } + $c->prependExtensionConfig('framework', [ 'secret' => 'dunglas.fr', 'validation' => ['enable_annotations' => true], diff --git a/tests/Fixtures/app/config/config_symfony_uid.yml b/tests/Fixtures/app/config/config_symfony_uid.yml new file mode 100644 index 00000000000..08e71b1de12 --- /dev/null +++ b/tests/Fixtures/app/config/config_symfony_uid.yml @@ -0,0 +1,6 @@ +# Add custom configuration for PHP > 7.1 +# @TODO in 3.0, merge this configuration in config_common.yml, config_mysql.yml, config_postgres.yml, config_sqlite.yml +doctrine: + dbal: + types: + symfony_uuid: Symfony\Bridge\Doctrine\Types\UuidType