|
13 | 13 | namespace HyperfTest\Database; |
14 | 14 |
|
15 | 15 | use Hyperf\Collection\Arr; |
| 16 | +use Hyperf\Collection\Collection; |
| 17 | +use Hyperf\Contract\Arrayable; |
16 | 18 | use Hyperf\Contract\Castable; |
17 | 19 | use Hyperf\Contract\CastsAttributes; |
18 | 20 | use Hyperf\Contract\CastsInboundAttributes; |
19 | 21 | use Hyperf\Database\Exception\InvalidCastException; |
| 22 | +use Hyperf\Database\Model\Casts\ArrayObject; |
| 23 | +use Hyperf\Database\Model\Casts\AsArrayObject; |
20 | 24 | use Hyperf\Database\Model\CastsValue; |
21 | 25 | use Hyperf\Database\Model\Model; |
22 | 26 | use HyperfTest\Database\Stubs\ContainerStub; |
| 27 | +use JsonSerializable; |
23 | 28 | use Mockery; |
24 | 29 | use PHPUnit\Framework\Attributes\CoversNothing; |
25 | 30 | use PHPUnit\Framework\TestCase; |
@@ -263,6 +268,129 @@ public function testThrowExceptionWhenCastClassNotExist() |
263 | 268 | $model = new TestModelWithCustomCast(); |
264 | 269 | $model->invalid_caster = 'foo'; |
265 | 270 | } |
| 271 | + |
| 272 | + public function testAsArrayObjectCasting() |
| 273 | + { |
| 274 | + $model = new TestModelWithArrayObjectCast(); |
| 275 | + |
| 276 | + // Test setting array data |
| 277 | + $data = ['name' => 'Hyperf', 'version' => '3.1', 'features' => ['fast', 'modern']]; |
| 278 | + $model->config = $data; |
| 279 | + |
| 280 | + $this->assertInstanceOf(ArrayObject::class, $model->config); |
| 281 | + $this->assertEquals($data, $model->config->toArray()); |
| 282 | + $this->assertEquals(json_encode($data), $model->getAttributes()['config']); |
| 283 | + |
| 284 | + // Test ArrayObject methods |
| 285 | + $this->assertEquals('Hyperf', $model->config['name']); |
| 286 | + $this->assertEquals('3.1', $model->config['version']); |
| 287 | + $this->assertCount(3, $model->config); |
| 288 | + |
| 289 | + // Test collect method |
| 290 | + $collection = $model->config->collect(); |
| 291 | + $this->assertInstanceOf(Collection::class, $collection); |
| 292 | + $this->assertEquals($data, $collection->toArray()); |
| 293 | + |
| 294 | + // Test modification |
| 295 | + $model->config['type'] = 'framework'; |
| 296 | + $this->assertEquals('framework', $model->config['type']); |
| 297 | + $this->assertCount(4, $model->config); |
| 298 | + |
| 299 | + // Test JSON serialization |
| 300 | + $this->assertEquals($model->config->toArray(), $model->config->jsonSerialize()); |
| 301 | + |
| 302 | + // Test with null value |
| 303 | + $model->config = null; |
| 304 | + $this->assertNull($model->config); |
| 305 | + |
| 306 | + // Test with empty array |
| 307 | + $model->config = []; |
| 308 | + $this->assertInstanceOf(ArrayObject::class, $model->config); |
| 309 | + $this->assertCount(0, $model->config); |
| 310 | + $this->assertEquals([], $model->config->toArray()); |
| 311 | + } |
| 312 | + |
| 313 | + public function testAsArrayObjectCastingFromDatabase() |
| 314 | + { |
| 315 | + $model = new TestModelWithArrayObjectCast(); |
| 316 | + |
| 317 | + // Simulate loading from database |
| 318 | + $jsonData = json_encode(['database' => 'mysql', 'driver' => 'swoole']); |
| 319 | + $model->setRawAttributes(['config' => $jsonData]); |
| 320 | + |
| 321 | + $this->assertInstanceOf(ArrayObject::class, $model->config); |
| 322 | + $this->assertEquals('mysql', $model->config['database']); |
| 323 | + $this->assertEquals('swoole', $model->config['driver']); |
| 324 | + $this->assertEquals(['database' => 'mysql', 'driver' => 'swoole'], $model->config->toArray()); |
| 325 | + } |
| 326 | + |
| 327 | + public function testAsArrayObjectCastingInvalidJson() |
| 328 | + { |
| 329 | + $model = new TestModelWithArrayObjectCast(); |
| 330 | + $this->expectExceptionMessage('Syntax error'); |
| 331 | + |
| 332 | + // Test with invalid JSON - should return null |
| 333 | + $model->setRawAttributes(['config' => 'invalid json']); |
| 334 | + $this->assertNull($model->config); |
| 335 | + |
| 336 | + // Test with non-array JSON - should return null |
| 337 | + $model->setRawAttributes(['config' => json_encode('string value')]); |
| 338 | + $this->assertNull($model->config); |
| 339 | + |
| 340 | + $model->setRawAttributes(['config' => json_encode(123)]); |
| 341 | + $this->assertNull($model->config); |
| 342 | + } |
| 343 | + |
| 344 | + public function testArrayObjectCollectMethod() |
| 345 | + { |
| 346 | + $data = ['a' => 1, 'b' => 2, 'c' => 3]; |
| 347 | + $arrayObject = new ArrayObject($data); |
| 348 | + |
| 349 | + $collection = $arrayObject->collect(); |
| 350 | + $this->assertInstanceOf(Collection::class, $collection); |
| 351 | + $this->assertEquals($data, $collection->toArray()); |
| 352 | + |
| 353 | + // Test collection methods work |
| 354 | + $filtered = $arrayObject->collect()->filter(fn ($value) => $value > 1); |
| 355 | + $this->assertEquals(['b' => 2, 'c' => 3], $filtered->toArray()); |
| 356 | + } |
| 357 | + |
| 358 | + public function testArrayObjectInterfaces() |
| 359 | + { |
| 360 | + $data = ['key' => 'value', 'number' => 42]; |
| 361 | + $arrayObject = new ArrayObject($data); |
| 362 | + |
| 363 | + // Test Arrayable interface |
| 364 | + $this->assertInstanceOf(Arrayable::class, $arrayObject); |
| 365 | + $this->assertEquals($data, $arrayObject->toArray()); |
| 366 | + |
| 367 | + // Test JsonSerializable interface |
| 368 | + $this->assertInstanceOf(JsonSerializable::class, $arrayObject); |
| 369 | + $this->assertEquals($data, $arrayObject->jsonSerialize()); |
| 370 | + |
| 371 | + // Test array access |
| 372 | + $this->assertEquals('value', $arrayObject['key']); |
| 373 | + $this->assertEquals(42, $arrayObject['number']); |
| 374 | + |
| 375 | + // Test modification |
| 376 | + $arrayObject['new_key'] = 'new_value'; |
| 377 | + $this->assertEquals('new_value', $arrayObject['new_key']); |
| 378 | + } |
| 379 | + |
| 380 | + public function testArrayObjectSerialization() |
| 381 | + { |
| 382 | + $model = new TestModelWithArrayObjectCast(); |
| 383 | + $data = ['serialized' => true, 'data' => [1, 2, 3]]; |
| 384 | + $model->config = $data; |
| 385 | + |
| 386 | + // Test model serialization/unserialization |
| 387 | + $serialized = serialize($model); |
| 388 | + $unserialized = unserialize($serialized); |
| 389 | + |
| 390 | + $this->assertInstanceOf(ArrayObject::class, $unserialized->config); |
| 391 | + $this->assertEquals($data, $unserialized->config->toArray()); |
| 392 | + $this->assertEquals(json_encode($data), $unserialized->getAttributes()['config']); |
| 393 | + } |
266 | 394 | } |
267 | 395 |
|
268 | 396 | class TestModelWithCustomCast extends Model |
@@ -290,6 +418,23 @@ class TestModelWithCustomCast extends Model |
290 | 418 | ]; |
291 | 419 | } |
292 | 420 |
|
| 421 | +class TestModelWithArrayObjectCast extends Model |
| 422 | +{ |
| 423 | + /** |
| 424 | + * The attributes that aren't mass assignable. |
| 425 | + */ |
| 426 | + protected array $guarded = []; |
| 427 | + |
| 428 | + /** |
| 429 | + * The attributes that should be cast to native types. |
| 430 | + */ |
| 431 | + protected array $casts = [ |
| 432 | + 'config' => AsArrayObject::class, |
| 433 | + 'settings' => AsArrayObject::class, |
| 434 | + 'metadata' => AsArrayObject::class, |
| 435 | + ]; |
| 436 | +} |
| 437 | + |
293 | 438 | class CastUsing implements Castable |
294 | 439 | { |
295 | 440 | /** |
|
0 commit comments