-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractModelTest.php
More file actions
171 lines (135 loc) · 5.29 KB
/
Copy pathAbstractModelTest.php
File metadata and controls
171 lines (135 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
declare(strict_types=1);
namespace Dotcms\PhpSdk\Tests\Model;
use Dotcms\PhpSdk\Model\Core\AbstractModel;
use PHPUnit\Framework\TestCase;
class AbstractModelTest extends TestCase
{
/**
* A concrete implementation of AbstractModel for testing
*/
private function createConcreteModel(array $additionalProperties = []): ConcreteModel
{
return new ConcreteModel('test-id', 'Test Title', $additionalProperties);
}
public function testConstructorAndBasicProperties(): void
{
$model = $this->createConcreteModel();
$this->assertEquals('test-id', $model->identifier);
$this->assertEquals('Test Title', $model->title);
}
public function testAdditionalProperties(): void
{
$additionalProps = [
'customProp1' => 'value1',
'customProp2' => 123,
'customProp3' => ['nested' => 'value'],
];
$model = $this->createConcreteModel($additionalProps);
// Test accessing via array access
$this->assertEquals('value1', $model['customProp1']);
$this->assertEquals(123, $model['customProp2']);
$this->assertEquals(['nested' => 'value'], $model['customProp3']);
// Test accessing via jsonSerialize
$json = $model->jsonSerialize();
$this->assertEquals('value1', $json['customProp1']);
$this->assertEquals(123, $json['customProp2']);
$this->assertEquals(['nested' => 'value'], $json['customProp3']);
}
public function testArrayAccessExists(): void
{
$model = $this->createConcreteModel(['customProp' => 'value']);
$this->assertTrue(isset($model['identifier']));
$this->assertTrue(isset($model['title']));
$this->assertTrue(isset($model['customProp']));
$this->assertFalse(isset($model['nonExistentProp']));
// Test with non-string offset
$this->assertFalse(isset($model[123]));
}
public function testArrayAccessGet(): void
{
$model = $this->createConcreteModel(['customProp' => 'value']);
$this->assertEquals('test-id', $model['identifier']);
$this->assertEquals('Test Title', $model['title']);
$this->assertEquals('value', $model['customProp']);
$this->assertNull($model['nonExistentProp']);
// Test with non-string offset
$this->assertNull($model[123]);
}
public function testArrayAccessSetThrowsException(): void
{
$model = $this->createConcreteModel();
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Properties are read-only');
$model['identifier'] = 'new-value';
}
public function testArrayAccessUnsetThrowsException(): void
{
$model = $this->createConcreteModel();
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Properties are read-only');
unset($model['identifier']);
}
public function testJsonSerialize(): void
{
$additionalProps = ['customProp' => 'value'];
$model = $this->createConcreteModel($additionalProps);
$expected = [
'identifier' => 'test-id',
'title' => 'Test Title',
'customProp' => 'value',
];
$this->assertEquals($expected, $model->jsonSerialize());
// Test JSON encoding
$this->assertEquals(json_encode($expected), json_encode($model));
}
public function testMagicGetMethod(): void
{
$additionalProps = ['buttonText' => 'Click Me', 'caption' => 'Test Caption'];
$model = $this->createConcreteModel($additionalProps);
// Test accessing existing properties
$this->assertEquals('test-id', $model->identifier);
$this->assertEquals('Test Title', $model->title);
// Test accessing additional properties via magic __get
$this->assertEquals('Click Me', $model->buttonText);
$this->assertEquals('Test Caption', $model->caption);
// Test accessing non-existent property
$this->assertNull($model->nonExistentProp);
}
public function testMagicIssetMethod(): void
{
$additionalProps = ['buttonText' => 'Click Me', 'emptyProp' => ''];
$model = $this->createConcreteModel($additionalProps);
// Test isset on existing properties
$this->assertTrue(isset($model->identifier));
$this->assertTrue(isset($model->title));
// Test isset on additional properties
$this->assertTrue(isset($model->buttonText));
$this->assertTrue(isset($model->emptyProp)); // Even empty string should return true for isset
// Test isset on non-existent property
$this->assertFalse(isset($model->nonExistentProp));
}
}
/**
* Concrete implementation of AbstractModel for testing
*/
class ConcreteModel extends AbstractModel
{
public function __construct(
public readonly string $identifier,
public readonly string $title,
array $additionalProperties = [],
) {
$this->setAdditionalProperties($additionalProperties);
}
public function jsonSerialize(): array
{
return array_merge(
[
'identifier' => $this->identifier,
'title' => $this->title,
],
$this->getAdditionalProperties()
);
}
}