1 | <?php |
||
14 | |||
15 | final class DecoratorTest extends TestCase |
||
16 | { |
||
17 | use RpcRequestTrait; |
||
18 | |||
19 | public function testLoggableClient() |
||
20 | { |
||
21 | if (!interface_exists(LoggerInterface::class)) { |
||
22 | self::markTestSkipped('install psr/log in order to run these tests'); |
||
23 | } |
||
24 | |||
25 | $logger = self::getMockBuilder(AbstractLogger::class)->setMethods(['log'])->getMock(); |
||
26 | $logger->expects(self::atLeastOnce())->method('log'); |
||
27 | |||
28 | $rq1 = $this->getRequestMock('/test1', ['param1' => 'test']); |
||
29 | $rs1 = $this->getResponseMock(true, ['param1' => 'test']); |
||
30 | /** @var RpcRequestInterface[] $requests */ |
||
31 | $requests = [$rq1]; |
||
32 | |||
33 | $client = new LoggableRpcClient($this->getClientMock([$rq1], [$rs1]), $logger); |
||
34 | |||
35 | self::assertEquals($rs1, $client->invoke($requests)->getResponse($rq1)); |
||
36 | } |
||
37 | |||
38 | public function testCacheableClient() |
||
39 | { |
||
40 | if (!interface_exists(CacheItemPoolInterface::class)) { |
||
41 | self::markTestSkipped('install psr/cache in order to run these tests'); |
||
42 | } |
||
43 | |||
44 | $rq1 = $this->getRequestMock('/test1', ['param1' => 'test']); |
||
45 | $rq2 = $this->getRequestMock('/test1', ['param1' => 'test']); |
||
46 | $rs1 = $this->getResponseMock(true, ['payload' => bin2hex(random_bytes(20))]); |
||
47 | |||
48 | $cache = $this->getCache(); |
||
49 | |||
50 | $client = new CacheableRpcClient($this->getClientMock([$rq1], [$rs1]), $cache, 5); |
||
51 | $response = $client->invoke([$rq1])->getResponse($rq1); |
||
52 | self::assertEquals($rs1, $response); |
||
53 | |||
54 | $client = new CacheableRpcClient($this->getClientMock(), $cache, 5); |
||
55 | $response = $client->invoke([$rq2])->getResponse($rq2); |
||
56 | self::assertEquals($rs1, $response); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * @return CacheItemPoolInterface |
||
61 | */ |
||
62 | private function getCache() |
||
63 | { |
||
64 | static $items = []; |
||
65 | $cache = $this->prophesize(CacheItemPoolInterface::class); |
||
66 | $that = $this; |
||
67 | $cache->getItem(Argument::type('string'))->will( |
||
68 | function ($args) use (&$items, $that) { |
||
69 | $key = $args[0]; |
||
70 | if (!array_key_exists($key, $items)) { |
||
71 | $item = $that->prophesize(CacheItemInterface::class); |
||
72 | |||
73 | $item->getKey()->willReturn($key); |
||
74 | $item->isHit()->willReturn(false); |
||
75 | $item->set(Argument::any())->will( |
||
76 | function ($args) use ($item) { |
||
77 | $item->get()->willReturn($args[0]); |
||
78 | } |
||
79 | ); |
||
80 | $item->expiresAfter(Argument::type('int'))->willReturn($item); |
||
81 | $item->expiresAfter(Argument::exact(null))->willReturn($item); |
||
82 | $item->expiresAfter(Argument::type(\DateInterval::class))->willReturn($item); |
||
83 | $item->expiresAt(Argument::type(\DateTimeInterface::class))->willReturn($item); |
||
84 | $items[$key] = $item; |
||
85 | } |
||
86 | |||
87 | return $items[$key]->reveal(); |
||
88 | } |
||
89 | ); |
||
90 | $cache->save(Argument::type(CacheItemInterface::class))->will( |
||
91 | function ($args) use (&$items) { |
||
92 | $item = $args[0]; |
||
93 | $items[$item->getKey()]->isHit()->willReturn(true); |
||
94 | } |
||
95 | ); |
||
96 | |||
97 | return $cache->reveal(); |
||
100 |