Skip to content

Commit 1f73d51

Browse files
committed
Add tests for the CachedPropertyNameCollectionFactory class
1 parent 0ddd3ec commit 1f73d51

File tree

2 files changed

+91
-6
lines changed

2 files changed

+91
-6
lines changed

src/Metadata/Property/Factory/CachedPropertyNameCollectionFactory.php

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

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

6662
return $propertyNameCollection;
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\CachedPropertyNameCollectionFactory;
15+
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
16+
use ApiPlatform\Core\Metadata\Property\PropertyNameCollection;
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 CachedPropertyNameCollectionFactoryTest 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 PropertyNameCollection(['id', 'name', 'description', 'dummy']))->shouldBeCalled();
32+
33+
$cacheItemPool = $this->prophesize(CacheItemPoolInterface::class);
34+
$cacheItemPool->getItem($this->generateCacheKey())->willReturn($cacheItem->reveal())->shouldBeCalled();
35+
36+
$decoratedPropertyNameCollectionFactory = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
37+
38+
$cachedPropertyNameCollectionFactory = new CachedPropertyNameCollectionFactory($cacheItemPool->reveal(), $decoratedPropertyNameCollectionFactory->reveal());
39+
$resultedPropertyNameCollection = $cachedPropertyNameCollectionFactory->create(Dummy::class);
40+
41+
$this->assertInstanceOf(PropertyNameCollection::class, $resultedPropertyNameCollection);
42+
$this->assertEquals(new PropertyNameCollection(['id', 'name', 'description', 'dummy']), $resultedPropertyNameCollection);
43+
}
44+
45+
public function testCreateWithItemNotHit()
46+
{
47+
$resourceNameCollection = new PropertyNameCollection(['id', 'name', 'description', 'dummy']);
48+
49+
$decoratedPropertyNameCollectionFactory = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
50+
$decoratedPropertyNameCollectionFactory->create(Dummy::class, [])->willReturn($resourceNameCollection)->shouldBeCalled();
51+
52+
$cacheItem = $this->prophesize(CacheItemInterface::class);
53+
$cacheItem->isHit()->willReturn(false)->shouldBeCalled();
54+
$cacheItem->set($resourceNameCollection)->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+
$cachedPropertyNameCollectionFactory = new CachedPropertyNameCollectionFactory($cacheItemPool->reveal(), $decoratedPropertyNameCollectionFactory->reveal());
61+
$resultedPropertyNameCollection = $cachedPropertyNameCollectionFactory->create(Dummy::class);
62+
63+
$this->assertInstanceOf(PropertyNameCollection::class, $resultedPropertyNameCollection);
64+
$this->assertEquals(new PropertyNameCollection(['id', 'name', 'description', 'dummy']), $resultedPropertyNameCollection);
65+
}
66+
67+
public function testCreateWithGetCacheItemThrowsCacheException()
68+
{
69+
$decoratedPropertyNameCollectionFactory = $this->prophesize(PropertyNameCollectionFactoryInterface::class);
70+
$decoratedPropertyNameCollectionFactory->create(Dummy::class, [])->willReturn(new PropertyNameCollection(['id', 'name', 'description', 'dummy']))->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+
$cachedPropertyNameCollectionFactory = new CachedPropertyNameCollectionFactory($cacheItemPool->reveal(), $decoratedPropertyNameCollectionFactory->reveal());
79+
$resultedPropertyNameCollection = $cachedPropertyNameCollectionFactory->create(Dummy::class);
80+
81+
$this->assertInstanceOf(PropertyNameCollection::class, $resultedPropertyNameCollection);
82+
$this->assertEquals(new PropertyNameCollection(['id', 'name', 'description', 'dummy']), $resultedPropertyNameCollection);
83+
}
84+
85+
private function generateCacheKey(string $resourceClass = Dummy::class, array $options = [])
86+
{
87+
return CachedPropertyNameCollectionFactory::CACHE_KEY_PREFIX.md5(serialize([$resourceClass, $options]));
88+
}
89+
}

0 commit comments

Comments
 (0)