|
| 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