Skip to content

Commit

Permalink
Fix tests + Update PhpUnit (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed May 6, 2024
1 parent 4d9aaca commit a160e2a
Show file tree
Hide file tree
Showing 21 changed files with 84 additions and 459 deletions.
8 changes: 3 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ Thumbs.db
# composer itself is not needed
composer.phar

# phpunit itself is not needed
phpunit.phar
# local phpunit config
# PhpUnit
/phpunit.phar
/phpunit.xml
# phpunit cache
.phpunit.result.cache
/.phpunit.cache
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@
},
"require-dev": {
"maglnet/composer-require-checker": "^4.2",
"phpunit/phpunit": "^9.6",
"phpunit/phpunit": "^10.5",
"rector/rector": "^1.0",
"roave/infection-static-analysis-plugin": "^1.16",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^5.21"
"vimeo/psalm": "^5.21",
"yiisoft/db-sqlite": "^1.2"
},
"autoload": {
"psr-4": {
Expand Down
29 changes: 15 additions & 14 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="vendor/autoload.php"
colors="true"
verbose="true"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheDirectory=".phpunit.cache"
requireCoverageMetadata="false"
beStrictAboutCoverageMetadata="true"
beStrictAboutOutputDuringTests="true"
executionOrder="random"
failOnRisky="true"
failOnWarning="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false"
executionOrder="random"
resolveDependencies="true">
colors="true"
>
<php>
<ini name="error_reporting" value="-1"/>
<ini name="error_reporting" value="-1" />
</php>

<testsuites>
<testsuite name="Data-Db">
<testsuite name="Yii Data DB tests">
<directory>./tests</directory>
</testsuite>
</testsuites>

<coverage>
<source>
<include>
<directory>./src</directory>
<directory suffix=".php">./src</directory>
</include>
</coverage>
</source>
</phpunit>
51 changes: 24 additions & 27 deletions tests/DataFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Data\Db\Tests;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Yiisoft\Data\Db\QueryDataReader;
use Yiisoft\Data\Db\Tests\Support\TestTrait;
Expand All @@ -23,47 +24,45 @@ final class DataFilterTest extends TestCase
{
use TestTrait;

public function simpleDataProvider(): array
public static function simpleDataProvider(): array
{
return [
'equals' => [
new Equals('equals', 1),
'[equals] = 1',
'`equals` = 1',
],
'between' => [
new Between('column', 100, 300),
'[column] BETWEEN 100 AND 300',
'`column` BETWEEN 100 AND 300',
],
'greater-than' => [
new GreaterThan('column', 1000),
'[column] > 1000',
'`column` > 1000',
],
'greater-than-or-equal' => [
new GreaterThanOrEqual('column', 3.5),
'[column] >= 3.5',
'`column` >= \'3.5\'',
],
'less-than' => [
new LessThan('column', 10.7),
'[column] < 10.7',
'`column` < \'10.7\'',
],
'less-than-or-equal' => [
new LessThanOrEqual('column', 100),
'[column] <= 100',
'`column` <= 100',
],
'in' => [
new In('column', [10, 20, 30]),
'[column] IN (10, 20, 30)',
'`column` IN (10, 20, 30)',
],
'like' => [
new Like('column', 'foo'),
"[column] LIKE '%foo%'",
"`column` LIKE '%foo%' ESCAPE '\'",
],
];
}

/**
* @dataProvider simpleDataProvider
*/
#[DataProvider('simpleDataProvider')]
public function testSimpleFilter(FilterInterface $filter, string $condition): void
{
$db = $this->getConnection();
Expand All @@ -73,55 +72,53 @@ public function testSimpleFilter(FilterInterface $filter, string $condition): vo
$dataReader = (new QueryDataReader($query))
->withFilter($filter);

$expected = 'SELECT * FROM [customer] WHERE ' . $condition;
$expected = 'SELECT * FROM `customer` WHERE ' . $condition;

$this->assertSame(
$dataReader->getPreparedQuery()->createCommand()->getRawSql(),
$expected,
);
}

public function notDataProvider(): array
public static function notDataProvider(): array
{
return [
'equals' => [
new Not(new Equals('equals', 1)),
'[equals] != 1',
'`equals` != 1',
],
'between' => [
new Not(new Between('column', 100, 300)),
'[column] NOT BETWEEN 100 AND 300',
'`column` NOT BETWEEN 100 AND 300',
],
'greater-than' => [
new Not(new GreaterThan('column', 1000)),
'[column] <= 1000',
'`column` <= 1000',
],
'greater-than-or-equal' => [
'greater-than-or-equal1' => [
new Not(new GreaterThanOrEqual('column', 3.5)),
'[column] < 3.5',
'`column` < \'3.5\'',
],
'less-than' => [
new Not(new LessThan('column', 10.7)),
'[column] >= 10.7',
'`column` >= \'10.7\'',
],
'less-than-or-equal' => [
new Not(new LessThanOrEqual('column', 100)),
'[column] > 100',
'`column` > 100',
],
'in' => [
new Not(new In('column', [10, 20, 30])),
'[column] NOT IN (10, 20, 30)',
'`column` NOT IN (10, 20, 30)',
],
'like' => [
new Not(new Like('column', 'foo')),
"[column] NOT LIKE '%foo%'",
"`column` NOT LIKE '%foo%' ESCAPE '\'",
],
];
}

/**
* @dataProvider notDataProvider
*/
#[DataProvider('notDataProvider')]
public function testNotFilter(FilterInterface $filter, string $condition): void
{
$db = $this->getConnection();
Expand All @@ -131,7 +128,7 @@ public function testNotFilter(FilterInterface $filter, string $condition): void
$dataReader = (new QueryDataReader($query))
->withFilter($filter);

$expected = 'SELECT * FROM [customer] WHERE ' . $condition;
$expected = 'SELECT * FROM `customer` WHERE ' . $condition;

$this->assertSame(
$dataReader->getPreparedQuery()->createCommand()->getRawSql(),
Expand Down
17 changes: 8 additions & 9 deletions tests/DataReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Data\Db\Tests;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use stdClass;
use Yiisoft\Data\Db\QueryDataReader;
Expand Down Expand Up @@ -70,7 +71,7 @@ public function testLimit(): void
self::assertStringEndsWith('LIMIT 1 OFFSET 1', $actual);
}

public function sortDataProvider(): array
public static function sortDataProvider(): array
{
return [
[
Expand All @@ -79,15 +80,15 @@ public function sortDataProvider(): array
'email',
])
->withOrderString('-name,email'),
'[name] DESC, [email]',
'`name` DESC, `email`',
],
[
Sort::any([
'name',
'email',
])
->withOrderString('-name,-email'),
'[name] DESC, [email] DESC',
'`name` DESC, `email` DESC',
],
[
Sort::any([
Expand All @@ -96,15 +97,15 @@ public function sortDataProvider(): array
])
->withoutDefaultSorting()
->withOrderString('-email'),
'[email] DESC',
'`email` DESC',
],
[
Sort::any([
'name',
'email',
])
->withOrderString('-email'),
'[email] DESC, [name]',
'`email` DESC, `name`',
],
[
Sort::any([
Expand All @@ -118,14 +119,12 @@ public function sortDataProvider(): array
],
])
->withOrderString('-name'),
'[name] DESC NULLS LAST',
'`name` DESC NULLS LAST',
],
];
}

/**
* @dataProvider sortDataProvider
*/
#[DataProvider('sortDataProvider')]
public function testSort(Sort $sort, string $expected): void
{
$db = $this->getConnection();
Expand Down

0 comments on commit a160e2a

Please sign in to comment.