1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
<?php
namespace Illuminate\Tests\Database;
use Illuminate\Console\OutputStyle;
use Illuminate\Console\View\Components\Factory;
use Illuminate\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\ConnectionResolverInterface;
use Illuminate\Database\Console\Seeds\SeedCommand;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Seeder;
use Illuminate\Events\NullDispatcher;
use Illuminate\Testing\Assert;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
class SeedCommandTest extends TestCase
{
public function testHandle()
{
$input = new ArrayInput(['--force' => true, '--database' => 'sqlite']);
$output = new NullOutput;
$outputStyle = new OutputStyle($input, $output);
$seeder = m::mock(Seeder::class);
$seeder->shouldReceive('setContainer')->once()->andReturnSelf();
$seeder->shouldReceive('setCommand')->once()->andReturnSelf();
$seeder->shouldReceive('__invoke')->once();
$resolver = m::mock(ConnectionResolverInterface::class);
$resolver->shouldReceive('getDefaultConnection')->once();
$resolver->shouldReceive('setDefaultConnection')->once()->with('sqlite');
$container = m::mock(Container::class);
$container->shouldReceive('call');
$container->shouldReceive('environment')->once()->andReturn('testing');
$container->shouldReceive('runningUnitTests')->andReturn('true');
$container->shouldReceive('make')->with('DatabaseSeeder')->andReturn($seeder);
$container->shouldReceive('make')->with(OutputStyle::class, m::any())->andReturn(
$outputStyle
);
$container->shouldReceive('make')->with(Factory::class, m::any())->andReturn(
new Factory($outputStyle)
);
$command = new SeedCommand($resolver);
$command->setLaravel($container);
// call run to set up IO, then fire manually.
$command->run($input, $output);
$command->handle();
$container->shouldHaveReceived('call')->with([$command, 'handle']);
}
public function testWithoutModelEvents()
{
$input = new ArrayInput([
'--force' => true,
'--database' => 'sqlite',
'--class' => UserWithoutModelEventsSeeder::class,
]);
$output = new NullOutput;
$outputStyle = new OutputStyle($input, $output);
$instance = new UserWithoutModelEventsSeeder();
$seeder = m::mock($instance);
$seeder->shouldReceive('setContainer')->once()->andReturnSelf();
$seeder->shouldReceive('setCommand')->once()->andReturnSelf();
$resolver = m::mock(ConnectionResolverInterface::class);
$resolver->shouldReceive('getDefaultConnection')->once();
$resolver->shouldReceive('setDefaultConnection')->once()->with('sqlite');
$container = m::mock(Container::class);
$container->shouldReceive('call');
$container->shouldReceive('environment')->once()->andReturn('testing');
$container->shouldReceive('runningUnitTests')->andReturn('true');
$container->shouldReceive('make')->with(UserWithoutModelEventsSeeder::class)->andReturn($seeder);
$container->shouldReceive('make')->with(OutputStyle::class, m::any())->andReturn(
$outputStyle
);
$container->shouldReceive('make')->with(Factory::class, m::any())->andReturn(
new Factory($outputStyle)
);
$command = new SeedCommand($resolver);
$command->setLaravel($container);
Model::setEventDispatcher($dispatcher = m::mock(Dispatcher::class));
// call run to set up IO, then fire manually.
$command->run($input, $output);
$command->handle();
Assert::assertSame($dispatcher, Model::getEventDispatcher());
$container->shouldHaveReceived('call')->with([$command, 'handle']);
}
protected function tearDown(): void
{
Model::unsetEventDispatcher();
m::close();
}
}
class UserWithoutModelEventsSeeder extends Seeder
{
use WithoutModelEvents;
public function run()
{
Assert::assertInstanceOf(NullDispatcher::class, Model::getEventDispatcher());
}
}
|