From a6d3a76898a8d90f70583be92625ae68955d7248 Mon Sep 17 00:00:00 2001 From: Mdewet Date: Thu, 29 Nov 2018 18:32:57 +0100 Subject: [PATCH 1/8] Add tests for custom guessers --- features/extension.feature | 4 ++-- tests/app/Entity/Beer.php | 7 +++--- tests/app/Populator/Guesser/GuidGuesser.php | 23 +++++++++++++++++++ tests/app/behat.yml.dist | 2 ++ .../app/features/bootstrap/FeatureContext.php | 6 ++++- 5 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 tests/app/Populator/Guesser/GuidGuesser.php diff --git a/features/extension.feature b/features/extension.feature index ad60e6d..344c5c2 100644 --- a/features/extension.feature +++ b/features/extension.feature @@ -24,7 +24,7 @@ Feature: I can execute Behat tests with current API Extension }, "id": { "type": [ - "integer" + "string" ] }, "company": { @@ -194,7 +194,7 @@ Feature: I can execute Behat tests with current API Extension }, "id": { "type": [ - "integer" + "string" ] }, "name": { diff --git a/tests/app/Entity/Beer.php b/tests/app/Entity/Beer.php index 711cf6a..293f55e 100644 --- a/tests/app/Entity/Beer.php +++ b/tests/app/Entity/Beer.php @@ -39,10 +39,9 @@ class Beer { /** - * @var int * @ORM\Id - * @ORM\Column(type="integer") - * @ORM\GeneratedValue(strategy="AUTO") + * @ORM\Column(type="guid") + * @ORM\GeneratedValue(strategy="UUID") * @Groups({"beer_list_read", "beer_read"}) */ private $id; @@ -159,7 +158,7 @@ public function __construct() $this->images = new ArrayCollection(); } - public function getId(): int + public function getId(): ?string { return $this->id; } diff --git a/tests/app/Populator/Guesser/GuidGuesser.php b/tests/app/Populator/Guesser/GuidGuesser.php new file mode 100644 index 0000000..e35cced --- /dev/null +++ b/tests/app/Populator/Guesser/GuidGuesser.php @@ -0,0 +1,23 @@ + + */ +class GuidGuesser extends AbstractGuesser +{ + public function supports(array $mapping): bool + { + return \in_array($mapping['type'], [Type::GUID], true); + } + public function getValue(array $mapping): string + { + return $this->faker->uuid; + } +} diff --git a/tests/app/behat.yml.dist b/tests/app/behat.yml.dist index c78dc69..a680b08 100644 --- a/tests/app/behat.yml.dist +++ b/tests/app/behat.yml.dist @@ -23,6 +23,8 @@ default: symfony2: ~ Behatch\Extension: ~ ApiExtension: + guessers: + - ApiExtension\App\Populator\Guesser\GuidGuesser services: metadataFactory: '@test.api_platform.metadata.resource.metadata_factory.annotation' iriConverter: '@test.api_platform.iri_converter' diff --git a/tests/app/features/bootstrap/FeatureContext.php b/tests/app/features/bootstrap/FeatureContext.php index d77992f..32c9161 100644 --- a/tests/app/features/bootstrap/FeatureContext.php +++ b/tests/app/features/bootstrap/FeatureContext.php @@ -50,9 +50,13 @@ public function initDatabase() $schema = new SchemaTool($manager); $schema->createSchema($classes); } else { + $purger = new ORMPurger($manager); - $purger->setPurgeMode(ORMPurger::PURGE_MODE_TRUNCATE); + $purger->setPurgeMode(ORMPurger::PURGE_MODE_DELETE); $purger->purge(); + $classes = $manager->getMetadataFactory()->getAllMetadata(); + $schema = new SchemaTool($manager); + $schema->createSchema($classes); } } } From be6f84d9fed143db1f960cd72052ef0baf15287d Mon Sep 17 00:00:00 2001 From: Mdewet Date: Thu, 29 Nov 2018 19:30:48 +0100 Subject: [PATCH 2/8] remove serialization groups for user id --- tests/app/Entity/Beer.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/app/Entity/Beer.php b/tests/app/Entity/Beer.php index 293f55e..98dd858 100644 --- a/tests/app/Entity/Beer.php +++ b/tests/app/Entity/Beer.php @@ -42,7 +42,6 @@ class Beer * @ORM\Id * @ORM\Column(type="guid") * @ORM\GeneratedValue(strategy="UUID") - * @Groups({"beer_list_read", "beer_read"}) */ private $id; From d601c894f9fe8fb23baa4cd9d65c5b0b1175c5fe Mon Sep 17 00:00:00 2001 From: Mdewet Date: Sun, 2 Dec 2018 12:38:44 +0100 Subject: [PATCH 3/8] Remove GuidGuesser add Ean13Guesser --- features/extension.feature | 4 +-- tests/app/Entity/Beer.php | 33 ++++++++++++++++-- tests/app/Kernel.php | 1 + .../{GuidGuesser.php => Ean13Guesser.php} | 7 ++-- tests/app/Type/EanType.php | 34 +++++++++++++++++++ tests/app/behat.yml.dist | 2 +- tests/app/doctrine.yaml | 4 +++ .../app/features/bootstrap/FeatureContext.php | 12 +++---- 8 files changed, 82 insertions(+), 15 deletions(-) rename tests/app/Populator/Guesser/{GuidGuesser.php => Ean13Guesser.php} (66%) create mode 100644 tests/app/Type/EanType.php create mode 100644 tests/app/doctrine.yaml diff --git a/features/extension.feature b/features/extension.feature index 344c5c2..ad60e6d 100644 --- a/features/extension.feature +++ b/features/extension.feature @@ -24,7 +24,7 @@ Feature: I can execute Behat tests with current API Extension }, "id": { "type": [ - "string" + "integer" ] }, "company": { @@ -194,7 +194,7 @@ Feature: I can execute Behat tests with current API Extension }, "id": { "type": [ - "string" + "integer" ] }, "name": { diff --git a/tests/app/Entity/Beer.php b/tests/app/Entity/Beer.php index 98dd858..b38f8fc 100644 --- a/tests/app/Entity/Beer.php +++ b/tests/app/Entity/Beer.php @@ -39,9 +39,11 @@ class Beer { /** + * @var int * @ORM\Id - * @ORM\Column(type="guid") - * @ORM\GeneratedValue(strategy="UUID") + * @ORM\Column(type="integer") + * @ORM\GeneratedValue(strategy="AUTO") + * @Groups({"beer_list_read", "beer_read"}) */ private $id; @@ -54,6 +56,14 @@ class Beer */ private $company; + /** + * @var string + * @ORM\Column(type="ean") + * @Groups({"beer_read", "beer_write"}) + * @Assert\NotNull + */ + private $ean13; + /** * @var string * @ORM\Column @@ -157,7 +167,7 @@ public function __construct() $this->images = new ArrayCollection(); } - public function getId(): ?string + public function getId(): int { return $this->id; } @@ -332,4 +342,21 @@ public function setMisc($misc = null) { $this->misc = $misc; } + + /** + * @return string + */ + public function getEan13(): string + { + return $this->ean13; + } + + /** + * @param string $ean13 + */ + public function setEan13(string $ean13): void + { + $this->ean13 = $ean13; + } + } diff --git a/tests/app/Kernel.php b/tests/app/Kernel.php index 08b7728..c4ff074 100644 --- a/tests/app/Kernel.php +++ b/tests/app/Kernel.php @@ -13,6 +13,7 @@ namespace ApiExtension\App; +use ApiExtension\ServiceContainer\ApiExtension; use ApiPlatform\Core\Bridge\Symfony\Bundle\ApiPlatformBundle; use Doctrine\Bundle\DoctrineBundle\DoctrineBundle; use Symfony\Bundle\FrameworkBundle\FrameworkBundle; diff --git a/tests/app/Populator/Guesser/GuidGuesser.php b/tests/app/Populator/Guesser/Ean13Guesser.php similarity index 66% rename from tests/app/Populator/Guesser/GuidGuesser.php rename to tests/app/Populator/Guesser/Ean13Guesser.php index e35cced..a5c0010 100644 --- a/tests/app/Populator/Guesser/GuidGuesser.php +++ b/tests/app/Populator/Guesser/Ean13Guesser.php @@ -5,19 +5,20 @@ namespace ApiExtension\App\Populator\Guesser; use Doctrine\DBAL\Types\Type; +use ApiExtension\App\Type\EanType; use ApiExtension\Populator\Guesser\AbstractGuesser; /** * @author Mathieu Dewet */ -class GuidGuesser extends AbstractGuesser +class Ean13Guesser extends AbstractGuesser { public function supports(array $mapping): bool { - return \in_array($mapping['type'], [Type::GUID], true); + return \in_array($mapping['type'], [EanType::EAN], true); } public function getValue(array $mapping): string { - return $this->faker->uuid; + return $this->faker->ean13; } } diff --git a/tests/app/Type/EanType.php b/tests/app/Type/EanType.php new file mode 100644 index 0000000..a1371c9 --- /dev/null +++ b/tests/app/Type/EanType.php @@ -0,0 +1,34 @@ + */ @@ -28,6 +29,9 @@ public function __construct(ManagerRegistry $doctrine, string $cacheDir) { $this->doctrine = $doctrine; $this->cacheDir = $cacheDir; + if (!Type::hasType(EanType::EAN)) { + Type::addType('ean', EanType::class); + } } /** @@ -50,13 +54,9 @@ public function initDatabase() $schema = new SchemaTool($manager); $schema->createSchema($classes); } else { - $purger = new ORMPurger($manager); - $purger->setPurgeMode(ORMPurger::PURGE_MODE_DELETE); + $purger->setPurgeMode(ORMPurger::PURGE_MODE_TRUNCATE); $purger->purge(); - $classes = $manager->getMetadataFactory()->getAllMetadata(); - $schema = new SchemaTool($manager); - $schema->createSchema($classes); } } } From c9560cc7509f0298abd48d76382388a6846ace3c Mon Sep 17 00:00:00 2001 From: Mdewet Date: Mon, 3 Dec 2018 14:43:01 +0100 Subject: [PATCH 4/8] fix behat --- features/extension.feature | 5 +++++ tests/app/Kernel.php | 1 - tests/app/Type/EanType.php | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/features/extension.feature b/features/extension.feature index ad60e6d..e49cab8 100644 --- a/features/extension.feature +++ b/features/extension.feature @@ -55,6 +55,11 @@ Feature: I can execute Behat tests with current API Extension "string" ] }, + "ean13": { + "type": [ + "string" + ] + }, "type": { "type": [ "string", diff --git a/tests/app/Kernel.php b/tests/app/Kernel.php index c4ff074..08b7728 100644 --- a/tests/app/Kernel.php +++ b/tests/app/Kernel.php @@ -13,7 +13,6 @@ namespace ApiExtension\App; -use ApiExtension\ServiceContainer\ApiExtension; use ApiPlatform\Core\Bridge\Symfony\Bundle\ApiPlatformBundle; use Doctrine\Bundle\DoctrineBundle\DoctrineBundle; use Symfony\Bundle\FrameworkBundle\FrameworkBundle; diff --git a/tests/app/Type/EanType.php b/tests/app/Type/EanType.php index a1371c9..8999df1 100644 --- a/tests/app/Type/EanType.php +++ b/tests/app/Type/EanType.php @@ -31,4 +31,4 @@ public function getName() { return self::EAN; } -} \ No newline at end of file +} From 2b1802e9805650a5deda62a33091368ec43387b2 Mon Sep 17 00:00:00 2001 From: Mdewet Date: Mon, 3 Dec 2018 14:47:18 +0100 Subject: [PATCH 5/8] fix behat --- features/extension.feature | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/features/extension.feature b/features/extension.feature index e49cab8..f135d85 100644 --- a/features/extension.feature +++ b/features/extension.feature @@ -50,12 +50,12 @@ Feature: I can execute Behat tests with current API Extension "name" ] }, - "name": { + "ean13": { "type": [ "string" ] }, - "ean13": { + "name": { "type": [ "string" ] From 509f9e4e5dd90f8582b4ce541ddbd2eb02c3243e Mon Sep 17 00:00:00 2001 From: Mdewet Date: Mon, 3 Dec 2018 14:53:36 +0100 Subject: [PATCH 6/8] fix behat --- features/extension.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/extension.feature b/features/extension.feature index f135d85..4c555c1 100644 --- a/features/extension.feature +++ b/features/extension.feature @@ -52,7 +52,7 @@ Feature: I can execute Behat tests with current API Extension }, "ean13": { "type": [ - "string" + "any" ] }, "name": { From 9364bd090b7d2bad0cb7a99f3bfe1b4990cea04e Mon Sep 17 00:00:00 2001 From: Mdewet Date: Mon, 3 Dec 2018 15:00:03 +0100 Subject: [PATCH 7/8] fix behat --- features/extension.feature | 1 + 1 file changed, 1 insertion(+) diff --git a/features/extension.feature b/features/extension.feature index 4c555c1..86e14a7 100644 --- a/features/extension.feature +++ b/features/extension.feature @@ -151,6 +151,7 @@ Feature: I can execute Behat tests with current API Extension "@type", "id", "company", + "ean13", "name", "active", "price", From 78ff187c9a3a19a6c83ddbf35d75d1b1c5de5983 Mon Sep 17 00:00:00 2001 From: Mdewet Date: Mon, 3 Dec 2018 15:06:56 +0100 Subject: [PATCH 8/8] fix behat --- composer.json | 2 +- features/bootstrap/FeatureContext.php | 1 + features/extension.feature | 4 +--- tests/app/Entity/Beer.php | 4 ++-- tests/app/Kernel.php | 6 ++++++ tests/app/Type/EanType.php | 6 ++++-- tests/app/doctrine.yaml | 4 ---- 7 files changed, 15 insertions(+), 12 deletions(-) delete mode 100644 tests/app/doctrine.yaml diff --git a/composer.json b/composer.json index 21f45cc..2beae22 100644 --- a/composer.json +++ b/composer.json @@ -51,7 +51,7 @@ "doctrine/doctrine-bundle": "^1.8", "friendsofphp/php-cs-fixer": "^2.10", "phpstan/phpstan": "^0.9", - "phpunit/phpunit": "^4.5 || ^5.0 || ^6.0 || ^7.0", + "phpunit/phpunit": "^5.7.10 || ^6.0 || ^7.0", "symfony/asset": "^3.4 || ^4.0", "symfony/dom-crawler": "^3.0", "symfony/framework-bundle": "^3.4 || ^4.0", diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php index b513772..58761e1 100755 --- a/features/bootstrap/FeatureContext.php +++ b/features/bootstrap/FeatureContext.php @@ -148,6 +148,7 @@ public function theJsonOutputShouldBeEqualTo(PyStringNode $text) throw new \LogicException('Invalid JSON.'); } $diff = array_map('unserialize', array_diff(array_map('serialize', $output), array_map('serialize', $json))); + if (0 < count($diff)) { throw new \LogicException("JSON response does not match:\n".print_r($diff, true)); } diff --git a/features/extension.feature b/features/extension.feature index 86e14a7..0aeeb2a 100644 --- a/features/extension.feature +++ b/features/extension.feature @@ -51,9 +51,7 @@ Feature: I can execute Behat tests with current API Extension ] }, "ean13": { - "type": [ - "any" - ] + "type": "any" }, "name": { "type": [ diff --git a/tests/app/Entity/Beer.php b/tests/app/Entity/Beer.php index b38f8fc..9fc4e1d 100644 --- a/tests/app/Entity/Beer.php +++ b/tests/app/Entity/Beer.php @@ -60,7 +60,7 @@ class Beer * @var string * @ORM\Column(type="ean") * @Groups({"beer_read", "beer_write"}) - * @Assert\NotNull + * @Assert\NotBlank */ private $ean13; @@ -354,7 +354,7 @@ public function getEan13(): string /** * @param string $ean13 */ - public function setEan13(string $ean13): void + public function setEan13(string $ean13) { $this->ean13 = $ean13; } diff --git a/tests/app/Kernel.php b/tests/app/Kernel.php index 08b7728..7b18384 100644 --- a/tests/app/Kernel.php +++ b/tests/app/Kernel.php @@ -61,6 +61,12 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load 'driver' => 'pdo_sqlite', 'path' => '%kernel.cache_dir%/db.sqlite', 'charset' => 'UTF8', + 'mapping_types' => [ + 'ean' => 'string', + ], + 'types' => [ + 'ean' => 'ApiExtension\App\Type\EanType', + ], ], 'orm' => [ 'auto_generate_proxy_classes' => true, diff --git a/tests/app/Type/EanType.php b/tests/app/Type/EanType.php index 8999df1..224758c 100644 --- a/tests/app/Type/EanType.php +++ b/tests/app/Type/EanType.php @@ -6,7 +6,7 @@ use Doctrine\DBAL\Platforms\AbstractPlatform; /** - * My custom datatype. + * My custom ean type. */ class EanType extends Type { @@ -14,7 +14,9 @@ class EanType extends Type public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) { - return self::EAN; + $fieldDeclaration['length'] = 13; + + return $platform->getVarcharTypeDeclarationSQL($fieldDeclaration); } public function convertToPHPValue($value, AbstractPlatform $platform) diff --git a/tests/app/doctrine.yaml b/tests/app/doctrine.yaml deleted file mode 100644 index bdfbb4a..0000000 --- a/tests/app/doctrine.yaml +++ /dev/null @@ -1,4 +0,0 @@ -doctrine: - dbal: - types: - ean: ApiExtension\App\Type\EanType