EloquentModelStringCastingTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace Illuminate\Tests\Integration\Database;
  3. use Illuminate\Database\Eloquent\Model as Eloquent;
  4. use Illuminate\Database\Schema\Blueprint;
  5. use Illuminate\Support\Facades\Schema;
  6. use stdClass;
  7. class EloquentModelStringCastingTest extends DatabaseTestCase
  8. {
  9. protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
  10. {
  11. Schema::create('casting_table', function (Blueprint $table) {
  12. $table->increments('id');
  13. $table->string('array_attributes');
  14. $table->string('json_attributes');
  15. $table->string('object_attributes');
  16. $table->timestamps();
  17. });
  18. }
  19. /**
  20. * Tests...
  21. */
  22. public function testSavingCastedAttributesToDatabase()
  23. {
  24. /** @var \Illuminate\Tests\Integration\Database\StringCasts $model */
  25. $model = StringCasts::create([
  26. 'array_attributes' => ['key1' => 'value1'],
  27. 'json_attributes' => ['json_key' => 'json_value'],
  28. 'object_attributes' => ['json_key' => 'json_value'],
  29. ]);
  30. $this->assertSame(['key1' => 'value1'], $model->getOriginal('array_attributes'));
  31. $this->assertSame(['key1' => 'value1'], $model->getAttribute('array_attributes'));
  32. $this->assertSame(['json_key' => 'json_value'], $model->getOriginal('json_attributes'));
  33. $this->assertSame(['json_key' => 'json_value'], $model->getAttribute('json_attributes'));
  34. $stdClass = new stdClass;
  35. $stdClass->json_key = 'json_value';
  36. $this->assertEquals($stdClass, $model->getOriginal('object_attributes'));
  37. $this->assertEquals($stdClass, $model->getAttribute('object_attributes'));
  38. }
  39. public function testSavingCastedEmptyAttributesToDatabase()
  40. {
  41. /** @var \Illuminate\Tests\Integration\Database\StringCasts $model */
  42. $model = StringCasts::create([
  43. 'array_attributes' => [],
  44. 'json_attributes' => [],
  45. 'object_attributes' => [],
  46. ]);
  47. $this->assertSame([], $model->getOriginal('array_attributes'));
  48. $this->assertSame([], $model->getAttribute('array_attributes'));
  49. $this->assertSame([], $model->getOriginal('json_attributes'));
  50. $this->assertSame([], $model->getAttribute('json_attributes'));
  51. $this->assertSame([], $model->getOriginal('object_attributes'));
  52. $this->assertSame([], $model->getAttribute('object_attributes'));
  53. }
  54. }
  55. /**
  56. * Eloquent Models...
  57. */
  58. class StringCasts extends Eloquent
  59. {
  60. /**
  61. * @var string
  62. */
  63. protected $table = 'casting_table';
  64. /**
  65. * @var string[]
  66. */
  67. protected $guarded = [];
  68. /**
  69. * @var array
  70. */
  71. protected $casts = [
  72. 'array_attributes' => 'array',
  73. 'json_attributes' => 'json',
  74. 'object_attributes' => 'object',
  75. ];
  76. }