Skip to content

Commit

Permalink
Extend tests
Browse files Browse the repository at this point in the history
  • Loading branch information
misantron committed Jun 9, 2023
1 parent 166f947 commit 44907af
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 32 deletions.
6 changes: 3 additions & 3 deletions src/Constraint/DataSetIsEqual.php
Expand Up @@ -23,7 +23,7 @@ class DataSetIsEqual extends Constraint
/**
* @var IDataSet
*/
protected $value;
protected IDataSet $value;

/**
* @param IDataSet $value
Expand All @@ -41,8 +41,8 @@ public function __construct(IDataSet $value)
public function toString(): string
{
return sprintf(
'is equal to expected %s',
implode(',', $this->value->getTableNames())
'is equal to expected dataset %s',
$this->value
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/Constraint/TableIsEqual.php
Expand Up @@ -23,7 +23,7 @@ class TableIsEqual extends Constraint
/**
* @var ITable
*/
protected $value;
protected ITable $value;

/**
* @param ITable $value
Expand All @@ -42,7 +42,7 @@ public function toString(): string
{
return sprintf(
'is equal to expected %s',
$this->value->getTableMetaData()->getTableName()
$this->value
);
}

Expand Down
8 changes: 4 additions & 4 deletions src/DataSet/AbstractDataSet.php
Expand Up @@ -20,15 +20,15 @@ abstract class AbstractDataSet implements IDataSet
{
public function __toString()
{
/** @var ITable[] $iterator */
$iterator = $this->getIterator();

$dataSetString = '';

$tables = [];
foreach ($iterator as $table) {
$dataSetString .= $table;
$tables[] = $table->getTableMetadata()->getTableName();
}

return $dataSetString;
return '[' . implode(',', $tables) . ']';
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/DataSet/DefaultDataSet.php
Expand Up @@ -19,9 +19,9 @@ class DefaultDataSet extends AbstractDataSet
/**
* An array of ITable objects.
*
* @var array
* @var ITable[]
*/
protected $tables;
protected array $tables;

