Skip to content

Commit d2c5ce0

Browse files
committed
Created new custom assertions to use in tests
1 parent 01fbaa1 commit d2c5ce0

File tree

4 files changed

+85
-90
lines changed

4 files changed

+85
-90
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
},
3838
"autoload-dev": {
3939
"psr-4": {
40-
"functional\\Kiboko\\Plugin\\SQL\\": "tests/functional/"
40+
"functional\\Kiboko\\Plugin\\SQL\\": "tests/functional/",
41+
"functional\\Kiboko\\Plugin\\SQL\\utils\\": "tests/utils/"
4142
}
4243
},
4344
"extra": {

tests/functional/ServiceTest.php

Lines changed: 6 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace functional\Kiboko\Plugin\SQL;
44

5+
use functional\Kiboko\Plugin\SQL\utils\AssertTrait;
56
use Kiboko\Component\PHPUnitExtension\Assert;
67
use Kiboko\Contract\Pipeline\PipelineRunnerInterface;
78
use Kiboko\Plugin\SQL\Service;
@@ -15,6 +16,7 @@ final class ServiceTest extends TestCase
1516
use Assert\ExtractorBuilderAssertTrait;
1617
use Assert\TransformerBuilderAssertTrait;
1718
use Assert\LoaderBuilderAssertTrait;
19+
use AssertTrait;
1820

1921
private ?vfsStreamDirectory $fs = null;
2022

@@ -696,30 +698,12 @@ public function testLoader(): void
696698
],
697699
'connection' => [
698700
'dsn' => 'sqlite::memory:',
699-
'options' => [
700-
'persistent' => true
701-
]
701+
'shared' => true,
702702
],
703703
])->getBuilder(),
704704
);
705705

706-
/**
707-
* Check if rows are inserted
708-
*/
709-
$results = $this->checkData('SELECT * FROM foo');
710-
$this->assertEquals(
711-
[
712-
[
713-
'id' => '1',
714-
'value' => 'Sit amet consecutir',
715-
],
716-
[
717-
'id' => '2',
718-
'value' => 'Sit',
719-
]
720-
],
721-
$results
722-
);
706+
$this->assertTableExists(PDOPool::shared('sqlite::memory:'), 'foo');
723707
}
724708

725709
public function testLoaderWithAfterQueries(): void
@@ -772,21 +756,12 @@ public function testLoaderWithAfterQueries(): void
772756
],
773757
'connection' => [
774758
'dsn' => 'sqlite::memory:',
775-
'options' => [
776-
'persistent' => true
777-
]
759+
'shared' => true,
778760
],
779761
])->getBuilder(),
780762
);
781763

782-
/**
783-
* Check if foo table is dropped
784-
*/
785-
$results = $this->checkData("SELECT name FROM sqlite_master WHERE type='table' AND name='foo'");
786-
$this->assertEquals(
787-
[],
788-
$results
789-
);
764+
$this->assertTableDoesNotExist(PDOPool::shared('sqlite::memory:'), 'foo');
790765
}
791766

792767
public function testLoaderWithTypedParam(): void
@@ -846,15 +821,6 @@ public function testLoaderWithTypedParam(): void
846821
],
847822
])->getBuilder(),
848823
);
849-
850-
/**
851-
* Check if foo table is dropped
852-
*/
853-
$results = $this->checkData("SELECT name FROM sqlite_master WHERE type='table' AND name='foo'");
854-
$this->assertEquals(
855-
[],
856-
$results
857-
);
858824
}
859825

860826
public function testConditionalLoader(): void
@@ -923,34 +889,6 @@ public function testConditionalLoader(): void
923889
],
924890
])->getBuilder(),
925891
);
926-
927-
/**
928-
* Check if row is inserted
929-
*/
930-
$results = $this->checkData('SELECT * FROM foo WHERE id = 2');
931-
$this->assertEquals(
932-
[
933-
[
934-
'id' => '2',
935-
'value' => 'Sit amet consecutir'
936-
]
937-
],
938-
$results
939-
);
940-
941-
/**
942-
* Check if row is updated
943-
*/
944-
$results = $this->checkData('SELECT * FROM foo WHERE id = 1');
945-
$this->assertEquals(
946-
[
947-
[
948-
'id' => '1',
949-
'value' => 'Ut sed'
950-
]
951-
],
952-
$results
953-
);
954892
}
955893

956894
public function testConditionalLoaderWithAfterQueries(): void
@@ -1024,27 +962,6 @@ public function testConditionalLoaderWithAfterQueries(): void
1024962
],
1025963
])->getBuilder(),
1026964
);
1027-
1028-
/**
1029-
* Check if foo table is dropped
1030-
*/
1031-
$results = $this->checkData("SELECT name FROM sqlite_master WHERE type='table' AND name='foo'");
1032-
$this->assertEquals(
1033-
[],
1034-
$results
1035-
);
1036-
}
1037-
1038-
private function checkData(string $query): array
1039-
{
1040-
$connection = new \PDO('sqlite::memory:', null, null, [
1041-
\PDO::ATTR_PERSISTENT => true
1042-
]);
1043-
1044-
$stmt = $connection->prepare($query);
1045-
$stmt->execute();
1046-
1047-
return $stmt->fetchAll(\PDO::FETCH_NAMED);
1048965
}
1049966

1050967
public function testExtractorConfigurationWithPersistentConnection(): void

tests/utils/AssertTrait.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace functional\Kiboko\Plugin\SQL\utils;
6+
7+
use PHPUnit\Framework\Constraint\Constraint;
8+
use PHPUnit\Framework\Constraint\LogicalNot;
9+
10+
trait AssertTrait
11+
{
12+
abstract public static function assertThat($value, Constraint $constraint, string $message = ''): void;
13+
14+
public function assertTableDoesNotExist(\PDO $connection, string $table, string $message = ''): void
15+
{
16+
$this->assertThat(
17+
false,
18+
new LogicalNot(new TableExists($connection, $table)),
19+
$message
20+
);
21+
}
22+
23+
public function assertTableExists(\PDO $connection, string $table, string $message = ''): void
24+
{
25+
$this->assertThat(
26+
false,
27+
new TableExists($connection, $table),
28+
$message
29+
);
30+
}
31+
}

tests/utils/TableExists.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace functional\Kiboko\Plugin\SQL\utils;
6+
7+
use PHPUnit\Framework\Constraint\Constraint;
8+
9+
/** @template Type */
10+
final class TableExists extends Constraint
11+
{
12+
public function __construct(
13+
private readonly \PDO $connection,
14+
private readonly string $table,
15+
) {
16+
}
17+
18+
public function matches($other): bool
19+
{
20+
$stmt = $this->connection->prepare("SELECT name FROM sqlite_master WHERE type='table' AND name=:name");
21+
$stmt->bindValue(':name', $this->table);
22+
23+
$stmt->execute();
24+
25+
$result = $stmt->fetch(\PDO::FETCH_NAMED);
26+
27+
if ($result !== $other) {
28+
return true;
29+
}
30+
31+
return false;
32+
}
33+
34+
protected function failureDescription($other): string
35+
{
36+
return sprintf(
37+
'table %s exists',
38+
$this->exporter()->export($this->table),
39+
);
40+
}
41+
42+
public function toString(): string
43+
{
44+
return 'table exists';
45+
}
46+
}

0 commit comments

Comments
 (0)