From b3e1b3f259bb22c4f9a0ceb47e20599462e06200 Mon Sep 17 00:00:00 2001 From: Alexey Rogachev Date: Thu, 17 Oct 2024 19:50:43 +0500 Subject: [PATCH] Add tests for handlers (data) (#60) * 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 --- .github/workflows/mssql.yml | 7 +- composer.json | 3 +- src/AbstractQueryDataReader.php | 3 + .../NotSupportedFilterOptionException.php | 20 +++ ...st.php => BaseQueryDataReaderTestCase.php} | 5 +- tests/Base/DataTrait.php | 115 ++++++++++++++++++ .../BaseReaderWithAllTestCase.php | 12 ++ .../BaseReaderWithAnyTestCase.php | 12 ++ .../BaseReaderWithBetweenTestCase.php | 12 ++ .../BaseReaderWithEqualsNullTestCase.php | 12 ++ .../BaseReaderWithEqualsTestCase.php | 12 ++ ...seReaderWithGreaterThanOrEqualTestCase.php | 12 ++ .../BaseReaderWithGreaterThanTestCase.php | 12 ++ .../BaseReaderWithInTestCase.php | 12 ++ .../BaseReaderWithLessThanOrEqualTestCase.php | 12 ++ .../BaseReaderWithLessThanTestCase.php | 12 ++ .../BaseReaderWithLikeTestCase.php | 20 +++ .../BaseReaderWithNotTestCase.php | 12 ++ tests/Base/TestCase.php | 12 -- tests/Mssql/DatabaseTrait.php | 8 +- tests/Mssql/QueryDataReaderTest.php | 4 +- .../ReaderWithHandler/ReaderWithAllTest.php | 13 ++ .../ReaderWithHandler/ReaderWithAnyTest.php | 13 ++ .../ReaderWithBetweenTest.php | 13 ++ .../ReaderWithEqualsNullTest.php | 13 ++ .../ReaderWithEqualsTest.php | 13 ++ .../ReaderWithGreaterThanOrEqualTest.php | 13 ++ .../ReaderWithGreaterThanTest.php | 13 ++ .../ReaderWithHandler/ReaderWithInTest.php | 13 ++ .../ReaderWithLessThanOrEqualTest.php | 13 ++ .../ReaderWithLessThanTest.php | 13 ++ .../ReaderWithHandler/ReaderWithLikeTest.php | 13 ++ .../ReaderWithHandler/ReaderWithNotTest.php | 13 ++ tests/Mysql/DatabaseTrait.php | 2 +- tests/Mysql/QueryDataReaderTest.php | 4 +- .../ReaderWithFilter/ReaderWithAllTest.php | 13 ++ .../ReaderWithFilter/ReaderWithAnyTest.php | 13 ++ .../ReaderWithBetweenTest.php | 13 ++ .../ReaderWithEqualsNullTest.php | 13 ++ .../ReaderWithFilter/ReaderWithEqualsTest.php | 13 ++ .../ReaderWithGreaterThanOrEqualTest.php | 13 ++ .../ReaderWithGreaterThanTest.php | 13 ++ .../ReaderWithFilter/ReaderWithInTest.php | 13 ++ .../ReaderWithLessThanOrEqualTest.php | 13 ++ .../ReaderWithLessThanTest.php | 13 ++ .../ReaderWithFilter/ReaderWithLikeTest.php | 23 ++++ .../ReaderWithFilter/ReaderWithNotTest.php | 13 ++ tests/Oracle/DatabaseTrait.php | 2 +- tests/Oracle/QueryDataReaderTest.php | 4 +- .../ReaderWithHandler/ReaderWithAllTest.php | 13 ++ .../ReaderWithHandler/ReaderWithAnyTest.php | 13 ++ .../ReaderWithBetweenTest.php | 13 ++ .../ReaderWithEqualsNullTest.php | 13 ++ .../ReaderWithEqualsTest.php | 13 ++ .../ReaderWithGreaterThanOrEqualTest.php | 13 ++ .../ReaderWithGreaterThanTest.php | 13 ++ .../ReaderWithHandler/ReaderWithInTest.php | 13 ++ .../ReaderWithLessThanOrEqualTest.php | 13 ++ .../ReaderWithLessThanTest.php | 13 ++ .../ReaderWithHandler/ReaderWithLikeTest.php | 27 ++++ .../ReaderWithHandler/ReaderWithNotTest.php | 13 ++ tests/Pgsql/DatabaseTrait.php | 2 +- tests/Pgsql/QueryDataReaderTest.php | 4 +- .../ReaderWithFilter/ReaderWithAllTest.php | 13 ++ .../ReaderWithFilter/ReaderWithAnyTest.php | 13 ++ .../ReaderWithBetweenTest.php | 13 ++ .../ReaderWithEqualsNullTest.php | 13 ++ .../ReaderWithFilter/ReaderWithEqualsTest.php | 13 ++ .../ReaderWithGreaterThanOrEqualTest.php | 13 ++ .../ReaderWithGreaterThanTest.php | 13 ++ .../ReaderWithFilter/ReaderWithInTest.php | 13 ++ .../ReaderWithLessThanOrEqualTest.php | 13 ++ .../ReaderWithLessThanTest.php | 13 ++ .../ReaderWithFilter/ReaderWithLikeTest.php | 27 ++++ .../ReaderWithFilter/ReaderWithNotTest.php | 13 ++ tests/Sqlite/DatabaseTrait.php | 2 +- tests/Sqlite/QueryDataReaderTest.php | 4 +- .../ReaderWithFilter/ReaderWithAllTest.php | 13 ++ .../ReaderWithFilter/ReaderWithAnyTest.php | 13 ++ .../ReaderWithBetweenTest.php | 13 ++ .../ReaderWithEqualsNullTest.php | 13 ++ .../ReaderWithFilter/ReaderWithEqualsTest.php | 13 ++ .../ReaderWithGreaterThanOrEqualTest.php | 13 ++ .../ReaderWithGreaterThanTest.php | 13 ++ .../ReaderWithFilter/ReaderWithInTestCase.php | 13 ++ .../ReaderWithLessThanOrEqualTest.php | 13 ++ .../ReaderWithLessThanTest.php | 13 ++ .../ReaderWithFilter/ReaderWithLikeTest.php | 13 ++ .../ReaderWithFilter/ReaderWithNotTest.php | 13 ++ 89 files changed, 1145 insertions(+), 26 deletions(-) create mode 100644 src/Exception/NotSupportedFilterOptionException.php rename tests/Base/{QueryDataReaderTest.php => BaseQueryDataReaderTestCase.php} (99%) create mode 100644 tests/Base/DataTrait.php create mode 100644 tests/Base/Reader/ReaderWithFilter/BaseReaderWithAllTestCase.php create mode 100644 tests/Base/Reader/ReaderWithFilter/BaseReaderWithAnyTestCase.php create mode 100644 tests/Base/Reader/ReaderWithFilter/BaseReaderWithBetweenTestCase.php create mode 100644 tests/Base/Reader/ReaderWithFilter/BaseReaderWithEqualsNullTestCase.php create mode 100644 tests/Base/Reader/ReaderWithFilter/BaseReaderWithEqualsTestCase.php create mode 100644 tests/Base/Reader/ReaderWithFilter/BaseReaderWithGreaterThanOrEqualTestCase.php create mode 100644 tests/Base/Reader/ReaderWithFilter/BaseReaderWithGreaterThanTestCase.php create mode 100644 tests/Base/Reader/ReaderWithFilter/BaseReaderWithInTestCase.php create mode 100644 tests/Base/Reader/ReaderWithFilter/BaseReaderWithLessThanOrEqualTestCase.php create mode 100644 tests/Base/Reader/ReaderWithFilter/BaseReaderWithLessThanTestCase.php create mode 100644 tests/Base/Reader/ReaderWithFilter/BaseReaderWithLikeTestCase.php create mode 100644 tests/Base/Reader/ReaderWithFilter/BaseReaderWithNotTestCase.php delete mode 100644 tests/Base/TestCase.php create mode 100644 tests/Mssql/ReaderWithHandler/ReaderWithAllTest.php create mode 100644 tests/Mssql/ReaderWithHandler/ReaderWithAnyTest.php create mode 100644 tests/Mssql/ReaderWithHandler/ReaderWithBetweenTest.php create mode 100644 tests/Mssql/ReaderWithHandler/ReaderWithEqualsNullTest.php create mode 100644 tests/Mssql/ReaderWithHandler/ReaderWithEqualsTest.php create mode 100644 tests/Mssql/ReaderWithHandler/ReaderWithGreaterThanOrEqualTest.php create mode 100644 tests/Mssql/ReaderWithHandler/ReaderWithGreaterThanTest.php create mode 100644 tests/Mssql/ReaderWithHandler/ReaderWithInTest.php create mode 100644 tests/Mssql/ReaderWithHandler/ReaderWithLessThanOrEqualTest.php create mode 100644 tests/Mssql/ReaderWithHandler/ReaderWithLessThanTest.php create mode 100644 tests/Mssql/ReaderWithHandler/ReaderWithLikeTest.php create mode 100644 tests/Mssql/ReaderWithHandler/ReaderWithNotTest.php create mode 100644 tests/Mysql/ReaderWithFilter/ReaderWithAllTest.php create mode 100644 tests/Mysql/ReaderWithFilter/ReaderWithAnyTest.php create mode 100644 tests/Mysql/ReaderWithFilter/ReaderWithBetweenTest.php create mode 100644 tests/Mysql/ReaderWithFilter/ReaderWithEqualsNullTest.php create mode 100644 tests/Mysql/ReaderWithFilter/ReaderWithEqualsTest.php create mode 100644 tests/Mysql/ReaderWithFilter/ReaderWithGreaterThanOrEqualTest.php create mode 100644 tests/Mysql/ReaderWithFilter/ReaderWithGreaterThanTest.php create mode 100644 tests/Mysql/ReaderWithFilter/ReaderWithInTest.php create mode 100644 tests/Mysql/ReaderWithFilter/ReaderWithLessThanOrEqualTest.php create mode 100644 tests/Mysql/ReaderWithFilter/ReaderWithLessThanTest.php create mode 100644 tests/Mysql/ReaderWithFilter/ReaderWithLikeTest.php create mode 100644 tests/Mysql/ReaderWithFilter/ReaderWithNotTest.php create mode 100644 tests/Oracle/ReaderWithHandler/ReaderWithAllTest.php create mode 100644 tests/Oracle/ReaderWithHandler/ReaderWithAnyTest.php create mode 100644 tests/Oracle/ReaderWithHandler/ReaderWithBetweenTest.php create mode 100644 tests/Oracle/ReaderWithHandler/ReaderWithEqualsNullTest.php create mode 100644 tests/Oracle/ReaderWithHandler/ReaderWithEqualsTest.php create mode 100644 tests/Oracle/ReaderWithHandler/ReaderWithGreaterThanOrEqualTest.php create mode 100644 tests/Oracle/ReaderWithHandler/ReaderWithGreaterThanTest.php create mode 100644 tests/Oracle/ReaderWithHandler/ReaderWithInTest.php create mode 100644 tests/Oracle/ReaderWithHandler/ReaderWithLessThanOrEqualTest.php create mode 100644 tests/Oracle/ReaderWithHandler/ReaderWithLessThanTest.php create mode 100644 tests/Oracle/ReaderWithHandler/ReaderWithLikeTest.php create mode 100644 tests/Oracle/ReaderWithHandler/ReaderWithNotTest.php create mode 100644 tests/Pgsql/ReaderWithFilter/ReaderWithAllTest.php create mode 100644 tests/Pgsql/ReaderWithFilter/ReaderWithAnyTest.php create mode 100644 tests/Pgsql/ReaderWithFilter/ReaderWithBetweenTest.php create mode 100644 tests/Pgsql/ReaderWithFilter/ReaderWithEqualsNullTest.php create mode 100644 tests/Pgsql/ReaderWithFilter/ReaderWithEqualsTest.php create mode 100644 tests/Pgsql/ReaderWithFilter/ReaderWithGreaterThanOrEqualTest.php create mode 100644 tests/Pgsql/ReaderWithFilter/ReaderWithGreaterThanTest.php create mode 100644 tests/Pgsql/ReaderWithFilter/ReaderWithInTest.php create mode 100644 tests/Pgsql/ReaderWithFilter/ReaderWithLessThanOrEqualTest.php create mode 100644 tests/Pgsql/ReaderWithFilter/ReaderWithLessThanTest.php create mode 100644 tests/Pgsql/ReaderWithFilter/ReaderWithLikeTest.php create mode 100644 tests/Pgsql/ReaderWithFilter/ReaderWithNotTest.php create mode 100644 tests/Sqlite/ReaderWithFilter/ReaderWithAllTest.php create mode 100644 tests/Sqlite/ReaderWithFilter/ReaderWithAnyTest.php create mode 100644 tests/Sqlite/ReaderWithFilter/ReaderWithBetweenTest.php create mode 100644 tests/Sqlite/ReaderWithFilter/ReaderWithEqualsNullTest.php create mode 100644 tests/Sqlite/ReaderWithFilter/ReaderWithEqualsTest.php create mode 100644 tests/Sqlite/ReaderWithFilter/ReaderWithGreaterThanOrEqualTest.php create mode 100644 tests/Sqlite/ReaderWithFilter/ReaderWithGreaterThanTest.php create mode 100644 tests/Sqlite/ReaderWithFilter/ReaderWithInTestCase.php create mode 100644 tests/Sqlite/ReaderWithFilter/ReaderWithLessThanOrEqualTest.php create mode 100644 tests/Sqlite/ReaderWithFilter/ReaderWithLessThanTest.php create mode 100644 tests/Sqlite/ReaderWithFilter/ReaderWithLikeTest.php create mode 100644 tests/Sqlite/ReaderWithFilter/ReaderWithNotTest.php diff --git a/.github/workflows/mssql.yml b/.github/workflows/mssql.yml index 667ce8f..4341cea 100644 --- a/.github/workflows/mssql.yml +++ b/.github/workflows/mssql.yml @@ -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' }} @@ -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 diff --git a/composer.json b/composer.json index a6df501..94926f7 100644 --- a/composer.json +++ b/composer.json @@ -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"] }, diff --git a/src/AbstractQueryDataReader.php b/src/AbstractQueryDataReader.php index 9320a3d..16644f0 100644 --- a/src/AbstractQueryDataReader.php +++ b/src/AbstractQueryDataReader.php @@ -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; diff --git a/src/Exception/NotSupportedFilterOptionException.php b/src/Exception/NotSupportedFilterOptionException.php new file mode 100644 index 0000000..27cbc36 --- /dev/null +++ b/src/Exception/NotSupportedFilterOptionException.php @@ -0,0 +1,20 @@ +getConnection(); diff --git a/tests/Base/DataTrait.php b/tests/Base/DataTrait.php new file mode 100644 index 0000000..146aeb8 --- /dev/null +++ b/tests/Base/DataTrait.php @@ -0,0 +1,115 @@ +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(); + } +} diff --git a/tests/Base/Reader/ReaderWithFilter/BaseReaderWithAllTestCase.php b/tests/Base/Reader/ReaderWithFilter/BaseReaderWithAllTestCase.php new file mode 100644 index 0000000..7aa5200 --- /dev/null +++ b/tests/Base/Reader/ReaderWithFilter/BaseReaderWithAllTestCase.php @@ -0,0 +1,12 @@ +charset('UTF8MB4'); $db = new Connection($pdoDriver, new SchemaCache(new ArrayCache())); diff --git a/tests/Mssql/QueryDataReaderTest.php b/tests/Mssql/QueryDataReaderTest.php index 84764d2..cc559be 100644 --- a/tests/Mssql/QueryDataReaderTest.php +++ b/tests/Mssql/QueryDataReaderTest.php @@ -4,7 +4,9 @@ namespace Yiisoft\Data\Db\Tests\Mssql; -final class QueryDataReaderTest extends \Yiisoft\Data\Db\Tests\Base\QueryDataReaderTest +use Yiisoft\Data\Db\Tests\Base\BaseQueryDataReaderTestCase; + +final class QueryDataReaderTest extends BaseQueryDataReaderTestCase { use DatabaseTrait; diff --git a/tests/Mssql/ReaderWithHandler/ReaderWithAllTest.php b/tests/Mssql/ReaderWithHandler/ReaderWithAllTest.php new file mode 100644 index 0000000..0b83314 --- /dev/null +++ b/tests/Mssql/ReaderWithHandler/ReaderWithAllTest.php @@ -0,0 +1,13 @@ +