MigratorTest.php 3.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace Illuminate\Tests\Integration\Migration;
  3. use Illuminate\Support\Facades\DB;
  4. use Mockery;
  5. use Mockery\Mock;
  6. use Orchestra\Testbench\TestCase;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. class MigratorTest extends TestCase
  9. {
  10. /**
  11. * @var Mock
  12. */
  13. private $output;
  14. protected function setUp(): void
  15. {
  16. parent::setUp();
  17. $this->output = Mockery::mock(OutputInterface::class);
  18. $this->subject = $this->app->make('migrator');
  19. $this->subject->setOutput($this->output);
  20. $this->subject->getRepository()->createRepository();
  21. }
  22. public function testMigrate()
  23. {
  24. $this->expectOutput('<comment>Migrating:</comment> 2014_10_12_000000_create_people_table');
  25. $this->expectOutput(Mockery::pattern('#<info>Migrated:</info> 2014_10_12_000000_create_people_table (.*)#'));
  26. $this->expectOutput('<comment>Migrating:</comment> 2015_10_04_000000_modify_people_table');
  27. $this->expectOutput(Mockery::pattern('#<info>Migrated:</info> 2015_10_04_000000_modify_people_table (.*)#'));
  28. $this->expectOutput('<comment>Migrating:</comment> 2016_10_04_000000_modify_people_table');
  29. $this->expectOutput(Mockery::pattern('#<info>Migrated:</info> 2016_10_04_000000_modify_people_table (.*)#'));
  30. $this->subject->run([__DIR__.'/fixtures']);
  31. self::assertTrue(DB::getSchemaBuilder()->hasTable('people'));
  32. self::assertTrue(DB::getSchemaBuilder()->hasColumn('people', 'first_name'));
  33. self::assertTrue(DB::getSchemaBuilder()->hasColumn('people', 'last_name'));
  34. }
  35. public function testRollback()
  36. {
  37. $this->getConnection()->statement('CREATE TABLE people(id INT, first_name VARCHAR, last_name VARCHAR);');
  38. $this->subject->getRepository()->log('2014_10_12_000000_create_people_table', 1);
  39. $this->subject->getRepository()->log('2015_10_04_000000_modify_people_table', 1);
  40. $this->subject->getRepository()->log('2016_10_04_000000_modify_people_table', 1);
  41. $this->expectOutput('<comment>Rolling back:</comment> 2016_10_04_000000_modify_people_table');
  42. $this->expectOutput(Mockery::pattern('#<info>Rolled back:</info> 2016_10_04_000000_modify_people_table (.*)#'));
  43. $this->expectOutput('<comment>Rolling back:</comment> 2015_10_04_000000_modify_people_table');
  44. $this->expectOutput(Mockery::pattern('#<info>Rolled back:</info> 2015_10_04_000000_modify_people_table (.*)#'));
  45. $this->expectOutput('<comment>Rolling back:</comment> 2014_10_12_000000_create_people_table');
  46. $this->expectOutput(Mockery::pattern('#<info>Rolled back:</info> 2014_10_12_000000_create_people_table (.*)#'));
  47. $this->subject->rollback([__DIR__.'/fixtures']);
  48. self::assertFalse(DB::getSchemaBuilder()->hasTable('people'));
  49. }
  50. public function testPretendMigrate()
  51. {
  52. $this->expectOutput('<info>CreatePeopleTable:</info> create table "people" ("id" integer not null primary key autoincrement, "name" varchar not null, "email" varchar not null, "password" varchar not null, "remember_token" varchar, "created_at" datetime, "updated_at" datetime)');
  53. $this->expectOutput('<info>CreatePeopleTable:</info> create unique index "people_email_unique" on "people" ("email")');
  54. $this->expectOutput('<info>ModifyPeopleTable:</info> alter table "people" add column "first_name" varchar');
  55. $this->expectOutput('<info>2016_10_04_000000_modify_people_table:</info> alter table "people" add column "last_name" varchar');
  56. $this->subject->run([__DIR__.'/fixtures'], ['pretend' => true]);
  57. self::assertFalse(DB::getSchemaBuilder()->hasTable('people'));
  58. }
  59. private function expectOutput($argument): void
  60. {
  61. $this->output->shouldReceive('writeln')->once()->with($argument);
  62. }
  63. }