Skip to content

Commit

Permalink
[9.x] Fixes output when running db:seed or using --seed in migrat…
Browse files Browse the repository at this point in the history
…e commands (#43593)

* Fixes output on seeds

* Apply fixes from StyleCI

* Fixes tests

Co-authored-by: StyleCI Bot <bot@styleci.io>
  • Loading branch information
nunomaduro and StyleCIBot committed Aug 8, 2022
1 parent 93090dc commit c717a1d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
29 changes: 19 additions & 10 deletions src/Illuminate/Database/Seeder.php
Expand Up @@ -3,7 +3,7 @@
namespace Illuminate\Database;

use Illuminate\Console\Command;
use Illuminate\Console\View\Components\Task;
use Illuminate\Console\View\Components\TwoColumnDetail;
use Illuminate\Container\Container;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Support\Arr;
Expand Down Expand Up @@ -49,20 +49,29 @@ public function call($class, $silent = false, array $parameters = [])

$name = get_class($seeder);

if ($silent || ! isset($this->command)) {
$seeder->__invoke($parameters);
} else {
with(new Task($this->command->getOutput()))->render(
if ($silent === false && isset($this->command)) {
with(new TwoColumnDetail($this->command->getOutput()))->render(
$name,
fn () => $seeder->__invoke($parameters),
'<fg=yellow;options=bold>RUNNING</>'
);
}

static::$called[] = $class;
}
$startTime = microtime(true);

$seeder->__invoke($parameters);

if ($silent === false && isset($this->command)) {
$runTime = number_format((microtime(true) - $startTime) * 1000, 2);

with(new TwoColumnDetail($this->command->getOutput()))->render(
$name,
"<fg=gray>$runTime ms</> <fg=green;options=bold>DONE</>"
);

if (! $silent && $this->command->getOutput()) {
$this->command->getOutput()->writeln('');
$this->command->getOutput()->writeln('');
}

static::$called[] = $class;
}

return $this;
Expand Down
3 changes: 1 addition & 2 deletions tests/Database/DatabaseSeederTest.php
Expand Up @@ -38,15 +38,14 @@ public function testCallResolveTheClassAndCallsRun()
$seeder = new TestSeeder;
$seeder->setContainer($container = m::mock(Container::class));
$output = m::mock(OutputInterface::class);
$output->shouldReceive('writeln')->twice();
$output->shouldReceive('writeln')->times(3);
$command = m::mock(Command::class);
$command->shouldReceive('getOutput')->times(3)->andReturn($output);
$seeder->setCommand($command);
$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('__invoke')->once();
$output->shouldReceive('write')->times(3);

$seeder->call('ClassName');
}
Expand Down

0 comments on commit c717a1d

Please sign in to comment.