Skip to content

Commit

Permalink
[9.x] Improve output for some Artisan commands (laravel#43547)
Browse files Browse the repository at this point in the history
* Improve output for some Artisan commands

* Improve output for migrate:fresh command

* Fix tests

Co-authored-by: Alvin Lau <alvinlau@fimmick.com>
  • Loading branch information
2 people authored and Ken committed Aug 9, 2022
1 parent 32ba1b3 commit faaf397
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 11 deletions.
2 changes: 0 additions & 2 deletions src/Illuminate/Database/Console/Migrations/FreshCommand.php
Expand Up @@ -90,8 +90,6 @@ protected function needsSeeding()
*/
protected function runSeeder($database)
{
$this->newLine();

$this->call('db:seed', array_filter([
'--database' => $database,
'--class' => $this->option('seeder') ?: 'Database\\Seeders\\DatabaseSeeder',
Expand Down
4 changes: 0 additions & 4 deletions src/Illuminate/Database/Console/Migrations/MigrateCommand.php
Expand Up @@ -91,10 +91,6 @@ public function handle()
// seed task to re-populate the database, which is convenient when adding
// a migration and a seed at the same time, as it is only this command.
if ($this->option('seed') && ! $this->option('pretend')) {
if (! empty($migrations)) {
$this->newLine();
}

$this->call('db:seed', [
'--class' => $this->option('seeder') ?: 'Database\\Seeders\\DatabaseSeeder',
'--force' => true,
Expand Down
2 changes: 0 additions & 2 deletions src/Illuminate/Database/Console/Migrations/RefreshCommand.php
Expand Up @@ -132,8 +132,6 @@ protected function needsSeeding()
*/
protected function runSeeder($database)
{
$this->newLine();

$this->call('db:seed', array_filter([
'--database' => $database,
'--class' => $this->option('seeder') ?: 'Database\\Seeders\\DatabaseSeeder',
Expand Down
10 changes: 9 additions & 1 deletion src/Illuminate/Database/Migrations/Migrator.php
Expand Up @@ -180,6 +180,10 @@ public function runPending(array $migrations, array $options = [])
}

$this->fireMigrationEvent(new MigrationsEnded('up'));

if ($this->output) {
$this->output->writeln('');
}
}

/**
Expand Down Expand Up @@ -233,7 +237,11 @@ public function rollback($paths = [], array $options = [])
return [];
}

return $this->rollbackMigrations($migrations, $paths, $options);
return tap($this->rollbackMigrations($migrations, $paths, $options), function () {
if ($this->output) {
$this->output->writeln('');
}
});
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Illuminate/Database/Seeder.php
Expand Up @@ -61,6 +61,10 @@ public function call($class, $silent = false, array $parameters = [])
static::$called[] = $class;
}

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

return $this;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseSeederTest.php
Expand Up @@ -38,9 +38,9 @@ public function testCallResolveTheClassAndCallsRun()
$seeder = new TestSeeder;
$seeder->setContainer($container = m::mock(Container::class));
$output = m::mock(OutputInterface::class);
$output->shouldReceive('writeln')->once();
$output->shouldReceive('writeln')->twice();
$command = m::mock(Command::class);
$command->shouldReceive('getOutput')->once()->andReturn($output);
$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);
Expand Down
6 changes: 6 additions & 0 deletions tests/Integration/Migration/MigratorTest.php
Expand Up @@ -32,6 +32,8 @@ public function testMigrate()
$this->expectTask('2015_10_04_000000_modify_people_table', 'DONE');
$this->expectTask('2016_10_04_000000_modify_people_table', 'DONE');

$this->output->shouldReceive('writeln')->once();

$this->subject->run([__DIR__.'/fixtures']);

$this->assertTrue(DB::getSchemaBuilder()->hasTable('people'));
Expand Down Expand Up @@ -64,6 +66,8 @@ public function testRollback()
$this->expectTask('2015_10_04_000000_modify_people_table', 'DONE');
$this->expectTask('2014_10_12_000000_create_people_table', 'DONE');

$this->output->shouldReceive('writeln')->once();

$this->subject->rollback([__DIR__.'/fixtures']);

$this->assertFalse(DB::getSchemaBuilder()->hasTable('people'));
Expand All @@ -85,6 +89,8 @@ public function testPretendMigrate()
$this->expectTwoColumnDetail('2016_10_04_000000_modify_people_table');
$this->expectBulletList(['alter table "people" add column "last_name" varchar']);

$this->output->shouldReceive('writeln')->once();

$this->subject->run([__DIR__.'/fixtures'], ['pretend' => true]);

$this->assertFalse(DB::getSchemaBuilder()->hasTable('people'));
Expand Down

0 comments on commit faaf397

Please sign in to comment.