factory() public static method

public static factory ( ) : PHPUnit_Util_PHP
return PHPUnit_Util_PHP
Esempio n. 1
0
 /**
  * @param array $requestArguments
  * @param bool $failOnFailure
  * @return Response
  */
 protected function fetchFrontendResponse(array $requestArguments, $failOnFailure = true)
 {
     if (!empty($requestArguments['url'])) {
         $requestUrl = '/' . ltrim($requestArguments['url'], '/');
     } else {
         $requestUrl = '/?' . GeneralUtility::implodeArrayForUrl('', $requestArguments);
     }
     if (property_exists($this, 'instancePath')) {
         $instancePath = $this->instancePath;
     } else {
         $instancePath = ORIGINAL_ROOT . 'typo3temp/functional-' . substr(sha1(get_class($this)), 0, 7);
     }
     $arguments = array('documentRoot' => $instancePath, 'requestUrl' => 'http://localhost' . $requestUrl);
     $template = new \Text_Template(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/Functional/Fixtures/Frontend/request.tpl');
     $template->setVar(array('arguments' => var_export($arguments, true), 'originalRoot' => ORIGINAL_ROOT));
     $php = \PHPUnit_Util_PHP::factory();
     $response = $php->runJob($template->render());
     $result = json_decode($response['stdout'], true);
     if ($result === null) {
         $this->fail('Frontend Response is empty');
     }
     if ($failOnFailure && $result['status'] === Response::STATUS_Failure) {
         $this->fail('Frontend Response has failure:' . LF . $result['error']);
     }
     $response = new Response($result['status'], $result['content'], $result['error']);
     return $response;
 }
Esempio n. 2
0
 /**
  * Constructs a test case with the given filename.
  *
  * @param string           $filename
  * @param PHPUnit_Util_PHP $phpUtil
  *
  * @throws PHPUnit_Framework_Exception
  */
 public function __construct(string $filename, $phpUtil = null)
 {
     if (!is_file($filename)) {
         throw new PHPUnit_Framework_Exception(sprintf('File "%s" does not exist.', $filename));
     }
     $this->filename = $filename;
     $this->phpUtil = $phpUtil ?: PHPUnit_Util_PHP::factory();
 }
Esempio n. 3
0
 /**
  * Runs a test and collects its result in a TestResult instance.
  *
  * @param  PHPUnit_Framework_TestResult $result
  * @return PHPUnit_Framework_TestResult
  */
 public function run(PHPUnit_Framework_TestResult $result = null)
 {
     $sections = $this->parse();
     $code = $this->render($sections['FILE']);
     if ($result === null) {
         $result = new PHPUnit_Framework_TestResult();
     }
     $php = PHPUnit_Util_PHP::factory();
     $skip = false;
     $time = 0;
     $settings = $this->settings;
     $result->startTest($this);
     if (isset($sections['INI'])) {
         $settings = array_merge($settings, $this->parseIniSection($sections['INI']));
     }
     if (isset($sections['SKIPIF'])) {
         $jobResult = $php->runJob($sections['SKIPIF'], $settings);
         if (!strncasecmp('skip', ltrim($jobResult['stdout']), 4)) {
             if (preg_match('/^\\s*skip\\s*(.+)\\s*/i', $jobResult['stdout'], $message)) {
                 $message = substr($message[1], 2);
             } else {
                 $message = '';
             }
             $result->addFailure($this, new PHPUnit_Framework_SkippedTestError($message), 0);
             $skip = true;
         }
     }
     if (!$skip) {
         PHP_Timer::start();
         $jobResult = $php->runJob($code, $settings);
         $time = PHP_Timer::stop();
         if (isset($sections['EXPECT'])) {
             $assertion = 'assertEquals';
             $expected = $sections['EXPECT'];
         } else {
             $assertion = 'assertStringMatchesFormat';
             $expected = $sections['EXPECTF'];
         }
         $output = preg_replace('/\\r\\n/', "\n", trim($jobResult['stdout']));
         $expected = preg_replace('/\\r\\n/', "\n", trim($expected));
         try {
             PHPUnit_Framework_Assert::$assertion($expected, $output);
         } catch (PHPUnit_Framework_AssertionFailedError $e) {
             $result->addFailure($this, $e, $time);
         } catch (Throwable $t) {
             $result->addError($this, $t, $time);
         } catch (Exception $e) {
             $result->addError($this, $e, $time);
         }
     }
     $result->endTest($this, $time);
     return $result;
 }
Esempio n. 4
0
 /**
  * @dataProvider loadTestCases
  */
 public function testCases($sections)
 {
     $php = PHPUnit_Util_PHP::factory();
     if (isset($sections['SKIPIF'])) {
         $jobResult = $php->runJob($sections['SKIPIF'], $this->settings);
         if (!strncasecmp('skip', ltrim($jobResult['stdout']), 4)) {
             if (preg_match('/^\\s*skip\\s*(.+)\\s*/i', $jobResult['stdout'], $message)) {
                 $message = substr($message[1], 2);
             } else {
                 $message = '';
             }
             self::markTestSkipped($message);
         }
     }
     $code = strtr($sections['FILE'], array('__DIR__' => $sections['DIRNAME'], '__FILE__' => $sections['FILENAME']));
     $jobResult = $php->runJob($code, $this->settings);
     $output = preg_replace('/\\r\\n/', "\n", trim($jobResult['stdout']));
     if (isset($sections['EXPECT'])) {
         self::assertEquals(preg_replace('/\\r\\n/', "\n", trim($sections['EXPECT'])), $output);
     } else {
         self::assertStringMatchesFormat(preg_replace('/\\r\\n/', "\n", trim($sections['EXPECTF'])), $output);
     }
 }
Esempio n. 5
0
 /**
  * Runs the test case and collects the results in a TestResult object.
  * If no TestResult object is passed a new one will be created.
  *
  * @param  PHPUnit_Framework_TestResult $result
  * @return PHPUnit_Framework_TestResult
  * @throws PHPUnit_Framework_Exception
  */
 public function run(PHPUnit_Framework_TestResult $result = null)
 {
     if ($result === null) {
         $result = $this->createResult();
     }
     if (!$this instanceof PHPUnit_Framework_Warning) {
         $this->setTestResultObject($result);
         $this->setUseErrorHandlerFromAnnotation();
     }
     if ($this->useErrorHandler !== null) {
         $oldErrorHandlerSetting = $result->getConvertErrorsToExceptions();
         $result->convertErrorsToExceptions($this->useErrorHandler);
     }
     if (!$this instanceof PHPUnit_Framework_Warning && !$this->handleDependencies()) {
         return;
     }
     if ($this->runTestInSeparateProcess === true && $this->inIsolation !== true && !$this instanceof PHPUnit_Extensions_SeleniumTestCase && !$this instanceof PHPUnit_Extensions_PhptTestCase) {
         $class = new ReflectionClass($this);
         $template = new Text_Template(__DIR__ . '/../Util/PHP/Template/TestCaseMethod.tpl');
         if ($this->preserveGlobalState) {
             $constants = PHPUnit_Util_GlobalState::getConstantsAsString();
             $globals = PHPUnit_Util_GlobalState::getGlobalsAsString();
             $includedFiles = PHPUnit_Util_GlobalState::getIncludedFilesAsString();
             $iniSettings = PHPUnit_Util_GlobalState::getIniSettingsAsString();
         } else {
             $constants = '';
             if (!empty($GLOBALS['__PHPUNIT_BOOTSTRAP'])) {
                 $globals = '$GLOBALS[\'__PHPUNIT_BOOTSTRAP\'] = ' . var_export($GLOBALS['__PHPUNIT_BOOTSTRAP'], true) . ";\n";
             } else {
                 $globals = '';
             }
             $includedFiles = '';
             $iniSettings = '';
         }
         $coverage = $result->getCollectCodeCoverageInformation() ? 'true' : 'false';
         $isStrictAboutTestsThatDoNotTestAnything = $result->isStrictAboutTestsThatDoNotTestAnything() ? 'true' : 'false';
         $isStrictAboutOutputDuringTests = $result->isStrictAboutOutputDuringTests() ? 'true' : 'false';
         $isStrictAboutTestSize = $result->isStrictAboutTestSize() ? 'true' : 'false';
         $isStrictAboutTodoAnnotatedTests = $result->isStrictAboutTodoAnnotatedTests() ? 'true' : 'false';
         if (defined('PHPUNIT_COMPOSER_INSTALL')) {
             $composerAutoload = var_export(PHPUNIT_COMPOSER_INSTALL, true);
         } else {
             $composerAutoload = '\'\'';
         }
         if (defined('__PHPUNIT_PHAR__')) {
             $phar = var_export(__PHPUNIT_PHAR__, true);
         } else {
             $phar = '\'\'';
         }
         $data = var_export(serialize($this->data), true);
         $dataName = var_export($this->dataName, true);
         $dependencyInput = var_export(serialize($this->dependencyInput), true);
         $includePath = var_export(get_include_path(), true);
         // must do these fixes because TestCaseMethod.tpl has unserialize('{data}') in it, and we can't break BC
         // the lines above used to use addcslashes() rather than var_export(), which breaks null byte escape sequences
         $data = "'." . $data . ".'";
         $dataName = "'.(" . $dataName . ").'";
         $dependencyInput = "'." . $dependencyInput . ".'";
         $includePath = "'." . $includePath . ".'";
         $template->setVar(array('composerAutoload' => $composerAutoload, 'phar' => $phar, 'filename' => $class->getFileName(), 'className' => $class->getName(), 'methodName' => $this->name, 'collectCodeCoverageInformation' => $coverage, 'data' => $data, 'dataName' => $dataName, 'dependencyInput' => $dependencyInput, 'constants' => $constants, 'globals' => $globals, 'include_path' => $includePath, 'included_files' => $includedFiles, 'iniSettings' => $iniSettings, 'isStrictAboutTestsThatDoNotTestAnything' => $isStrictAboutTestsThatDoNotTestAnything, 'isStrictAboutOutputDuringTests' => $isStrictAboutOutputDuringTests, 'isStrictAboutTestSize' => $isStrictAboutTestSize, 'isStrictAboutTodoAnnotatedTests' => $isStrictAboutTodoAnnotatedTests));
         $this->prepareTemplate($template);
         $php = PHPUnit_Util_PHP::factory();
         $php->runTestJob($template->render(), $this, $result);
     } else {
         $result->run($this);
     }
     if ($this->useErrorHandler !== null) {
         $result->convertErrorsToExceptions($oldErrorHandlerSetting);
     }
     $this->result = null;
     return $result;
 }
Esempio n. 6
0
 /**
  * @param int $pageId
  * @param int $languageId
  * @param int $backendUserId
  * @param int $workspaceId
  * @param bool $failOnFailure
  * @return Response
  */
 protected function getFrontendResponse($pageId, $languageId = 0, $backendUserId = 0, $workspaceId = 0, $failOnFailure = TRUE)
 {
     $pageId = (int) $pageId;
     $languageId = (int) $languageId;
     $additionalParameter = '';
     if (!empty($backendUserId)) {
         $additionalParameter .= '&backendUserId=' . (int) $backendUserId;
     }
     if (!empty($workspaceId)) {
         $additionalParameter .= '&workspaceId=' . (int) $workspaceId;
     }
     $arguments = array('documentRoot' => $this->instancePath, 'requestUrl' => 'http://localhost/?id=' . $pageId . '&L=' . $languageId . $additionalParameter);
     $template = new \Text_Template(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/Functional/Fixtures/Frontend/request.tpl');
     $template->setVar(array('arguments' => var_export($arguments, TRUE), 'originalRoot' => ORIGINAL_ROOT));
     $php = \PHPUnit_Util_PHP::factory();
     $response = $php->runJob($template->render());
     $result = json_decode($response['stdout'], TRUE);
     if ($result === FALSE) {
         $this->fail('Frontend Response is empty');
     }
     if ($failOnFailure && $result['status'] === Response::STATUS_Failure) {
         $this->fail('Frontend Response has failure:' . LF . $result['error']);
     }
     $response = new Response($result['status'], $result['content'], $result['error']);
     return $response;
 }
Esempio n. 7
0
 /**
  * Runs the test case and collects the results in a TestResult object.
  * If no TestResult object is passed a new one will be created.
  *
  * @param  PHPUnit_Framework_TestResult $result
  * @return PHPUnit_Framework_TestResult
  * @throws PHPUnit_Framework_Exception
  */
 public function run(PHPUnit_Framework_TestResult $result = NULL)
 {
     if ($result === NULL) {
         $result = $this->createResult();
     }
     if (!$this instanceof PHPUnit_Framework_Warning) {
         $this->setTestResultObject($result);
         $this->setUseErrorHandlerFromAnnotation();
         $this->setUseOutputBufferingFromAnnotation();
     }
     if ($this->useErrorHandler !== NULL) {
         $oldErrorHandlerSetting = $result->getConvertErrorsToExceptions();
         $result->convertErrorsToExceptions($this->useErrorHandler);
     }
     if (!$this->handleDependencies()) {
         return;
     }
     if ($this->runTestInSeparateProcess === TRUE && $this->inIsolation !== TRUE && !$this instanceof PHPUnit_Extensions_SeleniumTestCase && !$this instanceof PHPUnit_Extensions_PhptTestCase) {
         $class = new ReflectionClass($this);
         $template = new Text_Template(sprintf('%s%sProcess%sTestCaseMethod.tpl', __DIR__, DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR));
         if ($this->preserveGlobalState) {
             $constants = PHPUnit_Util_GlobalState::getConstantsAsString();
             $globals = PHPUnit_Util_GlobalState::getGlobalsAsString();
             $includedFiles = PHPUnit_Util_GlobalState::getIncludedFilesAsString();
         } else {
             $constants = '';
             $globals = '';
             $includedFiles = '';
         }
         if ($result->getCollectCodeCoverageInformation()) {
             $coverage = 'TRUE';
         } else {
             $coverage = 'FALSE';
         }
         if ($result->isStrict()) {
             $strict = 'TRUE';
         } else {
             $strict = 'FALSE';
         }
         $data = var_export(serialize($this->data), TRUE);
         $dependencyInput = var_export(serialize($this->dependencyInput), TRUE);
         $includePath = var_export(get_include_path(), TRUE);
         // must do these fixes because TestCaseMethod.tpl has unserialize('{data}') in it, and we can't break BC
         // the lines above used to use addcslashes() rather than var_export(), which breaks null byte escape sequences
         $data = "'." . $data . ".'";
         $dependencyInput = "'." . $dependencyInput . ".'";
         $includePath = "'." . $includePath . ".'";
         $template->setVar(array('filename' => $class->getFileName(), 'className' => $class->getName(), 'methodName' => $this->name, 'collectCodeCoverageInformation' => $coverage, 'data' => $data, 'dataName' => $this->dataName, 'dependencyInput' => $dependencyInput, 'constants' => $constants, 'globals' => $globals, 'include_path' => $includePath, 'included_files' => $includedFiles, 'strict' => $strict));
         $this->prepareTemplate($template);
         $php = PHPUnit_Util_PHP::factory();
         $php->runJob($template->render(), $this, $result);
     } else {
         $result->run($this);
     }
     if ($this->useErrorHandler !== NULL) {
         $result->convertErrorsToExceptions($oldErrorHandlerSetting);
     }
     $this->result = NULL;
     return $result;
 }
Esempio n. 8
0
 /**
  * Runs the test case and collects the results in a TestResult object.
  * If no TestResult object is passed a new one will be created.
  *
  * @param  PHPUnit_Framework_TestResult $result
  * @return PHPUnit_Framework_TestResult
  * @throws InvalidArgumentException
  */
 public function run(PHPUnit_Framework_TestResult $result = NULL)
 {
     if ($result === NULL) {
         $result = $this->createResult();
     }
     $this->setTestResultObject($result);
     $this->setUseErrorHandlerFromAnnotation();
     $this->setUseOutputBufferingFromAnnotation();
     if ($this->useErrorHandler !== NULL) {
         $oldErrorHandlerSetting = $result->getConvertErrorsToExceptions();
         $result->convertErrorsToExceptions($this->useErrorHandler);
     }
     if (!$this->handleDependencies()) {
         return;
     }
     if ($this->runTestInSeparateProcess === TRUE && $this->inIsolation !== TRUE && !$this instanceof PHPUnit_Extensions_SeleniumTestCase && !$this instanceof PHPUnit_Extensions_PhptTestCase) {
         $class = new ReflectionClass($this);
         $template = new Text_Template(sprintf('%s%sProcess%sTestCaseMethod.tpl', dirname(__FILE__), DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR));
         if ($this->preserveGlobalState) {
             $constants = PHPUnit_Util_GlobalState::getConstantsAsString();
             $globals = PHPUnit_Util_GlobalState::getGlobalsAsString();
             $includedFiles = PHPUnit_Util_GlobalState::getIncludedFilesAsString();
         } else {
             $constants = '';
             $globals = '';
             $includedFiles = '';
         }
         if ($result->getCollectCodeCoverageInformation()) {
             $coverage = 'TRUE';
         } else {
             $coverage = 'FALSE';
         }
         if ($result->isStrict()) {
             $strict = 'TRUE';
         } else {
             $strict = 'FALSE';
         }
         $data = addcslashes(serialize($this->data), "'");
         $dependencyInput = addcslashes(serialize($this->dependencyInput), "'");
         $includePath = addslashes(get_include_path());
         $template->setVar(array('filename' => $class->getFileName(), 'className' => $class->getName(), 'methodName' => $this->name, 'collectCodeCoverageInformation' => $coverage, 'data' => $data, 'dataName' => $this->dataName, 'dependencyInput' => $dependencyInput, 'constants' => $constants, 'globals' => $globals, 'include_path' => $includePath, 'included_files' => $includedFiles, 'strict' => $strict));
         $this->prepareTemplate($template);
         $php = PHPUnit_Util_PHP::factory();
         $php->runJob($template->render(), $this, $result);
     } else {
         $result->run($this);
     }
     if ($this->useErrorHandler !== NULL) {
         $result->convertErrorsToExceptions($oldErrorHandlerSetting);
     }
     $this->result = NULL;
     return $result;
 }
 /**
  * @return \ZephirTestCase\CodeRunner
  */
 public static function getInstance()
 {
     return new CodeRunner(ZephirExtensionBuilderFactory::getInstance(), \PHPUnit_Util_PHP::factory());
 }