PageRenderTime 57ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/laravel/framework/tests/Database/DatabaseMigratorTest.php

https://bitbucket.org/vervcreations/projectis.at
PHP | 200 lines | 162 code | 38 blank | 0 comment | 0 complexity | cb50cefc26aaabff174474e8bb9efd02 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. use Mockery as m;
  3. class DatabaseMigratorTest extends PHPUnit_Framework_TestCase {
  4. public function tearDown()
  5. {
  6. m::close();
  7. }
  8. public function testMigrationAreRunUpWhenOutstandingMigrationsExist()
  9. {
  10. $migrator = $this->getMock('Illuminate\Database\Migrations\Migrator', array('resolve'), array(
  11. m::mock('Illuminate\Database\Migrations\MigrationRepositoryInterface'),
  12. $resolver = m::mock('Illuminate\Database\ConnectionResolverInterface'),
  13. m::mock('Illuminate\Filesystem\Filesystem'),
  14. ));
  15. $migrator->getFilesystem()->shouldReceive('glob')->once()->with(__DIR__.'/*_*.php')->andReturn(array(
  16. __DIR__.'/2_bar.php',
  17. __DIR__.'/1_foo.php',
  18. __DIR__.'/3_baz.php',
  19. ));
  20. $migrator->getFilesystem()->shouldReceive('requireOnce')->with(__DIR__.'/2_bar.php');
  21. $migrator->getFilesystem()->shouldReceive('requireOnce')->with(__DIR__.'/1_foo.php');
  22. $migrator->getFilesystem()->shouldReceive('requireOnce')->with(__DIR__.'/3_baz.php');
  23. $migrator->getRepository()->shouldReceive('getRan')->once()->andReturn(array(
  24. '1_foo',
  25. ));
  26. $migrator->getRepository()->shouldReceive('getNextBatchNumber')->once()->andReturn(1);
  27. $migrator->getRepository()->shouldReceive('log')->once()->with('2_bar', 1);
  28. $migrator->getRepository()->shouldReceive('log')->once()->with('3_baz', 1);
  29. $barMock = m::mock('stdClass');
  30. $barMock->shouldReceive('up')->once();
  31. $bazMock = m::mock('stdClass');
  32. $bazMock->shouldReceive('up')->once();
  33. $migrator->expects($this->at(0))->method('resolve')->with($this->equalTo('2_bar'))->will($this->returnValue($barMock));
  34. $migrator->expects($this->at(1))->method('resolve')->with($this->equalTo('3_baz'))->will($this->returnValue($bazMock));
  35. $migrator->run(__DIR__);
  36. }
  37. public function testUpMigrationCanBePretended()
  38. {
  39. $migrator = $this->getMock('Illuminate\Database\Migrations\Migrator', array('resolve'), array(
  40. m::mock('Illuminate\Database\Migrations\MigrationRepositoryInterface'),
  41. $resolver = m::mock('Illuminate\Database\ConnectionResolverInterface'),
  42. m::mock('Illuminate\Filesystem\Filesystem'),
  43. ));
  44. $migrator->getFilesystem()->shouldReceive('glob')->once()->with(__DIR__.'/*_*.php')->andReturn(array(
  45. __DIR__.'/2_bar.php',
  46. __DIR__.'/1_foo.php',
  47. __DIR__.'/3_baz.php',
  48. ));
  49. $migrator->getFilesystem()->shouldReceive('requireOnce')->with(__DIR__.'/2_bar.php');
  50. $migrator->getFilesystem()->shouldReceive('requireOnce')->with(__DIR__.'/1_foo.php');
  51. $migrator->getFilesystem()->shouldReceive('requireOnce')->with(__DIR__.'/3_baz.php');
  52. $migrator->getRepository()->shouldReceive('getRan')->once()->andReturn(array(
  53. '1_foo',
  54. ));
  55. $migrator->getRepository()->shouldReceive('getNextBatchNumber')->once()->andReturn(1);
  56. $barMock = m::mock('stdClass');
  57. $barMock->shouldReceive('getConnection')->once()->andReturn(null);
  58. $barMock->shouldReceive('up')->once();
  59. $bazMock = m::mock('stdClass');
  60. $bazMock->shouldReceive('getConnection')->once()->andReturn(null);
  61. $bazMock->shouldReceive('up')->once();
  62. $migrator->expects($this->at(0))->method('resolve')->with($this->equalTo('2_bar'))->will($this->returnValue($barMock));
  63. $migrator->expects($this->at(1))->method('resolve')->with($this->equalTo('3_baz'))->will($this->returnValue($bazMock));
  64. $connection = m::mock('stdClass');
  65. $connection->shouldReceive('pretend')->with(m::type('Closure'))->andReturnUsing(function($closure)
  66. {
  67. $closure();
  68. return array(array('query' => 'foo'));
  69. },
  70. function($closure)
  71. {
  72. $closure();
  73. return array(array('query' => 'bar'));
  74. });
  75. $resolver->shouldReceive('connection')->with(null)->andReturn($connection);
  76. $migrator->run(__DIR__, true);
  77. }
  78. public function testNothingIsDoneWhenNoMigrationsAreOutstanding()
  79. {
  80. $migrator = $this->getMock('Illuminate\Database\Migrations\Migrator', array('resolve'), array(
  81. m::mock('Illuminate\Database\Migrations\MigrationRepositoryInterface'),
  82. $resolver = m::mock('Illuminate\Database\ConnectionResolverInterface'),
  83. m::mock('Illuminate\Filesystem\Filesystem'),
  84. ));
  85. $migrator->getFilesystem()->shouldReceive('glob')->once()->with(__DIR__.'/*_*.php')->andReturn(array(
  86. __DIR__.'/1_foo.php',
  87. ));
  88. $migrator->getFilesystem()->shouldReceive('requireOnce')->with(__DIR__.'/1_foo.php');
  89. $migrator->getRepository()->shouldReceive('getRan')->once()->andReturn(array(
  90. '1_foo',
  91. ));
  92. $migrator->run(__DIR__);
  93. }
  94. public function testLastBatchOfMigrationsCanBeRolledBack()
  95. {
  96. $migrator = $this->getMock('Illuminate\Database\Migrations\Migrator', array('resolve'), array(
  97. m::mock('Illuminate\Database\Migrations\MigrationRepositoryInterface'),
  98. $resolver = m::mock('Illuminate\Database\ConnectionResolverInterface'),
  99. m::mock('Illuminate\Filesystem\Filesystem'),
  100. ));
  101. $migrator->getRepository()->shouldReceive('getLast')->once()->andReturn(array(
  102. $fooMigration = new MigratorTestMigrationStub('foo'),
  103. $barMigration = new MigratorTestMigrationStub('bar'),
  104. ));
  105. $barMock = m::mock('stdClass');
  106. $barMock->shouldReceive('down')->once();
  107. $fooMock = m::mock('stdClass');
  108. $fooMock->shouldReceive('down')->once();
  109. $migrator->expects($this->at(0))->method('resolve')->with($this->equalTo('foo'))->will($this->returnValue($barMock));
  110. $migrator->expects($this->at(1))->method('resolve')->with($this->equalTo('bar'))->will($this->returnValue($fooMock));
  111. $migrator->getRepository()->shouldReceive('delete')->once()->with($barMigration);
  112. $migrator->getRepository()->shouldReceive('delete')->once()->with($fooMigration);
  113. $migrator->rollback();
  114. }
  115. public function testRollbackMigrationsCanBePretended()
  116. {
  117. $migrator = $this->getMock('Illuminate\Database\Migrations\Migrator', array('resolve'), array(
  118. m::mock('Illuminate\Database\Migrations\MigrationRepositoryInterface'),
  119. $resolver = m::mock('Illuminate\Database\ConnectionResolverInterface'),
  120. m::mock('Illuminate\Filesystem\Filesystem'),
  121. ));
  122. $migrator->getRepository()->shouldReceive('getLast')->once()->andReturn(array(
  123. $fooMigration = new MigratorTestMigrationStub('foo'),
  124. $barMigration = new MigratorTestMigrationStub('bar'),
  125. ));
  126. $barMock = m::mock('stdClass');
  127. $barMock->shouldReceive('getConnection')->once()->andReturn(null);
  128. $barMock->shouldReceive('down')->once();
  129. $fooMock = m::mock('stdClass');
  130. $fooMock->shouldReceive('getConnection')->once()->andReturn(null);
  131. $fooMock->shouldReceive('down')->once();
  132. $migrator->expects($this->at(0))->method('resolve')->with($this->equalTo('foo'))->will($this->returnValue($barMock));
  133. $migrator->expects($this->at(1))->method('resolve')->with($this->equalTo('bar'))->will($this->returnValue($fooMock));
  134. $connection = m::mock('stdClass');
  135. $connection->shouldReceive('pretend')->with(m::type('Closure'))->andReturnUsing(function($closure)
  136. {
  137. $closure();
  138. return array(array('query' => 'bar'));
  139. },
  140. function($closure)
  141. {
  142. $closure();
  143. return array(array('query' => 'foo'));
  144. });
  145. $resolver->shouldReceive('connection')->with(null)->andReturn($connection);
  146. $migrator->rollback(true);
  147. }
  148. public function testNothingIsRolledBackWhenNothingInRepository()
  149. {
  150. $migrator = $this->getMock('Illuminate\Database\Migrations\Migrator', array('resolve'), array(
  151. m::mock('Illuminate\Database\Migrations\MigrationRepositoryInterface'),
  152. $resolver = m::mock('Illuminate\Database\ConnectionResolverInterface'),
  153. m::mock('Illuminate\Filesystem\Filesystem'),
  154. ));
  155. $migrator->getRepository()->shouldReceive('getLast')->once()->andReturn(array());
  156. $migrator->rollback();
  157. }
  158. }
  159. class MigratorTestMigrationStub {
  160. public function __construct($migration) { $this->migration = $migration; }
  161. public $migration;
  162. }