Skip to content

Commit 6bfb720

Browse files
committed
test: adds PropertyDefinition tests
1 parent 79979ed commit 6bfb720

File tree

1 file changed

+229
-0
lines changed

1 file changed

+229
-0
lines changed
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
<?php
2+
3+
namespace StellarWP\Models\Tests\Unit;
4+
5+
use StellarWP\Models\ModelPropertyDefinition;
6+
use StellarWP\Models\Tests\ModelsTestCase;
7+
8+
/**
9+
* @coversDefaultClass \StellarWP\Models\ModelPropertyDefinition
10+
*/
11+
class ModelPropertyDefinitionTest extends ModelsTestCase {
12+
/**
13+
* @since 2.0.0
14+
*
15+
* @covers ::default
16+
* @covers ::hasDefault
17+
* @covers ::getDefault
18+
*/
19+
public function testDefaultAndHasDefault() {
20+
$definition = new ModelPropertyDefinition();
21+
22+
$this->assertFalse($definition->hasDefault());
23+
24+
$definition->default('test');
25+
$this->assertTrue($definition->hasDefault());
26+
$this->assertSame('test', $definition->getDefault());
27+
28+
// Test with closure
29+
$definition = new ModelPropertyDefinition();
30+
$definition->default(function() {
31+
return 'closure-value';
32+
});
33+
34+
$this->assertTrue($definition->hasDefault());
35+
$this->assertSame('closure-value', $definition->getDefault());
36+
}
37+
38+
/**
39+
* @since 2.0.0
40+
*
41+
* @covers ::type
42+
* @covers ::getType
43+
* @covers ::supportsType
44+
*/
45+
public function testTypeAndGetType() {
46+
$definition = new ModelPropertyDefinition();
47+
48+
// Default type should be string
49+
$this->assertSame(['string'], $definition->getType());
50+
51+
// Set single type
52+
$definition->type('int');
53+
$this->assertSame(['int'], $definition->getType());
54+
55+
// Set multiple types
56+
$definition = new ModelPropertyDefinition();
57+
$definition->type('int', 'string', 'bool');
58+
$this->assertSame(['int', 'string', 'bool'], $definition->getType());
59+
60+
// Test supportsType
61+
$this->assertTrue($definition->supportsType('int'));
62+
$this->assertTrue($definition->supportsType('string'));
63+
$this->assertTrue($definition->supportsType('bool'));
64+
$this->assertFalse($definition->supportsType('array'));
65+
}
66+
67+
/**
68+
* @since 2.0.0
69+
*
70+
* @covers ::nullable
71+
* @covers ::isNullable
72+
*/
73+
public function testNullableAndIsNullable() {
74+
$definition = new ModelPropertyDefinition();
75+
76+
$this->assertFalse($definition->isNullable());
77+
78+
$definition->nullable();
79+
$this->assertTrue($definition->isNullable());
80+
}
81+
82+
/**
83+
* @since 2.0.0
84+
*
85+
* @covers ::required
86+
* @covers ::isRequired
87+
*/
88+
public function testRequiredAndIsRequired() {
89+
$definition = new ModelPropertyDefinition();
90+
91+
$this->assertFalse($definition->isRequired());
92+
93+
$definition->required();
94+
$this->assertTrue($definition->isRequired());
95+
}
96+
97+
/**
98+
* @since 2.0.0
99+
*
100+
* @covers ::requiredOnSave
101+
* @covers ::isRequiredOnSave
102+
*/
103+
public function testRequiredOnSaveAndIsRequiredOnSave() {
104+
$definition = new ModelPropertyDefinition();
105+
106+
$this->assertFalse($definition->isRequiredOnSave());
107+
108+
$definition->requiredOnSave();
109+
$this->assertTrue($definition->isRequiredOnSave());
110+
}
111+
112+
/**
113+
* @since 2.0.0
114+
*
115+
* @covers ::lock
116+
* @covers ::isLocked
117+
* @covers ::checkLock
118+
*/
119+
public function testLockAndIsLocked() {
120+
$definition = new ModelPropertyDefinition();
121+
122+
$this->assertFalse($definition->isLocked());
123+
124+
$definition->lock();
125+
$this->assertTrue($definition->isLocked());
126+
127+
// Once locked, attempting to modify should throw exception
128+
$this->expectException(\RuntimeException::class);
129+
$this->expectExceptionMessage('Property is locked');
130+
131+
$definition->default('something');
132+
}
133+
134+
/**
135+
* @since 2.0.0
136+
*
137+
* @covers ::isValidValue
138+
* @covers ::supportsType
139+
*/
140+
public function testIsValidValue() {
141+
$definition = new ModelPropertyDefinition();
142+
$definition->type('string');
143+
144+
$this->assertTrue($definition->isValidValue('test'));
145+
$this->assertFalse($definition->isValidValue(123));
146+
147+
// Test with nullable
148+
$definition->nullable();
149+
$this->assertTrue($definition->isValidValue(null));
150+
151+
// Test with multiple types
152+
$definition = new ModelPropertyDefinition();
153+
$definition->type('int', 'string');
154+
155+
$this->assertTrue($definition->isValidValue('test'));
156+
$this->assertTrue($definition->isValidValue(123));
157+
$this->assertFalse($definition->isValidValue(true));
158+
159+
// Test with object type
160+
$definition = new ModelPropertyDefinition();
161+
$definition->type('object');
162+
163+
$this->assertTrue($definition->isValidValue(new \stdClass()));
164+
$this->assertFalse($definition->isValidValue('test'));
165+
166+
// Test with specific class
167+
$definition = new ModelPropertyDefinition();
168+
$definition->type(\stdClass::class);
169+
170+
$this->assertTrue($definition->isValidValue(new \stdClass()));
171+
$this->assertFalse($definition->isValidValue(new \Exception()));
172+
}
173+
174+
/**
175+
* @since 2.0.0
176+
*
177+
* @covers ::castWith
178+
* @covers ::cast
179+
* @covers ::canCast
180+
*/
181+
public function testCastWithAndCast() {
182+
$definition = new ModelPropertyDefinition();
183+
184+
$this->assertFalse($definition->canCast());
185+
186+
$definition->castWith(function($value, $property) {
187+
return (int) $value;
188+
});
189+
190+
$this->assertTrue($definition->canCast());
191+
$this->assertSame(123, $definition->cast('123'));
192+
193+
// Test exception when no cast method set
194+
$definition = new ModelPropertyDefinition();
195+
196+
$this->expectException(\RuntimeException::class);
197+
$this->expectExceptionMessage('No cast method set');
198+
199+
$definition->cast('test');
200+
}
201+
202+
/**
203+
* @since 2.0.0
204+
*
205+
* @covers ::fromShorthand
206+
*/
207+
public function testFromShorthand() {
208+
// Test with string
209+
$definition = ModelPropertyDefinition::fromShorthand('int');
210+
211+
$this->assertSame(['int'], $definition->getType());
212+
$this->assertTrue($definition->isNullable());
213+
$this->assertFalse($definition->hasDefault());
214+
215+
// Test with array
216+
$definition = ModelPropertyDefinition::fromShorthand(['string', 'default-value']);
217+
218+
$this->assertSame(['string'], $definition->getType());
219+
$this->assertTrue($definition->isNullable());
220+
$this->assertTrue($definition->hasDefault());
221+
$this->assertSame('default-value', $definition->getDefault());
222+
223+
// Test invalid shorthand
224+
$this->expectException(\InvalidArgumentException::class);
225+
$this->expectExceptionMessage('Invalid shorthand property definition');
226+
227+
ModelPropertyDefinition::fromShorthand(['string', 'too', 'many', 'items']);
228+
}
229+
}

0 commit comments

Comments
 (0)