PageRenderTime 233ms CodeModel.GetById 60ms app.highlight 111ms RepoModel.GetById 51ms app.codeStats 1ms

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