|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bankiru\Api\Doctrine\Tests; |
|
4
|
|
|
|
|
5
|
|
|
use Bankiru\Api\Doctrine\Test\Entity\CustomEntity; |
|
6
|
|
|
use Prophecy\Argument; |
|
7
|
|
|
use Psr\Cache\CacheItemInterface; |
|
8
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
9
|
|
|
use Psr\Log\LoggerInterface; |
|
10
|
|
|
|
|
11
|
|
|
class EntityCacheTest extends AbstractEntityManagerTest |
|
12
|
|
|
{ |
|
13
|
|
|
private static $cache; |
|
14
|
|
|
|
|
15
|
|
|
public function testEntityCache() |
|
16
|
|
|
{ |
|
17
|
|
|
$this->getClient()->push( |
|
18
|
|
|
$this->getResponseMock( |
|
19
|
|
|
true, |
|
20
|
|
|
(object)[ |
|
21
|
|
|
'id' => '1', |
|
22
|
|
|
'payload' => 'test-payload', |
|
23
|
|
|
] |
|
24
|
|
|
) |
|
25
|
|
|
); |
|
26
|
|
|
|
|
27
|
|
|
$repository = $this->getManager()->getRepository(CustomEntity::class); |
|
28
|
|
|
/** @var CustomEntity $entity */ |
|
29
|
|
|
$entity = $repository->find(1); |
|
30
|
|
|
|
|
31
|
|
|
self::assertInstanceOf(CustomEntity::class, $entity); |
|
32
|
|
|
self::assertEquals(1, $entity->getId()); |
|
33
|
|
|
self::assertInternalType('int', $entity->getId()); |
|
34
|
|
|
self::assertEquals('test-payload', $entity->getPayload()); |
|
35
|
|
|
|
|
36
|
|
|
$this->createEntityManager($this->getClientNames()); |
|
37
|
|
|
|
|
38
|
|
|
$repository = $this->getManager()->getRepository(CustomEntity::class); |
|
39
|
|
|
/** @var CustomEntity $entity */ |
|
40
|
|
|
$entity = $repository->find(1); |
|
41
|
|
|
|
|
42
|
|
|
self::assertInstanceOf(CustomEntity::class, $entity); |
|
43
|
|
|
self::assertEquals(1, $entity->getId()); |
|
44
|
|
|
self::assertInternalType('int', $entity->getId()); |
|
45
|
|
|
self::assertEquals('test-payload', $entity->getPayload()); |
|
46
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected function createConfiguration() |
|
50
|
|
|
{ |
|
51
|
|
|
$configuration = parent::createConfiguration(); |
|
52
|
|
|
|
|
53
|
|
|
$log = $this->prophesize(LoggerInterface::class); |
|
54
|
|
|
$log->debug(Argument::any(), Argument::any())->shouldBeCalled(); |
|
55
|
|
|
|
|
56
|
|
|
$configuration->setApiCache($this->getCache()); |
|
57
|
|
|
$configuration->setApiCacheLogger($log->reveal()); |
|
58
|
|
|
$configuration->setCacheConfiguration( |
|
59
|
|
|
CustomEntity::class, |
|
60
|
|
|
[ |
|
61
|
|
|
'ttl' => 900, |
|
62
|
|
|
'enabled' => true, |
|
63
|
|
|
] |
|
64
|
|
|
); |
|
65
|
|
|
|
|
66
|
|
|
return $configuration; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return mixed |
|
71
|
|
|
*/ |
|
72
|
|
|
private function getCache() |
|
73
|
|
|
{ |
|
74
|
|
|
if (null === self::$cache) { |
|
75
|
|
|
self::$cache = $this->createCache(); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return self::$cache; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return CacheItemPoolInterface |
|
83
|
|
|
* @throws \LogicException |
|
84
|
|
|
* |
|
85
|
|
|
* @link https://gist.github.com/scaytrase/3cf9c5ece4218280669c |
|
86
|
|
|
*/ |
|
87
|
|
|
private function createCache() |
|
88
|
|
|
{ |
|
89
|
|
|
static $items = []; |
|
90
|
|
|
$cache = $this->prophesize(CacheItemPoolInterface::class); |
|
91
|
|
|
$that = $this; |
|
92
|
|
|
$cache->getItem(Argument::type('string'))->will( |
|
93
|
|
|
function ($args) use (&$items, $that) { |
|
94
|
|
|
$key = $args[0]; |
|
95
|
|
|
if (!array_key_exists($key, $items)) { |
|
96
|
|
|
$item = $that->prophesize(CacheItemInterface::class); |
|
97
|
|
|
$item->getKey()->willReturn($key); |
|
98
|
|
|
$item->isHit()->willReturn(false); |
|
99
|
|
|
$item->get()->willReturn(null); |
|
100
|
|
|
$item->set(Argument::any())->will( |
|
101
|
|
|
function ($args) use ($item) { |
|
102
|
|
|
$item->get()->willReturn($args[0]); |
|
103
|
|
|
|
|
104
|
|
|
return $item; |
|
105
|
|
|
} |
|
106
|
|
|
); |
|
107
|
|
|
$item->expiresAfter(Argument::type('int'))->willReturn($item); |
|
108
|
|
|
$item->expiresAfter(Argument::exact(null))->willReturn($item); |
|
109
|
|
|
$item->expiresAfter(Argument::type(\DateInterval::class))->willReturn($item); |
|
110
|
|
|
$item->expiresAt(Argument::type(\DateTimeInterface::class))->willReturn($item); |
|
111
|
|
|
$items[$key] = $item; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
return $items[$key]->reveal(); |
|
115
|
|
|
} |
|
116
|
|
|
); |
|
117
|
|
|
$cache->save(Argument::type(CacheItemInterface::class))->will( |
|
118
|
|
|
function ($args) use (&$items) { |
|
119
|
|
|
$item = $args[0]; |
|
120
|
|
|
$items[$item->getKey()]->isHit()->willReturn(true); |
|
121
|
|
|
} |
|
122
|
|
|
); |
|
123
|
|
|
|
|
124
|
|
|
return $cache->reveal(); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|