function setup() { $this->application = new ApplicationWithTerminalWidth('TestApplication', '0.0.0'); $this->commandFactory = new AnnotatedCommandFactory(); // $factory->addListener(...); $alterOptionsEventManager = new AlterOptionsCommandEvent($this->application); $eventDispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher(); $eventDispatcher->addSubscriber($this->commandFactory->commandProcessor()->hookManager()); $eventDispatcher->addSubscriber($alterOptionsEventManager); $this->application->setDispatcher($eventDispatcher); $this->application->setAutoExit(false); $discovery = new CommandFileDiscovery(); $discovery->setSearchPattern('*CommandFile.php')->setIncludeFilesAtBase(false)->setSearchLocations(['alpha']); chdir(__DIR__); $commandFiles = $discovery->discover('.', '\\Consolidation\\TestUtils'); $formatter = new FormatterManager(); $formatter->addDefaultFormatters(); $formatter->addDefaultSimplifiers(); $terminalWidthOption = new PrepareTerminalWidthOption(); $terminalWidthOption->setApplication($this->application); $this->commandFactory->commandProcessor()->setFormatterManager($formatter); $this->commandFactory->commandProcessor()->addPrepareFormatter($terminalWidthOption); $this->commandFactory->setIncludeAllPublicMethods(false); $this->addDiscoveredCommands($this->commandFactory, $commandFiles); $helpCommandfile = new HelpCommand($this->application); $commandList = $this->commandFactory->createCommandsFromClass($helpCommandfile); foreach ($commandList as $command) { $this->application->add($command); } }
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); }
/** * Discovers command classes using CommandFileDiscovery * * @param string[] $options Elements as follow * string path The full path to the directory to search for commands * string namespace The full namespace associated with given the command directory * @return TerminusCommand[] An array of TerminusCommand instances */ private function getCommands(array $options = ['path' => null, 'namespace' => null]) { $discovery = new CommandFileDiscovery(); $discovery->setSearchPattern('*Command.php')->setSearchLocations([]); return $discovery->discover($options['path'], $options['namespace']); }