|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Tests\Integration\Database; |
| 4 | + |
| 5 | +use Illuminate\Database\Schema\Blueprint; |
| 6 | +use Illuminate\Support\Facades\DB; |
| 7 | +use Illuminate\Support\Facades\Schema; |
| 8 | +use Orchestra\Testbench\Attributes\RequiresDatabase; |
| 9 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 10 | + |
| 11 | +require_once 'Enums.php'; |
| 12 | + |
| 13 | +class QueryBuilderUpdateTest extends DatabaseTestCase |
| 14 | +{ |
| 15 | + protected function afterRefreshingDatabase() |
| 16 | + { |
| 17 | + Schema::create('example', function (Blueprint $table) { |
| 18 | + $table->increments('id'); |
| 19 | + $table->string('name')->nullable(); |
| 20 | + $table->string('title')->nullable(); |
| 21 | + $table->string('status')->nullable(); |
| 22 | + $table->json('payload')->nullable(); |
| 23 | + }); |
| 24 | + } |
| 25 | + |
| 26 | + #[DataProvider('jsonValuesDataProvider')] |
| 27 | + #[RequiresDatabase(['sqlite', 'mysql', 'mariadb'])] |
| 28 | + public function testBasicUpdateForJson($column, $given, $expected) |
| 29 | + { |
| 30 | + DB::table('example')->insert([ |
| 31 | + 'name' => 'Taylor Otwell', |
| 32 | + 'title' => 'Mr.', |
| 33 | + ]); |
| 34 | + |
| 35 | + DB::table('example')->update([$column => $given]); |
| 36 | + |
| 37 | + $this->assertDatabaseHas('example', [ |
| 38 | + 'name' => 'Taylor Otwell', |
| 39 | + 'title' => 'Mr.', |
| 40 | + $column => $column === 'payload' ? $this->castAsJson($expected) : $expected, |
| 41 | + ]); |
| 42 | + } |
| 43 | + |
| 44 | + public static function jsonValuesDataProvider() |
| 45 | + { |
| 46 | + yield ['payload', ['Laravel', 'Founder'], ['Laravel', 'Founder']]; |
| 47 | + yield ['payload', collect(['Laravel', 'Founder']), ['Laravel', 'Founder']]; |
| 48 | + yield ['status', StringStatus::draft, 'draft']; |
| 49 | + } |
| 50 | +} |
0 commit comments