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