Skip to content

Commit d15250d

Browse files
committed
Add tests for the CachedResourceMetadataFactory class
1 parent 1f73d51 commit d15250d

File tree

2 files changed

+91
-6
lines changed

2 files changed

+91
-6
lines changed

src/Metadata/Resource/Factory/CachedResourceMetadataFactory.php

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

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

6662
return $resourceMetadata;
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\Resource\Factory;
13+
14+
use ApiPlatform\Core\Metadata\Resource\Factory\CachedResourceMetadataFactory;
15+
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
16+
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
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 CachedResourceMetadataFactoryTest 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 ResourceMetadata(null, 'Dummy.'))->shouldBeCalled();
32+
33+
$cacheItemPool = $this->prophesize(CacheItemPoolInterface::class);
34+
$cacheItemPool->getItem($this->generateCacheKey())->willReturn($cacheItem->reveal())->shouldBeCalled();
35+
36+
$decoratedResourceMetadataFactory = $this->prophesize(ResourceMetadataFactoryInterface::class);
37+
38+
$cachedResourceMetadataFactory = new CachedResourceMetadataFactory($cacheItemPool->reveal(), $decoratedResourceMetadataFactory->reveal());
39+
$resultedResourceMetadata = $cachedResourceMetadataFactory->create(Dummy::class);
40+
41+
$this->assertInstanceOf(ResourceMetadata::class, $resultedResourceMetadata);
42+
$this->assertEquals(new ResourceMetadata(null, 'Dummy.'), $resultedResourceMetadata);
43+
}
44+
45+
public function testCreateWithItemNotHit()
46+
{
47+
$propertyMetadata = new ResourceMetadata(null, 'Dummy.');
48+
49+
$decoratedResourceMetadataFactory = $this->prophesize(ResourceMetadataFactoryInterface::class);
50+
$decoratedResourceMetadataFactory->create(Dummy::class)->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+
$cachedResourceMetadataFactory = new CachedResourceMetadataFactory($cacheItemPool->reveal(), $decoratedResourceMetadataFactory->reveal());
61+
$resultedResourceMetadata = $cachedResourceMetadataFactory->create(Dummy::class);
62+
63+
$this->assertInstanceOf(ResourceMetadata::class, $resultedResourceMetadata);
64+
$this->assertEquals(new ResourceMetadata(null, 'Dummy.'), $resultedResourceMetadata);
65+
}
66+
67+
public function testCreateWithGetCacheItemThrowsCacheException()
68+
{
69+
$decoratedResourceMetadataFactory = $this->prophesize(ResourceMetadataFactoryInterface::class);
70+
$decoratedResourceMetadataFactory->create(Dummy::class)->willReturn(new ResourceMetadata(null, '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+
$cachedResourceMetadataFactory = new CachedResourceMetadataFactory($cacheItemPool->reveal(), $decoratedResourceMetadataFactory->reveal());
79+
$resultedResourceMetadata = $cachedResourceMetadataFactory->create(Dummy::class);
80+
81+
$this->assertInstanceOf(ResourceMetadata::class, $resultedResourceMetadata);
82+
$this->assertEquals(new ResourceMetadata(null, 'Dummy.'), $resultedResourceMetadata);
83+
}
84+
85+
private function generateCacheKey(string $resourceClass = Dummy::class)
86+
{
87+
return CachedResourceMetadataFactory::CACHE_KEY_PREFIX.md5(serialize([$resourceClass]));
88+
}
89+
}

0 commit comments

Comments
 (0)