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

StorageConnectionFactoryTest::testGetConnection()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 39
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 30
nc 1
nop 2
dl 0
loc 39
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the StorageConnectionFactoryTest 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\StorageConnectionFactory;
12
use eZ\Bundle\EzPublishCoreBundle\ApiLoader\RepositoryConfigurationProvider;
13
use eZ\Publish\Core\MVC\ConfigResolverInterface;
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15
use PHPUnit\Framework\TestCase;
16
17
class StorageConnectionFactoryTest extends TestCase
18
{
19
    /**
20
     * @dataProvider getConnectionProvider
21
     */
22
    public function testGetConnection($repositoryAlias, $doctrineConnection)
23
    {
24
        $repositories = array(
25
            $repositoryAlias => array(
26
                'storage' => array(
27
                    'engine' => 'legacy',
28
                    'connection' => $doctrineConnection,
29
                ),
30
            ),
31
        );
32
33
        $configResolver = $this->getConfigResolverMock();
34
        $configResolver
35
            ->expects($this->once())
36
            ->method('getParameter')
37
            ->with('repository')
38
            ->will($this->returnValue($repositoryAlias));
39
40
        $container = $this->getContainerMock();
41
        $container
42
            ->expects($this->once())
43
            ->method('has')
44
            ->with("doctrine.dbal.{$doctrineConnection}_connection")
45
            ->will($this->returnValue(true));
46
        $container
47
            ->expects($this->once())
48
            ->method('get')
49
            ->with("doctrine.dbal.{$doctrineConnection}_connection")
50
            ->will($this->returnValue($this->getMockBuilder('Doctrine\DBAL\Connection')->disableOriginalConstructor()->getMock()));
51
52
        $repositoryConfigurationProvider = new RepositoryConfigurationProvider($configResolver, $repositories);
53
        $factory = new StorageConnectionFactory($repositoryConfigurationProvider);
54
        $factory->setContainer($container);
55
        $connection = $factory->getConnection();
56
        $this->assertInstanceOf(
57
            'Doctrine\DBAL\Connection',
58
            $connection
59
        );
60
    }
61
62
    public function getConnectionProvider()
63
    {
64
        return array(
65
            array('my_repository', 'my_doctrine_connection'),
66
            array('foo', 'default'),
67
            array('répository_de_dédé', 'la_connexion_de_bébêrt'),
68
        );
69
    }
70
71
    /**
72
     * @expectedException \eZ\Bundle\EzPublishCoreBundle\ApiLoader\Exception\InvalidRepositoryException
73
     */
74
    public function testGetConnectionInvalidRepository()
75
    {
76
        $repositories = array(
77
            'foo' => array(
78
                'storage' => array(
79
                    'engine' => 'legacy',
80
                    'connection' => 'my_doctrine_connection',
81
                ),
82
            ),
83
        );
84
85
        $configResolver = $this->getConfigResolverMock();
86
        $configResolver
87
            ->expects($this->once())
88
            ->method('getParameter')
89
            ->with('repository')
90
            ->will($this->returnValue('inexistent_repository'));
91
92
        $repositoryConfigurationProvider = new RepositoryConfigurationProvider($configResolver, $repositories);
93
        $factory = new StorageConnectionFactory($repositoryConfigurationProvider);
94
        $factory->setContainer($this->getContainerMock());
95
        $factory->getConnection();
96
    }
97
98
    /**
99
     * @expectedException \InvalidArgumentException
100
     */
101
    public function testGetConnectionInvalidConnection()
102
    {
103
        $repositoryConfigurationProviderMock = $this->createMock(RepositoryConfigurationProvider::class);
104
        $repositoryConfig = array(
105
            'alias' => 'foo',
106
            'storage' => array(
107
                'engine' => 'legacy',
108
                'connection' => 'my_doctrine_connection',
109
            ),
110
        );
111
        $repositoryConfigurationProviderMock
112
            ->expects($this->once())
113
            ->method('getRepositoryConfig')
114
            ->will($this->returnValue($repositoryConfig));
115
116
        $container = $this->getContainerMock();
117
        $container
118
            ->expects($this->once())
119
            ->method('has')
120
            ->with('doctrine.dbal.my_doctrine_connection_connection')
121
            ->will($this->returnValue(false));
122
        $container
123
            ->expects($this->once())
124
            ->method('getParameter')
125
            ->with('doctrine.connections')
126
            ->will($this->returnValue(array()));
127
        $factory = new StorageConnectionFactory($repositoryConfigurationProviderMock);
128
        $factory->setContainer($container);
129
        $factory->getConnection();
130
    }
131
132
    protected function getConfigResolverMock()
133
    {
134
        return $this->createMock(ConfigResolverInterface::class);
135
    }
136
137
    protected function getContainerMock()
138
    {
139
        return $this->createMock(ContainerInterface::class);
140
    }
141
}
142