Failed Conditions
Push — master ( edfbda...298c91 )
by Luís
16s
created

AbstractDriverTest::getExceptionConversions()

Size

Total Lines 50
Code Lines 15

Duplication

Lines 38
Ratio 76 %

Importance

Changes 0
Metric Value
dl 38
loc 50
c 0
b 0
f 0
eloc 15
nc 3
nop 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A AbstractDriverTest.php$1 ➔ __construct() 6 6 1
A AbstractDriverTest.php$1 ➔ getSQLState() 3 3 1
A AbstractDriverTest.php$1 ➔ getErrorCode() 3 3 1
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Driver;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Driver\DriverException;
7
use Doctrine\DBAL\Driver\ExceptionConverterDriver;
8
use Doctrine\DBAL\VersionAwarePlatformDriver;
9
use Doctrine\Tests\DbalTestCase;
10
11
abstract class AbstractDriverTest extends DbalTestCase
12
{
13
    const EXCEPTION_CONNECTION = 'Doctrine\DBAL\Exception\ConnectionException';
14
    const EXCEPTION_CONSTRAINT_VIOLATION = 'Doctrine\DBAL\Exception\ConstraintViolationException';
15
    const EXCEPTION_DATABASE_OBJECT_EXISTS = 'Doctrine\DBAL\Exception\DatabaseObjectExistsException';
16
    const EXCEPTION_DATABASE_OBJECT_NOT_FOUND = 'Doctrine\DBAL\Exception\DatabaseObjectNotFoundException';
17
    const EXCEPTION_DRIVER = 'Doctrine\DBAL\Exception\DriverException';
18
    const EXCEPTION_FOREIGN_KEY_CONSTRAINT_VIOLATION = 'Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException';
19
    const EXCEPTION_INVALID_FIELD_NAME = 'Doctrine\DBAL\Exception\InvalidFieldNameException';
20
    const EXCEPTION_NON_UNIQUE_FIELD_NAME = 'Doctrine\DBAL\Exception\NonUniqueFieldNameException';
21
    const EXCEPTION_NOT_NULL_CONSTRAINT_VIOLATION = 'Doctrine\DBAL\Exception\NotNullConstraintViolationException';
22
    const EXCEPTION_READ_ONLY = 'Doctrine\DBAL\Exception\ReadOnlyException';
23
    const EXCEPTION_SERVER = 'Doctrine\DBAL\Exception\ServerException';
24
    const EXCEPTION_SYNTAX_ERROR = 'Doctrine\DBAL\Exception\SyntaxErrorException';
25
    const EXCEPTION_TABLE_EXISTS = 'Doctrine\DBAL\Exception\TableExistsException';
26
    const EXCEPTION_TABLE_NOT_FOUND = 'Doctrine\DBAL\Exception\TableNotFoundException';
27
    const EXCEPTION_UNIQUE_CONSTRAINT_VIOLATION = 'Doctrine\DBAL\Exception\UniqueConstraintViolationException';
28
    const EXCEPTION_DEADLOCK = 'Doctrine\DBAL\Exception\DeadlockException';
29
    const EXCEPTION_LOCK_WAIT_TIMEOUT = 'Doctrine\DBAL\Exception\LockWaitTimeoutException';
30
31
    /**
32
     * The driver mock under test.
33
     *
34
     * @var \Doctrine\DBAL\Driver
35
     */
36
    protected $driver;
37
38
    protected function setUp()
39
    {
40
        parent::setUp();
41
42
        $this->driver = $this->createDriver();
43
    }
44
45
    public function testConvertsException()
46
    {
47
        if ( ! $this->driver instanceof ExceptionConverterDriver) {
48
            $this->markTestSkipped('This test is only intended for exception converter drivers.');
49
        }
50
51
        $data = $this->getExceptionConversions();
52
53
        if (empty($data)) {
54
            $this->fail(
55
                sprintf(
56
                    'No test data found for test %s. You have to return test data from %s.',
57
                    get_class($this) . '::' . __FUNCTION__,
58
                    get_class($this) . '::getExceptionConversionData'
59
                )
60
            );
61
        }
62
63
        $driverException = new class extends \Exception implements DriverException
64
        {
65
            public function __construct()
66
            {
67
                parent::__construct('baz');
68
            }
69
70
            /**
71
             * {@inheritDoc}
72
             */
73
            public function getErrorCode()
74
            {
75
                return 'foo';
76
            }
77
78
            /**
79
             * {@inheritDoc}
80
             */
81
            public function getSQLState()
82
            {
83
                return 'bar';
84
            }
85
        };
86
87
        $data[] = array($driverException, self::EXCEPTION_DRIVER);
88
89
        $message = 'DBAL exception message';
90
91
        foreach ($data as $item) {
92
            /** @var $driverException \Doctrine\DBAL\Driver\DriverException */
93
            list($driverException, $convertedExceptionClassName) = $item;
94
95
            $convertedException = $this->driver->convertException($message, $driverException);
96
97
            self::assertSame($convertedExceptionClassName, get_class($convertedException));
98
99
            self::assertSame($driverException->getErrorCode(), $convertedException->getErrorCode());
100
            self::assertSame($driverException->getSQLState(), $convertedException->getSQLState());
101
            self::assertSame($message, $convertedException->getMessage());
102
        }
103
    }
104
105
    public function testCreatesDatabasePlatformForVersion()
106
    {
107
        if ( ! $this->driver instanceof VersionAwarePlatformDriver) {
108
            $this->markTestSkipped('This test is only intended for version aware platform drivers.');
109
        }
110
111
        $data = $this->getDatabasePlatformsForVersions();
112
113
        self::assertNotEmpty(
114
            $data,
115
            sprintf(
116
                'No test data found for test %s. You have to return test data from %s.',
117
                get_class($this) . '::' . __FUNCTION__,
118
                get_class($this) . '::getDatabasePlatformsForVersions'
119
            )
120
        );
121
122
        foreach ($data as $item) {
123
            $generatedVersion = get_class($this->driver->createDatabasePlatformForVersion($item[0]));
124
125
            self::assertSame(
126
                $item[1],
127
                $generatedVersion,
128
                sprintf(
129
                    'Expected platform for version "%s" should be "%s", "%s" given',
130
                    $item[0],
131
                    $item[1],
132
                    $generatedVersion
133
                )
134
            );
135
        }
136
    }
137
138
    /**
139
     * @expectedException \Doctrine\DBAL\DBALException
140
     */
141
    public function testThrowsExceptionOnCreatingDatabasePlatformsForInvalidVersion()
142
    {
143
        if ( ! $this->driver instanceof VersionAwarePlatformDriver) {
144
            $this->markTestSkipped('This test is only intended for version aware platform drivers.');
145
        }
146
147
        $this->driver->createDatabasePlatformForVersion('foo');
148
    }
149
150 View Code Duplication
    public function testReturnsDatabaseName()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
    {
152
        $params = array(
153
            'user'     => 'foo',
154
            'password' => 'bar',
155
            'dbname'   => 'baz',
156
        );
157
158
        $connection = $this->getConnectionMock();
159
160
        $connection->expects($this->once())
161
            ->method('getParams')
162
            ->will($this->returnValue($params));
163
164
        self::assertSame($params['dbname'], $this->driver->getDatabase($connection));
165
    }
166
167
    public function testReturnsDatabasePlatform()
168
    {
169
        self::assertEquals($this->createPlatform(), $this->driver->getDatabasePlatform());
170
    }
171
172
    public function testReturnsSchemaManager()
173
    {
174
        $connection    = $this->getConnectionMock();
175
        $schemaManager = $this->driver->getSchemaManager($connection);
176
177
        self::assertEquals($this->createSchemaManager($connection), $schemaManager);
178
        self::assertAttributeSame($connection, '_conn', $schemaManager);
179
    }
180
181
    /**
182
     * Factory method for creating the driver instance under test.
183
     *
184
     * @return \Doctrine\DBAL\Driver
185
     */
186
    abstract protected function createDriver();
187
188
    /**
189
     * Factory method for creating the the platform instance return by the driver under test.
190
     *
191
     * The platform instance returned by this method must be the same as returned by
192
     * the driver's getDatabasePlatform() method.
193
     *
194
     * @return \Doctrine\DBAL\Platforms\AbstractPlatform
195
     */
196
    abstract protected function createPlatform();
197
198
    /**
199
     * Factory method for creating the the schema manager instance return by the driver under test.
200
     *
201
     * The schema manager instance returned by this method must be the same as returned by
202
     * the driver's getSchemaManager() method.
203
     *
204
     * @param Connection $connection The underlying connection to use.
205
     *
206
     * @return \Doctrine\DBAL\Schema\AbstractSchemaManager
207
     */
208
    abstract protected function createSchemaManager(Connection $connection);
209
210
    protected function getConnectionMock()
211
    {
212
        return $this->getMockBuilder('Doctrine\DBAL\Connection')
213
            ->disableOriginalConstructor()
214
            ->getMock();
215
    }
216
217
    protected function getDatabasePlatformsForVersions()
218
    {
219
        return array();
220
    }
221
222
    protected function getExceptionConversionData()
223
    {
224
        return array();
225
    }
226
227
    private function getExceptionConversions()
228
    {
229
        $data = array();
230
231
        foreach ($this->getExceptionConversionData() as $convertedExceptionClassName => $errors) {
232
            foreach ($errors as $error) {
233 View Code Duplication
                $driverException = new class ($error[0], $error[1], $error[2])
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
234
                    extends \Exception
235
                    implements DriverException
236
                {
237
                    /**
238
                     * @var mixed
239
                     */
240
                    private $errorCode;
241
242
                    /**
243
                     * @var mixed
244
                     */
245
                    private $sqlState;
246
247
                    public function __construct($errorCode, $sqlState, $message)
248
                    {
249
                        parent::__construct($message);
250
251
                        $this->errorCode = $errorCode;
252
                        $this->sqlState  = $sqlState;
253
                    }
254
255
                    /**
256
                     * {@inheritDoc}
257
                     */
258
                    public function getErrorCode()
259
                    {
260
                        return $this->errorCode;
261
                    }
262
263
                    /**
264
                     * {@inheritDoc}
265
                     */
266
                    public function getSQLState()
267
                    {
268
                        return $this->sqlState;
269
                    }
270
                };
271
272
                $data[] = array($driverException, $convertedExceptionClassName);
273
            }
274
        }
275
276
        return $data;
277
    }
278
}
279