Skip to content

Commit

Permalink
Fix Behat tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alanpoulain committed Jun 1, 2019
1 parent 6adb383 commit 4b2af33
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 12 deletions.
2 changes: 1 addition & 1 deletion features/bootstrap/HydraContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion features/graphql/mutation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
2 changes: 0 additions & 2 deletions features/hydra/docs.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 0 additions & 1 deletion features/jsonld/context.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
45 changes: 38 additions & 7 deletions src/Bridge/Doctrine/MongoDbOdm/PropertyInfo/DoctrineExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,7 +31,7 @@
* @author Kévin Dunglas <[email protected]>
* @author Alan Poulain <[email protected]>
*/
final class DoctrineExtractor implements PropertyListExtractorInterface, PropertyTypeExtractorInterface
final class DoctrineExtractor implements PropertyListExtractorInterface, PropertyTypeExtractorInterface, PropertyAccessExtractorInterface
{
private $objectManager;

Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* 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 <[email protected]>
* @author Alan Poulain <[email protected]>
*/
class DoctrineGeneratedValue
{
/**
* @Id(strategy="INCREMENT", type="integer")
*/
public $id;

/**
* @Field
*/
public $foo;
}

0 comments on commit 4b2af33

Please sign in to comment.