/** * * @param CliEngine $engine * @param array $params * @param mixed $injectables * @return void */ public function processCommand(CliEngine $engine, $params = array(), $injectables = null) { // we need to wrap our code to catch old-style PHP errors $legacyHandler = new Legacy_ErrorHandler(); try { $returnCode = $legacyHandler->run([$this, 'processInsideLegacyHandler'], [$engine, $params, $injectables]); return $returnCode; } catch (Exception $e) { $injectables->output->logCliError($e->getMessage()); exit(1); } }
/** * * @param CliEngine $engine * @param array $params * @param Injectables|null $injectables * @return integer */ public function processCommand(CliEngine $engine, $params = array(), $injectables = null) { // we need to wrap our code to catch old-style PHP errors $legacyHandler = new Legacy_ErrorHandler(); // run our code try { $returnCode = $legacyHandler->run([$this, 'processInsideLegacyHandler'], [$engine, $params, $injectables]); return $returnCode; } catch (Exception $e) { $injectables->output->logCliError($e->getMessage()); $engine->options->dev = true; if (isset($engine->options->dev) && $engine->options->dev) { $injectables->output->logCliError("Stack trace is:\n\n" . $e->getTraceAsString()); } // stop the browser if available if (isset($this->st)) { $this->st->stopDevice(); } // tell the calling process that things did not end well exit(1); } }
/** * @param StoryTeller $st * @param Injectables $injectables * @param array $phases */ public function playPhases($activity, StoryTeller $st, Injectables $injectables, $phases, $thingBeingPlayed) { // shorthand $output = $st->getOutput(); // we are going to need something to help us load each of our // phases $phaseLoader = $injectables->phaseLoader; // pre-load all of the phases, before we execute them // this will trigger any PHP syntax errors now rather than // when we're part-way through executing our code $phasesToPlay = []; foreach ($phases as $phaseName => $isActive) { $phase = $phaseLoader->loadPhase($st, $phaseName); $phasesToPlay[$phaseName] = ['phase' => $phase, 'isActive' => $isActive]; } // the result of playing this group of phases $groupResult = null; if ($thingBeingPlayed) { $groupResult = $thingBeingPlayed->getResult(); $groupResult->setActivity($activity); } // we need to wrap our code to catch old-style PHP errors $legacyHandler = new Legacy_ErrorHandler(); // execute each phase, until either: // // 1. all listed phases have been executed, or // 2. one of the phases says that the story has failed foreach ($phasesToPlay as $phaseName => $phaseData) { // shorthand $phase = $phaseData['phase']; $isActive = $phaseData['isActive']; try { // tell the world that we're running this phase $output->startPhase($phase); // play the phase $phaseResult = $legacyHandler->run([$this, 'playPhase'], [$st, $injectables, $phase, $isActive, $thingBeingPlayed]); // remember the result of this phase //$phaseResults->addResult($phase, $phaseResult); // now, what do we do? $nextAction = $phaseResult->getNextAction(); switch ($nextAction) { case self::NEXT_SKIP: // why? if ($phaseResult->getPhaseIsBlacklisted()) { if ($groupResult) { $groupResult->setPhaseGroupHasBeenBlacklisted($phaseResult); } $output->logPhaseSkipped($phaseName, self::MSG_PHASE_BLACKLISTED . ': ' . $phaseResult->getMessage()); } else { if ($phaseResult->getPhaseCannotRun()) { $output->logPhaseSkipped($phaseName, $phaseResult->getMessage()); } else { if ($groupResult) { $groupResult->setPhaseGroupIsIncomplete($phaseResult); } $output->logPhaseSkipped($phaseName, self::MSG_PHASE_INCOMPLETE); } } // tell the output plugins that this phase is over $output->endPhase($phase, $phaseResult); return; case self::NEXT_FAIL: if ($groupResult) { $groupResult->setPhaseGroupHasFailed($phaseResult); } $output->logPhaseError($phaseName, self::MSG_PHASE_FAILED . ': ' . $phaseResult->getMessage()); // tell the output plugins that this phase is over $output->endPhase($phase, $phaseResult); return; case self::NEXT_CONTINUE: if ($groupResult) { // keep the result up to date, in case this // is the last one if ($phaseResult->getPhaseHasBeenSkipped()) { $groupResult->setPhaseGroupHasBeenSkipped(); } else { $groupResult->setPhaseGroupHasSucceeded(); } } // tell the output plugins that this phase is over $output->endPhase($phase, $phaseResult); } } catch (Exception $e) { // tell our output plugins what happened $output->logPhaseError($phaseName, "uncaught exception: " . (string) $e->getMessage() . $e->getTraceAsString()); // we need to create a dummy phase result for this $phaseResult = new Phase_Result($phaseName); $phaseResult->setPlayingFailed($phaseResult::ERROR, self::MSG_PHASE_FAILED, $e); // tell the world that this phase is over $output->endPhase($phase, $phaseResult); // this is a fatal exception if ($groupResult) { $groupResult->setPhaseGroupHasError($phaseResult); } // run no more phases return; } } // all done // if ($groupResult) { // $groupResult->setPhaseGroupHasSucceeded(); // } }
/** * * @param CliEngine $engine * @param array $params * @param mixed $additionalContext * @return int */ public function processCommand(CliEngine $engine, $params = array(), $additionalContext = null) { // do we have the name of the file to create? if (!isset($params[0])) { echo "*** error: you must specify which story to create\n"; exit(1); } // we're going to be dealing with some prehistoric parts of PHP $legacyHandler = new Legacy_ErrorHandler(); // create the path to the story $storyFolder = dirname($params[0]); if (!file_exists($storyFolder)) { try { $legacyHandler->run(function () use($storyFolder) { mkdir($storyFolder, 0755, true); }); } catch (Exception $e) { echo "*** error: unable to create folder '{$storyFolder}'\n"; exit(1); } } // create the story inside the folder $story = <<<EOS <?php EOS; if (isset($engine->options->basedOn)) { foreach ($engine->options->basedOn as $templateClass) { $story .= "use {$templateClass};\n"; } } $story .= <<<EOS // ======================================================================== // // STORY DETAILS // // ------------------------------------------------------------------------ \$story = newStoryFor('Top-Level Category') ->inGroup(['Group inside Top-level Category']) ->called('Your story name') EOS; if (isset($engine->options->basedOn)) { $i = 0; foreach ($engine->options->basedOn as $templateClass) { if ($i == 0) { $story .= "\n ->basedOn(new " . basename(str_replace('\\', '/', $templateClass)) . ")"; } else { $story .= "\n ->andBasedOn(new " . basename(str_replace('\\', '/', $templateClass)) . ")"; } $i++; } } $story .= ";"; $story .= <<<EOS \$story->requiresStoryplayerVersion(2); // ======================================================================== // // TEST SETUP / TEAR-DOWN // // ------------------------------------------------------------------------ /* \$story->addTestSetup(function() { // setup the conditions for this specific test }); */ /* \$story->addTestTeardown(function() { // undo anything that you did in addTestSetup() }); */ // ======================================================================== // // PRE-TEST PREDICTION // // ------------------------------------------------------------------------ /* \$story->addPreTestPrediction(function() { // if it is okay for your story to fail, detect that here }); */ // ======================================================================== // // PRE-TEST INSPECTION // // ------------------------------------------------------------------------ /* \$story->addPreTestInspection(function() { // get the checkpoint - we're going to store data in here \$checkpoint = getCheckpoint(); // store any data that your story is about to change, so that you // can do a before and after comparison }); */ // ======================================================================== // // POSSIBLE ACTION(S) // // ------------------------------------------------------------------------ /* \$story->addAction(function() { // this is where you perform the steps of your user story }); */ // ======================================================================== // // POST-TEST INSPECTION // // ------------------------------------------------------------------------ \$story->addPostTestInspection(function() { // the information to guide our checks is in the checkpoint \$checkpoint = getCheckpoint(); // gather new data, and make sure that your action actually changed // something. never assume that the action worked just because it // completed to the end with no errors or exceptions! }); EOS; // does the file already exist? if (file_exists($params[0])) { // has the user used --force? if (!isset($engine->options->force) || !$engine->options->force) { echo "*** error: file '{$params[0]}' already exists\n"; echo "use --force to replace this file with the new story file\n"; exit(1); } } try { $legacyHandler->run(function () use($params, $story) { file_put_contents($params[0], $story); }); } catch (Exception $e) { echo "*** error: " . $e->getMessage() . "\n"; exit(1); } // all done return 0; }
/** * * @param CliEngine $engine * @param array $params * @param Injectables|null $injectables * @return integer */ public function processCommand(CliEngine $engine, $params = array(), $injectables = null) { // we need to wrap our code to catch old-style PHP errors $legacyHandler = new Legacy_ErrorHandler(); try { $returnCode = $legacyHandler->run([$this, 'processInsideLegacyHandler'], [$engine, $params, $injectables]); return $returnCode; } catch (Exception $e) { $injectables->output->logCliError($e->getMessage()); $engine->options->dev = true; if (isset($engine->options->dev) && $engine->options->dev) { $injectables->output->logCliError("Stack trace is:\n\n" . $e->getTraceAsString()); } exit(1); } }