/**
* @param array $tables
Expand Down
2 changes: 1 addition & 1 deletion src/DataSet/IDataSet.php
Expand Up @@ -14,7 +14,7 @@
/**
* Provides a basic interface for creating and reading data from data sets.
*/
interface IDataSet extends \IteratorAggregate
interface IDataSet extends \IteratorAggregate, \Stringable
{
/**
* Returns an array of table names contained in the dataset.
Expand Down
2 changes: 1 addition & 1 deletion src/DataSet/ITable.php
Expand Up @@ -14,7 +14,7 @@
/**
* Provides a basic interface for creating and reading data from data sets.
*/
interface ITable
interface ITable extends \Stringable
{
/**
* Returns the table's meta data.
Expand Down
18 changes: 6 additions & 12 deletions src/Database/Metadata/Firebird.php
Expand Up @@ -16,6 +16,10 @@
*/
class Firebird extends AbstractMetadata
{
protected array $columns = [];

protected array $keys = [];

/**
* The command used to perform a TRUNCATE operation.
*
Expand All @@ -30,17 +34,7 @@ class Firebird extends AbstractMetadata
*/
public function getTableNames()
{
$query = "
SELECT DISTINCT
TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE
TABLE_TYPE='BASE TABLE' AND
TABLE_SCHEMA = ?
ORDER BY TABLE_NAME
";

$query = "
$query = '

Check warning on line 37 in src/Database/Metadata/Firebird.php

View check run for this annotation

Codecov / codecov/patch

src/Database/Metadata/Firebird.php#L37

Added line #L37 was not covered by tests
select
RDB$RELATION_NAME as TABLE_NAME
from RDB$RELATIONS
Expand All @@ -49,7 +43,7 @@ public function getTableNames()
(RDB$RELATION_TYPE is null)) and
(RDB$SYSTEM_FLAG = 0)
order by (RDB$RELATION_NAME)
";
';

Check warning on line 46 in src/Database/Metadata/Firebird.php

View check run for this annotation

Codecov / codecov/patch

src/Database/Metadata/Firebird.php#L46

Added line #L46 was not covered by tests

$statement = $this->pdo->prepare($query);
$statement->execute([$this->getSchema()]);
Expand Down
4 changes: 2 additions & 2 deletions src/Database/Metadata/PgSQL.php
Expand Up @@ -16,9 +16,9 @@
*/
class PgSQL extends AbstractMetadata
{
private array $columns = [];
protected array $columns = [];

private array $keys = [];
protected array $keys = [];

/**
* Returns an array containing the names of all the tables in the database.
Expand Down
74 changes: 74 additions & 0 deletions tests/Constraint/DataSetIsEqualTest.php
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PHPUnit\DbUnit\Tests\Constraint;

use PHPUnit\DbUnit\Constraint\DataSetIsEqual;
use PHPUnit\DbUnit\DataSet\DefaultDataSet;
use PHPUnit\DbUnit\DataSet\DefaultTable;
use PHPUnit\DbUnit\DataSet\DefaultTableMetadata;
use PHPUnit\DbUnit\Exception\InvalidArgumentException;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;

class DataSetIsEqualTest extends TestCase
{
public function testMatch(): void
{
$constraint = $this->createConstraint();

$dataSet = new DefaultDataSet([
new DefaultTable(
new DefaultTableMetadata('table1', ['id', 'col1'], ['id']),
),
new DefaultTable(
new DefaultTableMetadata('table2', ['id', 'col2'], ['id']),
),
]);

self::assertTrue($constraint->evaluate($dataSet, '', true));
self::assertFalse($constraint->evaluate(new DefaultDataSet(), '', true));
self::assertSame('is equal to expected dataset [table1,table2]', $constraint->toString());
}

public function testMatchInvalidType(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Only dataset instance can be matched');

$constraint = $this->createConstraint();
$constraint->evaluate(null);
}

public function testExpectationFailed(): void
{
$this->expectException(ExpectationFailedException::class);
$this->expectExceptionMessage('Failed asserting that [] is equal to expected dataset [table1,table2].');

self::assertThat(new DefaultDataSet(), $this->createConstraint());
}

private function createConstraint(): DataSetIsEqual
{
$dataSet = new DefaultDataSet([
new DefaultTable(
new DefaultTableMetadata('table1', ['id', 'col1'], ['id']),
),
new DefaultTable(
new DefaultTableMetadata('table2', ['id', 'col2'], ['id']),
),
]);

return new DataSetIsEqual($dataSet);
}
}
82 changes: 82 additions & 0 deletions tests/Constraint/TableIsEqualTest.php
@@ -0,0 +1,82 @@
<?php

declare(strict_types=1);

/*
* This file is part of DbUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace PHPUnit\DbUnit\Tests\Constraint;

use PHPUnit\DbUnit\Constraint\TableIsEqual;
use PHPUnit\DbUnit\DataSet\DefaultTable;
use PHPUnit\DbUnit\DataSet\DefaultTableMetadata;
use PHPUnit\DbUnit\Exception\InvalidArgumentException;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;

class TableIsEqualTest extends TestCase
{
public function testMatch(): void
{
$constraint = $this->createConstraint();

$table1 = new DefaultTable(
new DefaultTableMetadata('table1', ['id', 'col1'], ['id']),
);
$table2 = new DefaultTable(
new DefaultTableMetadata('table2', ['id', 'col2'], ['id']),
);

self::assertTrue($constraint->evaluate($table1, '', true));
self::assertFalse($constraint->evaluate($table2, '', true));
self::assertSame(
sprintf('is equal to expected %s', $table1),
$constraint->toString()
);
}

public function testMatchInvalidType(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Only table instance can be matched');

$constraint = $this->createConstraint();
$constraint->evaluate(null);
}

public function testExpectationFailed(): void
{
$table1 = new DefaultTable(
new DefaultTableMetadata('table1', ['id', 'col1'], ['id']),
);
$table2 = new DefaultTable(
new DefaultTableMetadata('table2', ['id', 'col2'], ['id']),
);

$this->expectException(ExpectationFailedException::class);
$this->expectExceptionMessage(
sprintf(
'Failed asserting that %s is equal to expected %s.',
$table2,
$table1
)
);

self::assertThat($table2, $this->createConstraint());
}

private function createConstraint(): TableIsEqual
{
$table = new DefaultTable(
new DefaultTableMetadata('table1', ['id', 'col1'], ['id']),
);

return new TableIsEqual($table);
}
}
10 changes: 5 additions & 5 deletions tests/Constraint/TableRowCountTest.php
Expand Up @@ -21,14 +21,14 @@ public function testConstraint(): void
{
$constraint = new TableRowCount('name', 42);

$this->assertTrue($constraint->evaluate(42, '', true));
$this->assertFalse($constraint->evaluate(24, '', true));
$this->assertSame('is equal to expected row count 42', $constraint->toString());
self::assertTrue($constraint->evaluate(42, '', true));
self::assertFalse($constraint->evaluate(24, '', true));
self::assertSame('is equal to expected row count 42', $constraint->toString());

try {
$this->assertThat(24, $constraint);
self::assertThat(24, $constraint);
} catch (ExpectationFailedException $e) {
$this->assertSame(
self::assertSame(
'Failed asserting that 24 is equal to expected row count 42.',
$e->getMessage()
);
Expand Down

0 comments on commit 44907af

Please sign in to comment.