Skip to content

Commit

Permalink
Fix tests (#323)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov authored Oct 13, 2024
1 parent a7288c5 commit b0213bf
Show file tree
Hide file tree
Showing 19 changed files with 67 additions and 43 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:

env:
COMPOSER_ROOT_VERSION: 1.0.0
EXTENSIONS: pdo, pdo_sqlsrv-5.10.1
EXTENSIONS: pdo, pdo_sqlsrv-5.12

runs-on: ${{ matrix.mssql.os || 'ubuntu-latest' }}

Expand Down Expand Up @@ -63,6 +63,11 @@ jobs:
options: --name=mssql --health-cmd="/opt/mssql-tools${{ matrix.mssql.odbc-version }}/bin/sqlcmd ${{ matrix.mssql.flag }} -S localhost -U SA -P 'YourStrong!Passw0rd' -Q 'SELECT 1'" --health-interval=10s --health-timeout=5s --health-retries=3

steps:
- name: Install ODBC driver.
run: |
sudo curl https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/prod.list | sudo tee /etc/apt/sources.list.d/mssql-release.list
sudo ACCEPT_EULA=Y apt-get install -y msodbcsql18
- name: Checkout.
uses: actions/checkout@v3

Expand Down
7 changes: 6 additions & 1 deletion .github/workflows/mutation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:

env:
COMPOSER_ROOT_VERSION: 1.0.0
EXTENSIONS: pdo, pdo_sqlsrv-5.10.1
EXTENSIONS: pdo, pdo_sqlsrv-5.12

runs-on: ${{ matrix.os }}

Expand All @@ -44,6 +44,11 @@ jobs:
options: --name=mssql --health-cmd="/opt/mssql-tools18/bin/sqlcmd -S localhost -U SA -P 'YourStrong!Passw0rd' -C -Q 'SELECT 1'" --health-interval=10s --health-timeout=5s --health-retries=3

steps:
- name: Install ODBC driver.
run: |
sudo curl https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/prod.list | sudo tee /etc/apt/sources.list.d/mssql-release.list
sudo ACCEPT_EULA=Y apt-get install -y msodbcsql18
- name: Checkout.
uses: actions/checkout@v3

Expand Down
40 changes: 27 additions & 13 deletions src/Dsn.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
*/
final class Dsn extends AbstractDsn
{
/**
* @psalm-param array<string,string> $options
*/
public function __construct(
private string $driver,
private string $host,
private string|null $databaseName = null,
private string $port = '1433'
string $driver = 'sqlsrv',
string $host = 'localhost',
string|null $databaseName = null,
string|null $port = '1433',
array $options = []
) {
parent::__construct($driver, $host, $databaseName, $port);
parent::__construct($driver, $host, $databaseName, $port, $options);
}

/**
Expand All @@ -39,16 +43,26 @@ public function __construct(
*/
public function asString(): string
{
if ($this->port !== '') {
$server = "Server=$this->host,$this->port;";
} else {
$server = "Server=$this->host;";
$driver = $this->getDriver();
$host = $this->getHost();
$port = $this->getPort();
$databaseName = $this->getDatabaseName();
$options = $this->getOptions();

$dsn = "$driver:Server=$host";

if (!empty($port)) {
$dsn .= ",$port";
}

if (!empty($databaseName)) {
$dsn .= ";Database=$databaseName";
}

if (!empty($this->databaseName)) {
$dsn = "$this->driver:" . $server . "Database=$this->databaseName";
} else {
$dsn = "$this->driver:" . $server;
if (!empty($options)) {
foreach ($options as $key => $value) {

Check failure on line 63 in src/Dsn.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

MixedAssignment

src/Dsn.php:63:42: MixedAssignment: Unable to determine the type that $value is being assigned to (see https://psalm.dev/032)

Check failure on line 63 in src/Dsn.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

MixedAssignment

src/Dsn.php:63:42: MixedAssignment: Unable to determine the type that $value is being assigned to (see https://psalm.dev/032)

Check failure on line 63 in src/Dsn.php

View workflow job for this annotation

GitHub Actions / PHP 8.3

MixedAssignment

src/Dsn.php:63:42: MixedAssignment: Unable to determine the type that $value is being assigned to (see https://psalm.dev/032)
$dsn .= ";$key=$value";
}
}

return $dsn;
Expand Down
4 changes: 2 additions & 2 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,15 +313,15 @@ public function testDropDefaultValueSql(): void

public function testShowDatabases(): void
{
$dsn = new Dsn('sqlsrv', 'localhost');
$dsn = new Dsn(options: ['Encrypt' => 'no']);
$db = new Connection(
new Driver($dsn->asString(), 'SA', 'YourStrong!Passw0rd'),
DbHelper::getSchemaCache(),
);

$command = $db->createCommand();

$this->assertSame('sqlsrv:Server=localhost,1433;', $db->getDriver()->getDsn());
$this->assertSame('sqlsrv:Server=localhost,1433;Encrypt=no', $db->getDriver()->getDsn());
$this->assertSame(['yiitest'], $command->showDatabases());
}

Expand Down
6 changes: 3 additions & 3 deletions tests/DsnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ public function testAsString(): void

public function testAsStringWithDatabaseName(): void
{
$this->assertSame('sqlsrv:Server=127.0.0.1,1433;', (new Dsn('sqlsrv', '127.0.0.1'))->asString());
$this->assertSame('sqlsrv:Server=127.0.0.1,1433', (new Dsn('sqlsrv', '127.0.0.1'))->asString());
}

public function testAsStringWithDatabaseNameWithEmptyString(): void
{
$this->assertSame('sqlsrv:Server=127.0.0.1,1433;', (new Dsn('sqlsrv', '127.0.0.1', ''))->asString());
$this->assertSame('sqlsrv:Server=127.0.0.1,1433', (new Dsn('sqlsrv', '127.0.0.1', ''))->asString());
}

public function testAsStringWithDatabaseNameWithNull(): void
{
$this->assertSame('sqlsrv:Server=127.0.0.1,1433;', (new Dsn('sqlsrv', '127.0.0.1', null))->asString());
$this->assertSame('sqlsrv:Server=127.0.0.1,1433', (new Dsn('sqlsrv', '127.0.0.1', null))->asString());
}

public function testAsStringWithEmptyPort(): void
Expand Down
4 changes: 2 additions & 2 deletions tests/Support/TestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ protected function getConnection(bool $fixture = false): PdoConnectionInterface

protected static function getDb(): PdoConnectionInterface
{
$dsn = (new Dsn('sqlsrv', 'localhost', 'yiitest'))->asString();
$dsn = (new Dsn(databaseName: 'yiitest', options: ['Encrypt' => 'no']))->asString();

return new Connection(
new Driver($dsn, 'SA', 'YourStrong!Passw0rd'),
Expand All @@ -48,7 +48,7 @@ protected static function getDb(): PdoConnectionInterface
protected function getDsn(): string
{
if ($this->dsn === '') {
$this->dsn = (new Dsn('sqlsrv', 'localhost', 'yiitest'))->asString();
$this->dsn = (new Dsn(databaseName: 'yiitest', options: ['Encrypt' => 'no']))->asString();
}

return $this->dsn;
Expand Down
2 changes: 1 addition & 1 deletion tests/Type/CharTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public function testValueException(): void

$this->expectException(Exception::class);
$this->expectExceptionMessage(
'[ODBC Driver 17 for SQL Server][SQL Server]String or binary data would be truncated'
'[SQL Server]String or binary data would be truncated'
);

$command->insert('char', ['Mychar1' => '01234567891'])->execute();
Expand Down
2 changes: 1 addition & 1 deletion tests/Type/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public function testValueException(): void

$this->expectException(Exception::class);
$this->expectExceptionMessage(
'SQLSTATE[22007]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Conversion failed when converting date and/or time from character string.'
'[SQL Server]Conversion failed when converting date and/or time from character string.'
);

$db->createCommand()->insert('date', ['Mydate1' => '0000-00-00'])->execute();
Expand Down
4 changes: 2 additions & 2 deletions tests/Type/DecimalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public function testMaxValueException(): void

$this->expectException(Exception::class);
$this->expectExceptionMessage(
"SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The number '199999999999999997748809823456034029570' is out of the range for numeric representation (maximum precision 38)."
"[SQL Server]The number '199999999999999997748809823456034029570' is out of the range for numeric representation (maximum precision 38)."
);

$command->insert(
Expand Down Expand Up @@ -266,7 +266,7 @@ public function testMinValueException(): void

$this->expectException(Exception::class);
$this->expectExceptionMessage(
"SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The number '199999999999999997748809823456034029570' is out of the range for numeric representation (maximum precision 38)."
"[SQL Server]The number '199999999999999997748809823456034029570' is out of the range for numeric representation (maximum precision 38)."
);

$command->insert(
Expand Down
4 changes: 2 additions & 2 deletions tests/Type/FloatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public function testMaxValueException(): void

$this->expectException(Exception::class);
$this->expectExceptionMessage(
"SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The floating point value '1.80E+308' is out of the range of computer representation (8 bytes)."
"[SQL Server]The floating point value '1.80E+308' is out of the range of computer representation (8 bytes)."
);

$command->insert('float', ['Myfloat1' => new Expression('1.80E+308')])->execute();
Expand Down Expand Up @@ -256,7 +256,7 @@ public function testMinValueException(): void

$this->expectException(Exception::class);
$this->expectExceptionMessage(
"SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The floating point value '1.80E+308' is out of the range of computer representation (8 bytes)."
"[SQL Server]The floating point value '1.80E+308' is out of the range of computer representation (8 bytes)."
);

$command->insert('float', ['Myfloat1' => new Expression('-1.80E+308')])->execute();
Expand Down
4 changes: 2 additions & 2 deletions tests/Type/IntTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function testMaxValueException(): void

$this->expectException(Exception::class);
$this->expectExceptionMessage(
'SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Arithmetic overflow'
'[SQL Server]Arithmetic overflow'
);

$command->insert('int', ['Myint1' => 2_147_483_648])->execute();
Expand Down Expand Up @@ -252,7 +252,7 @@ public function testMinValueException(): void

$this->expectException(Exception::class);
$this->expectExceptionMessage(
'SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Arithmetic overflow'
'[SQL Server]Arithmetic overflow'
);

$command->insert('int', ['Myint1' => -2_147_483_649])->execute();
Expand Down
4 changes: 2 additions & 2 deletions tests/Type/MoneyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function testMaxValueException(): void

$this->expectException(Exception::class);
$this->expectExceptionMessage(
'SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Arithmetic overflow error converting expression to data type money.'
'[SQL Server]Arithmetic overflow error converting expression to data type money.'
);

$command->insert('money', ['Mymoney1' => '922337203685478.5808'])->execute();
Expand Down Expand Up @@ -252,7 +252,7 @@ public function testMinValueException(): void

$this->expectException(Exception::class);
$this->expectExceptionMessage(
'SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Arithmetic overflow error converting expression to data type money.'
'[SQL Server]Arithmetic overflow error converting expression to data type money.'
);

$command->insert('money', ['Mymoney1' => '-922337203685480.5808'])->execute();
Expand Down
2 changes: 1 addition & 1 deletion tests/Type/NumericTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ public function testMinValueException(): void

$this->expectException(Exception::class);
$this->expectExceptionMessage(
"SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The number '199999999999999997748809823456034029570' is out of the range for numeric representation (maximum precision 38)."
"[SQL Server]The number '199999999999999997748809823456034029570' is out of the range for numeric representation (maximum precision 38)."
);

$command->insert(
Expand Down
4 changes: 2 additions & 2 deletions tests/Type/RealTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public function testMaxValueException(): void

$this->expectException(Exception::class);
$this->expectExceptionMessage(
'SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Arithmetic overflow error for type real'
'[SQL Server]Arithmetic overflow error for type real'
);

$command->insert('real', ['Myreal1' => new Expression('4.4E+38')])->execute();
Expand Down Expand Up @@ -253,7 +253,7 @@ public function testMinValueException(): void

$this->expectException(Exception::class);
$this->expectExceptionMessage(
'SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Arithmetic overflow error for type real'
'[SQL Server]Arithmetic overflow error for type real'
);

$command->insert('real', ['Myreal1' => new Expression('-4.4E+38')])->execute();
Expand Down
4 changes: 2 additions & 2 deletions tests/Type/SmallIntTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function testMaxValueException(): void

$this->expectException(Exception::class);
$this->expectExceptionMessage(
'SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Arithmetic overflow'
'[SQL Server]Arithmetic overflow'
);

$command->insert('smallint', ['Mysmallint1' => 32768])->execute();
Expand Down Expand Up @@ -252,7 +252,7 @@ public function testMinValueException(): void

$this->expectException(Exception::class);
$this->expectExceptionMessage(
'SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Arithmetic overflow'
'[SQL Server]Arithmetic overflow'
);

$command->insert('smallint', ['Mysmallint1' => -32769])->execute();
Expand Down
4 changes: 2 additions & 2 deletions tests/Type/SmallMoneyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function testMaxValueException(): void

$this->expectException(Exception::class);
$this->expectExceptionMessage(
'SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Arithmetic overflow error converting expression to data type smallmoney.'
'[SQL Server]Arithmetic overflow error converting expression to data type smallmoney.'
);

$command->insert('smallmoney', ['Mysmallmoney1' => '214749.3647'])->execute();
Expand Down Expand Up @@ -252,7 +252,7 @@ public function testMinValueException(): void

$this->expectException(Exception::class);
$this->expectExceptionMessage(
'SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Arithmetic overflow error converting expression to data type smallmoney.'
'[SQL Server]Arithmetic overflow error converting expression to data type smallmoney.'
);

$command->insert('smallmoney', ['Mysmallmoney1' => '-214749.3648'])->execute();
Expand Down
4 changes: 2 additions & 2 deletions tests/Type/TinyIntTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public function testMaxValueException(): void

$this->expectException(Exception::class);
$this->expectExceptionMessage(
'SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Arithmetic overflow'
'[SQL Server]Arithmetic overflow'
);

$command->insert('tinyint', ['Mytinyint1' => 256])->execute();
Expand Down Expand Up @@ -267,7 +267,7 @@ public function testMinValueException(): void

$this->expectException(Exception::class);
$this->expectExceptionMessage(
'SQLSTATE[22003]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Arithmetic overflow'
'[SQL Server]Arithmetic overflow'
);

$command->insert('tinyint', ['Mytinyint1' => -1])->execute();
Expand Down
2 changes: 1 addition & 1 deletion tests/Type/UniqueidentifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function testValueException(): void

$this->expectException(Exception::class);
$this->expectExceptionMessage(
'SQLSTATE[42000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Conversion failed when converting from a character string to uniqueidentifier.'
'[SQL Server]Conversion failed when converting from a character string to uniqueidentifier.'
);

$command = $db->createCommand();
Expand Down
2 changes: 1 addition & 1 deletion tests/Type/VarCharTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public function testValueException(): void

$this->expectException(Exception::class);
$this->expectExceptionMessage(
'[Microsoft][ODBC Driver 17 for SQL Server][SQL Server]String or binary data would be truncated'
'[SQL Server]String or binary data would be truncated'
);

$command->insert('varchar', ['Myvarchar1' => '01234567891'])->execute();
Expand Down

0 comments on commit b0213bf

Please sign in to comment.