| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- <?php
- declare(strict_types=1);
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 1.2.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Console;
- use Cake\Console\Shell;
- use Cake\Console\ShellDispatcher;
- use Cake\TestSuite\TestCase;
- /**
- * ShellDispatcherTest
- */
- class ShellDispatcherTest extends TestCase
- {
- /**
- * @var \Cake\Console\ShellDispatcher|\PHPUnit\Framework\MockObject\MockObject
- */
- protected $dispatcher;
- /**
- * setUp method
- */
- public function setUp(): void
- {
- parent::setUp();
- $this->loadPlugins(['TestPlugin', 'Company/TestPluginThree']);
- static::setAppNamespace();
- $this->dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
- ->addMethods(['_stop'])
- ->getMock();
- }
- /**
- * teardown
- */
- public function tearDown(): void
- {
- parent::tearDown();
- ShellDispatcher::resetAliases();
- $this->clearPlugins();
- }
- /**
- * Test error on missing shell
- */
- public function testFindShellMissing(): void
- {
- $this->expectException(\Cake\Console\Exception\MissingShellException::class);
- $this->dispatcher->findShell('nope');
- }
- /**
- * Test error on missing plugin shell
- */
- public function testFindShellMissingPlugin(): void
- {
- $this->expectException(\Cake\Console\Exception\MissingShellException::class);
- $this->dispatcher->findShell('test_plugin.nope');
- }
- /**
- * Verify loading of (plugin-) shells
- */
- public function testFindShell(): void
- {
- $result = $this->dispatcher->findShell('sample');
- $this->assertInstanceOf('TestApp\Shell\SampleShell', $result);
- $result = $this->dispatcher->findShell('test_plugin.example');
- $this->assertInstanceOf('TestPlugin\Shell\ExampleShell', $result);
- $this->assertSame('TestPlugin', $result->plugin);
- $this->assertSame('Example', $result->name);
- $result = $this->dispatcher->findShell('TestPlugin.example');
- $this->assertInstanceOf('TestPlugin\Shell\ExampleShell', $result);
- $result = $this->dispatcher->findShell('TestPlugin.Example');
- $this->assertInstanceOf('TestPlugin\Shell\ExampleShell', $result);
- }
- /**
- * testAddShortPluginAlias
- */
- public function testAddShortPluginAlias(): void
- {
- $expected = [
- 'Company' => 'Company/TestPluginThree.company',
- 'Example' => 'TestPlugin.example',
- ];
- $result = $this->dispatcher->addShortPluginAliases();
- $this->assertSame($expected, $result, 'Should return the list of aliased plugin shells');
- ShellDispatcher::alias('Example', 'SomeOther.PluginsShell');
- $expected = [
- 'Company' => 'Company/TestPluginThree.company',
- 'Example' => 'SomeOther.PluginsShell',
- ];
- $result = $this->dispatcher->addShortPluginAliases();
- $this->assertSame($expected, $result, 'Should not overwrite existing aliases');
- }
- /**
- * Test getting shells with aliases.
- */
- public function testFindShellAliased(): void
- {
- ShellDispatcher::alias('short', 'test_plugin.example');
- $result = $this->dispatcher->findShell('short');
- $this->assertInstanceOf('TestPlugin\Shell\ExampleShell', $result);
- $this->assertSame('TestPlugin', $result->plugin);
- $this->assertSame('Example', $result->name);
- }
- /**
- * Test finding a shell that has a matching alias.
- *
- * Aliases should not overload concrete shells.
- */
- public function testFindShellAliasedAppShadow(): void
- {
- ShellDispatcher::alias('sample', 'test_plugin.example');
- $result = $this->dispatcher->findShell('sample');
- $this->assertInstanceOf('TestApp\Shell\SampleShell', $result);
- $this->assertEmpty($result->plugin);
- $this->assertSame('Sample', $result->name);
- }
- /**
- * Verify dispatch handling stop errors
- */
- public function testDispatchShellWithAbort(): void
- {
- $io = $this->getMockBuilder('Cake\Console\ConsoleIo')->getMock();
- $shell = $this->getMockBuilder('Cake\Console\Shell')
- ->addMethods(['main'])
- ->setConstructorArgs([$io])
- ->getMock();
- $shell->expects($this->once())
- ->method('main')
- ->will($this->returnCallback(function () use ($shell): void {
- $shell->abort('Bad things', 99);
- }));
- $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
- ->onlyMethods(['findShell'])
- ->getMock();
- $dispatcher->expects($this->any())
- ->method('findShell')
- ->with('aborter')
- ->will($this->returnValue($shell));
- $dispatcher->args = ['aborter'];
- $result = $dispatcher->dispatch();
- $this->assertSame(99, $result, 'Should return the exception error code.');
- }
- /**
- * Verify correct dispatch of Shell subclasses with a main method
- */
- public function testDispatchShellWithMain(): void
- {
- $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
- ->onlyMethods(['findShell'])
- ->getMock();
- $Shell = $this->getMockBuilder('Cake\Console\Shell')
- ->disableOriginalConstructor()
- ->getMock();
- $Shell->expects($this->exactly(2))->method('initialize');
- $Shell->expects($this->exactly(2))
- ->method('runCommand')
- ->will($this->returnValue(null));
- $dispatcher->expects($this->any())
- ->method('findShell')
- ->with('mock_with_main')
- ->will($this->returnValue($Shell));
- $dispatcher->args = ['mock_with_main'];
- $result = $dispatcher->dispatch();
- $this->assertSame(Shell::CODE_SUCCESS, $result);
- $this->assertEquals([], $dispatcher->args);
- $dispatcher->args = ['mock_with_main'];
- $result = $dispatcher->dispatch();
- $this->assertSame(Shell::CODE_SUCCESS, $result);
- $this->assertEquals([], $dispatcher->args);
- }
- /**
- * Verifies correct dispatch of Shell subclasses with integer exit codes.
- */
- public function testDispatchShellWithIntegerSuccessCode(): void
- {
- $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
- ->onlyMethods(['findShell'])
- ->getMock();
- $Shell = $this->getMockBuilder('Cake\Console\Shell')
- ->disableOriginalConstructor()
- ->getMock();
- $Shell->expects($this->once())->method('initialize');
- $Shell->expects($this->once())->method('runCommand')
- ->with(['initdb'])
- ->will($this->returnValue(Shell::CODE_SUCCESS));
- $dispatcher->expects($this->any())
- ->method('findShell')
- ->with('mock_without_main')
- ->will($this->returnValue($Shell));
- $dispatcher->args = ['mock_without_main', 'initdb'];
- $result = $dispatcher->dispatch();
- $this->assertSame(Shell::CODE_SUCCESS, $result);
- }
- /**
- * Verifies correct dispatch of Shell subclasses with custom integer exit codes.
- */
- public function testDispatchShellWithCustomIntegerCodes(): void
- {
- $customErrorCode = 3;
- $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
- ->onlyMethods(['findShell'])
- ->getMock();
- $Shell = $this->getMockBuilder('Cake\Console\Shell')
- ->disableOriginalConstructor()
- ->getMock();
- $Shell->expects($this->once())->method('initialize');
- $Shell->expects($this->once())->method('runCommand')
- ->with(['initdb'])
- ->will($this->returnValue($customErrorCode));
- $dispatcher->expects($this->any())
- ->method('findShell')
- ->with('mock_without_main')
- ->will($this->returnValue($Shell));
- $dispatcher->args = ['mock_without_main', 'initdb'];
- $result = $dispatcher->dispatch();
- $this->assertSame($customErrorCode, $result);
- }
- /**
- * Verify correct dispatch of Shell subclasses without a main method
- */
- public function testDispatchShellWithoutMain(): void
- {
- $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
- ->onlyMethods(['findShell'])
- ->getMock();
- $Shell = $this->getMockBuilder('Cake\Console\Shell')
- ->disableOriginalConstructor()
- ->getMock();
- $Shell->expects($this->once())->method('initialize');
- $Shell->expects($this->once())->method('runCommand')
- ->with(['initdb'])
- ->will($this->returnValue(true));
- $dispatcher->expects($this->any())
- ->method('findShell')
- ->with('mock_without_main')
- ->will($this->returnValue($Shell));
- $dispatcher->args = ['mock_without_main', 'initdb'];
- $result = $dispatcher->dispatch();
- $this->assertSame(Shell::CODE_SUCCESS, $result);
- }
- /**
- * Verify you can dispatch a plugin's main shell with the shell name alone
- */
- public function testDispatchShortPluginAlias(): void
- {
- $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
- ->onlyMethods(['_shellExists', '_createShell'])
- ->getMock();
- $Shell = $this->getMockBuilder('Cake\Console\Shell')
- ->disableOriginalConstructor()
- ->getMock();
- $dispatcher->expects($this->exactly(2))
- ->method('_shellExists')
- ->withConsecutive(['example'], ['TestPlugin.Example'])
- ->will($this->onConsecutiveCalls(null, 'TestPlugin\Console\Command\TestPluginShell'));
- $dispatcher->expects($this->once())
- ->method('_createShell')
- ->with('TestPlugin\Console\Command\TestPluginShell', 'TestPlugin.Example')
- ->will($this->returnValue($Shell));
- $dispatcher->args = ['example'];
- $result = $dispatcher->dispatch();
- $this->assertSame(Shell::CODE_SUCCESS, $result);
- }
- /**
- * Ensure short plugin shell usage is case/camelized insensitive
- */
- public function testDispatchShortPluginAliasCamelized(): void
- {
- $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
- ->onlyMethods(['_shellExists', '_createShell'])
- ->getMock();
- $Shell = $this->getMockBuilder('Cake\Console\Shell')
- ->disableOriginalConstructor()
- ->getMock();
- $dispatcher->expects($this->exactly(2))
- ->method('_shellExists')
- ->withConsecutive(['Example'], ['TestPlugin.Example'])
- ->will($this->onConsecutiveCalls(null, 'TestPlugin\Console\Command\TestPluginShell'));
- $dispatcher->expects($this->once())
- ->method('_createShell')
- ->with('TestPlugin\Console\Command\TestPluginShell', 'TestPlugin.Example')
- ->will($this->returnValue($Shell));
- $dispatcher->args = ['Example'];
- $result = $dispatcher->dispatch();
- $this->assertSame(Shell::CODE_SUCCESS, $result);
- }
- /**
- * Verify that in case of conflict, app shells take precedence in alias list
- */
- public function testDispatchShortPluginAliasConflict(): void
- {
- $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
- ->onlyMethods(['_shellExists', '_createShell'])
- ->getMock();
- $Shell = $this->getMockBuilder('Cake\Console\Shell')
- ->disableOriginalConstructor()
- ->getMock();
- $dispatcher->expects($this->once())
- ->method('_shellExists')
- ->with('sample')
- ->will($this->returnValue('App\Shell\SampleShell'));
- $dispatcher->expects($this->once())
- ->method('_createShell')
- ->with('App\Shell\SampleShell', 'sample')
- ->will($this->returnValue($Shell));
- $dispatcher->args = ['sample'];
- $result = $dispatcher->dispatch();
- $this->assertSame(Shell::CODE_SUCCESS, $result);
- }
- /**
- * Verify shifting of arguments
- */
- public function testShiftArgs(): void
- {
- $this->dispatcher->args = ['a', 'b', 'c'];
- $this->assertSame('a', $this->dispatcher->shiftArgs());
- $this->assertSame($this->dispatcher->args, ['b', 'c']);
- $this->dispatcher->args = ['a' => 'b', 'c', 'd'];
- $this->assertSame('b', $this->dispatcher->shiftArgs());
- $this->assertSame($this->dispatcher->args, ['c', 'd']);
- $this->dispatcher->args = ['a', 'b' => 'c', 'd'];
- $this->assertSame('a', $this->dispatcher->shiftArgs());
- $this->assertSame($this->dispatcher->args, ['b' => 'c', 'd']);
- $this->dispatcher->args = [0 => 'a', 2 => 'b', 30 => 'c'];
- $this->assertSame('a', $this->dispatcher->shiftArgs());
- $this->assertSame($this->dispatcher->args, [0 => 'b', 1 => 'c']);
- $this->dispatcher->args = [];
- $this->assertNull($this->dispatcher->shiftArgs());
- $this->assertSame([], $this->dispatcher->args);
- }
- /**
- * Test how `bin/cake --help` works.
- */
- public function testHelpOption(): void
- {
- $this->expectWarning();
- $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
- ->addMethods(['_stop'])
- ->getMock();
- $dispatcher->args = ['--help'];
- $dispatcher->dispatch();
- }
- /**
- * Test how `bin/cake --version` works.
- */
- public function testVersionOption(): void
- {
- $this->expectWarning();
- $dispatcher = $this->getMockBuilder('Cake\Console\ShellDispatcher')
- ->addMethods(['_stop'])
- ->getMock();
- $dispatcher->args = ['--version'];
- $dispatcher->dispatch();
- }
- }
|