|
7 | 7 | use Codeception\Event\SuiteEvent;
|
8 | 8 | use Codeception\Event\TestEvent;
|
9 | 9 | use Codeception\Event\FailEvent;
|
10 |
| -use Codeception\Test\Cest; |
11 | 10 | use Codeception\Events;
|
12 | 11 | use Codeception\Platform\Extension;
|
13 | 12 | use Codeception\Exception\ConfigurationException;
|
|
29 | 28 |
|
30 | 29 | const OUTPUT_DIRECTORY_PARAMETER = 'outputDirectory';
|
31 | 30 | const DELETE_PREVIOUS_RESULTS_PARAMETER = 'deletePreviousResults';
|
32 |
| -const IGNORED_ANNOTATION_PARAMETER = 'ignoredAnnotations'; |
33 | 31 | const DEFAULT_RESULTS_DIRECTORY = 'allure-results';
|
34 | 32 | const DEFAULT_REPORT_DIRECTORY = 'allure-report';
|
35 | 33 | const INITIALIZED_PARAMETER = '_initialized';
|
@@ -74,16 +72,16 @@ class AllureAdapter extends Extension
|
74 | 72 |
|
75 | 73 | /**
|
76 | 74 | * Extra annotations to ignore in addition to standard PHPUnit annotations.
|
| 75 | + * |
| 76 | + * @param array $ignoredAnnotations |
77 | 77 | */
|
78 |
| - public function _initialize() |
| 78 | + public function _initialize(array $ignoredAnnotations = []) |
79 | 79 | {
|
80 | 80 | parent::_initialize();
|
81 | 81 | Annotation\AnnotationProvider::registerAnnotationNamespaces();
|
82 | 82 | // Add standard PHPUnit annotations
|
83 | 83 | Annotation\AnnotationProvider::addIgnoredAnnotations($this->ignoredAnnotations);
|
84 | 84 | // Add custom ignored annotations
|
85 |
| - $ignoredAnnotations = |
86 |
| - $this->tryGetOption(IGNORED_ANNOTATION_PARAMETER, []); |
87 | 85 | Annotation\AnnotationProvider::addIgnoredAnnotations($ignoredAnnotations);
|
88 | 86 | $outputDirectory = $this->getOutputDirectory();
|
89 | 87 | $deletePreviousResults =
|
@@ -223,29 +221,43 @@ public function testBefore(TestEvent $testEvent)
|
223 | 221 | {
|
224 | 222 | $test = $testEvent->getTest();
|
225 | 223 | $testName = $test->getName();
|
226 |
| - $testClass = $test instanceof Cest |
227 |
| - ? get_class($test->getTestClass()) |
228 |
| - : get_class($test); |
229 | 224 | $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 | + } |
233 | 237 | }
|
| 238 | + |
234 | 239 | $this->getLifecycle()->fire($event);
|
235 | 240 | }
|
236 | 241 |
|
237 | 242 | public function testStart(TestEvent $testEvent)
|
238 | 243 | {
|
239 | 244 | $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(); |
244 | 246 | $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 | + } |
248 | 259 | }
|
| 260 | + |
249 | 261 | $this->getLifecycle()->fire($event);
|
250 | 262 | }
|
251 | 263 |
|
@@ -326,4 +338,83 @@ public function setLifecycle(Allure $lifecycle)
|
326 | 338 | $this->lifecycle = $lifecycle;
|
327 | 339 | }
|
328 | 340 |
|
| 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 | + |
329 | 420 | }
|
0 commit comments