|
4 | 4 |
|
5 | 5 | use Illuminate\Console\OutputStyle;
|
6 | 6 | use Illuminate\Container\Container;
|
| 7 | +use Illuminate\Contracts\Events\Dispatcher; |
7 | 8 | use Illuminate\Database\ConnectionResolverInterface;
|
8 | 9 | use Illuminate\Database\Console\Seeds\SeedCommand;
|
| 10 | +use Illuminate\Database\Console\Seeds\WithoutModelEvents; |
| 11 | +use Illuminate\Database\Eloquent\Model; |
9 | 12 | use Illuminate\Database\Seeder;
|
| 13 | +use Illuminate\Events\NullDispatcher; |
| 14 | +use Illuminate\Testing\Assert; |
10 | 15 | use Mockery as m;
|
11 | 16 | use PHPUnit\Framework\TestCase;
|
12 | 17 | use Symfony\Component\Console\Input\ArrayInput;
|
@@ -46,8 +51,61 @@ public function testHandle()
|
46 | 51 | $container->shouldHaveReceived('call')->with([$command, 'handle']);
|
47 | 52 | }
|
48 | 53 |
|
| 54 | + public function testWithoutModelEvents() |
| 55 | + { |
| 56 | + $input = new ArrayInput([ |
| 57 | + '--force' => true, |
| 58 | + '--database' => 'sqlite', |
| 59 | + '--class' => UserWithoutModelEventsSeeder::class, |
| 60 | + ]); |
| 61 | + $output = new NullOutput; |
| 62 | + |
| 63 | + $instance = new UserWithoutModelEventsSeeder(); |
| 64 | + |
| 65 | + $seeder = m::mock($instance); |
| 66 | + $seeder->shouldReceive('setContainer')->once()->andReturnSelf(); |
| 67 | + $seeder->shouldReceive('setCommand')->once()->andReturnSelf(); |
| 68 | + |
| 69 | + $resolver = m::mock(ConnectionResolverInterface::class); |
| 70 | + $resolver->shouldReceive('getDefaultConnection')->once(); |
| 71 | + $resolver->shouldReceive('setDefaultConnection')->once()->with('sqlite'); |
| 72 | + |
| 73 | + $container = m::mock(Container::class); |
| 74 | + $container->shouldReceive('call'); |
| 75 | + $container->shouldReceive('environment')->once()->andReturn('testing'); |
| 76 | + $container->shouldReceive('make')->with(UserWithoutModelEventsSeeder::class)->andReturn($seeder); |
| 77 | + $container->shouldReceive('make')->with(OutputStyle::class, m::any())->andReturn( |
| 78 | + new OutputStyle($input, $output) |
| 79 | + ); |
| 80 | + |
| 81 | + $command = new SeedCommand($resolver); |
| 82 | + $command->setLaravel($container); |
| 83 | + |
| 84 | + Model::setEventDispatcher($dispatcher = m::mock(Dispatcher::class)); |
| 85 | + |
| 86 | + // call run to set up IO, then fire manually. |
| 87 | + $command->run($input, $output); |
| 88 | + $command->handle(); |
| 89 | + |
| 90 | + Assert::assertSame($dispatcher, Model::getEventDispatcher()); |
| 91 | + |
| 92 | + $container->shouldHaveReceived('call')->with([$command, 'handle']); |
| 93 | + } |
| 94 | + |
49 | 95 | protected function tearDown(): void
|
50 | 96 | {
|
| 97 | + Model::unsetEventDispatcher(); |
| 98 | + |
51 | 99 | m::close();
|
52 | 100 | }
|
53 | 101 | }
|
| 102 | + |
| 103 | +class UserWithoutModelEventsSeeder extends Seeder |
| 104 | +{ |
| 105 | + use WithoutModelEvents; |
| 106 | + |
| 107 | + public function run() |
| 108 | + { |
| 109 | + Assert::assertInstanceOf(NullDispatcher::class, Model::getEventDispatcher()); |
| 110 | + } |
| 111 | +} |
0 commit comments