Skip to content

Commit

Permalink
Merge ae541be into f8ccee0
Browse files Browse the repository at this point in the history
  • Loading branch information
OskarStark committed May 5, 2020
2 parents f8ccee0 + ae541be commit 4991c6e
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 7 deletions.
Expand Up @@ -13,7 +13,6 @@

namespace ApiPlatform\Core\Bridge\Symfony\PropertyInfo\Metadata\Property;

use ApiPlatform\Core\Exception\RuntimeException;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
use ApiPlatform\Core\Metadata\Property\PropertyNameCollection;
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
Expand All @@ -36,16 +35,11 @@ public function __construct(PropertyInfoExtractorInterface $propertyInfo)

/**
* {@inheritdoc}
*
* @throws RuntimeException
*/
public function create(string $resourceClass, array $options = []): PropertyNameCollection
{
$properties = $this->propertyInfo->getProperties($resourceClass, $options);
if (null === $properties) {
throw new RuntimeException(sprintf('There is no PropertyInfo extractor supporting the class "%s".', $resourceClass));
}

return new PropertyNameCollection($properties);
return new PropertyNameCollection($properties ?? []);
}
}
@@ -0,0 +1,73 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\Symfony\PropertyInfo\Metadata\Property;

use ApiPlatform\Core\Bridge\Symfony\PropertyInfo\Metadata\Property\PropertyInfoPropertyNameCollectionFactory;
use ApiPlatform\Core\Tests\Fixtures\DummyObjectWithOnlyPrivateProperty;
use ApiPlatform\Core\Tests\Fixtures\DummyObjectWithOnlyPublicProperty;
use ApiPlatform\Core\Tests\Fixtures\DummyObjectWithoutProperty;
use ApiPlatform\Core\Tests\Fixtures\DummyObjectWithPublicAndPrivateProperty;
use PHPUnit\Framework\TestCase;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;

/**
* @author Oskar Stark <oskarstark@googlemail.com>
*/
class PropertyInfoPropertyNameCollectionFactoryTest extends TestCase
{
public function testCreateMethodReturnsEmptyPropertyNameCollectionForObjectWithOnlyPrivateProperty()
{
$factory = new PropertyInfoPropertyNameCollectionFactory(new PropertyInfoExtractor([
new ReflectionExtractor(),
]));

$collection = $factory->create(DummyObjectWithOnlyPrivateProperty::class);

self::assertCount(0, $collection->getIterator());
}

public function testCreateMethodReturnsEmptyPropertyNameCollectionForObjectWithoutProperties()
{
$factory = new PropertyInfoPropertyNameCollectionFactory(new PropertyInfoExtractor([
new ReflectionExtractor(),
]));

$collection = $factory->create(DummyObjectWithoutProperty::class);

self::assertCount(0, $collection->getIterator());
}

public function testCreateMethodReturnsProperPropertyNameCollectionForObjectWithPublicAndPrivateProperty()
{
$factory = new PropertyInfoPropertyNameCollectionFactory(new PropertyInfoExtractor([
new ReflectionExtractor(),
]));

$collection = $factory->create(DummyObjectWithPublicAndPrivateProperty::class);

self::assertCount(1, $collection->getIterator());
}

public function testCreateMethodReturnsProperPropertyNameCollectionForObjectWithPublicProperty()
{
$factory = new PropertyInfoPropertyNameCollectionFactory(new PropertyInfoExtractor([
new ReflectionExtractor(),
]));

$collection = $factory->create(DummyObjectWithOnlyPublicProperty::class);

self::assertCount(1, $collection->getIterator());
}
}
19 changes: 19 additions & 0 deletions tests/Fixtures/DummyObjectWithOnlyPrivateProperty.php
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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;

class DummyObjectWithOnlyPrivateProperty
{
private $foo;
}
19 changes: 19 additions & 0 deletions tests/Fixtures/DummyObjectWithOnlyPublicProperty.php
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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;

class DummyObjectWithOnlyPublicProperty
{
public $foo;
}
20 changes: 20 additions & 0 deletions tests/Fixtures/DummyObjectWithPublicAndPrivateProperty.php
@@ -0,0 +1,20 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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;

class DummyObjectWithPublicAndPrivateProperty
{
public $foo;
private $bar;
}
18 changes: 18 additions & 0 deletions tests/Fixtures/DummyObjectWithoutProperty.php
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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;

class DummyObjectWithoutProperty
{
}

0 comments on commit 4991c6e

Please sign in to comment.