Skip to content

Commit 5088ee4

Browse files
esteban-serfebaev
authored andcommitted
basic cept test format support (fixes #30, via #31)
1 parent fbdb300 commit 5088ee4

File tree

1 file changed

+109
-18
lines changed

1 file changed

+109
-18
lines changed

src/Yandex/Allure/Adapter/AllureAdapter.php

Lines changed: 109 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Codeception\Event\SuiteEvent;
88
use Codeception\Event\TestEvent;
99
use Codeception\Event\FailEvent;
10-
use Codeception\Test\Cest;
1110
use Codeception\Events;
1211
use Codeception\Platform\Extension;
1312
use Codeception\Exception\ConfigurationException;
@@ -29,7 +28,6 @@
2928

3029
const OUTPUT_DIRECTORY_PARAMETER = 'outputDirectory';
3130
const DELETE_PREVIOUS_RESULTS_PARAMETER = 'deletePreviousResults';
32-
const IGNORED_ANNOTATION_PARAMETER = 'ignoredAnnotations';
3331
const DEFAULT_RESULTS_DIRECTORY = 'allure-results';
3432
const DEFAULT_REPORT_DIRECTORY = 'allure-report';
3533
const INITIALIZED_PARAMETER = '_initialized';
@@ -74,16 +72,16 @@ class AllureAdapter extends Extension
7472

7573
/**
7674
* Extra annotations to ignore in addition to standard PHPUnit annotations.
75+
*
76+
* @param array $ignoredAnnotations
7777
*/
78-
public function _initialize()
78+
public function _initialize(array $ignoredAnnotations = [])
7979
{
8080
parent::_initialize();
8181
Annotation\AnnotationProvider::registerAnnotationNamespaces();
8282
// Add standard PHPUnit annotations
8383
Annotation\AnnotationProvider::addIgnoredAnnotations($this->ignoredAnnotations);
8484
// Add custom ignored annotations
85-
$ignoredAnnotations =
86-
$this->tryGetOption(IGNORED_ANNOTATION_PARAMETER, []);
8785
Annotation\AnnotationProvider::addIgnoredAnnotations($ignoredAnnotations);
8886
$outputDirectory = $this->getOutputDirectory();
8987
$deletePreviousResults =
@@ -223,29 +221,43 @@ public function testBefore(TestEvent $testEvent)
223221
{
224222
$test = $testEvent->getTest();
225223
$testName = $test->getName();
226-
$testClass = $test instanceof Cest
227-
? get_class($test->getTestClass())
228-
: get_class($test);
229224
$event = new TestCaseStartedEvent($this->uuid, $testName);
230-
if (class_exists($testClass, false)) {
231-
$annotationManager = new Annotation\AnnotationManager(Annotation\AnnotationProvider::getClassAnnotations($testClass));
232-
$annotationManager->updateTestCaseEvent($event);
225+
if ($test instanceof \Codeception\Test\Cest) {
226+
$testClass = get_class($test->getTestClass());
227+
if (class_exists($testClass, false)) {
228+
$annotationManager = new Annotation\AnnotationManager(Annotation\AnnotationProvider::getClassAnnotations($testClass));
229+
$annotationManager->updateTestCaseEvent($event);
230+
}
231+
} else if ($test instanceof \Codeception\Test\Cept) {
232+
$annotations = $this->getCeptAnnotations($test);
233+
if (count($annotations) > 0) {
234+
$annotationManager = new Annotation\AnnotationManager($annotations);
235+
$annotationManager->updateTestCaseEvent($event);
236+
}
233237
}
238+
234239
$this->getLifecycle()->fire($event);
235240
}
236241

237242
public function testStart(TestEvent $testEvent)
238243
{
239244
$test = $testEvent->getTest();
240-
$testName = $test->getName(false);
241-
$className = $test instanceof Cest
242-
? get_class($test->getTestClass())
243-
: get_class($test);
245+
$testName = $test->getName();
244246
$event = new TestCaseStartedEvent($this->uuid, $testName);
245-
if (method_exists($className, $testName)){
246-
$annotationManager = new Annotation\AnnotationManager(Annotation\AnnotationProvider::getMethodAnnotations($className, $testName));
247-
$annotationManager->updateTestCaseEvent($event);
247+
if ($test instanceof \Codeception\Test\Cest) {
248+
$className = get_class($test->getTestClass());
249+
if (method_exists($className, $testName)){
250+
$annotationManager = new Annotation\AnnotationManager(Annotation\AnnotationProvider::getMethodAnnotations($className, $testName));
251+
$annotationManager->updateTestCaseEvent($event);
252+
}
253+
} else if ($test instanceof \Codeception\Test\Cept) {
254+
$annotations = $this->getCeptAnnotations($test);
255+
if (count($annotations) > 0) {
256+
$annotationManager = new Annotation\AnnotationManager($annotations);
257+
$annotationManager->updateTestCaseEvent($event);
258+
}
248259
}
260+
249261
$this->getLifecycle()->fire($event);
250262
}
251263

@@ -326,4 +338,83 @@ public function setLifecycle(Allure $lifecycle)
326338
$this->lifecycle = $lifecycle;
327339
}
328340

341+
/**
342+
*
343+
* @param \Codeception\TestInterface $test
344+
* @return array
345+
*/
346+
private function getCeptAnnotations($test)
347+
{
348+
$tokens = token_get_all($test->getSourceCode());
349+
$comments = array();
350+
$annotations = [];
351+
foreach($tokens as $token) {
352+
if($token[0] == T_DOC_COMMENT || $token[0] == T_COMMENT) {
353+
$comments[] = $token[1];
354+
}
355+
}
356+
foreach($comments as $comment) {
357+
$lines = preg_split ('/$\R?^/m', $comment);
358+
foreach($lines as $line) {
359+
$output = [];
360+
if (preg_match('/\*\s\@(.*)\((.*)\)/', $line, $output) > 0) {
361+
\Codeception\Util\Debug::debug($output);
362+
if ($output[1] == "Features") {
363+
$feature = new \Yandex\Allure\Adapter\Annotation\Features();
364+
$features = $this->splitAnnotationContent($output[2]);
365+
foreach($features as $featureName) {
366+
$feature->featureNames[] = $featureName;
367+
}
368+
$annotations[get_class($feature)] = $feature;
369+
} else if ($output[1] == 'Title') {
370+
$title = new \Yandex\Allure\Adapter\Annotation\Title();
371+
$title_content = str_replace('"', '', $output[2]);
372+
$title->value = $title_content;
373+
$annotations[get_class($title)] = $title;
374+
} else if ($output[1] == 'Description') {
375+
$description = new \Yandex\Allure\Adapter\Annotation\Description();
376+
$description_content = str_replace('"', '', $output[2]);
377+
$description->value = $description_content;
378+
$annotations[get_class($description)] = $description;
379+
} else if ($output[1] == 'Stories') {
380+
$stories = $this->splitAnnotationContent($output[2]);
381+
$story = new \Yandex\Allure\Adapter\Annotation\Stories();
382+
foreach($stories as $storyName) {
383+
$story->stories[] = $storyName;
384+
}
385+
$annotations[get_class($story)] = $story;
386+
} else if ($output[1] == 'Issues') {
387+
$issues = $this->splitAnnotationContent($output[2]);
388+
$issue = new \Yandex\Allure\Adapter\Annotation\Stories();
389+
foreach($issues as $issueName) {
390+
$issues->issuesKeys[] = $issueName;
391+
}
392+
$annotations[get_class($issue)] = $issue;
393+
} else {
394+
\Codeception\Util\Debug::debug("Tag not detected: ".$output[1]);
395+
}
396+
}
397+
}
398+
}
399+
return $annotations;
400+
}
401+
402+
/**
403+
*
404+
* @param string $string
405+
* @return array
406+
*/
407+
private function splitAnnotationContent($string)
408+
{
409+
$parts = [];
410+
$detected = str_replace('{', '', $string);
411+
$detected = str_replace('}', '', $detected);
412+
$detected = str_replace('"', '', $detected);
413+
$parts = explode(',', $detected);
414+
if (count($parts) == 0 && count($detected) > 0) {
415+
$parts[] = $detected;
416+
}
417+
return $parts;
418+
}
419+
329420
}

0 commit comments

Comments
 (0)