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;
}