|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace StefanoTreeTest\DbTester; |
|
6
|
|
|
|
|
7
|
|
|
use PDO; |
|
8
|
|
|
|
|
9
|
|
|
trait DbTestCaseTrait |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var null|Connection |
|
13
|
|
|
*/ |
|
14
|
|
|
private $connection = null; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Performs operation returned by getSetUpOperation(). |
|
18
|
|
|
*/ |
|
19
|
|
|
protected function setUp(): void |
|
20
|
|
|
{ |
|
21
|
|
|
$this->recreateDbScheme(); |
|
22
|
|
|
$this->getConnection() |
|
23
|
|
|
->insertInitData($this->getDataSet()); |
|
24
|
|
|
parent::setUp(); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Performs operation returned by getTearDownOperation(). |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function tearDown(): void |
|
31
|
|
|
{ |
|
32
|
|
|
parent::tearDown(); |
|
33
|
|
|
$this->connection = null; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
abstract public function recreateDbScheme(); |
|
37
|
|
|
|
|
38
|
|
|
public function assertCompareDataSet(array $tables, $expectedDataSetArrayFile, $message = '') |
|
39
|
|
|
{ |
|
40
|
|
|
$currentDataSet = $this->getConnection() |
|
41
|
|
|
->createDataSourceFromCurrentDatabaseState($tables); |
|
42
|
|
|
$expectedDataSet = $this->createArrayDataSet(include $expectedDataSetArrayFile); |
|
43
|
|
|
|
|
44
|
|
|
$actualData = array(); |
|
45
|
|
|
$expectedData = array(); |
|
46
|
|
|
foreach ($tables as $table) { |
|
47
|
|
|
$expectedData[$table] = $expectedDataSet->getTableData($table, true); |
|
48
|
|
|
$actualData[$table] = $currentDataSet->getTableData($table, true); |
|
49
|
|
|
} |
|
50
|
|
|
// todo better validation with better, readable error result |
|
51
|
|
|
self::assertEquals($expectedData, $actualData, $message); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Returns fresh test database connection. |
|
56
|
|
|
* |
|
57
|
|
|
* @return PDO |
|
58
|
|
|
*/ |
|
59
|
|
|
abstract protected function getPdoConnection(); |
|
60
|
|
|
|
|
61
|
|
|
private function getConnection(): Connection |
|
62
|
|
|
{ |
|
63
|
|
|
if (null == $this->connection) { |
|
64
|
|
|
$this->connection = new Connection($this->getPdoConnection()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $this->connection; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Returns the initial test dataset. |
|
72
|
|
|
* |
|
73
|
|
|
* @return ArrayDataSource |
|
74
|
|
|
*/ |
|
75
|
|
|
abstract protected function getDataSet(); |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param array $data |
|
79
|
|
|
* |
|
80
|
|
|
* @return ArrayDataSource |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function createArrayDataSet(array $data) |
|
83
|
|
|
{ |
|
84
|
|
|
return new ArrayDataSource($data); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|