/**
  * @dataProvider getExecutionTests
  */
 public function testExecuteCode($filename, $fileSource, array $variables, $expectedOutput, $expectedLanguageError)
 {
     $path = $this->testDir . '/' . $filename;
     file_put_contents($path, $fileSource);
     $context = new CodingContext($this->testDir);
     foreach ($variables as $name => $val) {
         $context->addVariable($name, $val);
     }
     $result = new CodingExecutionResult(array($filename => $fileSource));
     $worker = new PhpWorker();
     $worker->executeCode($this->testDir, $filename, $context, $result);
     $this->assertEquals($expectedOutput, $result->getOutput());
     $this->assertEquals($expectedLanguageError, $result->getLanguageError());
 }
 public function executeTwig($templateName, CodingContext $context, CodingExecutionResult $codingExecutionResult)
 {
     $errorHandler = TwigErrorHandler::register();
     try {
         $output = $this->getTwigEnvironment()->render($templateName, $context->getVariables());
         $codingExecutionResult->setOutput($output);
     } catch (TwigException $error) {
         $codingExecutionResult->setLanguageError($error->getMessage());
     } catch (\Twig_Error $error) {
         // not doing anything special here... but in the future, we might
         // fetch more information about line numbers, etc
         $codingExecutionResult->setLanguageError($error->getMessage());
     } catch (\Exception $error) {
         $codingExecutionResult->setLanguageError($error->getMessage());
     }
     $errorHandler->restore();
     return $codingExecutionResult;
 }
 /**
  * Execute the code and modify the CodingExecutionResult
  *
  * @param string $rootDir Where all the files have been placed
  * @param string $entryPointFilename
  * @param CodingContext $context
  * @param CodingExecutionResult $result
  */
 public function executeCode($rootDir, $entryPointFilename, CodingContext $context, CodingExecutionResult $result)
 {
     // makes all notices/warning into exceptions, which is good!
     Debug::enable();
     $languageError = null;
     extract($context->getVariables());
     ob_start();
     try {
         require $rootDir . '/' . $entryPointFilename;
     } catch (\ErrorException $e) {
         $message = sprintf('%s in %s on line %s', $e->getMessage(), $e->getFile(), $e->getLine());
         $languageError = $message;
     }
     $contents = ob_get_contents();
     ob_end_clean();
     $result->setOutput($contents);
     $result->setLanguageError(ActivityRunner::cleanError($languageError, $rootDir));
     $result->setDeclaredVariables(get_defined_vars());
 }
 /**
  * Execute the code and modify the CodingExecutionResult
  *
  * @param string $rootDir Where all the files have been placed
  * @param string $entryPointFilename
  * @param CodingContext $context
  * @param CodingExecutionResult $result
  */
 public function executeCode($rootDir, $entryPointFilename, CodingContext $context, CodingExecutionResult $result)
 {
     Debug::enable();
     $errorHandler = TwigErrorHandler::register();
     $twig = $this->getTwigEnvironment($rootDir);
     try {
         $output = $twig->render($entryPointFilename, $context->getVariables());
         $result->setOutput($output);
     } catch (TwigException $error) {
         $result->setLanguageError($error->getMessage());
     } catch (\Twig_Error $error) {
         // not doing anything special here... but in the future, we might
         // fetch more information about line numbers, etc
         $result->setLanguageError($error->getMessage());
     } catch (\Exception $error) {
         $result->setLanguageError($error->getMessage());
     }
     $errorHandler->restore();
 }
 public function setupContext(CodingContext $context)
 {
     $request = $context->fakeHttpRequest('/new_toy.php', 'GET');
     $request->addHeader('User-Agent', 'Mozilla/5.0 (iPad; U; CPU OS 3_2_1 like Mac OS X; en-us) AppleWebKit/531.21.10');
 }
 public function setupContext(CodingContext $context)
 {
     $context->addVariable('whatIWantForXmas', 'Puppy');
 }
 public function setupContext(CodingContext $context)
 {
     $request = $context->fakeHttpRequest('/new_toy.php', 'POST');
     $request->setPOSTData(array('name' => 'Fluffy Pig Stuffed Animal', 'description' => 'Your dog will *love* to chew and destroy this adorable pig!'));
 }
 public function setupContext(CodingContext $context)
 {
     $context->fakeHttpRequest('GET', '/aboutUs.php');
 }
 public function setupContext(CodingContext $context)
 {
     $request = $context->fakeHttpRequest('/new_toy.php', 'GET');
 }