Completed
Pull Request — master (#3297)
by Sergei
12:24
created

AbstractOracleDriverTest::testEasyConnectString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Driver;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Driver\AbstractOracleDriver;
7
use Doctrine\DBAL\Platforms\OraclePlatform;
8
use Doctrine\DBAL\Schema\OracleSchemaManager;
9
use ReflectionMethod;
10
11
class AbstractOracleDriverTest extends AbstractDriverTest
12
{
13
    public function testReturnsDatabaseName()
14
    {
15
        $params = array(
16
            'user'     => 'foo',
17
            'password' => 'bar',
18
            'dbname'   => 'baz',
19
        );
20
21
        $connection = $this->getConnectionMock();
22
23
        $connection->expects($this->once())
24
            ->method('getParams')
25
            ->will($this->returnValue($params));
26
27
        self::assertSame($params['user'], $this->driver->getDatabase($connection));
28
    }
29
30
    public function testReturnsDatabaseNameWithConnectDescriptor()
31
    {
32
        $params = array(
33
            'user'             => 'foo',
34
            'password'         => 'bar',
35
            'connectionstring' => '(DESCRIPTION=' .
36
                '(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))' .
37
                '(CONNECT_DATA=(SERVICE_NAME=baz)))'
38
        );
39
40
        $connection = $this->getConnectionMock();
41
42
        $connection->expects($this->once())
43
            ->method('getParams')
44
            ->will($this->returnValue($params));
45
46
        self::assertSame($params['user'], $this->driver->getDatabase($connection));
47
    }
48
49
    /**
50
     * @param mixed[] $params
51
     * @dataProvider easyConnectStringProvider
52
     */
53
    public function testEasyConnectString(array $params, string $expected) : void
54
    {
55
        $driver = $this->createMock(AbstractOracleDriver::class);
56
57
        $reflection = new ReflectionMethod($driver, 'getEasyConnectString');
58
        $reflection->setAccessible(true);
59
60
        $this->assertSame($expected, $reflection->invoke($driver, $params));
61
    }
62
63
    /**
64
     * @return mixed[]
65
     */
66
    public static function easyConnectStringProvider() : iterable
67
    {
68
        return [
69
            'empty-params' => [[],''],
70
            'common-params' => [
71
                [
72
                    'host' => 'oracle.example.com',
73
                    'port' => 1521,
74
                    'dbname' => 'XE',
75
                ],
76
                '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=oracle.example.com)(PORT=1521))(CONNECT_DATA=(SID=XE)))',
77
            ],
78
            'no-db-name' => [
79
                ['host' => 'localhost'],
80
                '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))',
81
            ],
82
            'service' => [
83
                [
84
                    'host' => 'localhost',
85
                    'port' => 1521,
86
                    'service' => true,
87
                    'servicename' => 'BILLING',
88
                ],
89
                '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=BILLING)))',
90
            ],
91
            'advanced-params' => [
92
                [
93
                    'host' => 'localhost',
94
                    'port' => 41521,
95
                    'dbname' => 'XE',
96
                    'instancename' => 'SALES',
97
                    'pooled' => true,
98
                ],
99
                '(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=41521))(CONNECT_DATA=(SID=XE)(INSTANCE_NAME=SALES)(SERVER=POOLED)))',
100
            ],
101
        ];
102
    }
103
104
    protected function createDriver()
105
    {
106
        return $this->getMockForAbstractClass('Doctrine\DBAL\Driver\AbstractOracleDriver');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getMockFor...\AbstractOracleDriver') returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the return type mandated by Doctrine\Tests\DBAL\Driv...verTest::createDriver() of Doctrine\DBAL\Driver.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
107
    }
108
109
    protected function createPlatform()
110
    {
111
        return new OraclePlatform();
112
    }
113
114
    protected function createSchemaManager(Connection $connection)
115
    {
116
        return new OracleSchemaManager($connection);
117
    }
118
119
    protected function getExceptionConversionData()
120
    {
121
        return array(
122
            self::EXCEPTION_CONNECTION => array(
123
                array('1017', null, null),
124
                array('12545', null, null),
125
            ),
126
            self::EXCEPTION_FOREIGN_KEY_CONSTRAINT_VIOLATION => array(
127
                array('2292', null, null),
128
            ),
129
            self::EXCEPTION_INVALID_FIELD_NAME => array(
130
                array('904', null, null),
131
            ),
132
            self::EXCEPTION_NON_UNIQUE_FIELD_NAME => array(
133
                array('918', null, null),
134
                array('960', null, null),
135
            ),
136
            self::EXCEPTION_NOT_NULL_CONSTRAINT_VIOLATION => array(
137
                array('1400', null, null),
138
            ),
139
            self::EXCEPTION_SYNTAX_ERROR => array(
140
                array('923', null, null),
141
            ),
142
            self::EXCEPTION_TABLE_EXISTS => array(
143
                array('955', null, null),
144
            ),
145
            self::EXCEPTION_TABLE_NOT_FOUND => array(
146
                array('942', null, null),
147
            ),
148
            self::EXCEPTION_UNIQUE_CONSTRAINT_VIOLATION => array(
149
                array('1', null, null),
150
                array('2299', null, null),
151
                array('38911', null, null),
152
            ),
153
        );
154
    }
155
}
156