Completed
Push — master ( c7b9bc...da47b2 )
by André
17:49
created

getPersistenceHandlerMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the StorageEngineFactoryTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Bundle\EzPublishCoreBundle\Tests\ApiLoader;
10
11
use eZ\Bundle\EzPublishCoreBundle\ApiLoader\StorageEngineFactory;
12
use eZ\Bundle\EzPublishCoreBundle\ApiLoader\RepositoryConfigurationProvider;
13
use eZ\Publish\SPI\Persistence\Handler;
14
use eZ\Publish\Core\MVC\ConfigResolverInterface;
15
use PHPUnit\Framework\TestCase;
16
17
class StorageEngineFactoryTest extends TestCase
18
{
19
    public function testRegisterStorageEngine()
20
    {
21
        /** @var \eZ\Bundle\EzPublishCoreBundle\ApiLoader\RepositoryConfigurationProvider $repositoryConfigurationProvider */
22
        $repositoryConfigurationProvider = $this->createMock(RepositoryConfigurationProvider::class);
23
        $factory = new StorageEngineFactory($repositoryConfigurationProvider);
24
25
        $storageEngines = array(
26
            'foo' => $this->getPersistenceHandlerMock(),
27
            'bar' => $this->getPersistenceHandlerMock(),
28
            'baz' => $this->getPersistenceHandlerMock(),
29
        );
30
31
        foreach ($storageEngines as $identifier => $persistenceHandler) {
32
            $factory->registerStorageEngine($persistenceHandler, $identifier);
33
        }
34
35
        $this->assertSame($storageEngines, $factory->getStorageEngines());
36
    }
37
38
    public function testBuildStorageEngine()
39
    {
40
        $configResolver = $this->getConfigResolverMock();
41
        $repositoryAlias = 'main';
42
        $repositories = array(
43
            $repositoryAlias => array(
44
                'storage' => array(
45
                    'engine' => 'foo',
46
                ),
47
            ),
48
            'another' => array(
49
                'storage' => array(
50
                    'engine' => 'bar',
51
                ),
52
            ),
53
        );
54
        $expectedStorageEngine = $this->getPersistenceHandlerMock();
55
        $storageEngines = array(
56
            'foo' => $expectedStorageEngine,
57
            'bar' => $this->getPersistenceHandlerMock(),
58
            'baz' => $this->getPersistenceHandlerMock(),
59
        );
60
        $repositoryConfigurationProvider = new RepositoryConfigurationProvider($configResolver, $repositories);
0 ignored issues
show
Bug introduced by
It seems like $configResolver defined by $this->getConfigResolverMock() on line 40 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, eZ\Bundle\EzPublishCoreB...Provider::__construct() does only seem to accept object<eZ\Publish\Core\M...onfigResolverInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
61
        $factory = new StorageEngineFactory($repositoryConfigurationProvider);
62
        foreach ($storageEngines as $identifier => $persistenceHandler) {
63
            $factory->registerStorageEngine($persistenceHandler, $identifier);
64
        }
65
66
        $configResolver
67
            ->expects($this->once())
68
            ->method('getParameter')
69
            ->with('repository')
70
            ->will($this->returnValue($repositoryAlias));
71
72
        $this->assertSame($expectedStorageEngine, $factory->buildStorageEngine());
73
    }
74
75
    /**
76
     * @expectedException \eZ\Bundle\EzPublishCoreBundle\ApiLoader\Exception\InvalidStorageEngine
77
     */
78
    public function testBuildInvalidStorageEngine()
79
    {
80
        $configResolver = $this->getConfigResolverMock();
81
        $repositoryAlias = 'main';
82
        $repositories = array(
83
            $repositoryAlias => array(
84
                'storage' => array(
85
                    'engine' => 'undefined_storage_engine',
86
                ),
87
            ),
88
            'another' => array(
89
                'storage' => array(
90
                    'engine' => 'bar',
91
                ),
92
            ),
93
        );
94
95
        $storageEngines = array(
96
            'foo' => $this->getPersistenceHandlerMock(),
97
            'bar' => $this->getPersistenceHandlerMock(),
98
            'baz' => $this->getPersistenceHandlerMock(),
99
        );
100
101
        $repositoryConfigurationProvider = new RepositoryConfigurationProvider($configResolver, $repositories);
0 ignored issues
show
Bug introduced by
It seems like $configResolver defined by $this->getConfigResolverMock() on line 80 can also be of type object<PHPUnit_Framework_MockObject_MockObject>; however, eZ\Bundle\EzPublishCoreB...Provider::__construct() does only seem to accept object<eZ\Publish\Core\M...onfigResolverInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
102
        $factory = new StorageEngineFactory($repositoryConfigurationProvider);
103
        foreach ($storageEngines as $identifier => $persistenceHandler) {
104
            $factory->registerStorageEngine($persistenceHandler, $identifier);
105
        }
106
107
        $configResolver
108
            ->expects($this->once())
109
            ->method('getParameter')
110
            ->with('repository')
111
            ->will($this->returnValue($repositoryAlias));
112
113
        $this->assertSame($this->getPersistenceHandlerMock(), $factory->buildStorageEngine());
114
    }
115
116
    /**
117
     * @return \PHPUnit_Framework_MockObject_MockObject|\eZ\Publish\Core\MVC\ConfigResolverInterface
118
     */
119
    protected function getConfigResolverMock()
120
    {
121
        return $this->createMock(ConfigResolverInterface::class);
122
    }
123
124
    protected function getPersistenceHandlerMock()
125
    {
126
        return $this->createMock(Handler::class);
127
    }
128
}
129