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