@@ -2472,6 +2472,7 @@ public function testModelAttributesAreCastedWhenPresentInCastsPropertyOrCastsMet
2472
2472
$ obj ->foo = 'bar ' ;
2473
2473
$ model ->arrayAttribute = $ obj ;
2474
2474
$ model ->jsonAttribute = ['foo ' => 'bar ' ];
2475
+ $ model ->jsonAttributeWithUnicode = ['こんにちは ' => '世界 ' ];
2475
2476
$ model ->dateAttribute = '1969-07-20 ' ;
2476
2477
$ model ->datetimeAttribute = '1969-07-20 22:56:00 ' ;
2477
2478
$ model ->timestampAttribute = '1969-07-20 22:56:00 ' ;
@@ -2486,12 +2487,15 @@ public function testModelAttributesAreCastedWhenPresentInCastsPropertyOrCastsMet
2486
2487
$ this ->assertIsObject ($ model ->objectAttribute );
2487
2488
$ this ->assertIsArray ($ model ->arrayAttribute );
2488
2489
$ this ->assertIsArray ($ model ->jsonAttribute );
2490
+ $ this ->assertIsArray ($ model ->jsonAttributeWithUnicode );
2489
2491
$ this ->assertTrue ($ model ->boolAttribute );
2490
2492
$ this ->assertFalse ($ model ->booleanAttribute );
2491
2493
$ this ->assertEquals ($ obj , $ model ->objectAttribute );
2492
2494
$ this ->assertEquals (['foo ' => 'bar ' ], $ model ->arrayAttribute );
2493
2495
$ this ->assertEquals (['foo ' => 'bar ' ], $ model ->jsonAttribute );
2494
2496
$ this ->assertSame ('{"foo":"bar"} ' , $ model ->jsonAttributeValue ());
2497
+ $ this ->assertEquals (['こんにちは ' => '世界 ' ], $ model ->jsonAttributeWithUnicode );
2498
+ $ this ->assertSame ('{"こんにちは":"世界"} ' , $ model ->jsonAttributeWithUnicodeValue ());
2495
2499
$ this ->assertInstanceOf (Carbon::class, $ model ->dateAttribute );
2496
2500
$ this ->assertInstanceOf (Carbon::class, $ model ->datetimeAttribute );
2497
2501
$ this ->assertInstanceOf (BaseCollection::class, $ model ->collectionAttribute );
@@ -2510,12 +2514,14 @@ public function testModelAttributesAreCastedWhenPresentInCastsPropertyOrCastsMet
2510
2514
$ this ->assertIsObject ($ arr ['objectAttribute ' ]);
2511
2515
$ this ->assertIsArray ($ arr ['arrayAttribute ' ]);
2512
2516
$ this ->assertIsArray ($ arr ['jsonAttribute ' ]);
2517
+ $ this ->assertIsArray ($ arr ['jsonAttributeWithUnicode ' ]);
2513
2518
$ this ->assertIsArray ($ arr ['collectionAttribute ' ]);
2514
2519
$ this ->assertTrue ($ arr ['boolAttribute ' ]);
2515
2520
$ this ->assertFalse ($ arr ['booleanAttribute ' ]);
2516
2521
$ this ->assertEquals ($ obj , $ arr ['objectAttribute ' ]);
2517
2522
$ this ->assertEquals (['foo ' => 'bar ' ], $ arr ['arrayAttribute ' ]);
2518
2523
$ this ->assertEquals (['foo ' => 'bar ' ], $ arr ['jsonAttribute ' ]);
2524
+ $ this ->assertEquals (['こんにちは ' => '世界 ' ], $ arr ['jsonAttributeWithUnicode ' ]);
2519
2525
$ this ->assertSame ('1969-07-20 00:00:00 ' , $ arr ['dateAttribute ' ]);
2520
2526
$ this ->assertSame ('1969-07-20 22:56:00 ' , $ arr ['datetimeAttribute ' ]);
2521
2527
$ this ->assertEquals (-14173440 , $ arr ['timestampAttribute ' ]);
@@ -2544,6 +2550,7 @@ public function testModelAttributeCastingPreservesNull()
2544
2550
$ model ->objectAttribute = null ;
2545
2551
$ model ->arrayAttribute = null ;
2546
2552
$ model ->jsonAttribute = null ;
2553
+ $ model ->jsonAttributeWithUnicode = null ;
2547
2554
$ model ->dateAttribute = null ;
2548
2555
$ model ->datetimeAttribute = null ;
2549
2556
$ model ->timestampAttribute = null ;
@@ -2559,6 +2566,7 @@ public function testModelAttributeCastingPreservesNull()
2559
2566
$ this ->assertNull ($ attributes ['objectAttribute ' ]);
2560
2567
$ this ->assertNull ($ attributes ['arrayAttribute ' ]);
2561
2568
$ this ->assertNull ($ attributes ['jsonAttribute ' ]);
2569
+ $ this ->assertNull ($ attributes ['jsonAttributeWithUnicode ' ]);
2562
2570
$ this ->assertNull ($ attributes ['dateAttribute ' ]);
2563
2571
$ this ->assertNull ($ attributes ['datetimeAttribute ' ]);
2564
2572
$ this ->assertNull ($ attributes ['timestampAttribute ' ]);
@@ -2572,6 +2580,7 @@ public function testModelAttributeCastingPreservesNull()
2572
2580
$ this ->assertNull ($ model ->objectAttribute );
2573
2581
$ this ->assertNull ($ model ->arrayAttribute );
2574
2582
$ this ->assertNull ($ model ->jsonAttribute );
2583
+ $ this ->assertNull ($ model ->jsonAttributeWithUnicode );
2575
2584
$ this ->assertNull ($ model ->dateAttribute );
2576
2585
$ this ->assertNull ($ model ->datetimeAttribute );
2577
2586
$ this ->assertNull ($ model ->timestampAttribute );
@@ -2587,6 +2596,7 @@ public function testModelAttributeCastingPreservesNull()
2587
2596
$ this ->assertNull ($ array ['objectAttribute ' ]);
2588
2597
$ this ->assertNull ($ array ['arrayAttribute ' ]);
2589
2598
$ this ->assertNull ($ array ['jsonAttribute ' ]);
2599
+ $ this ->assertNull ($ array ['jsonAttributeWithUnicode ' ]);
2590
2600
$ this ->assertNull ($ array ['dateAttribute ' ]);
2591
2601
$ this ->assertNull ($ array ['datetimeAttribute ' ]);
2592
2602
$ this ->assertNull ($ array ['timestampAttribute ' ]);
@@ -2603,11 +2613,45 @@ public function testModelAttributeCastingFailsOnUnencodableData()
2603
2613
$ obj = new stdClass ;
2604
2614
$ obj ->foo = "b \xF8r " ;
2605
2615
$ model ->arrayAttribute = $ obj ;
2616
+
2617
+ $ model ->getAttributes ();
2618
+ }
2619
+
2620
+ public function testModelJsonCastingFailsOnUnencodableData ()
2621
+ {
2622
+ $ this ->expectException (JsonEncodingException::class);
2623
+ $ this ->expectExceptionMessage ('Unable to encode attribute [jsonAttribute] for model [Illuminate\Tests\Database\EloquentModelCastingStub] to JSON: Malformed UTF-8 characters, possibly incorrectly encoded. ' );
2624
+
2625
+ $ model = new EloquentModelCastingStub ;
2606
2626
$ model ->jsonAttribute = ['foo ' => "b \xF8r " ];
2607
2627
2608
2628
$ model ->getAttributes ();
2609
2629
}
2610
2630
2631
+ public function testModelAttributeCastingFailsOnUnencodableDataWithUnicode ()
2632
+ {
2633
+ $ this ->expectException (JsonEncodingException::class);
2634
+ $ this ->expectExceptionMessage ('Unable to encode attribute [jsonAttributeWithUnicode] for model [Illuminate\Tests\Database\EloquentModelCastingStub] to JSON: Malformed UTF-8 characters, possibly incorrectly encoded. ' );
2635
+
2636
+ $ model = new EloquentModelCastingStub ;
2637
+ $ model ->jsonAttributeWithUnicode = ['foo ' => "b \xF8r " ];
2638
+
2639
+ $ model ->getAttributes ();
2640
+ }
2641
+
2642
+ public function testJsonCastingRespectsUnicodeOption ()
2643
+ {
2644
+ $ data = ['こんにちは ' => '世界 ' ];
2645
+ $ model = new EloquentModelCastingStub ;
2646
+ $ model ->jsonAttribute = $ data ;
2647
+ $ model ->jsonAttributeWithUnicode = $ data ;
2648
+
2649
+ $ this ->assertSame ('{"\u3053\u3093\u306b\u3061\u306f":"\u4e16\u754c"} ' , $ model ->jsonAttributeValue ());
2650
+ $ this ->assertSame ('{"こんにちは":"世界"} ' , $ model ->jsonAttributeWithUnicodeValue ());
2651
+ $ this ->assertSame (['こんにちは ' => '世界 ' ], $ model ->jsonAttribute );
2652
+ $ this ->assertSame (['こんにちは ' => '世界 ' ], $ model ->jsonAttributeWithUnicode );
2653
+ }
2654
+
2611
2655
public function testModelAttributeCastingWithFloats ()
2612
2656
{
2613
2657
$ model = new EloquentModelCastingStub ;
@@ -3028,6 +3072,7 @@ public function testGetOriginalCastsAttributes()
3028
3072
$ collection = collect ($ array );
3029
3073
$ model ->arrayAttribute = $ array ;
3030
3074
$ model ->jsonAttribute = $ array ;
3075
+ $ model ->jsonAttributeWithUnicode = $ array ;
3031
3076
$ model ->collectionAttribute = $ collection ;
3032
3077
3033
3078
$ model ->syncOriginal ();
@@ -3044,6 +3089,9 @@ public function testGetOriginalCastsAttributes()
3044
3089
$ model ->jsonAttribute = [
3045
3090
'foo ' => 'bar2 ' ,
3046
3091
];
3092
+ $ model ->jsonAttributeWithUnicode = [
3093
+ 'foo ' => 'bar2 ' ,
3094
+ ];
3047
3095
$ model ->collectionAttribute = collect ([
3048
3096
'foo ' => 'bar2 ' ,
3049
3097
]);
@@ -3080,6 +3128,10 @@ public function testGetOriginalCastsAttributes()
3080
3128
$ this ->assertEquals (['foo ' => 'bar ' ], $ model ->getOriginal ('jsonAttribute ' ));
3081
3129
$ this ->assertEquals (['foo ' => 'bar2 ' ], $ model ->getAttribute ('jsonAttribute ' ));
3082
3130
3131
+ $ this ->assertEquals ($ array , $ model ->getOriginal ('jsonAttributeWithUnicode ' ));
3132
+ $ this ->assertEquals (['foo ' => 'bar ' ], $ model ->getOriginal ('jsonAttributeWithUnicode ' ));
3133
+ $ this ->assertEquals (['foo ' => 'bar2 ' ], $ model ->getAttribute ('jsonAttributeWithUnicode ' ));
3134
+
3083
3135
$ this ->assertEquals (['foo ' => 'bar ' ], $ model ->getOriginal ('collectionAttribute ' )->toArray ());
3084
3136
$ this ->assertEquals (['foo ' => 'bar2 ' ], $ model ->getAttribute ('collectionAttribute ' )->toArray ());
3085
3137
}
@@ -3596,6 +3648,7 @@ class EloquentModelCastingStub extends Model
3596
3648
'boolAttribute ' => 'bool ' ,
3597
3649
'objectAttribute ' => 'object ' ,
3598
3650
'jsonAttribute ' => 'json ' ,
3651
+ 'jsonAttributeWithUnicode ' => 'json:unicode ' ,
3599
3652
'dateAttribute ' => 'date ' ,
3600
3653
'timestampAttribute ' => 'timestamp ' ,
3601
3654
'ascollectionAttribute ' => AsCollection::class,
@@ -3633,6 +3686,11 @@ public function jsonAttributeValue()
3633
3686
return $ this ->attributes ['jsonAttribute ' ];
3634
3687
}
3635
3688
3689
+ public function jsonAttributeWithUnicodeValue ()
3690
+ {
3691
+ return $ this ->attributes ['jsonAttributeWithUnicode ' ];
3692
+ }
3693
+
3636
3694
protected function serializeDate (DateTimeInterface $ date )
3637
3695
{
3638
3696
return $ date ->format ('Y-m-d H:i:s ' );
0 commit comments