Skip to content

Commit d5916f2

Browse files
Fix: Allow objects without properties (#3544)
* Fix: Allow objects without properties * Refactor tests Co-authored-by: Andreas Möller <[email protected]> * Fix: CS * Add testcases Co-authored-by: Andreas Möller <[email protected]>
1 parent f8ccee0 commit d5916f2

File tree

6 files changed

+150
-7
lines changed

6 files changed

+150
-7
lines changed

src/Bridge/Symfony/PropertyInfo/Metadata/Property/PropertyInfoPropertyNameCollectionFactory.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

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

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

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

49-
return new PropertyNameCollection($properties);
43+
return new PropertyNameCollection($properties ?? []);
5044
}
5145
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Bridge\Symfony\PropertyInfo\Metadata\Property;
15+
16+
use ApiPlatform\Core\Bridge\Symfony\PropertyInfo\Metadata\Property\PropertyInfoPropertyNameCollectionFactory;
17+
use ApiPlatform\Core\Tests\Fixtures\DummyObjectWithOnlyPrivateProperty;
18+
use ApiPlatform\Core\Tests\Fixtures\DummyObjectWithOnlyPublicProperty;
19+
use ApiPlatform\Core\Tests\Fixtures\DummyObjectWithoutProperty;
20+
use ApiPlatform\Core\Tests\Fixtures\DummyObjectWithPublicAndPrivateProperty;
21+
use PHPUnit\Framework\TestCase;
22+
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
23+
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
24+
25+
/**
26+
* @author Oskar Stark <[email protected]>
27+
*/
28+
class PropertyInfoPropertyNameCollectionFactoryTest extends TestCase
29+
{
30+
public function testCreateMethodReturnsEmptyPropertyNameCollectionForObjectWithOnlyPrivateProperty()
31+
{
32+
$factory = new PropertyInfoPropertyNameCollectionFactory(new PropertyInfoExtractor([
33+
new ReflectionExtractor(),
34+
]));
35+
36+
$collection = $factory->create(DummyObjectWithOnlyPrivateProperty::class);
37+
38+
self::assertCount(0, $collection->getIterator());
39+
}
40+
41+
public function testCreateMethodReturnsEmptyPropertyNameCollectionForObjectWithoutProperties()
42+
{
43+
$factory = new PropertyInfoPropertyNameCollectionFactory(new PropertyInfoExtractor([
44+
new ReflectionExtractor(),
45+
]));
46+
47+
$collection = $factory->create(DummyObjectWithoutProperty::class);
48+
49+
self::assertCount(0, $collection->getIterator());
50+
}
51+
52+
public function testCreateMethodReturnsProperPropertyNameCollectionForObjectWithPublicAndPrivateProperty()
53+
{
54+
$factory = new PropertyInfoPropertyNameCollectionFactory(new PropertyInfoExtractor([
55+
new ReflectionExtractor(),
56+
]));
57+
58+
$collection = $factory->create(DummyObjectWithPublicAndPrivateProperty::class);
59+
60+
self::assertCount(1, $collection->getIterator());
61+
}
62+
63+
public function testCreateMethodReturnsProperPropertyNameCollectionForObjectWithPublicProperty()
64+
{
65+
$factory = new PropertyInfoPropertyNameCollectionFactory(new PropertyInfoExtractor([
66+
new ReflectionExtractor(),
67+
]));
68+
69+
$collection = $factory->create(DummyObjectWithOnlyPublicProperty::class);
70+
71+
self::assertCount(1, $collection->getIterator());
72+
}
73+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Fixtures;
15+
16+
class DummyObjectWithOnlyPrivateProperty
17+
{
18+
private $foo;
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Fixtures;
15+
16+
class DummyObjectWithOnlyPublicProperty
17+
{
18+
public $foo;
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Fixtures;
15+
16+
class DummyObjectWithPublicAndPrivateProperty
17+
{
18+
public $foo;
19+
private $bar;
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Tests\Fixtures;
15+
16+
class DummyObjectWithoutProperty
17+
{
18+
}

0 commit comments

Comments
 (0)