/** * Returns all of the hook names that may be called for this command. * * @return array */ public function getNames() { return HookManager::getNames($this, $this->commandCallback); }
function testCommandsAndHooksWithBetaFolder() { // First, search for commandfiles in the 'alpha' // directory. Note that this same functionality // is tested more thoroughly in isolation in // testCommandFileDiscovery.php $discovery = new CommandFileDiscovery(); $discovery->setSearchPattern('*CommandFile.php')->setIncludeFilesAtBase(false)->setSearchLocations(['alpha', 'beta']); chdir(__DIR__); $commandFiles = $discovery->discover('.', '\\Consolidation\\TestUtils'); $formatter = new FormatterManager(); $formatter->addDefaultFormatters(); $formatter->addDefaultSimplifiers(); $hookManager = new HookManager(); $commandProcessor = new CommandProcessor($hookManager); $commandProcessor->setFormatterManager($formatter); // Create a new factory, and load all of the files // discovered above. The command factory class is // tested in isolation in testAnnotatedCommandFactory.php, // but this is the only place where $factory = new AnnotatedCommandFactory(); $factory->setCommandProcessor($commandProcessor); // $factory->addListener(...); $factory->setIncludeAllPublicMethods(true); $this->addDiscoveredCommands($factory, $commandFiles); // A few asserts, to make sure that our hooks all get registered. $allRegisteredHooks = $hookManager->getAllHooks(); $registeredHookNames = array_keys($allRegisteredHooks); sort($registeredHookNames); $this->assertEquals('*,example:table,my-event', implode(',', $registeredHookNames)); $allHooksForExampleTable = $allRegisteredHooks['example:table']; $allHookPhasesForExampleTable = array_keys($allHooksForExampleTable); sort($allHookPhasesForExampleTable); $this->assertEquals('alter,option', implode(',', $allHookPhasesForExampleTable)); $this->assertContains('alterFormattersChinese', var_export($allHooksForExampleTable, true)); $alterHooksForExampleTable = $this->callProtected($hookManager, 'getHooks', [['example:table'], 'alter']); $this->assertContains('alterFormattersKanji', var_export($alterHooksForExampleTable, true)); $allHooksForAnyCommand = $allRegisteredHooks['*']; $allHookPhasesForAnyCommand = array_keys($allHooksForAnyCommand); sort($allHookPhasesForAnyCommand); $this->assertEquals('alter', implode(',', $allHookPhasesForAnyCommand)); $this->assertContains('alterFormattersKanji', var_export($allHooksForAnyCommand, true)); // Help should have the information from the hooks in the 'beta' folder $this->assertRunCommandViaApplicationContains('help example:table', ['Option added by @hook option example:table', 'example:table --french', 'Add a row with French numbers.', 'chinese', 'kanji']); // Confirm that the "unavailable" command is now available $this->assertTrue($this->application->has('unavailable:command')); $expectedOutputWithChinese = <<<EOT ------ ------ ------- I II III ------ ------ ------- One Two Three Eins Zwei Drei Ichi Ni San Uno Dos Tres 壹 貳 叁 ------ ------ ------- EOT; $this->assertRunCommandViaApplicationEquals('example:table --chinese', $expectedOutputWithChinese); $expectedOutputWithKanji = <<<EOT ------ ------ ------- I II III ------ ------ ------- One Two Three Eins Zwei Drei Ichi Ni San Uno Dos Tres 一 二 三 ------ ------ ------- EOT; $this->assertRunCommandViaApplicationEquals('example:table --kanji', $expectedOutputWithKanji); }