Skip to content

Commit 97fda1b

Browse files
authored
Merge pull request #839 from meyerbaptiste/add_tests_cached_metadata_classes
Add tests for cached metadata classes
2 parents bdef7dd + d15250d commit 97fda1b

File tree

6 files changed

+273
-18
lines changed

6 files changed

+273
-18
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
}

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
}

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\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+
}
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+
}
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)