From 4b2af33a71a09f66ac87a16c03dbe05b5e482609 Mon Sep 17 00:00:00 2001 From: Alan Poulain Date: Sat, 1 Jun 2019 17:22:37 +0200 Subject: [PATCH] Fix Behat tests --- features/bootstrap/HydraContext.php | 2 +- features/graphql/mutation.feature | 2 +- features/hydra/docs.feature | 2 - features/jsonld/context.feature | 1 - .../PropertyInfo/DoctrineExtractor.php | 45 ++++++++++++++++--- .../PropertyInfo/DoctrineExtractorTest.php | 10 +++++ .../Fixtures/DoctrineGeneratedValue.php | 37 +++++++++++++++ 7 files changed, 87 insertions(+), 12 deletions(-) create mode 100644 tests/Bridge/Doctrine/MongoDbOdm/PropertyInfo/Fixtures/DoctrineGeneratedValue.php diff --git a/features/bootstrap/HydraContext.php b/features/bootstrap/HydraContext.php index 84edc9ff52b..9b995f7c359 100644 --- a/features/bootstrap/HydraContext.php +++ b/features/bootstrap/HydraContext.php @@ -230,7 +230,7 @@ private function getPropertyInfo(string $propertyName, string $className): stdCl } } - throw new \InvalidArgumentException(sprintf('Property "%s" of class "%s" does\'nt exist', $propertyName, $className)); + throw new \InvalidArgumentException(sprintf('Property "%s" of class "%s" doesn\'t exist', $propertyName, $className)); } /** diff --git a/features/graphql/mutation.feature b/features/graphql/mutation.feature index 449cf421fba..10036262caf 100644 --- a/features/graphql/mutation.feature +++ b/features/graphql/mutation.feature @@ -324,7 +324,7 @@ Feature: GraphQL mutation support When I send the following GraphQL request: """ mutation { - createDummy(input: {_id: 12, name: "", foo: [], clientMutationId: "myId"}) { + createDummy(input: {name: "", foo: [], clientMutationId: "myId"}) { clientMutationId } } diff --git a/features/hydra/docs.feature b/features/hydra/docs.feature index e77d86b4769..9b8fa98f57c 100644 --- a/features/hydra/docs.feature +++ b/features/hydra/docs.feature @@ -56,8 +56,6 @@ Feature: Documentation support And the value of the node "hydra:title" of the Hydra class "Dummy" is "Dummy" And the value of the node "hydra:description" of the Hydra class "Dummy" is "Dummy." # Properties - And "id" property is readable for Hydra class "Dummy" - And "id" property is writable for Hydra class "Dummy" And "name" property is readable for Hydra class "Dummy" And "name" property is writable for Hydra class "Dummy" And "name" property is required for Hydra class "Dummy" diff --git a/features/jsonld/context.feature b/features/jsonld/context.feature index 42bacdca557..45f324a742f 100644 --- a/features/jsonld/context.feature +++ b/features/jsonld/context.feature @@ -41,7 +41,6 @@ Feature: JSON-LD contexts generation "jsonData": "Dummy/jsonData", "arrayData": "Dummy/arrayData", "nameConverted": "Dummy/nameConverted", - "id": "Dummy/id", "name": "http://schema.org/name", "alias": "https://schema.org/alternateName", "foo": "Dummy/foo" diff --git a/src/Bridge/Doctrine/MongoDbOdm/PropertyInfo/DoctrineExtractor.php b/src/Bridge/Doctrine/MongoDbOdm/PropertyInfo/DoctrineExtractor.php index e9fdac022a3..2d37e04c560 100644 --- a/src/Bridge/Doctrine/MongoDbOdm/PropertyInfo/DoctrineExtractor.php +++ b/src/Bridge/Doctrine/MongoDbOdm/PropertyInfo/DoctrineExtractor.php @@ -16,7 +16,9 @@ use Doctrine\Common\Collections\Collection; use Doctrine\Common\Persistence\Mapping\MappingException; use Doctrine\Common\Persistence\ObjectManager; +use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; use Doctrine\ODM\MongoDB\Types\Type as MongoDbType; +use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface; use Symfony\Component\PropertyInfo\PropertyListExtractorInterface; use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface; use Symfony\Component\PropertyInfo\Type; @@ -29,7 +31,7 @@ * @author Kévin Dunglas * @author Alan Poulain */ -final class DoctrineExtractor implements PropertyListExtractorInterface, PropertyTypeExtractorInterface +final class DoctrineExtractor implements PropertyListExtractorInterface, PropertyTypeExtractorInterface, PropertyAccessExtractorInterface { private $objectManager; @@ -43,9 +45,7 @@ public function __construct(ObjectManager $objectManager) */ public function getProperties($class, array $context = []) { - try { - $metadata = $this->objectManager->getClassMetadata($class); - } catch (MappingException $exception) { + if (null === $metadata = $this->getMetadata($class)) { return null; } @@ -57,9 +57,7 @@ public function getProperties($class, array $context = []) */ public function getTypes($class, $property, array $context = []) { - try { - $metadata = $this->objectManager->getClassMetadata($class); - } catch (MappingException $exception) { + if (null === $metadata = $this->getMetadata($class)) { return null; } @@ -111,6 +109,39 @@ public function getTypes($class, $property, array $context = []) } } + /** + * {@inheritdoc} + */ + public function isReadable($class, $property, array $context = []): ?bool + { + return null; + } + + /** + * {@inheritdoc} + */ + public function isWritable($class, $property, array $context = []): ?bool + { + if ( + null === ($metadata = $this->getMetadata($class)) + || ClassMetadata::GENERATOR_TYPE_NONE === $metadata->generatorType + || !\in_array($property, $metadata->getIdentifierFieldNames(), true) + ) { + return null; + } + + return false; + } + + private function getMetadata(string $class): ?ClassMetadata + { + try { + return $this->objectManager->getClassMetadata($class); + } catch (MappingException $exception) { + return null; + } + } + /** * Gets the corresponding built-in PHP type. */ diff --git a/tests/Bridge/Doctrine/MongoDbOdm/PropertyInfo/DoctrineExtractorTest.php b/tests/Bridge/Doctrine/MongoDbOdm/PropertyInfo/DoctrineExtractorTest.php index 2106410577a..2bf5c361644 100644 --- a/tests/Bridge/Doctrine/MongoDbOdm/PropertyInfo/DoctrineExtractorTest.php +++ b/tests/Bridge/Doctrine/MongoDbOdm/PropertyInfo/DoctrineExtractorTest.php @@ -18,6 +18,7 @@ use ApiPlatform\Core\Tests\Bridge\Doctrine\MongoDbOdm\PropertyInfo\Fixtures\DoctrineDummy; use ApiPlatform\Core\Tests\Bridge\Doctrine\MongoDbOdm\PropertyInfo\Fixtures\DoctrineEmbeddable; use ApiPlatform\Core\Tests\Bridge\Doctrine\MongoDbOdm\PropertyInfo\Fixtures\DoctrineFooType; +use ApiPlatform\Core\Tests\Bridge\Doctrine\MongoDbOdm\PropertyInfo\Fixtures\DoctrineGeneratedValue; use ApiPlatform\Core\Tests\Bridge\Doctrine\MongoDbOdm\PropertyInfo\Fixtures\DoctrineRelation; use ApiPlatform\Core\Tests\Bridge\Doctrine\MongoDbOdm\PropertyInfo\Fixtures\DoctrineWithEmbedded; use Doctrine\Common\Collections\Collection; @@ -193,6 +194,15 @@ public function testGetTypesCatchException(): void $this->assertNull($this->createExtractor()->getTypes('Not\Exist', 'baz')); } + public function testGeneratedValueNotWritable() + { + $extractor = $this->createExtractor(); + $this->assertFalse($extractor->isWritable(DoctrineGeneratedValue::class, 'id')); + $this->assertNull($extractor->isReadable(DoctrineGeneratedValue::class, 'id')); + $this->assertNull($extractor->isWritable(DoctrineGeneratedValue::class, 'foo')); + $this->assertNull($extractor->isReadable(DoctrineGeneratedValue::class, 'foo')); + } + private function createExtractor(): DoctrineExtractor { $config = DoctrineMongoDbOdmSetup::createAnnotationMetadataConfiguration([__DIR__.\DIRECTORY_SEPARATOR.'Fixtures'], true); diff --git a/tests/Bridge/Doctrine/MongoDbOdm/PropertyInfo/Fixtures/DoctrineGeneratedValue.php b/tests/Bridge/Doctrine/MongoDbOdm/PropertyInfo/Fixtures/DoctrineGeneratedValue.php new file mode 100644 index 00000000000..46ece808957 --- /dev/null +++ b/tests/Bridge/Doctrine/MongoDbOdm/PropertyInfo/Fixtures/DoctrineGeneratedValue.php @@ -0,0 +1,37 @@ + + * + * 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\Bridge\Doctrine\MongoDbOdm\PropertyInfo\Fixtures; + +use Doctrine\ODM\MongoDB\Mapping\Annotations\Document; +use Doctrine\ODM\MongoDB\Mapping\Annotations\Field; +use Doctrine\ODM\MongoDB\Mapping\Annotations\Id; + +/** + * @Document + * + * @author Kévin Dunglas + * @author Alan Poulain + */ +class DoctrineGeneratedValue +{ + /** + * @Id(strategy="INCREMENT", type="integer") + */ + public $id; + + /** + * @Field + */ + public $foo; +}