ConsoleApplicationTest.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace Illuminate\Tests\Console;
  3. use Illuminate\Console\Application;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Contracts\Events\Dispatcher;
  6. use Illuminate\Contracts\Foundation\Application as ApplicationContract;
  7. use Mockery as m;
  8. use PHPUnit\Framework\TestCase;
  9. use Symfony\Component\Console\Command\Command as SymfonyCommand;
  10. class ConsoleApplicationTest extends TestCase
  11. {
  12. protected function tearDown(): void
  13. {
  14. m::close();
  15. }
  16. public function testAddSetsLaravelInstance()
  17. {
  18. $app = $this->getMockConsole(['addToParent']);
  19. $command = m::mock(Command::class);
  20. $command->shouldReceive('setLaravel')->once()->with(m::type(ApplicationContract::class));
  21. $app->expects($this->once())->method('addToParent')->with($this->equalTo($command))->willReturn($command);
  22. $result = $app->add($command);
  23. $this->assertEquals($command, $result);
  24. }
  25. public function testLaravelNotSetOnSymfonyCommands()
  26. {
  27. $app = $this->getMockConsole(['addToParent']);
  28. $command = m::mock(SymfonyCommand::class);
  29. $command->shouldReceive('setLaravel')->never();
  30. $app->expects($this->once())->method('addToParent')->with($this->equalTo($command))->willReturn($command);
  31. $result = $app->add($command);
  32. $this->assertEquals($command, $result);
  33. }
  34. public function testResolveAddsCommandViaApplicationResolution()
  35. {
  36. $app = $this->getMockConsole(['addToParent']);
  37. $command = m::mock(SymfonyCommand::class);
  38. $app->getLaravel()->shouldReceive('make')->once()->with('foo')->andReturn(m::mock(SymfonyCommand::class));
  39. $app->expects($this->once())->method('addToParent')->with($this->equalTo($command))->willReturn($command);
  40. $result = $app->resolve('foo');
  41. $this->assertEquals($command, $result);
  42. }
  43. public function testCallFullyStringCommandLine()
  44. {
  45. $app = new Application(
  46. $app = m::mock(ApplicationContract::class, ['version' => '6.0']),
  47. $events = m::mock(Dispatcher::class, ['dispatch' => null, 'fire' => null]),
  48. 'testing'
  49. );
  50. $codeOfCallingArrayInput = $app->call('help', [
  51. '--raw' => true,
  52. '--format' => 'txt',
  53. '--no-interaction' => true,
  54. '--env' => 'testing',
  55. ]);
  56. $outputOfCallingArrayInput = $app->output();
  57. $codeOfCallingStringInput = $app->call(
  58. 'help --raw --format=txt --no-interaction --env=testing'
  59. );
  60. $outputOfCallingStringInput = $app->output();
  61. $this->assertSame($codeOfCallingArrayInput, $codeOfCallingStringInput);
  62. $this->assertSame($outputOfCallingArrayInput, $outputOfCallingStringInput);
  63. }
  64. protected function getMockConsole(array $methods)
  65. {
  66. $app = m::mock(ApplicationContract::class, ['version' => '6.0']);
  67. $events = m::mock(Dispatcher::class, ['dispatch' => null]);
  68. return $this->getMockBuilder(Application::class)->onlyMethods($methods)->setConstructorArgs([
  69. $app, $events, 'test-version',
  70. ])->getMock();
  71. }
  72. }