Completed
Push — master ( e0017c...6b1304 )
by Tom
14s queued 11s
created

DoctrineModuleTest/Service/CacheFactoryTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineModuleTest\Service;
6
7
use Doctrine\Common\Cache\ArrayCache;
8
use Doctrine\Common\Cache\ChainCache;
9
use DoctrineModule\Service\CacheFactory;
10
use Laminas\ServiceManager\ServiceManager;
11
use PHPUnit\Framework\TestCase as BaseTestCase;
12
use function assert;
13
14
/**
15
 * Test for {@see \DoctrineModule\Service\CacheFactory}
16
 */
17
class CacheFactoryTest extends BaseTestCase
18
{
19
    /**
20
     * @covers \DoctrineModule\Service\CacheFactory::createService
21
     */
22
    public function testWillSetNamespace() : void
23
    {
24
        $factory        = new CacheFactory('foo');
25
        $serviceManager = new ServiceManager();
26
        $serviceManager->setService(
27
            'config',
28
            [
29
                'doctrine' => [
30
                    'cache' => [
31
                        'foo' => ['namespace' => 'bar'],
32
                    ],
33
                ],
34
            ]
35
        );
36
37
        $service = $factory->createService($serviceManager);
38
        assert($service instanceof ArrayCache);
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() : void
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' => ['name' => 'blackhole'],
67
                    ],
68
                ],
69
            ]
70
        );
71
        $serviceManager->addAbstractFactory('Laminas\Cache\Service\StorageCacheAbstractServiceFactory');
72
73
        $cache = $factory->createService($serviceManager);
74
75
        $this->assertInstanceOf('DoctrineModule\Cache\LaminasStorageCache', $cache);
76
    }
77
78
    public function testCreatePredisCache() : void
79
    {
80
        $factory        = new CacheFactory('predis');
81
        $serviceManager = new ServiceManager();
82
        $serviceManager->setService(
83
            'config',
84
            [
85
                'doctrine' => [
86
                    'cache' => [
87
                        'predis' => [
88
                            'class' => 'Doctrine\Common\Cache\PredisCache',
89
                            'instance' => 'my_predis_alias',
90
                            'namespace' => 'DoctrineModule',
91
                        ],
92
                    ],
93
                ],
94
            ]
95
        );
96
        $serviceManager->setService(
97
            'my_predis_alias',
98
            $this->createMock('Predis\ClientInterface')
0 ignored issues
show
$this->createMock('Predis\\ClientInterface') is of type null, but the function expects a array|object.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
99
        );
100
        $cache = $factory->createService($serviceManager);
101
102
        $this->assertInstanceOf('Doctrine\Common\Cache\PredisCache', $cache);
103
    }
104
105
    public function testUseServiceFactory() : void
106
    {
107
        $factory        = new CacheFactory('chain');
108
        $serviceManager = new ServiceManager();
109
        $serviceManager->setService(
110
            'config',
111
            [
112
                'doctrine' => [
113
                    'cache' => [
114
                        'chain' => [
115
                            'class' => ChainCache::class,
116
                        ],
117
                    ],
118
                ],
119
            ]
120
        );
121
122
        $mock = $this->createMock(ChainCache::class);
123
124
        $serviceManager->setFactory(ChainCache::class, static function () use ($mock) {
125
            return $mock;
126
        });
127
128
        $cache = $factory->createService($serviceManager);
129
130
        $this->assertSame($mock, $cache);
131
    }
132
}
133