Completed
Pull Request — master (#14)
by Pavel
03:32
created

EntityCacheTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 123
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 0
Metric Value
wmc 6
lcom 2
cbo 9
dl 0
loc 123
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B testEntityCache() 0 40 1
A createConfiguration() 0 19 1
A getCache() 0 8 2
B createCache() 0 39 2
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
    }
56
57
    protected function createConfiguration()
58
    {
59
        $configuration = parent::createConfiguration();
60
61
        $log = $this->prophesize(LoggerInterface::class);
62
        $log->debug(Argument::any(), Argument::any())->shouldBeCalled();
63
64
        $configuration->setApiCache($this->getCache());
65
        $configuration->setApiCacheLogger($log->reveal());
66
        $configuration->setCacheConfiguration(
67
            CustomEntity::class,
68
            [
69
                'ttl'     => 900,
70
                'enabled' => true,
71
            ]
72
        );
73
74
        return $configuration;
75
    }
76
77
    /**
78
     * @return mixed
79
     */
80
    private function getCache()
81
    {
82
        if (null === self::$cache) {
83
            self::$cache = $this->createCache();
84
        }
85
86
        return self::$cache;
87
    }
88
89
    /**
90
     * @return CacheItemPoolInterface
91
     * @throws \LogicException
92
     *
93
     * @link https://gist.github.com/scaytrase/3cf9c5ece4218280669c
94
     */
95
    private function createCache()
96
    {
97
        static $items = [];
98
        $cache = $this->prophesize(CacheItemPoolInterface::class);
99
        $that  = $this;
100
        $cache->getItem(Argument::type('string'))->will(
101
            function ($args) use (&$items, $that) {
102
                $key = $args[0];
103
                if (!array_key_exists($key, $items)) {
104
                    $item = $that->prophesize(CacheItemInterface::class);
105
                    $item->getKey()->willReturn($key);
106
                    $item->isHit()->willReturn(false);
107
                    $item->get()->willReturn(null);
108
                    $item->set(Argument::any())->will(
109
                        function ($args) use ($item) {
110
                            $item->get()->willReturn($args[0]);
111
112
                            return $item;
113
                        }
114
                    );
115
                    $item->expiresAfter(Argument::type('int'))->willReturn($item);
116
                    $item->expiresAfter(Argument::exact(null))->willReturn($item);
117
                    $item->expiresAfter(Argument::type(\DateInterval::class))->willReturn($item);
118
                    $item->expiresAt(Argument::type(\DateTimeInterface::class))->willReturn($item);
119
                    $items[$key] = $item;
120
                }
121
122
                return $items[$key]->reveal();
123
            }
124
        );
125
        $cache->save(Argument::type(CacheItemInterface::class))->will(
126
            function ($args) use (&$items) {
127
                $item = $args[0];
128
                $items[$item->getKey()]->isHit()->willReturn(true);
129
            }
130
        );
131
132
        return $cache->reveal();
133
    }
134
}
135