|
109 | 109 | TestPlace::insert(array_merge(TestPlace::factory()->definition(), [
|
110 | 110 | 'point_with_line_string_cast' => DB::raw('POINT(0, 180)'),
|
111 | 111 | ]));
|
112 |
| - /** @var TestPlace $testPlace */ |
113 |
| - $testPlace = TestPlace::firstOrFail(); |
114 | 112 |
|
115 |
| - expect(function () use ($testPlace): void { |
116 |
| - $testPlace->getAttribute('point_with_line_string_cast'); |
| 113 | + expect(function (): void { |
| 114 | + // cast deserialization happens in setRawAttributes |
| 115 | + // called immediately after retrieving db record |
| 116 | + TestPlace::firstOrFail(); |
117 | 117 | })->toThrow(InvalidArgumentException::class);
|
118 | 118 | });
|
119 | 119 |
|
|
126 | 126 |
|
127 | 127 | expect($testPlace->point)->toEqual($point);
|
128 | 128 | });
|
| 129 | + |
| 130 | +it('is serializeable and JSON encodeable by standard serialize / JSON encode functions', function (): void { |
| 131 | + $point = new Point(0, 180); |
| 132 | + |
| 133 | + /** @var TestPlace $testPlace */ |
| 134 | + $testPlace = TestPlace::factory()->make(['point' => $point]); |
| 135 | + |
| 136 | + $serialized = serialize($testPlace); |
| 137 | + $json = json_encode($serialized); |
| 138 | + |
| 139 | + expect($json)->toBeTruthy('JSON encoding failed.'); |
| 140 | + |
| 141 | + // @phpstan-ignore-next-line |
| 142 | + $recoveredTestPlace = unserialize(json_decode($json)); |
| 143 | + |
| 144 | + expect($recoveredTestPlace)->toEqual($testPlace); |
| 145 | +}); |
| 146 | + |
| 147 | +it('is serializeable and JSON encodeable by standard serialize / JSON encode functions when retrieved from db', function (): void { |
| 148 | + $point = new Point(0, 180); |
| 149 | + |
| 150 | + /** @var TestPlace $testPlace */ |
| 151 | + $testPlace = TestPlace::factory()->make(['point' => $point]); |
| 152 | + |
| 153 | + $testPlace->save(); |
| 154 | + |
| 155 | + $testPlaceFromDb = TestPlace::find($testPlace->id); |
| 156 | + |
| 157 | + $serialized = serialize($testPlaceFromDb); |
| 158 | + $json = json_encode($serialized); |
| 159 | + |
| 160 | + expect($json)->toBeTruthy('JSON encoding failed.'); |
| 161 | + |
| 162 | + // @phpstan-ignore-next-line |
| 163 | + $recoveredTestPlace = unserialize(json_decode($json)); |
| 164 | + |
| 165 | + expect($recoveredTestPlace)->toEqual($testPlaceFromDb); |
| 166 | +}); |
0 commit comments