Skip to content

Commit

Permalink
Add tests for handlers (data) (#60)
Browse files Browse the repository at this point in the history
* Add tests for handlers (data)

* Apply fixes from StyleCI

* Fix mssql (certificate) and dropping table

* WIP

* Fix psalm

* WIP

* WIP

* WIP

* Apply fixes from StyleCI

* Fix mssql

* An attempt to fix Oracle

* An attempt to fix Oracle

* Fix

* Fix

* Fix

* Fix

* Fix

* Fix

* Fix

* Fix

* Fix

* Fix

---------

Co-authored-by: StyleCI Bot <[email protected]>
  • Loading branch information
arogachev and StyleCIBot authored Oct 17, 2024
1 parent 2674b0a commit b3e1b3f
Show file tree
Hide file tree
Showing 89 changed files with 1,145 additions and 26 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/mssql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
name: PHP ${{ matrix.php }}-mssql-${{ matrix.mssql.server }}

env:
extensions: pdo, pdo_sqlsrv-5.11.1
extensions: pdo, pdo_sqlsrv-5.12

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

Expand Down Expand Up @@ -62,6 +62,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
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
},
"autoload-dev": {
"psr-4": {
"Yiisoft\\Data\\Db\\Tests\\": "tests"
"Yiisoft\\Data\\Db\\Tests\\": "tests",
"Yiisoft\\Data\\Tests\\": "vendor/yiisoft/data/tests"
},
"files": ["tests/bootstrap.php"]
},
Expand Down
3 changes: 3 additions & 0 deletions src/AbstractQueryDataReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ abstract class AbstractQueryDataReader implements QueryDataReaderInterface
private ?Sort $sort = null;
private ?FilterInterface $filter = null;
private ?FilterInterface $having = null;
/**
* @psalm-var non-negative-int|null
*/
private ?int $limit = null;
private int $offset = 0;

Expand Down
20 changes: 20 additions & 0 deletions src/Exception/NotSupportedFilterOptionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Db\Exception;

use InvalidArgumentException;
use Throwable;

