Failed Conditions
Pull Request — master (#3033)
by Aleksey
15:08
created

AbstractSQLServerDriverTest::testDriverOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Doctrine\Tests\DBAL\Driver;
4
5
use Doctrine\DBAL\Connection;
6
use Doctrine\DBAL\Driver\AbstractSQLServerDriver;
7
use Doctrine\DBAL\Driver\Connection as DriverConnection;
8
use Doctrine\DBAL\Driver\PDOSqlsrv\Driver as PDOSqlsrvDriver;
9
use Doctrine\DBAL\Driver\SQLSrv\Driver as SQLSrvDriver;
10
use Doctrine\DBAL\Platforms\SQLServer2008Platform;
11
use Doctrine\DBAL\Schema\SQLServerSchemaManager;
12
use PDO;
13
14
class AbstractSQLServerDriverTest extends AbstractDriverTest
15
{
16
    protected function createDriver()
17
    {
18
        return $this->getMockForAbstractClass('Doctrine\DBAL\Driver\AbstractSQLServerDriver');
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->getMockFor...stractSQLServerDriver') 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...
19
    }
20
21
    protected function createPlatform()
22
    {
23
        return new SQLServer2008Platform();
24
    }
25
26
    protected function createSchemaManager(Connection $connection)
27
    {
28
        return new SQLServerSchemaManager($connection);
29
    }
30
31
    protected function getDatabasePlatformsForVersions()
32
    {
33
        return array(
34
            array('9', 'Doctrine\DBAL\Platforms\SQLServerPlatform'),
35
            array('9.00', 'Doctrine\DBAL\Platforms\SQLServerPlatform'),
36
            array('9.00.0', 'Doctrine\DBAL\Platforms\SQLServerPlatform'),
37
            array('9.00.1398', 'Doctrine\DBAL\Platforms\SQLServerPlatform'),
38
            array('9.00.1398.99', 'Doctrine\DBAL\Platforms\SQLServerPlatform'),
39
            array('9.00.1399', 'Doctrine\DBAL\Platforms\SQLServer2005Platform'),
40
            array('9.00.1399.0', 'Doctrine\DBAL\Platforms\SQLServer2005Platform'),
41
            array('9.00.1399.99', 'Doctrine\DBAL\Platforms\SQLServer2005Platform'),
42
            array('9.00.1400', 'Doctrine\DBAL\Platforms\SQLServer2005Platform'),
43
            array('9.10', 'Doctrine\DBAL\Platforms\SQLServer2005Platform'),
44
            array('9.10.9999', 'Doctrine\DBAL\Platforms\SQLServer2005Platform'),
45
            array('10.00.1599', 'Doctrine\DBAL\Platforms\SQLServer2005Platform'),
46
            array('10.00.1599.99', 'Doctrine\DBAL\Platforms\SQLServer2005Platform'),
47
            array('10.00.1600', 'Doctrine\DBAL\Platforms\SQLServer2008Platform'),
48
            array('10.00.1600.0', 'Doctrine\DBAL\Platforms\SQLServer2008Platform'),
49
            array('10.00.1600.99', 'Doctrine\DBAL\Platforms\SQLServer2008Platform'),
50
            array('10.00.1601', 'Doctrine\DBAL\Platforms\SQLServer2008Platform'),
51
            array('10.10', 'Doctrine\DBAL\Platforms\SQLServer2008Platform'),
52
            array('10.10.9999', 'Doctrine\DBAL\Platforms\SQLServer2008Platform'),
53
            array('11.00.2099', 'Doctrine\DBAL\Platforms\SQLServer2008Platform'),
54
            array('11.00.2099.99', 'Doctrine\DBAL\Platforms\SQLServer2008Platform'),
55
            array('11.00.2100', 'Doctrine\DBAL\Platforms\SQLServer2012Platform'),
56
            array('11.00.2100.0', 'Doctrine\DBAL\Platforms\SQLServer2012Platform'),
57
            array('11.00.2100.99', 'Doctrine\DBAL\Platforms\SQLServer2012Platform'),
58
            array('11.00.2101', 'Doctrine\DBAL\Platforms\SQLServer2012Platform'),
59
            array('12', 'Doctrine\DBAL\Platforms\SQLServer2012Platform'),
60
        );
61
    }
62
63
    protected function getDriver() : AbstractSQLServerDriver
64
    {
65
        return static::createDriver();
0 ignored issues
show
Bug Best Practice introduced by
The expression return static::createDriver() returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the type-hinted return Doctrine\DBAL\Driver\AbstractSQLServerDriver.
Loading history...
Bug Best Practice introduced by
The method Doctrine\Tests\DBAL\Driv...verTest::createDriver() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
        return static::/** @scrutinizer ignore-call */ createDriver();
Loading history...
66
    }
67
68
    protected function checkForSkippingTest(AbstractSQLServerDriver $driver) : void
69
    {
70
        if (extension_loaded('sqlsrv') && $driver instanceof SQLSrvDriver
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: {currentAssign}, Probably Intended Meaning: {alternativeAssign}
Loading history...
71
            || extension_loaded('pdo_sqlsrv') && $driver instanceof PDOSqlsrvDriver) {
72
            return;
73
        }
74
75
        $this->markTestSkipped('The test is only for the sqlsrv and the pdo_sqlsrv drivers');
76
    }
77
78
    protected function getConnection(AbstractSQLServerDriver $driver, array $driverOptions) : DriverConnection
79
    {
80
        return $driver->connect(
81
            [
82
                'host' => $GLOBALS['db_host'],
83
                'port' => $GLOBALS['db_port'],
84
            ],
85
            $GLOBALS['db_username'],
86
            $GLOBALS['db_password'],
87
            $driverOptions
88
        );
89
    }
90
91
    public function testConnectionOptions() : void
92
    {
93
        $driver = $this->getDriver();
94
        $this->checkForSkippingTest($driver);
95
        $connection = $this->getConnection($driver, ['APP' => 'APP_NAME']);
96
        $result     = $connection->query('select APP_NAME() as app')->fetch();
0 ignored issues
show
Unused Code introduced by
The call to Doctrine\DBAL\Driver\Connection::query() has too many arguments starting with 'select APP_NAME() as app'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
        $result     = $connection->/** @scrutinizer ignore-call */ query('select APP_NAME() as app')->fetch();

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
97
98
        self::assertSame('APP_NAME', $result['app']);
99
    }
100
101
    public function testDriverOptions() : void
102
    {
103
        $driver = $this->getDriver();
104
        $this->checkForSkippingTest($driver);
105
        $connection = $this->getConnection($driver, [PDO::ATTR_CASE => PDO::CASE_UPPER]);
106
107
        self::assertSame(PDO::CASE_UPPER, $connection->getAttribute(PDO::ATTR_CASE));
0 ignored issues
show
Bug introduced by
The method getAttribute() does not exist on Doctrine\DBAL\Driver\Connection. It seems like you code against a sub-type of Doctrine\DBAL\Driver\Connection such as Doctrine\DBAL\Driver\PDOSqlsrv\Connection or Doctrine\DBAL\Driver\PDOConnection. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
        self::assertSame(PDO::CASE_UPPER, $connection->/** @scrutinizer ignore-call */ getAttribute(PDO::ATTR_CASE));
Loading history...
108
    }
109
}
110