Skip to content

Commit b3ff357

Browse files
bendaviesmeyerbaptiste
authored andcommitted
Fix Cached identifiers extractor support for stringable identifers. (#1781)
* add missing test coverage for stringable identifiers * fix test method names * support string castable identifiers in CachedIdentifiersExtractor
1 parent 8a4e3a0 commit b3ff357

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

src/Api/CachedIdentifiersExtractor.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ public function getIdentifiersFromItem($item): array
7676
continue;
7777
}
7878

79+
if (method_exists($identifiers[$propertyName], '__toString')) {
80+
$identifiers[$propertyName] = (string) $identifiers[$propertyName];
81+
continue;
82+
}
83+
7984
$relatedResourceClass = $this->getObjectClass($identifiers[$propertyName]);
8085
if (!$relatedIdentifiers = $this->localCache[$relatedResourceClass] ?? false) {
8186
$relatedCacheKey = self::CACHE_KEY_PREFIX.md5($relatedResourceClass);

tests/Api/CachedIdentifiersExtractorTest.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use ApiPlatform\Core\Api\CachedIdentifiersExtractor;
1717
use ApiPlatform\Core\Api\IdentifiersExtractorInterface;
18+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Doctrine\Generator\Uuid;
1819
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
1920
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedDummy;
2021
use PHPUnit\Framework\TestCase;
@@ -26,7 +27,16 @@
2627
*/
2728
class CachedIdentifiersExtractorTest extends TestCase
2829
{
29-
public function testFirstPass()
30+
public function identifiersProvider()
31+
{
32+
yield [1, 1];
33+
yield [$uuid = new Uuid(), $uuid->__toString()];
34+
}
35+
36+
/**
37+
* @dataProvider identifiersProvider
38+
*/
39+
public function testFirstPass($identifier, $identifierValue)
3040
{
3141
$key = 'iri_identifiers'.md5(Dummy::class);
3242

@@ -39,14 +49,14 @@ public function testFirstPass()
3949
$cacheItemPool->save($cacheItem)->shouldBeCalled();
4050

4151
$dummy = new Dummy();
42-
$dummy->setId(1);
52+
$dummy->setId($identifier);
4353

4454
$decoration = $this->prophesize(IdentifiersExtractorInterface::class);
45-
$decoration->getIdentifiersFromItem($dummy)->shouldBeCalled()->willReturn(['id' => 1]);
55+
$decoration->getIdentifiersFromItem($dummy)->shouldBeCalled()->willReturn(['id' => $identifierValue]);
4656

4757
$identifiersExtractor = new CachedIdentifiersExtractor($cacheItemPool->reveal(), $decoration->reveal(), null);
4858

49-
$expectedResult = ['id' => 1];
59+
$expectedResult = ['id' => $identifierValue];
5060
$this->assertEquals($expectedResult, $identifiersExtractor->getIdentifiersFromItem($dummy));
5161
$this->assertEquals($expectedResult, $identifiersExtractor->getIdentifiersFromItem($dummy), 'Trigger the local cache');
5262

@@ -57,7 +67,10 @@ public function testFirstPass()
5767
$this->assertEquals($expectedResult, $identifiersExtractor->getIdentifiersFromResourceClass(Dummy::class), 'Trigger the local cache');
5868
}
5969

60-
public function testSecondPass()
70+
/**
71+
* @dataProvider identifiersProvider
72+
*/
73+
public function testSecondPass($identifier, $identifierValue)
6174
{
6275
$key = 'iri_identifiers'.md5(Dummy::class);
6376

@@ -69,19 +82,19 @@ public function testSecondPass()
6982
$cacheItemPool->getItem($key)->shouldBeCalled()->willReturn($cacheItem);
7083

7184
$dummy = new Dummy();
72-
$dummy->setId(1);
85+
$dummy->setId($identifier);
7386

7487
$decoration = $this->prophesize(IdentifiersExtractorInterface::class);
7588
$decoration->getIdentifiersFromItem($dummy)->shouldNotBeCalled();
7689

7790
$identifiersExtractor = new CachedIdentifiersExtractor($cacheItemPool->reveal(), $decoration->reveal(), null);
7891

79-
$expectedResult = ['id' => 1];
92+
$expectedResult = ['id' => $identifierValue];
8093
$this->assertEquals($expectedResult, $identifiersExtractor->getIdentifiersFromItem($dummy));
8194
$this->assertEquals($expectedResult, $identifiersExtractor->getIdentifiersFromItem($dummy), 'Trigger the local cache');
8295
}
8396

84-
public function testSecondPassWithRelatedNotCached()
97+
public function testFirstPassWithRelated()
8598
{
8699
$key = 'iri_identifiers'.md5(Dummy::class);
87100
$keyRelated = 'iri_identifiers'.md5(RelatedDummy::class);
@@ -114,7 +127,7 @@ public function testSecondPassWithRelatedNotCached()
114127
$this->assertEquals($expectedResult, $identifiersExtractor->getIdentifiersFromItem($dummy), 'Trigger the local cache');
115128
}
116129

117-
public function testSecondPassWithRelatedCached()
130+
public function testSecondPassWithRelated()
118131
{
119132
$key = 'iri_identifiers'.md5(Dummy::class);
120133
$keyRelated = 'iri_identifiers'.md5(RelatedDummy::class);

tests/Api/IdentifiersExtractorTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
1919
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
2020
use ApiPlatform\Core\Metadata\Property\PropertyNameCollection;
21+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Doctrine\Generator\Uuid;
2122
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
2223
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\RelatedDummy;
2324
use PHPUnit\Framework\TestCase;
@@ -78,6 +79,18 @@ public function testGetIdentifiersFromItem()
7879
$this->assertEquals(['id' => 1], $identifiersExtractor->getIdentifiersFromItem($dummy));
7980
}
8081

82+
public function testGetStringableIdentifiersFromItem()
83+
{
84+
list($propertyNameCollectionFactoryProphecy, $propertyMetadataFactoryProphecy) = $this->getMetadataFactoryProphecies(Dummy::class, ['id']);
85+
86+
$identifiersExtractor = new IdentifiersExtractor($propertyNameCollectionFactoryProphecy->reveal(), $propertyMetadataFactoryProphecy->reveal());
87+
88+
$dummy = new Dummy();
89+
$dummy->setId(new Uuid());
90+
91+
$this->assertEquals(['id' => 'foo'], $identifiersExtractor->getIdentifiersFromItem($dummy));
92+
}
93+
8194
public function testGetCompositeIdentifiersFromItem()
8295
{
8396
list($propertyNameCollectionFactoryProphecy, $propertyMetadataFactoryProphecy) = $this->getMetadataFactoryProphecies(Dummy::class, ['id', 'name']);

0 commit comments

Comments
 (0)