Skip to content

Commit

Permalink
[9.x] Make Command components Factory extensible (#43439)
Browse files Browse the repository at this point in the history
* Make Command components Factory extensible

* Fix issue for default bind

* Fix mocking in tests
  • Loading branch information
emiliopedrollo authored Jul 27, 2022
1 parent f4179b2 commit 1d26aae
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Console/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public function run(InputInterface $input, OutputInterface $output): int
OutputStyle::class, ['input' => $input, 'output' => $output]
);

$this->components = new Factory($this->output);
$this->components = $this->laravel->make(Factory::class, ['output' => $this->output]);

return parent::run(
$this->input = $input, $this->output
Expand Down
5 changes: 4 additions & 1 deletion tests/Console/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Console\Application;
use Illuminate\Console\Command;
use Illuminate\Console\OutputStyle;
use Illuminate\Console\View\Components\Factory;
use Mockery as m;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
Expand Down Expand Up @@ -35,7 +36,9 @@ public function handle()

$input = new ArrayInput([]);
$output = new NullOutput;
$application->shouldReceive('make')->with(OutputStyle::class, ['input' => $input, 'output' => $output])->andReturn(m::mock(OutputStyle::class));
$outputStyle = m::mock(OutputStyle::class);
$application->shouldReceive('make')->with(OutputStyle::class, ['input' => $input, 'output' => $output])->andReturn($outputStyle);
$application->shouldReceive('make')->with(Factory::class, ['output' => $outputStyle])->andReturn(m::mock(Factory::class));

$application->shouldReceive('call')->with([$command, 'handle'])->andReturnUsing(function () use ($command, $application) {
$commandCalled = m::mock(Command::class);
Expand Down
13 changes: 11 additions & 2 deletions tests/Database/SeedCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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;
Expand All @@ -23,6 +24,7 @@ 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();
Expand All @@ -38,7 +40,10 @@ public function testHandle()
$container->shouldReceive('environment')->once()->andReturn('testing');
$container->shouldReceive('make')->with('DatabaseSeeder')->andReturn($seeder);
$container->shouldReceive('make')->with(OutputStyle::class, m::any())->andReturn(
new OutputStyle($input, $output)
$outputStyle
);
$container->shouldReceive('make')->with(Factory::class, m::any())->andReturn(
new Factory($outputStyle)
);

$command = new SeedCommand($resolver);
Expand All @@ -59,6 +64,7 @@ public function testWithoutModelEvents()
'--class' => UserWithoutModelEventsSeeder::class,
]);
$output = new NullOutput;
$outputStyle = new OutputStyle($input, $output);

$instance = new UserWithoutModelEventsSeeder();

Expand All @@ -75,7 +81,10 @@ public function testWithoutModelEvents()
$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)
$outputStyle
);
$container->shouldReceive('make')->with(Factory::class, m::any())->andReturn(
new Factory($outputStyle)
);

$command = new SeedCommand($resolver);
Expand Down

0 comments on commit 1d26aae

Please sign in to comment.