final class NotSupportedFilterOptionException extends InvalidArgumentException
{
/**
* @param string $optionName Option name in filter.
* @param string $driverName Driver name of database.
*/
public function __construct(string $optionName, string $driverName, int $code = 0, ?Throwable $previous = null)
{
parent::__construct("\$$optionName option is not supported when using $driverName driver.", $code, $previous);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use DateTime;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Yiisoft\Data\Db\QueryDataReader;
use Yiisoft\Data\Db\Tests\Support\CustomerDataReader;
use Yiisoft\Data\Db\Tests\Support\CustomerDTO;
Expand All @@ -26,8 +27,10 @@
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Query\Query;

abstract class QueryDataReaderTest extends TestCase
abstract class BaseQueryDataReaderTestCase extends TestCase
{
use DataTrait;

public function testDataReader(): void
{
$db = $this->getConnection();
Expand Down
115 changes: 115 additions & 0 deletions tests/Base/DataTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Db\Tests\Base;

use DateTime;
use Yiisoft\Data\Db\QueryDataReader;
use Yiisoft\Data\Reader\DataReaderInterface;
use Yiisoft\Data\Tests\Common\FixtureTrait;
use Yiisoft\Db\Connection\ConnectionInterface;
use Yiisoft\Db\Driver\Pdo\PdoConnectionInterface;
use Yiisoft\Db\Expression\Expression;
use Yiisoft\Db\Query\Query;

trait DataTrait
{
use FixtureTrait;

protected static ?PdoConnectionInterface $connection = null;

abstract protected function makeConnection(): PdoConnectionInterface;

protected function getConnection(): PdoConnectionInterface
{
if (self::$connection === null) {
self::$connection = $this->makeConnection();
}

return self::$connection;
}

protected function setUp(): void
{
$this->populateDatabase();
}

protected function tearDown(): void
{
$this->dropDatabase();
}

protected function getReader(): DataReaderInterface
{
/** @var PdoConnectionInterface $db */
$db = $this->getConnection();

return new QueryDataReader((new Query($db))->from('user'));
}

protected function assertFixtures(array $expectedFixtureIndexes, array $actualFixtures): void
{
$processedActualFixtures = [];
foreach ($actualFixtures as $fixture) {
if (is_object($fixture)) {
$fixture = json_decode(json_encode($fixture), associative: true);
}

unset($fixture['id']);
$fixture['number'] = (int) $fixture['number'];
$fixture['balance'] = (float) $fixture['balance'];

if ($fixture['born_at'] !== null && $this->getConnection()->getDriverName() === 'oci') {
$fixture['born_at'] = DateTime::createFromFormat('d-M-y', $fixture['born_at'])->format('Y-m-d');
}

$processedActualFixtures[$fixture['number'] - 1] = $fixture;
}

parent::assertFixtures($expectedFixtureIndexes, $processedActualFixtures);
}

protected function populateDatabase(): void
{
/** @var PdoConnectionInterface $db */
$db = $this->getConnection();
if ($db->getSchema()->getTableSchema('{{%user}}') !== null) {
return;
}

$db
->createCommand()
->createTable(
'{{%user}}',
[
'id' => 'pk',
'number' => 'integer NOT NULL',
'email' => 'string(255) NOT NULL',
'balance' => 'float DEFAULT 0.0 NOT NULL',
'born_at' => 'date',
],
)
->execute();

$db->transaction(static function (ConnectionInterface $database): void {
foreach (self::$fixtures as $fixture) {
if ($fixture['born_at'] !== null && $database->getDriverName() === 'oci') {
$fixture['born_at'] = new Expression(
"TO_DATE(:born_at, 'yyyy-mm-dd')",
[':born_at' => $fixture['born_at']],
);
}

$database->createCommand()->insert('{{%user}}', $fixture)->execute();
}
});
}

protected function dropDatabase(): void
{
/** @var PdoConnectionInterface $db */
$db = $this->getConnection();
$db->createCommand()->dropTable('{{%user}}')->execute();
}
}
12 changes: 12 additions & 0 deletions tests/Base/Reader/ReaderWithFilter/BaseReaderWithAllTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Db\Tests\Base\Reader\ReaderWithFilter;

use Yiisoft\Data\Db\Tests\Base\DataTrait;

abstract class BaseReaderWithAllTestCase extends \Yiisoft\Data\Tests\Common\Reader\ReaderWithFilter\BaseReaderWithAllTestCase
{
use DataTrait;
}
12 changes: 12 additions & 0 deletions tests/Base/Reader/ReaderWithFilter/BaseReaderWithAnyTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Db\Tests\Base\Reader\ReaderWithFilter;

use Yiisoft\Data\Db\Tests\Base\DataTrait;

abstract class BaseReaderWithAnyTestCase extends \Yiisoft\Data\Tests\Common\Reader\ReaderWithFilter\BaseReaderWithAnyTestCase
{
use DataTrait;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Db\Tests\Base\Reader\ReaderWithFilter;

use Yiisoft\Data\Db\Tests\Base\DataTrait;

abstract class BaseReaderWithBetweenTestCase extends \Yiisoft\Data\Tests\Common\Reader\ReaderWithFilter\BaseReaderWithBetweenTestCase
{
use DataTrait;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Db\Tests\Base\Reader\ReaderWithFilter;

use Yiisoft\Data\Db\Tests\Base\DataTrait;

abstract class BaseReaderWithEqualsNullTestCase extends \Yiisoft\Data\Tests\Common\Reader\ReaderWithFilter\BaseReaderWithEqualsNullTestCase
{
use DataTrait;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Db\Tests\Base\Reader\ReaderWithFilter;

use Yiisoft\Data\Db\Tests\Base\DataTrait;

abstract class BaseReaderWithEqualsTestCase extends \Yiisoft\Data\Tests\Common\Reader\ReaderWithFilter\BaseReaderWithEqualsTestCase
{
use DataTrait;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Db\Tests\Base\Reader\ReaderWithFilter;

use Yiisoft\Data\Db\Tests\Base\DataTrait;

abstract class BaseReaderWithGreaterThanOrEqualTestCase extends \Yiisoft\Data\Tests\Common\Reader\ReaderWithFilter\BaseReaderWithGreaterThanOrEqualTestCase
{
use DataTrait;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Db\Tests\Base\Reader\ReaderWithFilter;

use Yiisoft\Data\Db\Tests\Base\DataTrait;

abstract class BaseReaderWithGreaterThanTestCase extends \Yiisoft\Data\Tests\Common\Reader\ReaderWithFilter\BaseReaderWithGreaterThanTestCase
{
use DataTrait;
}
12 changes: 12 additions & 0 deletions tests/Base/Reader/ReaderWithFilter/BaseReaderWithInTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Db\Tests\Base\Reader\ReaderWithFilter;

use Yiisoft\Data\Db\Tests\Base\DataTrait;

abstract class BaseReaderWithInTestCase extends \Yiisoft\Data\Tests\Common\Reader\ReaderWithFilter\BaseReaderWithInTestCase
{
use DataTrait;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Db\Tests\Base\Reader\ReaderWithFilter;

use Yiisoft\Data\Db\Tests\Base\DataTrait;

abstract class BaseReaderWithLessThanOrEqualTestCase extends \Yiisoft\Data\Tests\Common\Reader\ReaderWithFilter\BaseReaderWithLessThanOrEqualTestCase
{
use DataTrait;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Db\Tests\Base\Reader\ReaderWithFilter;

use Yiisoft\Data\Db\Tests\Base\DataTrait;

abstract class BaseReaderWithLessThanTestCase extends \Yiisoft\Data\Tests\Common\Reader\ReaderWithFilter\BaseReaderWithLessThanTestCase
{
use DataTrait;
}
20 changes: 20 additions & 0 deletions tests/Base/Reader/ReaderWithFilter/BaseReaderWithLikeTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Db\Tests\Base\Reader\ReaderWithFilter;

use Yiisoft\Data\Db\Tests\Base\DataTrait;

abstract class BaseReaderWithLikeTestCase extends \Yiisoft\Data\Tests\Common\Reader\ReaderWithFilter\BaseReaderWithLikeTestCase
{
use DataTrait;

public static function dataWithReader(): array
{
$data = parent::dataWithReader();
$data['search: contains, different case, case sensitive: null'] = ['email', 'SEED@', null, [2]];

return $data;
}
}
12 changes: 12 additions & 0 deletions tests/Base/Reader/ReaderWithFilter/BaseReaderWithNotTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Db\Tests\Base\Reader\ReaderWithFilter;

use Yiisoft\Data\Db\Tests\Base\DataTrait;

abstract class BaseReaderWithNotTestCase extends \Yiisoft\Data\Tests\Common\Reader\ReaderWithFilter\BaseReaderWithNotTestCase
{
use DataTrait;
}
12 changes: 0 additions & 12 deletions tests/Base/TestCase.php

This file was deleted.

8 changes: 6 additions & 2 deletions tests/Mssql/DatabaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@

trait DatabaseTrait
{
protected function getConnection(): PdoConnectionInterface
protected function makeConnection(): PdoConnectionInterface
{
$database = getenv('YII_MSSQL_DATABASE');
$host = getenv('YII_MSSQL_HOST');
$port = (int) getenv('YII_MSSQL_PORT');
$user = getenv('YII_MSSQL_USER');
$password = getenv('YII_MSSQL_PASSWORD');

$pdoDriver = new Driver("sqlsrv:Server=$host,$port;Database=$database", $user, $password);
$pdoDriver = new Driver(
"sqlsrv:Server=$host,$port;Database=$database;TrustServerCertificate=true",
$user,
$password,
);
$pdoDriver->charset('UTF8MB4');

$db = new Connection($pdoDriver, new SchemaCache(new ArrayCache()));
Expand Down
Loading

0 comments on commit b3e1b3f

Please sign in to comment.