Skip to content

Commit 0ddd3ec

Browse files
committed
Add tests for the CachedPropertyMetadataFactory class
1 parent bdef7dd commit 0ddd3ec

File tree

2 files changed

+91
-6
lines changed

2 files changed

+91
-6
lines changed

src/Metadata/Property/Factory/CachedPropertyMetadataFactory.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,8 @@ public function create(string $resourceClass, string $property, array $options =
5656
return $propertyMetadata;
5757
}
5858

59-
try {
60-
$cacheItem->set($propertyMetadata);
61-
$this->cacheItemPool->save($cacheItem);
62-
} catch (CacheException $e) {
63-
// do nothing
64-
}
59+
$cacheItem->set($propertyMetadata);
60+
$this->cacheItemPool->save($cacheItem);
6561

6662
return $propertyMetadata;
6763
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
namespace ApiPlatform\Core\Tests\Metadata\Property\Factory;
13+
14+
use ApiPlatform\Core\Metadata\Property\Factory\CachedPropertyMetadataFactory;
15+
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
16+
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
17+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Dummy;
18+
use Psr\Cache\CacheException;
19+
use Psr\Cache\CacheItemInterface;
20+
use Psr\Cache\CacheItemPoolInterface;
21+
22+
/**
23+
* @author Baptiste Meyer <[email protected]>
24+
*/
25+
class CachedPropertyMetadataFactoryTest extends \PHPUnit_Framework_TestCase
26+
{
27+
public function testCreateWithItemHit()
28+
{
29+
$cacheItem = $this->prophesize(CacheItemInterface::class);
30+
$cacheItem->isHit()->willReturn(true)->shouldBeCalled();
31+
$cacheItem->get()->willReturn(new PropertyMetadata(null, 'A dummy', true, true, null, null, false, false))->shouldBeCalled();
32+
33+
$cacheItemPool = $this->prophesize(CacheItemPoolInterface::class);
34+
$cacheItemPool->getItem($this->generateCacheKey())->willReturn($cacheItem->reveal())->shouldBeCalled();
35+
36+
$decoratedPropertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
37+
38+
$cachedPropertyMetadataFactory = new CachedPropertyMetadataFactory($cacheItemPool->reveal(), $decoratedPropertyMetadataFactory->reveal());
39+
$resultedPropertyMetadata = $cachedPropertyMetadataFactory->create(Dummy::class, 'dummy');
40+
41+
$this->assertInstanceOf(PropertyMetadata::class, $resultedPropertyMetadata);
42+
$this->assertEquals(new PropertyMetadata(null, 'A dummy', true, true, null, null, false, false), $resultedPropertyMetadata);
43+
}
44+
45+
public function testCreateWithItemNotHit()
46+
{
47+
$propertyMetadata = new PropertyMetadata(null, 'A dummy', true, true, null, null, false, false);
48+
49+
$decoratedPropertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
50+
$decoratedPropertyMetadataFactory->create(Dummy::class, 'dummy', [])->willReturn($propertyMetadata)->shouldBeCalled();
51+
52+
$cacheItem = $this->prophesize(CacheItemInterface::class);
53+
$cacheItem->isHit()->willReturn(false)->shouldBeCalled();
54+
$cacheItem->set($propertyMetadata)->willReturn($cacheItem->reveal())->shouldBeCalled();
55+
56+
$cacheItemPool = $this->prophesize(CacheItemPoolInterface::class);
57+
$cacheItemPool->getItem($this->generateCacheKey())->willReturn($cacheItem->reveal())->shouldBeCalled();
58+
$cacheItemPool->save($cacheItem->reveal())->willReturn(true)->shouldBeCalled();
59+
60+
$cachedPropertyMetadataFactory = new CachedPropertyMetadataFactory($cacheItemPool->reveal(), $decoratedPropertyMetadataFactory->reveal());
61+
$resultedPropertyMetadata = $cachedPropertyMetadataFactory->create(Dummy::class, 'dummy');
62+
63+
$this->assertInstanceOf(PropertyMetadata::class, $resultedPropertyMetadata);
64+
$this->assertEquals(new PropertyMetadata(null, 'A dummy', true, true, null, null, false, false), $resultedPropertyMetadata);
65+
}
66+
67+
public function testCreateWithGetCacheItemThrowsCacheException()
68+
{
69+
$decoratedPropertyMetadataFactory = $this->prophesize(PropertyMetadataFactoryInterface::class);
70+
$decoratedPropertyMetadataFactory->create(Dummy::class, 'dummy', [])->willReturn(new PropertyMetadata(null, 'A dummy', true, true, null, null, false, false))->shouldBeCalled();
71+
72+
$cacheException = $this->prophesize(CacheException::class);
73+
$cacheException->willExtend(\Exception::class);
74+
75+
$cacheItemPool = $this->prophesize(CacheItemPoolInterface::class);
76+
$cacheItemPool->getItem($this->generateCacheKey())->willThrow($cacheException->reveal())->shouldBeCalled();
77+
78+
$cachedPropertyMetadataFactory = new CachedPropertyMetadataFactory($cacheItemPool->reveal(), $decoratedPropertyMetadataFactory->reveal());
79+
$resultedPropertyMetadata = $cachedPropertyMetadataFactory->create(Dummy::class, 'dummy');
80+
81+
$this->assertInstanceOf(PropertyMetadata::class, $resultedPropertyMetadata);
82+
$this->assertEquals(new PropertyMetadata(null, 'A dummy', true, true, null, null, false, false), $resultedPropertyMetadata);
83+
}
84+
85+
private function generateCacheKey(string $resourceClass = Dummy::class, string $property = 'dummy', array $options = [])
86+
{
87+
return CachedPropertyMetadataFactory::CACHE_KEY_PREFIX.md5(serialize([$resourceClass, $property, $options]));
88+
}
89+
}

0 commit comments

Comments
 (0)