|
2 | 2 |
|
3 | 3 | namespace Illuminate\Tests\Integration\Migration;
|
4 | 4 |
|
| 5 | +use Illuminate\Database\Schema\Blueprint; |
5 | 6 | use Illuminate\Support\Facades\DB;
|
| 7 | +use Illuminate\Support\Facades\Schema; |
6 | 8 | use Mockery as m;
|
7 | 9 | use Orchestra\Testbench\TestCase;
|
8 | 10 | use Symfony\Component\Console\Output\OutputInterface;
|
@@ -98,6 +100,137 @@ public function testPretendMigrate()
|
98 | 100 | $this->assertFalse(DB::getSchemaBuilder()->hasTable('people'));
|
99 | 101 | }
|
100 | 102 |
|
| 103 | + public function testIgnorePretendModeForCallbackData() |
| 104 | + { |
| 105 | + // Create two tables with different columns so that we can query it later |
| 106 | + // with the new method DB::withoutPretending(). |
| 107 | + |
| 108 | + Schema::create('table_1', function (Blueprint $table) { |
| 109 | + $table->increments('id'); |
| 110 | + $table->string('column_1'); |
| 111 | + }); |
| 112 | + |
| 113 | + Schema::create('table_2', function (Blueprint $table) { |
| 114 | + $table->increments('id'); |
| 115 | + $table->string('column_2')->default('default_value'); |
| 116 | + }); |
| 117 | + |
| 118 | + // From here on we simulate to be in pretend mode. This normally is done by |
| 119 | + // running the migration with the option --pretend. |
| 120 | + |
| 121 | + DB::pretend(function () { |
| 122 | + // Returns an empty array because we are in pretend mode. |
| 123 | + $tablesEmpty = DB::select("SELECT name FROM sqlite_master WHERE type='table'"); |
| 124 | + |
| 125 | + $this->assertTrue([] === $tablesEmpty); |
| 126 | + |
| 127 | + // Returns an array with two tables because we ignore pretend mode. |
| 128 | + $tablesList = DB::withoutPretending(function (): array { |
| 129 | + return DB::select("SELECT name FROM sqlite_master WHERE type='table'"); |
| 130 | + }); |
| 131 | + |
| 132 | + $this->assertTrue([] !== $tablesList); |
| 133 | + |
| 134 | + // The following would not be possible in pretend mode, if the |
| 135 | + // method DB::withoutPretending() would not exists, |
| 136 | + // because nothing is executed in pretend mode. |
| 137 | + foreach ($tablesList as $table) { |
| 138 | + if (in_array($table->name, ['sqlite_sequence', 'migrations'])) { |
| 139 | + continue; |
| 140 | + } |
| 141 | + |
| 142 | + $columnsEmpty = DB::select("PRAGMA table_info($table->name)"); |
| 143 | + |
| 144 | + $this->assertTrue([] === $columnsEmpty); |
| 145 | + |
| 146 | + $columnsList = DB::withoutPretending(function () use ($table): array { |
| 147 | + return DB::select("PRAGMA table_info($table->name)"); |
| 148 | + }); |
| 149 | + |
| 150 | + $this->assertTrue([] !== $columnsList); |
| 151 | + $this->assertCount(2, $columnsList); |
| 152 | + |
| 153 | + // Confirm that we are still in pretend mode. This column should |
| 154 | + // not be added. We query the table columns again to ensure the |
| 155 | + // count is still two. |
| 156 | + DB::statement("ALTER TABLE $table->name ADD COLUMN column_3 varchar(255) DEFAULT 'default_value' NOT NULL"); |
| 157 | + |
| 158 | + $columnsList = DB::withoutPretending(function () use ($table): array { |
| 159 | + return DB::select("PRAGMA table_info($table->name)"); |
| 160 | + }); |
| 161 | + |
| 162 | + $this->assertCount(2, $columnsList); |
| 163 | + } |
| 164 | + }); |
| 165 | + |
| 166 | + Schema::dropIfExists('table_1'); |
| 167 | + Schema::dropIfExists('table_2'); |
| 168 | + } |
| 169 | + |
| 170 | + public function testIgnorePretendModeForCallbackOutputDynamicContentIsShown() |
| 171 | + { |
| 172 | + // Persist data to table we can work with. |
| 173 | + $this->expectInfo('Running migrations.'); |
| 174 | + $this->expectTask('2014_10_12_000000_create_people_is_dynamic_table', 'DONE'); |
| 175 | + |
| 176 | + $this->output->shouldReceive('writeln')->once(); |
| 177 | + |
| 178 | + $this->subject->run([__DIR__.'/pretending/2014_10_12_000000_create_people_is_dynamic_table.php'], ['pretend' => false]); |
| 179 | + |
| 180 | + $this->assertTrue(DB::getSchemaBuilder()->hasTable('people')); |
| 181 | + |
| 182 | + // Test the actual functionality. |
| 183 | + $this->expectInfo('Running migrations.'); |
| 184 | + $this->expectTwoColumnDetail('DynamicContentIsShown'); |
| 185 | + $this->expectBulletList([ |
| 186 | + 'create table "blogs" ("id" integer primary key autoincrement not null, "url" varchar, "name" varchar)', |
| 187 | + 'insert into "blogs" ("url") values (\'www.janedoe.com\'), (\'www.johndoe.com\')', |
| 188 | + 'ALTER TABLE \'pseudo_table_name\' MODIFY \'column_name\' VARCHAR(191)', |
| 189 | + 'select * from "people"', |
| 190 | + 'insert into "blogs" ("id", "name") values (1, \'Jane Doe Blog\')', |
| 191 | + 'insert into "blogs" ("id", "name") values (2, \'John Doe Blog\')', |
| 192 | + ]); |
| 193 | + |
| 194 | + $this->output->shouldReceive('writeln')->once(); |
| 195 | + |
| 196 | + $this->subject->run([__DIR__.'/pretending/2023_10_17_000000_dynamic_content_is_shown.php'], ['pretend' => true]); |
| 197 | + |
| 198 | + $this->assertFalse(DB::getSchemaBuilder()->hasTable('blogs')); |
| 199 | + |
| 200 | + Schema::dropIfExists('people'); |
| 201 | + } |
| 202 | + |
| 203 | + public function testIgnorePretendModeForCallbackOutputDynamicContentNotShown() |
| 204 | + { |
| 205 | + // Persist data to table we can work with. |
| 206 | + $this->expectInfo('Running migrations.'); |
| 207 | + $this->expectTask('2014_10_12_000000_create_people_non_dynamic_table', 'DONE'); |
| 208 | + |
| 209 | + $this->output->shouldReceive('writeln')->once(); |
| 210 | + |
| 211 | + $this->subject->run([__DIR__.'/pretending/2014_10_12_000000_create_people_non_dynamic_table.php'], ['pretend' => false]); |
| 212 | + |
| 213 | + $this->assertTrue(DB::getSchemaBuilder()->hasTable('people')); |
| 214 | + |
| 215 | + // Test the actual functionality. |
| 216 | + $this->expectInfo('Running migrations.'); |
| 217 | + $this->expectTwoColumnDetail('DynamicContentNotShown'); |
| 218 | + $this->expectBulletList([ |
| 219 | + 'create table "blogs" ("id" integer primary key autoincrement not null, "url" varchar, "name" varchar)', |
| 220 | + 'insert into "blogs" ("url") values (\'www.janedoe.com\'), (\'www.johndoe.com\')', |
| 221 | + 'ALTER TABLE \'pseudo_table_name\' MODIFY \'column_name\' VARCHAR(191)', |
| 222 | + 'select * from "people"', |
| 223 | + ]); |
| 224 | + |
| 225 | + $this->output->shouldReceive('writeln')->once(); |
| 226 | + |
| 227 | + $this->subject->run([__DIR__.'/pretending/2023_10_17_000000_dynamic_content_not_shown.php'], ['pretend' => true]); |
| 228 | + |
| 229 | + $this->assertFalse(DB::getSchemaBuilder()->hasTable('blogs')); |
| 230 | + |
| 231 | + Schema::dropIfExists('people'); |
| 232 | + } |
| 233 | + |
101 | 234 | protected function expectInfo($message): void
|
102 | 235 | {
|
103 | 236 | $this->output->shouldReceive('writeln')->once()->with(m::on(
|
|
0 commit comments