Skip to content

Commit

Permalink
Allow dependency injection on seeders run method.
Browse files Browse the repository at this point in the history
  • Loading branch information
guiwoda committed Oct 17, 2016
1 parent 48820f4 commit a8d7bde
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Console/Seeds/SeedCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function fire()
$this->resolver->setDefaultConnection($this->getDatabase());

Model::unguarded(function () {
$this->getSeeder()->run();
$this->getSeeder()->__invoke();
});
}

Expand Down
29 changes: 21 additions & 8 deletions src/Illuminate/Database/Seeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ abstract class Seeder
*/
protected $command;

/**
* Run the database seeds.
*
* @return void
*/
abstract public function run();

/**
* Seed the given connection from the given path.
*
Expand All @@ -36,7 +29,7 @@ abstract public function run();
*/
public function call($class)
{
$this->resolve($class)->run();
$this->resolve($class)->__invoke();

if (isset($this->command)) {
$this->command->getOutput()->writeln("<info>Seeded:</info> $class");
Expand Down Expand Up @@ -91,4 +84,24 @@ public function setCommand(Command $command)

return $this;
}

/**
* Run the database seeds.
*
* @return void
*
* @throws \InvalidArgumentException
*/
public function __invoke()
{
if (method_exists($this, 'run')) {
if (isset($this->container)) {
return $this->container->call([$this, 'run']);
}

return $this->run();
}

throw new \InvalidArgumentException("Method [run] missing from " . get_class($this));
}
}
25 changes: 23 additions & 2 deletions tests/Database/DatabaseSeederTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ public function run()
}
}

class TestDepsSeeder extends Seeder
{
public function run(Mockery\Mock $someDependency)
{
//
}
}

class DatabaseSeederTest extends PHPUnit_Framework_TestCase
{
public function tearDown()
Expand All @@ -27,10 +35,10 @@ public function testCallResolveTheClassAndCallsRun()
$command = m::mock('Illuminate\Console\Command');
$command->shouldReceive('getOutput')->once()->andReturn($output);
$seeder->setCommand($command);
$container->shouldReceive('make')->once()->with('ClassName')->andReturn($child = m::mock('StdClass'));
$container->shouldReceive('make')->once()->with('ClassName')->andReturn($child = m::mock(Seeder::class));
$child->shouldReceive('setContainer')->once()->with($container)->andReturn($child);
$child->shouldReceive('setCommand')->once()->with($command)->andReturn($child);
$child->shouldReceive('run')->once();
$child->shouldReceive('__invoke')->once();

$seeder->call('ClassName');
}
Expand All @@ -48,4 +56,17 @@ public function testSetCommand()
$command = m::mock('Illuminate\Console\Command');
$this->assertEquals($seeder->setCommand($command), $seeder);
}

public function testInjectDependenciesOnRunMethod()
{
$container = m::mock('Illuminate\Container\Container');
$container->shouldReceive('call');

$seeder = new TestDepsSeeder;
$seeder->setContainer($container);

$seeder->__invoke();

$container->shouldHaveReceived('call')->once()->with([$seeder, 'run']);
}
}
39 changes: 39 additions & 0 deletions tests/Database/SeedCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use Illuminate\Container\Container;
use Illuminate\Database\ConnectionResolverInterface;
use Illuminate\Database\Console\Seeds\SeedCommand;
use Illuminate\Database\Seeder;

class SeedCommandTest extends PHPUnit_Framework_TestCase
{
public function testFire()
{
$seeder = Mockery::mock(Seeder::class);
$seeder->shouldReceive('setContainer')->once()->andReturnSelf();
$seeder->shouldReceive('setCommand')->once()->andReturnSelf();
$seeder->shouldReceive('__invoke')->once();

$resolver = Mockery::mock(ConnectionResolverInterface::class);
$resolver->shouldReceive('setDefaultConnection')->once()->with('sqlite');

$container = Mockery::mock(Container::class);
$container->shouldReceive('call');
$container->shouldReceive('environment')->once()->andReturn('testing');
$container->shouldReceive('make')->with('DatabaseSeeder')->andReturn($seeder);

$command = new SeedCommand($resolver);
$command->setLaravel($container);

// call run to set up IO, then fire manually.
$command->run(new Symfony\Component\Console\Input\ArrayInput(['--force' => true, '--database' => 'sqlite']), new Symfony\Component\Console\Output\NullOutput);
$command->fire();

$container->shouldHaveReceived('call')->with([$command, 'fire']);
}

protected function tearDown()
{
Mockery::close();
}
}

0 comments on commit a8d7bde

Please sign in to comment.