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