1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Blackmine\Tests\Models; |
4
|
|
|
|
5
|
|
|
use Blackmine\Model\AbstractModel; |
6
|
|
|
use Blackmine\Model\ModelTrait; |
7
|
|
|
use Codeception\Test\Unit; |
8
|
|
|
|
9
|
|
|
abstract class AbstractModelTest extends Unit |
10
|
|
|
{ |
11
|
|
|
use ModelTrait; |
12
|
|
|
|
13
|
|
|
protected ?array $original_construct_values; |
14
|
|
|
protected ?array $expected_payload; |
15
|
|
|
protected ?array $expected_array; |
16
|
|
|
protected ?string $expected_json; |
17
|
|
|
|
18
|
|
|
protected AbstractModel $testable_entity; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @throws \JsonException |
22
|
|
|
*/ |
23
|
|
|
protected function _before(): void |
24
|
|
|
{ |
25
|
|
|
$this->initExpectations(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function testToArray(): void |
29
|
|
|
{ |
30
|
|
|
$this->assertEquals($this->expected_array, $this->testable_entity->toArray()); |
31
|
|
|
} |
32
|
|
|
/** |
33
|
|
|
* @throws \JsonException |
34
|
|
|
*/ |
35
|
|
|
public function testToJson(): void |
36
|
|
|
{ |
37
|
|
|
$this->assertJsonStringEqualsJsonString($this->expected_json, $this->testable_entity->toJson()); |
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function testJsonSerialize(): void |
41
|
|
|
{ |
42
|
|
|
$this->assertEquals($this->expected_array, $this->testable_entity->jsonSerialize()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @throws \JsonException |
47
|
|
|
*/ |
48
|
|
|
public function testJsonEncode(): void |
49
|
|
|
{ |
50
|
|
|
$this->assertJsonStringEqualsJsonString($this->expected_json, json_encode($this->testable_entity, JSON_THROW_ON_ERROR)); |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function testPayload(): void |
54
|
|
|
{ |
55
|
|
|
if ($this->expected_payload) { |
56
|
|
|
$this->assertEquals($this->expected_payload, $this->testable_entity->getPayload()); |
57
|
|
|
} else { |
58
|
|
|
$this->expectError(); |
59
|
|
|
$this->expectErrorMessage('Mandatory constant ENTITY_NAME not defined in model class: ' . static::TEST_MODEL); |
|
|
|
|
60
|
|
|
|
61
|
|
|
$this->testable_entity->getPayload(); |
62
|
|
|
|
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testGetters(): void |
67
|
|
|
{ |
68
|
|
|
foreach ($this->original_construct_values as $key => $value) { |
69
|
|
|
$getter = $this->getGetter($key); |
70
|
|
|
$this->assertEquals($value, $this->testable_entity->$getter()); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
/** |
74
|
|
|
* @throws \JsonException |
75
|
|
|
*/ |
76
|
|
|
private function initExpectations(): void |
77
|
|
|
{ |
78
|
|
|
$data_dir = __DIR__ . "/../data/"; |
79
|
|
|
$model_data_file = $data_dir . $this->getUnqualifiedClassName() . "TestData.php"; |
80
|
|
|
|
81
|
|
|
if (file_exists($model_data_file)) { |
82
|
|
|
$data = (require $model_data_file); |
83
|
|
|
if ($data["__payload"] !== false && is_array($data["__payload"])) { |
84
|
|
|
$this->expected_payload = $data["__payload"]; |
85
|
|
|
} else { |
86
|
|
|
$this->expected_payload = null; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (is_array($data["__construct"])) { |
90
|
|
|
$model_class = static::TEST_MODEL; |
|
|
|
|
91
|
|
|
$this->testable_entity = (new $model_class())->fromArray($data["__construct"]); |
92
|
|
|
$this->original_construct_values = $data["__construct"]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
if (is_array($data["__expects"])) { |
96
|
|
|
$this->expected_array = $data["__expects"]; |
97
|
|
|
$this->expected_json = json_encode($data["__expects"], JSON_THROW_ON_ERROR); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
private function getUnqualifiedClassName(): string |
103
|
|
|
{ |
104
|
|
|
$path = explode('\\', static::TEST_MODEL); |
|
|
|
|
105
|
|
|
return array_pop($path); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
|