|
19 | 19 | use ApiPlatform\Core\Exception\InvalidArgumentException;
|
20 | 20 | use PHPUnit\Framework\TestCase;
|
21 | 21 | use Prophecy\Argument;
|
| 22 | +use Psr\Cache\CacheException; |
22 | 23 | use Psr\Cache\CacheItemInterface;
|
23 | 24 | use Psr\Cache\CacheItemPoolInterface;
|
24 | 25 |
|
@@ -63,39 +64,39 @@ public function testGetRouteNameForItemRouteWithNoMatchingRoute()
|
63 | 64 | public function testGetRouteNameForItemRouteOnCacheMiss()
|
64 | 65 | {
|
65 | 66 | $cacheItemProphecy = $this->prophesize(CacheItemInterface::class);
|
66 |
| - $cacheItemProphecy->isHit()->willReturn(false)->shouldBeCalled(); |
67 |
| - $cacheItemProphecy->set('some_item_route')->shouldBeCalled(); |
| 67 | + $cacheItemProphecy->isHit()->willReturn(false)->shouldBeCalledTimes(1); |
| 68 | + $cacheItemProphecy->set('some_item_route')->shouldBeCalledTimes(1); |
68 | 69 |
|
69 | 70 | $cacheItemPoolProphecy = $this->prophesize(CacheItemPoolInterface::class);
|
70 |
| - $cacheItemPoolProphecy->getItem(Argument::type('string'))->willReturn($cacheItemProphecy); |
71 |
| - $cacheItemPoolProphecy->save($cacheItemProphecy)->willReturn(true)->shouldBeCalled(); |
| 71 | + $cacheItemPoolProphecy->getItem(Argument::type('string'))->shouldBeCalledTimes(1)->willReturn($cacheItemProphecy); |
| 72 | + $cacheItemPoolProphecy->save($cacheItemProphecy)->shouldBeCalledTimes(1)->willReturn(true); |
72 | 73 |
|
73 | 74 | $decoratedProphecy = $this->prophesize(RouteNameResolverInterface::class);
|
74 |
| - $decoratedProphecy->getRouteName('AppBundle\Entity\User', false, [])->willReturn('some_item_route')->shouldBeCalled(); |
| 75 | + $decoratedProphecy->getRouteName('AppBundle\Entity\User', false, [])->willReturn('some_item_route')->shouldBeCalledTimes(1); |
75 | 76 |
|
76 | 77 | $cachedRouteNameResolver = new CachedRouteNameResolver($cacheItemPoolProphecy->reveal(), $decoratedProphecy->reveal());
|
77 |
| - $actual = $cachedRouteNameResolver->getRouteName('AppBundle\Entity\User', false); |
78 | 78 |
|
79 |
| - $this->assertSame('some_item_route', $actual); |
| 79 | + $this->assertSame('some_item_route', $cachedRouteNameResolver->getRouteName('AppBundle\Entity\User', false)); |
| 80 | + $this->assertSame('some_item_route', $cachedRouteNameResolver->getRouteName('AppBundle\Entity\User', false), 'Trigger the local cache'); |
80 | 81 | }
|
81 | 82 |
|
82 | 83 | public function testGetRouteNameForItemRouteOnCacheHit()
|
83 | 84 | {
|
84 | 85 | $cacheItemProphecy = $this->prophesize(CacheItemInterface::class);
|
85 |
| - $cacheItemProphecy->isHit()->willReturn(true)->shouldBeCalled(); |
86 |
| - $cacheItemProphecy->get()->willReturn('some_item_route')->shouldBeCalled(); |
| 86 | + $cacheItemProphecy->isHit()->shouldBeCalledTimes(1)->willReturn(true); |
| 87 | + $cacheItemProphecy->get()->shouldBeCalledTimes(1)->willReturn('some_item_route'); |
87 | 88 |
|
88 | 89 | $cacheItemPoolProphecy = $this->prophesize(CacheItemPoolInterface::class);
|
89 |
| - $cacheItemPoolProphecy->getItem(Argument::type('string'))->willReturn($cacheItemProphecy); |
| 90 | + $cacheItemPoolProphecy->getItem(Argument::type('string'))->shouldBeCalledTimes(1)->willReturn($cacheItemProphecy); |
90 | 91 | $cacheItemPoolProphecy->save($cacheItemProphecy)->shouldNotBeCalled();
|
91 | 92 |
|
92 | 93 | $decoratedProphecy = $this->prophesize(RouteNameResolverInterface::class);
|
93 | 94 | $decoratedProphecy->getRouteName(Argument::cetera())->shouldNotBeCalled();
|
94 | 95 |
|
95 | 96 | $cachedRouteNameResolver = new CachedRouteNameResolver($cacheItemPoolProphecy->reveal(), $decoratedProphecy->reveal());
|
96 |
| - $actual = $cachedRouteNameResolver->getRouteName('AppBundle\Entity\User', OperationType::ITEM); |
97 | 97 |
|
98 |
| - $this->assertSame('some_item_route', $actual); |
| 98 | + $this->assertSame('some_item_route', $cachedRouteNameResolver->getRouteName('AppBundle\Entity\User', OperationType::ITEM)); |
| 99 | + $this->assertSame('some_item_route', $cachedRouteNameResolver->getRouteName('AppBundle\Entity\User', OperationType::ITEM), 'Trigger the local cache'); |
99 | 100 | }
|
100 | 101 |
|
101 | 102 | /**
|
@@ -123,38 +124,55 @@ public function testGetRouteNameForCollectionRouteWithNoMatchingRoute()
|
123 | 124 | public function testGetRouteNameForCollectionRouteOnCacheMiss()
|
124 | 125 | {
|
125 | 126 | $cacheItemProphecy = $this->prophesize(CacheItemInterface::class);
|
126 |
| - $cacheItemProphecy->isHit()->willReturn(false)->shouldBeCalled(); |
127 |
| - $cacheItemProphecy->set('some_collection_route')->shouldBeCalled(); |
| 127 | + $cacheItemProphecy->isHit()->shouldBeCalledTimes(1)->willReturn(false); |
| 128 | + $cacheItemProphecy->set('some_collection_route')->shouldBeCalledTimes(1); |
128 | 129 |
|
129 | 130 | $cacheItemPoolProphecy = $this->prophesize(CacheItemPoolInterface::class);
|
130 |
| - $cacheItemPoolProphecy->getItem(Argument::type('string'))->willReturn($cacheItemProphecy); |
131 |
| - $cacheItemPoolProphecy->save($cacheItemProphecy)->willReturn(true)->shouldBeCalled(); |
| 131 | + $cacheItemPoolProphecy->getItem(Argument::type('string'))->shouldBeCalledTimes(1)->willReturn($cacheItemProphecy); |
| 132 | + $cacheItemPoolProphecy->save($cacheItemProphecy)->shouldBeCalledTimes(1)->willReturn(true); |
132 | 133 |
|
133 | 134 | $decoratedProphecy = $this->prophesize(RouteNameResolverInterface::class);
|
134 |
| - $decoratedProphecy->getRouteName('AppBundle\Entity\User', true, [])->willReturn('some_collection_route')->shouldBeCalled(); |
| 135 | + $decoratedProphecy->getRouteName('AppBundle\Entity\User', true, [])->willReturn('some_collection_route')->shouldBeCalledTimes(1); |
135 | 136 |
|
136 | 137 | $cachedRouteNameResolver = new CachedRouteNameResolver($cacheItemPoolProphecy->reveal(), $decoratedProphecy->reveal());
|
137 |
| - $actual = $cachedRouteNameResolver->getRouteName('AppBundle\Entity\User', true); |
138 | 138 |
|
139 |
| - $this->assertSame('some_collection_route', $actual); |
| 139 | + $this->assertSame('some_collection_route', $cachedRouteNameResolver->getRouteName('AppBundle\Entity\User', true)); |
| 140 | + $this->assertSame('some_collection_route', $cachedRouteNameResolver->getRouteName('AppBundle\Entity\User', true), 'Trigger the local cache'); |
140 | 141 | }
|
141 | 142 |
|
142 | 143 | public function testGetRouteNameForCollectionRouteOnCacheHit()
|
143 | 144 | {
|
144 | 145 | $cacheItemProphecy = $this->prophesize(CacheItemInterface::class);
|
145 |
| - $cacheItemProphecy->isHit()->willReturn(true)->shouldBeCalled(); |
146 |
| - $cacheItemProphecy->get()->willReturn('some_collection_route')->shouldBeCalled(); |
| 146 | + $cacheItemProphecy->isHit()->willReturn(true)->shouldBeCalledTimes(1); |
| 147 | + $cacheItemProphecy->get()->willReturn('some_collection_route')->shouldBeCalledTimes(1); |
147 | 148 |
|
148 | 149 | $cacheItemPoolProphecy = $this->prophesize(CacheItemPoolInterface::class);
|
149 |
| - $cacheItemPoolProphecy->getItem(Argument::type('string'))->willReturn($cacheItemProphecy); |
| 150 | + $cacheItemPoolProphecy->getItem(Argument::type('string'))->shouldBeCalledTimes(1)->willReturn($cacheItemProphecy); |
150 | 151 | $cacheItemPoolProphecy->save($cacheItemProphecy)->shouldNotBeCalled();
|
151 | 152 |
|
152 | 153 | $decoratedProphecy = $this->prophesize(RouteNameResolverInterface::class);
|
153 | 154 | $decoratedProphecy->getRouteName(Argument::cetera())->shouldNotBeCalled();
|
154 | 155 |
|
155 | 156 | $cachedRouteNameResolver = new CachedRouteNameResolver($cacheItemPoolProphecy->reveal(), $decoratedProphecy->reveal());
|
156 |
| - $actual = $cachedRouteNameResolver->getRouteName('AppBundle\Entity\User', OperationType::COLLECTION); |
157 | 157 |
|
158 |
| - $this->assertSame('some_collection_route', $actual); |
| 158 | + $this->assertSame('some_collection_route', $cachedRouteNameResolver->getRouteName('AppBundle\Entity\User', OperationType::COLLECTION)); |
| 159 | + $this->assertSame('some_collection_route', $cachedRouteNameResolver->getRouteName('AppBundle\Entity\User', OperationType::COLLECTION), 'Trigger the local cache'); |
| 160 | + } |
| 161 | + |
| 162 | + public function testGetRouteNameWithCacheItemThrowsCacheException() |
| 163 | + { |
| 164 | + $cacheException = $this->prophesize(CacheException::class); |
| 165 | + $cacheException->willExtend(\Exception::class); |
| 166 | + |
| 167 | + $cacheItemPool = $this->prophesize(CacheItemPoolInterface::class); |
| 168 | + $cacheItemPool->getItem(Argument::type('string'))->shouldBeCalledTimes(1)->willThrow($cacheException->reveal()); |
| 169 | + |
| 170 | + $decoratedProphecy = $this->prophesize(RouteNameResolverInterface::class); |
| 171 | + $decoratedProphecy->getRouteName('AppBundle\Entity\User', OperationType::ITEM, [])->willReturn('some_item_route')->shouldBeCalledTimes(1); |
| 172 | + |
| 173 | + $cachedRouteNameResolver = new CachedRouteNameResolver($cacheItemPool->reveal(), $decoratedProphecy->reveal()); |
| 174 | + |
| 175 | + $this->assertSame('some_item_route', $cachedRouteNameResolver->getRouteName('AppBundle\Entity\User', OperationType::ITEM)); |
| 176 | + $this->assertSame('some_item_route', $cachedRouteNameResolver->getRouteName('AppBundle\Entity\User', OperationType::ITEM), 'Trigger the local cache'); |
159 | 177 | }
|
160 | 178 | }
|
0 commit comments