Skip to content

[9.x] Adds WithoutModelEvents trait for running seeds without Model Events #39922

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Illuminate/Database/Console/Seeds/WithoutModelEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Illuminate\Database\Console\Seeds;

use Illuminate\Database\Eloquent\Model;

trait WithoutModelEvents
{
/**
* Prevent model events from being dispatched by the given callback.
*
* @param callable $callback
* @return callable
*/
public function withoutModelEvents(callable $callback)
{
return fn () => Model::withoutEvents($callback);
}
}
15 changes: 12 additions & 3 deletions src/Illuminate/Database/Seeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Console\Command;
use Illuminate\Container\Container;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Support\Arr;
use InvalidArgumentException;

Expand Down Expand Up @@ -170,8 +171,16 @@ public function __invoke(array $parameters = [])
throw new InvalidArgumentException('Method [run] missing from '.get_class($this));
}

return isset($this->container)
? $this->container->call([$this, 'run'], $parameters)
: $this->run(...$parameters);
$callback = fn () => isset($this->container)
? $this->container->call([$this, 'run'], $parameters)
: $this->run(...$parameters);

$uses = array_flip(class_uses_recursive(static::class));

if (isset($uses[WithoutModelEvents::class])) {
$callback = $this->withoutModelEvents($callback);
}

return $callback();
}
}
58 changes: 58 additions & 0 deletions tests/Database/SeedCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@

use Illuminate\Console\OutputStyle;
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;
Expand Down Expand Up @@ -46,8 +51,61 @@ public function testHandle()
$container->shouldHaveReceived('call')->with([$command, 'handle']);
}

public function testWithoutModelEvents()
{
$input = new ArrayInput([
'--force' => true,
'--database' => 'sqlite',
'--class' => UserWithoutModelEventsSeeder::class,
]);
$output = new NullOutput;

$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('make')->with(UserWithoutModelEventsSeeder::class)->andReturn($seeder);
$container->shouldReceive('make')->with(OutputStyle::class, m::any())->andReturn(
new OutputStyle($input, $output)
);

$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());
}
}