/** * @param \Symfony\Component\DependencyInjection\ContainerInterface $container * @param \Doctrine\Common\Persistence\ManagerRegistry $doctrine * @param \Doctrine\Common\Persistence\ObjectManager $manager * @param \Behat\Behat\Hook\Scope\ScenarioScope $event * @param \Behat\Gherkin\Node\FeatureNode $feature * @param \Behat\Gherkin\Node\ScenarioNode $scenario * @param \Knp\FriendlyContexts\Alice\Loader\Yaml $loader * @param \Doctrine\Common\Persistence\Mapping\ClassMetadataFactory $metadataFactory * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $userMetadata * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $placeMetadata * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $productMetadata */ function let($container, $doctrine, $manager, $event, $loader, $feature, $scenario, $metadataFactory, $userMetadata, $placeMetadata, $productMetadata) { $doctrine->getManager()->willReturn($manager); $feature->getTags()->willReturn(['alice(Place)', 'admin']); $scenario->getTags()->willReturn(['alice(User)']); $event->getFeature()->willReturn($feature); $event->getScenario()->willReturn($scenario); $loader->load('user.yml')->willReturn([]); $loader->load('product.yml')->willReturn([]); $loader->load('place.yml')->willReturn([]); $loader->getCache()->willReturn([]); $loader->clearCache()->willReturn(null); $fixtures = ['User' => 'user.yml', 'Product' => 'product.yml', 'Place' => 'place.yml']; $config = ['alice' => ['fixtures' => $fixtures, 'dependencies' => []]]; $container->has(Argument::any())->willReturn(true); $container->hasParameter(Argument::any())->willReturn(true); $container->get('friendly.alice.loader.yaml')->willReturn($loader); $container->get('doctrine')->willReturn($doctrine); $container->getParameter('friendly.alice.fixtures')->willReturn($fixtures); $container->getParameter('friendly.alice.dependencies')->willReturn([]); $manager->getMetadataFactory()->willReturn($metadataFactory); $metadataFactory->getAllMetadata()->willReturn([$userMetadata, $placeMetadata, $productMetadata]); $userMetadata->getName()->willReturn('User'); $placeMetadata->getName()->willReturn('Place'); $productMetadata->getName()->willReturn('Product'); $this->initialize($config, $container); }
/** * @param FeatureNode $feature * * @return array */ public function extract(FeatureNode $feature) { $allScenarios = []; foreach ($feature->getScenarios() as $scenario) { $scenarios = []; switch (true) { case $scenario instanceof OutlineNode && $this->isParallelExamples($scenario): foreach ($scenario->getExamples() as $exampleNode) { $scenarios[] = new ScenarioInfo($feature->getFile(), $exampleNode->getLine()); } break; case $scenario instanceof OutlineNode: case $scenario instanceof ScenarioInterface: $scenarios[] = new ScenarioInfo($feature->getFile(), $scenario->getLine()); } if ($this->isParallelWait($scenario) || !$this->isParallel($scenario)) { $allScenarios[] = []; } $lastIndex = empty($allScenarios) ? 0 : count($allScenarios) - 1; if (!array_key_exists($lastIndex, $allScenarios)) { $allScenarios[$lastIndex] = []; } $allScenarios[$lastIndex] = array_merge($allScenarios[$lastIndex], $scenarios); if (!$this->isParallel($scenario)) { $allScenarios[] = []; } } return array_values(array_filter($allScenarios)); }
/** * Loads feature from provided feature hash. * * @param array $hash feature hash * @param integer $line feature definition line * * @return Behat\Gherkin\Node\FeatureNode */ protected function loadFeatureHash(array $hash, $line = 0) { $feature = new Node\FeatureNode(null, null, null, isset($hash['line']) ? $hash['line'] : $line); $feature->setKeyword(isset($hash['keyword']) ? $hash['keyword'] : 'Feature'); if (isset($hash['title'])) { $feature->setTitle($hash['title']); } if (isset($hash['description'])) { $feature->setDescription($hash['description']); } if (isset($hash['tags'])) { $feature->setTags($hash['tags']); } if (isset($hash['language'])) { $feature->setLanguage($hash['language']); } if (isset($hash['background'])) { $feature->setBackground($this->loadBackgroundHash($hash['background'])); } if (isset($hash['scenarios'])) { foreach ($hash['scenarios'] as $scenarioIterator => $scenarioHash) { if (isset($scenarioHash['type']) && 'outline' === $scenarioHash['type']) { $feature->addScenario($this->loadOutlineHash($scenarioHash, $scenarioIterator)); } else { $feature->addScenario($this->loadScenarioHash($scenarioHash, $scenarioIterator)); } } } return $feature; }
/** * {@inheritdoc} */ public function isFeatureMatch(FeatureNode $feature) { if ('/' === $this->filterString[0]) { return (bool) preg_match($this->filterString, $feature->getTitle()); } return false !== mb_strpos($feature->getTitle(), $this->filterString); }
/** * @param FeatureNode $featureNode * @param ScenarioInterface $scenarioNode * * @return string */ public function generateFileName(FeatureNode $featureNode, ScenarioInterface $scenarioNode) { $feature = $this->relativizePaths($featureNode->getFile()); $line = $scenarioNode->getLine(); $fileName = join('_', [$feature, $line]); return preg_replace('/[^A-Za-z0-9\\-]/', '_', mb_strtolower($fileName)) . '.png'; }
public function testCacheAndRead() { $feature = new FeatureNode('Some feature', 'some description'); $feature->addScenario(new ScenarioNode('Some scenario')); $this->cache->write('some_feature', $feature); $featureRead = $this->cache->read('some_feature'); $this->assertEquals($feature, $featureRead); }
function it_generates_filename_from_step_name(ScreenshotTaker $screenshotTaker, StepFilenameGenerator $filenameGenerator, Environment $env, FeatureNode $feature, StepNode $step, StepResult $result, Teardown $tearDown) { $event = new AfterStepTested($env->getWrappedObject(), $feature->getWrappedObject(), $step->getWrappedObject(), $result->getWrappedObject(), $tearDown->getWrappedObject()); $result->getResultCode()->willReturn(TestResult::FAILED); $filenameGenerator->convertStepToFileName($step)->willReturn('test.jpg')->shouldBeCalled(); $screenshotTaker->takeScreenshot('test.jpg')->shouldBeCalled(); $this->checkAfterStep($event); }
/** * {@inheritdoc} */ public function test(Environment $env, FeatureNode $feature, $skip) { $background = $feature->getBackground(); if (null === $background) { throw new FeatureHasNoBackgroundException(sprintf('Feature `%s` has no background that could be tested.', $feature->getFile()), $feature); } $results = $this->containerTester->test($env, $feature, $background, $skip); return new TestResults($results); }
/** * update feature results based on step tests results * @param FeatureNode $feature object * @param TestResult $testResult object * @return null */ public function updateFeatureStatus(FeatureNode $feature, TestResult $testResult) { $featurePath = $feature->getFile(); $resultCode = $testResult->getResultCode(); if ($resultCode > $this->testResults[$featurePath]["testResultCode"]) { $this->testResults[$featurePath]["testResultCode"] = $resultCode; $this->testResults[$featurePath]["testResult"] = self::RESULT_CODES_MAPPING[$resultCode]; } }
/** * Checks if Feature matches specified filter. * * @param FeatureNode $feature Feature instance * * @return Boolean */ public function isFeatureMatch(FeatureNode $feature) { foreach ($this->filterPaths as $path) { if (0 === strpos($feature->getFile(), $path)) { return true; } } return false; }
/** * {@inheritdoc} */ public function tearDown(Environment $env, FeatureNode $feature, $skip, TestResult $result) { $event = new BeforeBackgroundTeardown($env, $feature, $feature->getBackground(), $result); $this->eventDispatcher->dispatch(BackgroundTested::BEFORE_TEARDOWN, $event); $teardown = $this->baseTester->tearDown($env, $feature, $skip, $result); $event = new AfterBackgroundTested($env, $feature, $feature->getBackground(), $result, $teardown); $this->eventDispatcher->dispatch(BackgroundTested::AFTER, $event); return $teardown; }
/** * Filters feature according to the filter. * * @param FeatureNode $feature */ public function filterFeature(FeatureNode $feature) { $scenarios = $feature->getScenarios(); foreach ($scenarios as $i => $scenario) { if (!$this->isScenarioMatch($scenario)) { unset($scenarios[$i]); } } $feature->setScenarios($scenarios); }
/** * {@inheritdoc} */ public function test(Environment $env, FeatureNode $feature, Scenario $scenario, $skip = false) { $results = array(); if ($feature->hasBackground()) { $backgroundResult = $this->testBackground($env, $feature, $skip); $skip = !$backgroundResult->isPassed() || $skip; $results[] = $backgroundResult; } $results = array_merge($results, $this->containerTester->test($env, $feature, $scenario, $skip)); return new TestResults($results); }
/** * @param FeatureNode $feature * * @return string */ public function format(FeatureNode $feature) { $shortDesc = $feature->getKeyword() . ': ' . $feature->getTitle() . "\n"; if (!$feature->hasDescription()) { return rtrim($shortDesc); } $longDesc = implode(array_map(function ($descriptionLine) { return $this->indent() . trim($descriptionLine) . "\n"; }, explode("\n", $feature->getDescription()))); return $shortDesc . rtrim($longDesc); }
/** * Filters feature according to the filter. * * @param FeatureNode $feature * * @return FeatureNode */ public function filterFeature(FeatureNode $feature) { $scenarios = array(); foreach ($feature->getScenarios() as $scenario) { if (!$this->isScenarioMatch($feature, $scenario)) { continue; } $scenarios[] = $scenario; } return new FeatureNode($feature->getTitle(), $feature->getDescription(), $feature->getTags(), $feature->getBackground(), $scenarios, $feature->getKeyword(), $feature->getLanguage(), $feature->getFile(), $feature->getLine()); }
public function createFilename(FeatureNode $feature, ScenarioInterface $scenario, OutlineNode $outline = null) { $filename = Transliterator::transliterate($feature->getTitle(), $this->separator) . DIRECTORY_SEPARATOR; if ($outline) { $filename .= Transliterator::transliterate($outline->getTitle(), $this->separator) . DIRECTORY_SEPARATOR . $this->separator; } $filename .= Transliterator::transliterate($scenario->getTitle(), $this->separator); if ($outline) { $filename .= $this->separator; } return $filename; }
function its_printHeader_when_basePath_is_specified_should_output_file_and_line_with_relative_path(Formatter $formatter, FeatureNode $featureNode, Scenario $scenario, OutputPrinter $printer) { $folderName = 'foldername'; $filename = 'file'; $lineNumber = 2; $this->beConstructedWith($folderName); $formatter->getOutputPrinter()->shouldBeCalled()->willReturn($printer); $featureNode->getFile()->shouldBeCalled()->willReturn($folderName . '/' . $filename); $scenario->getLine()->shouldBeCalled()->willReturn($lineNumber); $printer->write("{$filename}:{$lineNumber}")->shouldBeCalled(); $this->printHeader($formatter, $featureNode, $scenario); }
/** * Prints scenario path comment. * * @param Formatter $formatter * @param FeatureNode $feature * @param Scenario $scenario * @param integer $indentation */ public function printScenarioPath(Formatter $formatter, FeatureNode $feature, Scenario $scenario, $indentation) { $printer = $formatter->getOutputPrinter(); if (!$formatter->getParameter('paths')) { $printer->writeln(); return; } $fileAndLine = sprintf('%s:%s', $this->relativizePaths($feature->getFile()), $scenario->getLine()); $headerWidth = $this->widthCalculator->calculateScenarioHeaderWidth($scenario, $indentation); $scenarioWidth = $this->widthCalculator->calculateScenarioWidth($scenario, $indentation, 2); $spacing = str_repeat(' ', max(0, $scenarioWidth - $headerWidth)); $printer->writeln(sprintf('%s {+comment}# %s{-comment}', $spacing, $fileAndLine)); }
/** * {@inheritDoc} */ public function printHeader(Formatter $formatter, FeatureNode $feature) { $stats = $this->statistics->getScenarioStatCounts(); if (0 === count($stats)) { $totalCount = 0; } else { $totalCount = array_sum($stats); } /** @var JUnitOutputPrinter $outputPrinter */ $outputPrinter = $formatter->getOutputPrinter(); $outputPrinter->addTestsuite(array('name' => $feature->getTitle(), 'tests' => $totalCount, 'skipped' => $stats[TestResult::SKIPPED], 'failures' => $stats[TestResult::FAILED], 'errors' => $stats[TestResult::PENDING] + $stats[StepResult::UNDEFINED])); $this->statistics->reset(); }
private function findStepNodes() { $scenarios = $this->featureNode->getScenarios(); if (count($scenarios) === 0) { throw new \InvalidArgumentException('Unable to find any scenarios in dummy feature'); } $scenario = $scenarios[0]; $steps = $scenario->getSteps(); if (count($steps) === 0) { throw new \InvalidArgumentException('Unable to find any steps in dummy feature'); } $this->stepNodes = $steps; }
public function test() { $this->makeContexts(); $description = explode("\n", $this->featureNode->getDescription()); foreach ($description as $line) { $this->getScenario()->runStep(new Comment($line)); } if ($background = $this->featureNode->getBackground()) { foreach ($background->getSteps() as $step) { $this->runStep($step); } } foreach ($this->scenarioNode->getSteps() as $step) { $this->runStep($step); } }
public function testTokens() { $step = new StepNode('When', 'Some "<text>" in <string>'); $scenario = new ScenarioNode(); $scenario->addStep($step); $feature = new FeatureNode(); $feature->addScenario($scenario); $feature->freeze(); $step1 = $step->createExampleRowStep(array('text' => 'change')); $this->assertNotSame($step, $step1); $this->assertEquals('Some "change" in <string>', $step1->getText()); $this->assertEquals('Some "<text>" in <string>', $step1->getCleanText()); $step2 = $step->createExampleRowStep(array('text' => 'change', 'string' => 'browser')); $this->assertNotSame($step, $step2); $this->assertEquals('Some "change" in browser', $step2->getText()); $this->assertEquals('Some "<text>" in <string>', $step2->getCleanText()); }
public function testLoader() { $gherkin = new Gherkin(); $gherkin->addLoader($loader = $this->getLoaderMock()); $gherkin->addFilter($nameFilter = $this->getNameFilterMock()); $gherkin->addFilter($tagFilter = $this->getTagFilterMock()); $feature = new FeatureNode(); $feature->addScenario($scenario = new ScenarioNode()); $loader->expects($this->once())->method('supports')->with($resource = 'some/feature/resource')->will($this->returnValue(true)); $loader->expects($this->once())->method('load')->with($resource)->will($this->returnValue(array($feature))); $nameFilter->expects($this->once())->method('isScenarioMatch')->with($scenario)->will($this->returnValue(true)); $tagFilter->expects($this->once())->method('isScenarioMatch')->with($scenario)->will($this->returnValue(true)); $features = $gherkin->load($resource); $this->assertEquals(1, count($features)); $scenarios = $features[0]->getScenarios(); $this->assertEquals(1, count($scenarios)); $this->assertSame($scenario, $scenarios[0]); }
/** * {@inheritdoc} */ protected function printFeatureDescription(FeatureNode $feature) { $lines = explode("\n", $feature->getDescription()); $this->writeln('<p>'); foreach ($lines as $line) { $this->writeln(htmlspecialchars($line) . "<br />"); } $this->writeln('</p>'); }
/** * Prints feature header. * * @param FeatureNode $feature Current feature node * @return [void] * * @uses printFeatureOrScenarioTags() * @uses printFeatureName() * @uses printFeatureDescription() */ protected function printFeatureHeader(FeatureNode $feature) { $this->writeln('<table style="' . $this->_getTableStyles() . '">'); $this->writeln('<colgroup> <col width="287"> <col width="121"> <col width="157"> <col width="77"> <col width="288"> </colgroup>'); $this->writeln('<tr> <th colspan=5 rowspan=1 style="' . $this->_getTitleStyles() . '"> Pantheon: ' . $feature->getTitle() . '<br>' . date('Y-m-d') . '</th> </tr>'); $this->writeln('<tr>'); $headers = array('Core Experience', 'Status', 'Errors', 'Workflow ID', 'Comments'); foreach ($headers as $header) { $this->writeln('<th style="' . $this->_getHeaderStyles() . '">' . $header . '</th>'); } }
/** * {@inheritdoc} * * @throws AmbiguousMatchException */ public function searchDefinition(Environment $environment, FeatureNode $feature, StepNode $step) { $suite = $environment->getSuite(); $language = $feature->getLanguage(); $stepText = $step->getText(); $multi = $step->getArguments(); $definitions = array(); $result = null; foreach ($this->repository->getEnvironmentDefinitions($environment) as $definition) { $definition = $this->translator->translateDefinition($suite, $definition, $language); if (!($newResult = $this->match($definition, $stepText, $multi))) { continue; } $result = $newResult; $definitions[] = $newResult->getMatchedDefinition(); } if (count($definitions) > 1) { throw new AmbiguousMatchException($result->getMatchedText(), $definitions); } return $result; }
/** * Dumps feature. * * @param FeatureNode $feature Feature instance * * @return string */ public function dumpFeature(FeatureNode $feature) { $language = $feature->getLanguage(); $this->keywords->setLanguage($language); $content = '' . $this->dumpLanguage($language) . ($feature->getTags() ? PHP_EOL . $this->dumpTags($feature->getTags(), 0) : '') . PHP_EOL . $this->dumpKeyword($this->keywords->getFeatureKeywords(), $feature->getTitle(), 0) . PHP_EOL . $this->dumpText($feature->getDescription(), 1); if ($feature->getBackground()) { $content .= $this->dumpBackground($feature->getBackground()); } $scenarios = $feature->getScenarios(); foreach ($scenarios as $scenario) { $content .= PHP_EOL . $this->dumpScenario($scenario); } return $content; }
/** * Filters feature according to the filter. * * @param FeatureNode $feature */ public function filterFeature(FeatureNode $feature) { $scenarios = $feature->getScenarios(); foreach ($scenarios as $i => $scenario) { if (!$this->isScenarioMatch($scenario)) { unset($scenarios[$i]); continue; } if ($scenario instanceof OutlineNode && $scenario->hasExamples()) { $lines = $scenario->getExamples()->getRowLines(); $rows = $scenario->getExamples()->getNumeratedRows(); if (current($lines) <= $this->filterLine && end($lines) >= $this->filterLine) { $scenario->getExamples()->setRows(array()); $scenario->getExamples()->addRow($rows[$lines[0]], $lines[0]); if ($lines[0] !== $this->filterLine) { $scenario->getExamples()->addRow($rows[$this->filterLine], $this->filterLine); } } } } $feature->setScenarios($scenarios); }
/** * Filters feature according to the filter and returns new one. * * @param FeatureNode $feature * * @return FeatureNode */ public function filterFeature(FeatureNode $feature) { $scenarios = array(); foreach ($feature->getScenarios() as $scenario) { if (!$this->isScenarioMatch($scenario)) { continue; } if ($scenario instanceof OutlineNode && $scenario->hasExamples()) { $table = $scenario->getExampleTable()->getTable(); $lines = array_keys($table); if (in_array($this->filterLine, $lines)) { $filteredTable = array($lines[0] => $table[$lines[0]]); if ($lines[0] !== $this->filterLine) { $filteredTable[$this->filterLine] = $table[$this->filterLine]; } $scenario = new OutlineNode($scenario->getTitle(), $scenario->getTags(), $scenario->getSteps(), new ExampleTableNode($filteredTable, $scenario->getExampleTable()->getKeyword()), $scenario->getKeyword(), $scenario->getLine()); } } $scenarios[] = $scenario; } return new FeatureNode($feature->getTitle(), $feature->getDescription(), $feature->getTags(), $feature->getBackground(), $scenarios, $feature->getKeyword(), $feature->getLanguage(), $feature->getFile(), $feature->getLine()); }
public function testLoader() { $customFilter1 = $this->getCustomFilterMock(); $customFilter2 = $this->getCustomFilterMock(); $gherkin = new Gherkin(); $gherkin->addLoader($loader = $this->getLoaderMock()); $gherkin->addFilter($nameFilter = $this->getNameFilterMock()); $gherkin->addFilter($tagFilter = $this->getTagFilterMock()); $feature = new FeatureNode(); $feature->addScenario($scenario = new ScenarioNode()); $loader->expects($this->once())->method('supports')->with($resource = 'some/feature/resource')->will($this->returnValue(true)); $loader->expects($this->once())->method('load')->with($resource)->will($this->returnValue(array($feature))); $nameFilter->expects($this->once())->method('filterFeature')->with($this->identicalTo($feature)); $tagFilter->expects($this->once())->method('filterFeature')->with($this->identicalTo($feature)); $customFilter1->expects($this->once())->method('filterFeature')->with($this->identicalTo($feature)); $customFilter2->expects($this->once())->method('filterFeature')->with($this->identicalTo($feature)); $features = $gherkin->load($resource, array($customFilter1, $customFilter2)); $this->assertEquals(1, count($features)); $this->assertTrue($feature->isFrozen()); $scenarios = $features[0]->getScenarios(); $this->assertEquals(1, count($scenarios)); $this->assertSame($scenario, $scenarios[0]); }