Passed
Push — main ( 788641...5bbbb9 )
by Diego
02:40
created

AbstractModelTest::testJsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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());
0 ignored issues
show
Bug introduced by
It seems like $this->expected_json can also be of type null; however, parameter $expectedJson of PHPUnit\Framework\Assert...tringEqualsJsonString() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

37
        $this->assertJsonStringEqualsJsonString(/** @scrutinizer ignore-type */ $this->expected_json, $this->testable_entity->toJson());
Loading history...
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));
0 ignored issues
show
Bug introduced by
It seems like $this->expected_json can also be of type null; however, parameter $expectedJson of PHPUnit\Framework\Assert...tringEqualsJsonString() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
        $this->assertJsonStringEqualsJsonString(/** @scrutinizer ignore-type */ $this->expected_json, json_encode($this->testable_entity, JSON_THROW_ON_ERROR));
Loading history...
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);
0 ignored issues
show
Bug introduced by
The constant Blackmine\Tests\Models\A...ctModelTest::TEST_MODEL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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;
0 ignored issues
show
Bug introduced by
The constant Blackmine\Tests\Models\A...ctModelTest::TEST_MODEL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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);
0 ignored issues
show
Bug introduced by
The constant Blackmine\Tests\Models\A...ctModelTest::TEST_MODEL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
105
        return array_pop($path);
106
    }
107
108
}
109