Completed
Push — master ( 753c87...0fd538 )
by Tom
01:44 queued 10s
created

CacheFactoryTest::testCreateLaminasCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
rs 9.424
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace DoctrineModuleTest\Service;
4
5
use Doctrine\Common\Cache\ChainCache;
6
use DoctrineModule\Service\CacheFactory;
7
use PHPUnit\Framework\TestCase as BaseTestCase;
8
use Laminas\ServiceManager\ServiceManager;
9
10
/**
11
 * Test for {@see \DoctrineModule\Service\CacheFactory}
12
 *
13
 * @author Marco Pivetta <[email protected]>
14
 */
15
class CacheFactoryTest extends BaseTestCase
16
{
17
    /**
18
     * @covers \DoctrineModule\Service\CacheFactory::createService
19
     */
20
    public function testWillSetNamespace()
21
    {
22
        $factory        = new CacheFactory('foo');
23
        $serviceManager = new ServiceManager();
24
        $serviceManager->setService(
25
            'config',
26
            [
27
                 'doctrine' => [
28
                     'cache' => [
29
                         'foo' => [
30
                             'namespace' => 'bar',
31
                         ],
32
                     ],
33
                 ],
34
            ]
35
        );
36
37
        /* @var $service \Doctrine\Common\Cache\ArrayCache */
38
        $service = $factory->createService($serviceManager);
39
40
        $this->assertInstanceOf('Doctrine\\Common\\Cache\\ArrayCache', $service);
41
        $this->assertSame('bar', $service->getNamespace());
42
    }
43
44
    /**
45
     * @covers \DoctrineModule\Service\CacheFactory::createService
46
     * @group 547
47
     */
48
    public function testCreateLaminasCache()
49
    {
50
        $factory        = new CacheFactory('phpunit');
51
        $serviceManager = new ServiceManager();
52
        $serviceManager->setService(
53
            'config',
54
            [
55
                'doctrine' => [
56
                    'cache' => [
57
                        'phpunit' => [
58
                            'class' => 'DoctrineModule\Cache\LaminasStorageCache',
59
                            'instance' => 'my-laminas-cache',
60
                            'namespace' => 'DoctrineModule',
61
                        ],
62
                    ],
63
                ],
64
                'caches' => [
65
                    'my-laminas-cache' => [
66
                        'adapter' => [
67
                            'name' => 'blackhole',
68
                        ],
69
                    ],
70
                ],
71
            ]
72
        );
73
        $serviceManager->addAbstractFactory('Laminas\Cache\Service\StorageCacheAbstractServiceFactory');
74
75
        $cache = $factory->createService($serviceManager);
76
77
        $this->assertInstanceOf('DoctrineModule\Cache\LaminasStorageCache', $cache);
78
    }
79
80
    public function testCreatePredisCache()
81
    {
82
        $factory        = new CacheFactory('predis');
83
        $serviceManager = new ServiceManager();
84
        $serviceManager->setService(
85
            'config',
86
            [
87
                'doctrine' => [
88
                    'cache' => [
89
                        'predis' => [
90
                            'class' => 'Doctrine\Common\Cache\PredisCache',
91
                            'instance' => 'my_predis_alias',
92
                            'namespace' => 'DoctrineModule',
93
                        ],
94
                    ],
95
                ],
96
            ]
97
        );
98
        $serviceManager->setService(
99
            'my_predis_alias',
100
            $this->createMock('Predis\ClientInterface')
101
        );
102
        $cache = $factory->createService($serviceManager);
103
104
        $this->assertInstanceOf('Doctrine\Common\Cache\PredisCache', $cache);
105
    }
106
107
    public function testUseServiceFactory()
108
    {
109
        $factory        = new CacheFactory('chain');
110
        $serviceManager = new ServiceManager();
111
        $serviceManager->setService(
112
            'config',
113
            [
114
                'doctrine' => [
115
                    'cache' => [
116
                        'chain' => [
117
                            'class' => ChainCache::class,
118
                        ],
119
                    ],
120
                ],
121
            ]
122
        );
123
124
        $mock = $this->createMock(ChainCache::class);
125
126
        $serviceManager->setFactory(ChainCache::class, function () use ($mock) {
127
            return $mock;
128
        });
129
130
        $cache = $factory->createService($serviceManager);
131
132
        $this->assertSame($mock, $cache);
133
    }
134
}
135