Completed
Push — master ( 50808e...33b518 )
by Gianluca
05:07
created

testCreateSQLLoggerWithCustomLogger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 14
nc 1
nop 0
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace DoctrineORMModuleTest\Service;
21
22
use PHPUnit_Framework_TestCase as TestCase;
23
use DoctrineORMModule\Service\SQLLoggerCollectorFactory;
24
use Doctrine\DBAL\Logging\DebugStack;
25
use Doctrine\ORM\Configuration as ORMConfiguration;
26
use Zend\ServiceManager\ServiceManager;
27
28
class SQLLoggerCollectorFactoryTest extends TestCase
29
{
30
    /**
31
     * @var ServiceManager
32
     */
33
    protected $services;
34
35
    /**
36
     * @var SQLLoggerCollectorFactory
37
     */
38
    protected $factory;
39
40
    /**
41
     * {@inheritDoc}
42
     */
43
    public function setUp()
44
    {
45
        parent::setUp();
46
        $this->services = new ServiceManager();
47
        $this->factory = new SQLLoggerCollectorFactory('orm_default');
48
    }
49
50
    public function testCreateSQLLoggerCollector()
51
    {
52
        $configuration = new ORMConfiguration();
53
        $this->services->setService('doctrine.configuration.orm_default', $configuration);
54
        $this->services->setService(
55
            'Config',
56
            array(
57
                'doctrine' => array(
58
                    'sql_logger_collector' => array(
59
                        'orm_default' => array(),
60
                    ),
61
                ),
62
            )
63
        );
64
        $service = $this->factory->createService($this->services);
65
        $this->assertInstanceOf('DoctrineORMModule\Collector\SQLLoggerCollector', $service);
66
        $this->assertInstanceOf('Doctrine\DBAL\Logging\SQLLogger', $configuration->getSQLLogger());
67
    }
68
69
    public function testCreateSQLLoggerWithCustomConfiguration()
70
    {
71
        $configuration = new ORMConfiguration();
72
        $this->services->setService('configuration_service_id', $configuration);
73
        $this->services->setService(
74
            'Config',
75
            array(
76
                'doctrine' => array(
77
                    'sql_logger_collector' => array(
78
                        'orm_default' => array(
79
                            'configuration' => 'configuration_service_id',
80
                        ),
81
                    ),
82
                ),
83
            )
84
        );
85
        $this->factory->createService($this->services);
86
        $this->assertInstanceOf('Doctrine\DBAL\Logging\SQLLogger', $configuration->getSQLLogger());
87
    }
88
89
    public function testCreateSQLLoggerWithPreviousExistingLoggerChainsLoggers()
90
    {
91
        $originalLogger = $this->getMock('Doctrine\DBAL\Logging\SQLLogger');
92
        $originalLogger
93
            ->expects($this->once())
94
            ->method('startQuery')
95
            ->with($this->equalTo('test query'));
96
        $injectedLogger = $this->getMock('Doctrine\DBAL\Logging\DebugStack');
97
        $injectedLogger
98
            ->expects($this->once())
99
            ->method('startQuery')
100
            ->with($this->equalTo('test query'));
101
102
        $configuration = new ORMConfiguration();
103
        $configuration->setSQLLogger($originalLogger);
104
        $this->services->setService('doctrine.configuration.orm_default', $configuration);
105
        $this->services->setService('custom_logger', $injectedLogger);
106
        $this->services->setService(
107
            'Config',
108
            array(
109
                'doctrine' => array(
110
                    'sql_logger_collector' => array(
111
                        'orm_default' => array(
112
                            'sql_logger' => 'custom_logger',
113
                        ),
114
                    ),
115
                ),
116
            )
117
        );
118
        $this->factory->createService($this->services);
119
        /* @var $logger \Doctrine\DBAL\Logging\SQLLogger */
120
        $logger = $configuration->getSQLLogger();
121
        $logger->startQuery('test query');
122
    }
123
124
    public function testCreateSQLLoggerWithCustomLogger()
125
    {
126
        $configuration = new ORMConfiguration();
127
        $logger = new DebugStack();
128
        $this->services->setService('doctrine.configuration.orm_default', $configuration);
129
        $this->services->setService('logger_service_id', $logger);
130
        $this->services->setService(
131
            'Config',
132
            array(
133
                'doctrine' => array(
134
                    'sql_logger_collector' => array(
135
                        'orm_default' => array(
136
                            'sql_logger' => 'logger_service_id',
137
                        ),
138
                    ),
139
                ),
140
            )
141
        );
142
        $this->factory->createService($this->services);
143
        $this->assertSame($logger, $configuration->getSQLLogger());
144
    }
145
146
    public function testCreateSQLLoggerWithCustomName()
147
    {
148
        $this->services->setService('doctrine.configuration.orm_default', new ORMConfiguration());
149
        $this->services->setService(
150
            'Config',
151
            array(
152
                'doctrine' => array(
153
                    'sql_logger_collector' => array(
154
                        'orm_default' => array(
155
                            'name' => 'test_collector_name',
156
                        ),
157
                    ),
158
                ),
159
            )
160
        );
161
        /* @var $service \DoctrineORMModule\Collector\SQLLoggerCollector */
162
        $service = $this->factory->createService($this->services);
163
        $this->assertSame('doctrine.sql_logger_collector.test_collector_name', $service->getName());
164
    }
165
}
166