/**
  * @param Activity $activity
  *
  * @return Result
  */
 public function run(Activity $activity)
 {
     $challenge = $activity->getChallengeObject();
     $worker = $this->getWorker($challenge->getExecutionMode());
     // 1) write all of the files        (different for contexts)
     // 2) call setupContext()           (different for contexts)
     // 3) call grade() and use results  (different for contexts)
     $filesToWrite = $this->getFilesToCreate($activity);
     $initialExecutionResult = new CodingExecutionResult($filesToWrite);
     // serialize this, so we can easily fetch the input files
     $filesToWrite['executionResult.cache'] = serialize($initialExecutionResult);
     // write the challenge class name
     $filesToWrite['ChallengeClass.php'] = $activity->getChallengeClassContents();
     // write our executor.php and execute that
     $executionCode = $this->twig->render('code_executor.php.twig', array('workerClass' => get_class($worker), 'projectPath' => $this->projectRootDir, 'challengeFilename' => 'ChallengeClass.php', 'challengeClassName' => $activity->getChallengeClassName(), 'entryPointFilename' => $challenge->getFileBuilder()->getEntryPointFilename()));
     $filesToWrite['execution.php'] = $executionCode;
     $codeExecutor = new CodeExecutor($filesToWrite, 'execution.php');
     $executionResult = $codeExecutor->executePhpProcess();
     $result = new Result($activity);
     // clean things up just in case - but the worker should take care of this
     $result->setLanguageError(self::cleanError($executionResult->getLanguageError(), $executionResult->getCodeDirectory()));
     $result->setGradingError($executionResult->getGradingError());
     $result->setOutput($executionResult->getOutput());
     return $result;
 }
    public function getIntegrationTests()
    {
        $tests = array();
        $activity = $this->createActivity('CreateVariableCoding');
        $activity->addInputFile('index.php', <<<EOF
<?php
\$airpupTag = 'I luv kittens';
?>

<h2><?php echo \$airpupTag; ?></h2>
EOF
);
        $result = new Result($activity);
        $result->setOutput("\n<h2>I luv kittens</h2>");
        $tests['correct_simple'] = array($activity, $result);
        /* TEST START: Undefined variable */
        $activity = $this->createActivity('CreateVariableCoding');
        $activity->addInputFile('index.php', <<<EOF
Hello!

<h2><?php echo \$airpupTag; ?></h2>
EOF
);
        $result = new Result($activity);
        $result->setOutput("Hello!\n\n<h2>");
        // prints this before die'ing
        $result->setLanguageError('Notice: Undefined variable: airpupTag in index.php on line 3');
        $tests['php_undefined_variable'] = array($activity, $result);
        return $tests;
        /* TEST START: Multiple files */
        $activity = $this->createActivity('MultipleFilesCoding');
        $activity->addInputFile('index.php', <<<EOF
<h2><?php echo \$whatILove; ?></h2>
EOF
);
        // don't send bootstrap.php - that should use the default
        $result = new Result($activity);
        $result->setOutput('<h2>Puppies</h2>');
        $tests['correct_multiple_files'] = array($activity, $result);
        /* TEST START */
        $activity = $this->createActivity('CreateVariableCoding');
        $activity->addInputFile('index.php', <<<EOF
<?php

echo 'I luv dogs';
EOF
);
        $result = new Result($activity);
        $result->setOutput('I luv dogs');
        $result->setGradingError('I don\'t see "I luv kittens" in the output.');
        $tests['incorrect_simple'] = array($activity, $result);
        /* TEST START */
        $activity = $this->createActivity('CreateVariableCoding');
        $activity->addInputFile('index.php', <<<EOF
<?php

echo 'I <3 Puppies!!!'
EOF
);
        $result = new Result($activity);
        $result->setOutput(null);
        $result->setLanguageError("PHP Parse error:  syntax error, unexpected end of file, expecting ',' or ';' in index.php on line 3");
        $tests['php_syntax_error'] = array($activity, $result);
        /* TEST START */
        $activity = $this->createActivity('TwigPrintVariable');
        $activity->addInputFile('homepage.twig', <<<EOF
<h2>{{ whatIWantForXmas }}</h2>
EOF
);
        $result = new Result($activity);
        $result->setOutput('<h2>Puppy</h2>');
        $tests['twig_correct_simple'] = array($activity, $result);
        /* TEST START */
        $activity = $this->createActivity('TwigPrintVariable');
        $activity->addInputFile('homepage.twig', <<<EOF
<h2>{{ bacon }}</h2>
EOF
);
        $result = new Result($activity);
        $result->setOutput('');
        $result->setLanguageError('Variable "bacon" does not exist in "homepage.twig" at line 1');
        $tests['twig_bad_variable'] = array($activity, $result);
        /* TEST START */
        $activity = $this->createActivity('TwigPrintVariable');
        $activity->addInputFile('homepage.twig', <<<EOF
<h1>{{ 'foo }}</h1>
EOF
);
        $result = new Result($activity);
        $result->setOutput('');
        $result->setLanguageError('Unexpected character "\'" in "homepage.twig" at line 1');
        $tests['twig_syntax_error'] = array($activity, $result);
        /* TEST START */
        $activity = $this->createActivity('TwigPrintVariable');
        $activity->addInputFile('homepage.twig', <<<EOF
<h1>{{ whatIWantForXmas }}</h1>
EOF
);
        $result = new Result($activity);
        $result->setOutput('<h1>Puppy</h1>');
        $result->setGradingError('I don\'t see any "h2" HTML element with the text "Puppy" in it.');
        $tests['twig_grading_error'] = array($activity, $result);
        return $tests;
    }