public function run(\PHPSpec\Runner\Reporter $reporter) { if ($reporter instanceof Reporter && $reporter->checkFailFast()) { return; } if ($this->testTargetRepository->testsOnlySpecifiedMethods() && !$this->testTargetRepository->shouldTreatElementAsTest(get_class($this->getExampleGroup()), $this->getMethodName())) { return; } $reporter->exampleStarted($this); parent::run($reporter); $reporter->exampleFinished($this); }
/** * @param \PHPUnit_Framework_Test $test * @param \Exception $e * @param float $time * @param string $failureOrError */ protected function writeFailureOrError(\PHPUnit_Framework_Test $test, \Exception $e, $time, $failureOrError) { $testIsArtificial = false; if (!$this->testStarted) { $this->startTest($test); $testIsArtificial = true; } if ($test instanceof \PHPUnit_Framework_SelfDescribing) { $message = $test->toString() . "\n\n"; } else { $message = ''; } $message .= \PHPUnit_Framework_TestFailure::exceptionToString($e) . "\n"; if ($test instanceof \PHPUnit_Framework_Warning) { $testClass = new \ReflectionClass($this->currentTestClassName); $file = $testClass->getFileName(); $line = 1; } elseif ($test instanceof \PHPUnit_Framework_WarningTestCase && preg_match('/^No tests found in class "([a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*)"\\.$/', $test->getMessage(), $matches)) { $testClass = new \ReflectionClass($this->currentTestClassName); $file = $testClass->getFileName(); $line = 1; } else { list($file, $line) = FailureTrace::findFileAndLineOfFailureOrError($this->testTargetRepository->getRequiredSuperTypes(), $e, new \ReflectionClass($test)); } $trace = \PHPUnit_Util_Filter::getFilteredStacktrace($e, true); $this->junitXMLWriter->{'write' . $failureOrError}($message . $trace, get_class($e), $file, $line, $message); if ($testIsArtificial) { $this->endTest($test, 0); } }
/** * @param \ReflectionClass $exampleGroupClass */ public function addExampleGroup(\ReflectionClass $exampleGroupClass) { if ($this->testTargetRepository->testsOnlySpecifiedClasses()) { if (!$this->testTargetRepository->shouldTreatElementAsTest($exampleGroupClass->getName())) { return; } } $examples = array(); foreach ($exampleGroupClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $exampleMethod) { /* @var $exampleMethod \ReflectionMethod */ if (strtolower(substr($exampleMethod->getName(), 0, 2)) == 'it') { if ($this->testTargetRepository->testsOnlySpecifiedMethods()) { if (!$this->testTargetRepository->shouldTreatElementAsTest($exampleGroupClass->getName(), $exampleMethod->getName())) { continue; } } $examples[] = $exampleMethod; } } if (count($examples) > 0) { $exampleGroup = $exampleGroupClass->newInstance(); $this->exampleGroups[] = $exampleGroup; $this->examplesByGroup[get_class($exampleGroup)] = $examples; } }
/** * @param \SimpleTestCase $testCase * @return integer * @since Method available since Release 2.11.1 */ public function countTestsInTestCase(\SimpleTestCase $testCase) { $tests = $this->getTestsInTestCase($testCase); $testCount = 0; if ($this->testTargetRepository->testsOnlySpecifiedMethods()) { foreach ($tests as $method) { if ($this->testTargetRepository->shouldTreatElementAsTest(get_class($testCase), $method)) { ++$testCount; } } } elseif ($this->testTargetRepository->testsOnlySpecifiedClasses()) { if ($this->testTargetRepository->shouldTreatElementAsTest(get_class($testCase))) { $testCount = count($tests); } } else { $testCount = count($tests); } return $testCount; }
/** * Collects all test cases included in the given file. * * @param string $file */ public function collectTestCasesFromFile($file) { if (!$this->testTargetRepository->shouldTreatFileAsTest($file)) { return; } foreach ($this->findNewClasses($file) as $newClass) { $collectingTypeFactory = $this->collectingTypeFactory->create($newClass, $this->testTargetRepository->getRequiredSuperTypes()); if ($collectingTypeFactory->isTest()) { $this->collectTestCase($newClass); } } }
/** * {@inheritdoc} */ protected function handleArguments(array $argv) { parent::handleArguments($argv); if (array_key_exists('testSuffixes', $this->arguments)) { $fileSystem = new FileSystem(); $filePatterns = array(); foreach ($this->arguments['testSuffixes'] as $testSuffix) { $filePatterns[] = preg_quote($testSuffix) . '$'; } $this->testTargetRepository->setResources(array(empty($this->arguments['testFile']) ? $this->arguments['test'] : $this->arguments['testFile'])); $this->testTargetRepository->setFilePattern(implode('|', $filePatterns)); } }
/** * @return array * * @throws \UnexpectedValueException */ protected function getMonitoringDirectories() { $watchDirs = array(); foreach (array_merge($this->watchDirs, $this->testTargetRepository->getResources()) as $directory) { if (!$this->legacyProxy->is_dir($directory)) { throw new \UnexpectedValueException(sprintf('A specified path [ %s ] is not found or not a directory.', $directory)); } $directory = $this->legacyProxy->realpath($directory); if ($directory === false) { throw new \UnexpectedValueException(sprintf('Cannnot get the absolute path of a specified directory [ %s ]. Make sure all elements of the absolute path have valid permissions.', $directory)); } if (!in_array($directory, $watchDirs)) { $watchDirs[] = $directory; } } return $watchDirs; }
/** * @param string $command * * @return array */ protected function buildOptions($command) { $options = array(); if (basename(trim($command, '\'"')) != 'testrunner') { $configFile = $this->getPHPConfigDir(); if ($configFile !== false) { $options[] = '-c'; $options[] = escapeshellarg($configFile); } $options[] = escapeshellarg($_SERVER['argv'][0]); } if ($this->terminal->shouldColor()) { $options[] = '--ansi'; } $options[] = escapeshellarg(strtolower($this->plugin->getPluginID())); if (!is_null($this->environment->getPreloadScript())) { $options[] = '-p ' . escapeshellarg($this->environment->getPreloadScript()); } $options[] = '-R'; if ($this->runner->shouldNotify()) { $options[] = '-m'; } if ($this->runner->shouldStopOnFailure()) { $options[] = '--stop-on-failure'; } if (!$this->testTargetRepository->isDefaultFilePattern()) { $options[] = '--test-file-pattern=' . escapeshellarg($this->testTargetRepository->getFilePattern()); } if ($this->runner->hasDetailedProgress()) { $options[] = '--detailed-progress'; } if (!is_null($this->commandLineOptionBuilder)) { $options = $this->commandLineOptionBuilder->build($options); } $this->testTargetRepository->walkOnResources(function ($resource, $index, TestTargetRepository $testTargetRepository) use(&$options) { $options[] = escapeshellarg($resource); }); return $options; }
/** * @return array */ protected function buildRunnerOptions() { $options = array(); if (basename(trim($this->runnerCommand, '\'"')) != 'testrunner') { $configFile = $this->getPHPConfigDir(); if ($configFile !== false) { $options[] = '-c'; $options[] = escapeshellarg($configFile); } $options[] = escapeshellarg($_SERVER['argv'][0]); } if ($this->terminal->shouldColor()) { $options[] = '--ansi'; } $options[] = escapeshellarg(strtolower(ApplicationContext::getInstance()->getPlugin()->getPluginID())); if (!is_null(ApplicationContext::getInstance()->getEnvironment()->getPreloadScript())) { $options[] = '-p ' . escapeshellarg(ApplicationContext::getInstance()->getEnvironment()->getPreloadScript()); } $options[] = '-R'; if ($this->runnerFactory->create()->shouldNotify()) { $options[] = '-m'; } if ($this->runnerFactory->create()->shouldStopOnFailure()) { $options[] = '--stop-on-failure'; } if (!$this->testTargetRepository->isDefaultFilePattern()) { $options[] = '--test-file-pattern=' . escapeshellarg($this->testTargetRepository->getFilePattern()); } if ($this->runnerFactory->create()->hasDetailedProgress()) { $options[] = '--detailed-progress'; } $options = array_merge($options, $this->doBuildRunnerOptions()); $this->testTargetRepository->walkOnResources(function ($resource, $index, TestTargetRepository $testTargetRepository) use(&$options) { $options[] = escapeshellarg($resource); }); return $options; }
/** * @param string $testCase * @param string $method * @return boolean */ public function shouldInvoke($testCase, $method) { return $this->testTargetRepository->shouldTreatElementAsTest($testCase); }
/** * @param \PHPSpec\Runner\ReporterEvent $reporterEvent * @param string $failureOrError */ protected function renderFailureOrError(ReporterEvent $reporterEvent, $failureOrError) { list($file, $line) = FailureTrace::findFileAndLineOfFailureOrError($this->testTargetRepository->getRequiredSuperTypes(), $reporterEvent->exception, new \ReflectionClass($this->testSuite->getExampleGroupClass($this->currentExampleGroupName))); $failureTrace = FailureTrace::buildFailureTrace($reporterEvent->exception->getTrace()); $this->junitXMLWriter->{'write' . $failureOrError}($reporterEvent->message . PHP_EOL . PHP_EOL . $failureTrace, get_class($reporterEvent->exception), $file, $line, $reporterEvent->message); }