Completed
Push — master ( 09e072...c7757e )
by Luís
15s
created

DBALExceptionTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 74
Duplicated Lines 20.27 %

Importance

Changes 0
Metric Value
wmc 6
dl 15
loc 74
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
testInvalidPlatformTypeScalar() 0 7 ?
testAvoidOverWrappingOnDriverException() 0 23 ?
A hp$0 ➔ testInvalidPlatformTypeObject() 0 7 1
A hp$0 ➔ testInvalidPlatformTypeScalar() 0 7 1
A hp$0 ➔ testDriverRequiredWithUrl() 13 13 1
A hp$0 ➔ getSQLState() 0 2 1
A hp$0 ➔ testAvoidOverWrappingOnDriverException() 0 23 1
A testDriverExceptionDuringQueryAcceptsBinaryData() 0 6 1
testInvalidPlatformTypeObject() 0 7 ?
testDriverRequiredWithUrl() 13 13 ?
A hp$0 ➔ getErrorCode() 0 2 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Doctrine\Tests\DBAL;
4
5
use Doctrine\DBAL\DBALException;
6
use Doctrine\DBAL\Exception\DriverException;
7
use Doctrine\DBAL\Driver\DriverException as InnerDriverException;
8
use Doctrine\Tests\DbalTestCase;
9
use Doctrine\DBAL\Driver;
10
11
class DBALExceptionTest extends DbalTestCase
12
{
13
    public function testDriverExceptionDuringQueryAcceptsBinaryData()
14
    {
15
        /* @var $driver Driver */
16
        $driver = $this->createMock(Driver::class);
17
        $e = DBALException::driverExceptionDuringQuery($driver, new \Exception, '', array('ABC', chr(128)));
18
        self::assertContains('with params ["ABC", "\x80"]', $e->getMessage());
19
    }
20
21
    public function testAvoidOverWrappingOnDriverException()
22
    {
23
        /* @var $driver Driver */
24
        $driver = $this->createMock(Driver::class);
25
        $inner = new class extends \Exception implements InnerDriverException
26
        {
27
            /**
28
             * {@inheritDoc}
29
             */
30
            public function getErrorCode()
31
            {
32
            }
33
34
            /**
35
             * {@inheritDoc}
36
             */
37
            public function getSQLState()
38
            {
39
            }
40
        };
41
        $ex = new DriverException('', $inner);
42
        $e = DBALException::driverExceptionDuringQuery($driver, $ex, '');
43
        self::assertSame($ex, $e);
44
    }
45
46 View Code Duplication
    public function testDriverRequiredWithUrl()
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...
47
    {
48
        $url = 'mysql://localhost';
49
        $exception = DBALException::driverRequired($url);
50
51
        self::assertInstanceOf(DBALException::class, $exception);
52
        self::assertSame(
53
            sprintf(
54
                "The options 'driver' or 'driverClass' are mandatory if a connection URL without scheme " .
55
                'is given to DriverManager::getConnection(). Given URL: %s',
56
                $url
57
            ),
58
            $exception->getMessage()
59
        );
60
    }
61
62
    /**
63
     * @group #2821
64
     */
65
    public function testInvalidPlatformTypeObject(): void
66
    {
67
        $exception = DBALException::invalidPlatformType(new \stdClass());
68
69
        self::assertSame(
70
            "Option 'platform' must be a subtype of 'Doctrine\DBAL\Platforms\AbstractPlatform', instance of 'stdClass' given",
71
            $exception->getMessage()
72
        );
73
    }
74
75
    /**
76
     * @group #2821
77
     */
78
    public function testInvalidPlatformTypeScalar(): void
79
    {
80
        $exception = DBALException::invalidPlatformType('some string');
81
82
        self::assertSame(
83
            "Option 'platform' must be an object and subtype of 'Doctrine\DBAL\Platforms\AbstractPlatform'. Got 'string'",
84
            $exception->getMessage()
85
        );
86
    }
87
}
88