|
10 | 10 |
|
11 | 11 | class StructTest extends TestCase |
12 | 12 | { |
13 | | - public function testStructSetAndGet() |
| 13 | + public function testConstructionAndFieldRegistration(): void |
14 | 14 | { |
15 | | - // Example 1 |
16 | 15 | $struct = new Struct([ |
| 16 | + 'id' => 'int', |
17 | 17 | 'name' => 'string', |
18 | | - 'age' => '?int', |
19 | | - 'balance' => 'float', |
20 | 18 | ]); |
21 | | - |
22 | | - // Test setting and getting field values using set/get methods |
23 | | - $struct->set('name', 'Nejc'); |
24 | | - $struct->set('age', null); // Nullable type |
25 | | - $struct->set('balance', 100.50); |
26 | | - |
27 | | - // Assertions |
28 | | - $this->assertEquals('Nejc', $struct->get('name')); |
29 | | - $this->assertNull($struct->get('age')); |
30 | | - $this->assertEquals(100.50, $struct->get('balance')); |
| 19 | + $fields = $struct->getFields(); |
| 20 | + $this->assertArrayHasKey('id', $fields); |
| 21 | + $this->assertArrayHasKey('name', $fields); |
| 22 | + $this->assertSame('int', $fields['id']['type']); |
| 23 | + $this->assertSame('string', $fields['name']['type']); |
| 24 | + $this->assertNull($fields['id']['value']); |
| 25 | + $this->assertNull($fields['name']['value']); |
31 | 26 | } |
32 | 27 |
|
33 | | - public function testMagicMethods() |
| 28 | + public function testSetAndGet(): void |
34 | 29 | { |
35 | | - // Example 1 with magic methods |
36 | 30 | $struct = new Struct([ |
| 31 | + 'id' => 'int', |
37 | 32 | 'name' => 'string', |
38 | | - 'age' => '?int', |
39 | | - 'balance' => 'float', |
40 | 33 | ]); |
41 | | - |
42 | | - // Test setting and getting field values using magic methods |
43 | | - $struct->name = 'John'; |
44 | | - $struct->age = null; |
45 | | - $struct->balance = 200.75; |
46 | | - |
47 | | - // Assertions |
48 | | - $this->assertEquals('John', $struct->name); |
49 | | - $this->assertNull($struct->age); |
50 | | - $this->assertEquals(200.75, $struct->balance); |
| 34 | + $struct->set('id', 42); |
| 35 | + $struct->set('name', 'Alice'); |
| 36 | + $this->assertSame(42, $struct->get('id')); |
| 37 | + $this->assertSame('Alice', $struct->get('name')); |
51 | 38 | } |
52 | 39 |
|
53 | | - public function testStructHelperFunction() |
| 40 | + public function testSetWrongTypeThrows(): void |
54 | 41 | { |
55 | | - // Example 2: using the `struct()` helper function (assuming it is defined) |
56 | | - $struct = struct([ |
57 | | - 'name' => 'string', |
58 | | - 'age' => '?int', |
59 | | - 'balance' => 'float', |
| 42 | + $struct = new Struct([ |
| 43 | + 'id' => 'int', |
60 | 44 | ]); |
| 45 | + $this->expectException(InvalidArgumentException::class); |
| 46 | + $struct->set('id', 'not an int'); |
| 47 | + } |
61 | 48 |
|
62 | | - // Test setting and getting field values using set/get methods |
63 | | - $struct->set('name', 'Test'); |
64 | | - $struct->set('age', null); |
65 | | - $struct->set('balance', 100.50); |
| 49 | + public function testSetNullableField(): void |
| 50 | + { |
| 51 | + $struct = new Struct([ |
| 52 | + 'desc' => '?string', |
| 53 | + ]); |
| 54 | + $struct->set('desc', null); |
| 55 | + $this->assertNull($struct->get('desc')); |
| 56 | + $struct->set('desc', 'hello'); |
| 57 | + $this->assertSame('hello', $struct->get('desc')); |
| 58 | + } |
66 | 59 |
|
67 | | - // Assertions |
68 | | - $this->assertEquals('Test', $struct->get('name')); |
69 | | - $this->assertNull($struct->get('age')); |
70 | | - $this->assertEquals(100.50, $struct->get('balance')); |
| 60 | + public function testSetNonNullableFieldNullThrows(): void |
| 61 | + { |
| 62 | + $struct = new Struct([ |
| 63 | + 'id' => 'int', |
| 64 | + ]); |
| 65 | + $this->expectException(InvalidArgumentException::class); |
| 66 | + $struct->set('id', null); |
71 | 67 | } |
72 | 68 |
|
73 | | - public function testInvalidFieldThrowsException() |
| 69 | + public function testSetSubclass(): void |
74 | 70 | { |
75 | 71 | $struct = new Struct([ |
76 | | - 'name' => 'string', |
| 72 | + 'obj' => 'stdClass', |
77 | 73 | ]); |
| 74 | + $obj = new class extends \stdClass {}; |
| 75 | + $struct->set('obj', $obj); |
| 76 | + $this->assertSame($obj, $struct->get('obj')); |
| 77 | + } |
78 | 78 |
|
| 79 | + public function testGetNonexistentFieldThrows(): void |
| 80 | + { |
| 81 | + $struct = new Struct([ |
| 82 | + 'id' => 'int', |
| 83 | + ]); |
79 | 84 | $this->expectException(InvalidArgumentException::class); |
80 | | - $this->expectExceptionMessage("Field 'age' does not exist in the struct."); |
81 | | - |
82 | | - $struct->set('age', 25); // This should throw an exception |
| 85 | + $struct->get('missing'); |
83 | 86 | } |
84 | 87 |
|
85 | | - public function testInvalidTypeThrowsException() |
| 88 | + public function testSetNonexistentFieldThrows(): void |
86 | 89 | { |
87 | 90 | $struct = new Struct([ |
88 | | - 'name' => 'string', |
| 91 | + 'id' => 'int', |
89 | 92 | ]); |
| 93 | + $this->expectException(InvalidArgumentException::class); |
| 94 | + $struct->set('missing', 123); |
| 95 | + } |
90 | 96 |
|
| 97 | + public function testDuplicateFieldThrows(): void |
| 98 | + { |
91 | 99 | $this->expectException(InvalidArgumentException::class); |
92 | | - $this->expectExceptionMessage("Field 'name' expects type 'string', but got 'int'."); |
| 100 | + // Simulate duplicate by calling addField directly via reflection |
| 101 | + $struct = new Struct(['id' => 'int']); |
| 102 | + $ref = new \ReflectionClass($struct); |
| 103 | + $method = $ref->getMethod('addField'); |
| 104 | + $method->setAccessible(true); |
| 105 | + $method->invoke($struct, 'id', 'int'); |
| 106 | + } |
93 | 107 |
|
94 | | - $struct->set('name', 123); // Invalid type |
| 108 | + public function testMagicGetSet(): void |
| 109 | + { |
| 110 | + $struct = new Struct([ |
| 111 | + 'foo' => 'int', |
| 112 | + ]); |
| 113 | + $struct->foo = 123; |
| 114 | + $this->assertSame(123, $struct->foo); |
95 | 115 | } |
96 | 116 | } |
0 commit comments