From 7ec5a91e13b7b50668f80ce7ad6b229a5cb3b17c Mon Sep 17 00:00:00 2001 From: Edward Surov Date: Thu, 12 Aug 2021 18:59:03 +0300 Subject: [PATCH 1/7] Lifecycle methods return detected context UUID where possible --- .github/workflows/build.yml | 48 +---- composer.json | 28 ++- phpunit.extension.xml | 19 ++ phpunit.report.xml | 2 +- src/Extension.php | 239 ++++++++++++++++++++++++ src/Internal/TestInfo.php | 93 +++++++++ test/report/Generate/AnnotationTest.php | 168 +++++++++++++++++ 7 files changed, 542 insertions(+), 55 deletions(-) create mode 100644 phpunit.extension.xml create mode 100644 src/Extension.php create mode 100644 src/Internal/TestInfo.php create mode 100644 test/report/Generate/AnnotationTest.php diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a9dad6e..638d429 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -9,58 +9,14 @@ on: - '*' jobs: - build71: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2.3.4 - - uses: shivammathur/setup-php@v2 - with: - php-version: '7.1.3' - - name: Install - run: composer update - - name: Test - run: composer test - build72: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2.3.4 - - uses: shivammathur/setup-php@v2 - with: - php-version: '7.2' - - name: Install - run: composer update - - name: Test - run: composer test - build73: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2.3.4 - - uses: shivammathur/setup-php@v2 - with: - php-version: '7.3' - - name: Install - run: composer update - - name: Test - run: composer test - build74: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v2.3.4 - - uses: shivammathur/setup-php@v2 - with: - php-version: '7.4' - - name: Install - run: composer update - - name: Test - run: composer test build80: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2.3.4 + - uses: actions/checkout@v2 - uses: shivammathur/setup-php@v2 with: php-version: '8.0' - name: Install run: composer update - name: Test - run: composer test \ No newline at end of file + run: composer test diff --git a/composer.json b/composer.json index 3220bbc..cde8c51 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "allure-framework/allure-phpunit", + "name": "remorhaz/allure-phpunit", "keywords": [ "phpunit", "testing", @@ -9,7 +9,7 @@ "cases", "allure" ], - "description": "Allure PHPUNit integration", + "description": "Allure PHPUnit integration", "homepage": "http://allure.qatools.ru/", "license": "Apache-2.0", "authors": [ @@ -17,6 +17,11 @@ "name": "Ivan Krutov", "email": "vania-pooh@yandex-team.ru", "role": "Developer" + }, + { + "name": "Edward Surov", + "email": "zoohie@gmail.com", + "role": "Developer" } ], "support": { @@ -24,29 +29,36 @@ "source": "https://github.com/allure-framework/allure-phpunit" }, "require": { - "php": ">=7.1", - "allure-framework/allure-php-api": "^1.3", - "phpunit/phpunit": "^7.2 | ^8 | ^9" + "php": "^8", + "remorhaz/allure-php-commons": "dev-master as 2.0.0", + "phpunit/phpunit": "^9" }, "require-dev": { "ext-dom": "*", - "mikey179/vfsstream": "^1" + "mikey179/vfsstream": "^1.6.9" }, "autoload": { "psr-0": { "Yandex": "src/" + }, + "psr-4": { + "Qameta\\Allure\\PHPUnit\\": "src/" } }, "autoload-dev": { "psr-0": { "Yandex": "test/" + }, + "psr-4": { + "Qameta\\Allure\\PHPUnit\\Test\\": "test/", + "Qameta\\Allure\\PHPUnit\\Test\\Report\\": "test/report/" } }, "scripts": { "test-unit": "vendor/bin/phpunit", "test-report": [ - "vendor/bin/phpunit --configuration=phpunit.report.xml --stderr 2> /dev/null; exit 0", - "vendor/bin/phpunit --testsuite=report" + "rm -rf ./build/allure-results", + "vendor/bin/phpunit --configuration=phpunit.extension.xml --stderr 2> /dev/null; exit 0" ], "test": [ "@test-unit", diff --git a/phpunit.extension.xml b/phpunit.extension.xml new file mode 100644 index 0000000..7ebd604 --- /dev/null +++ b/phpunit.extension.xml @@ -0,0 +1,19 @@ + + + + + test/report/Generate + + + + + + build/allure-results + + + + diff --git a/phpunit.report.xml b/phpunit.report.xml index 2440841..53063fc 100644 --- a/phpunit.report.xml +++ b/phpunit.report.xml @@ -1,7 +1,7 @@ diff --git a/src/Extension.php b/src/Extension.php new file mode 100644 index 0000000..4552b7f --- /dev/null +++ b/src/Extension.php @@ -0,0 +1,239 @@ +lifecycle = Allure::getLifecycle(); + $this->resultFactory = Allure::getResultFactory(); + } + + public function executeBeforeTest(string $test): void + { + $test = $this->createAnnotatedTest(TestInfo::parse($test)); + $this + ->lifecycle + ->scheduleTest($test); + $this + ->lifecycle + ->startTest($test->getUuid()); + } + + public function executeAfterTest(string $test, float $time): void + { + $testId = $this->lifecycle->getCurrentTest(); + $this->lifecycle->stopTest($testId); + $this->lifecycle->writeTest($testId); + } + + public function executeAfterTestFailure(string $test, string $message, float $time): void + { + $this->lifecycle->updateTest( + fn (TestResult $testResult) => $testResult + ->setStatus(Status::failed()) + ->setStatusDetails($this->createStatusDetails($message)), + ); + } + + public function executeAfterTestError(string $test, string $message, float $time): void + { + $this->lifecycle->updateTest( + fn (TestResult $testResult) => $testResult + ->setStatus(Status::failed()) + ->setStatusDetails($this->createStatusDetails($message)), + ); + } + + public function executeAfterIncompleteTest(string $test, string $message, float $time): void + { + $this->lifecycle->updateTest( + fn (TestResult $testResult) => $testResult + ->setStatus(Status::broken()) + ->setStatusDetails($this->createStatusDetails($message)), + ); + } + + public function executeAfterSkippedTest(string $test, string $message, float $time): void + { + $this->lifecycle->updateTest( + fn (TestResult $testResult) => $testResult + ->setStatus(Status::skipped()) + ->setStatusDetails($this->createStatusDetails($message)), + ); + } + + public function executeAfterTestWarning(string $test, string $message, float $time): void + { + $this->lifecycle->updateTest( + fn (TestResult $testResult) => $testResult + ->setStatus(Status::failed()) + ->setStatusDetails($this->createStatusDetails($message)), + ); + } + + public function executeAfterRiskyTest(string $test, string $message, float $time): void + { + $this->lifecycle->updateTest( + fn (TestResult $testResult) => $testResult + ->setStatus(Status::failed()) + ->setStatusDetails($this->createStatusDetails($message)), + ); + } + + public function executeAfterSuccessfulTest(string $test, float $time): void + { + $this->lifecycle->updateTest( + fn (TestResult $testResult) => $testResult + ->setStatus(Status::passed()) + ->setStatusDetails($this->createStatusDetails()), + ); + } + + private function createAnnotatedTest(TestInfo $info): TestResult + { + $test = $this + ->resultFactory + ->createTest(); + $class = $info->getClass(); + if (isset($class)) { + $test->addLabels(Label::testClass($class)); + } + $method = $info->getMethod(); + if (isset($method)) { + $test->addLabels(Label::testMethod($method)); + } + $parser = new AttributeParser($this->loadAnnotations($class, $method)); + + $fullName = $info->getFullName(); + $test + ->setName($parser->getTitle() ?? $info->getName()) + ->setFullName($fullName) + ->setDescriptionHtml($parser->getDescriptionHtml()) + ->setDescription($parser->getDescription()) + ->addLabels(...$parser->getLabels()) + ->addLinks(...$parser->getLinks()) + ->addParameters(...$parser->getParameters()); + + $dataLabel = $info->getDataLabel(); + if (isset($dataLabel)) { + $test->addParameters(new Parameter('dataset', $dataLabel)); + } + if (isset($fullName)) { + $parameterNames = implode( + '::', + array_map( + fn (Parameter $parameter) => $parameter->getName(), + $test->getParameters(), + ), + ); + $test->setTestCaseId(md5("{$fullName}::{$parameterNames}")); + if (isset($dataLabel)) { + $parameterValues = implode( + '::', + array_map( + fn (Parameter $parameter) => $parameter->getValue() ?? '', + $test->getParameters(), + ), + ); + $test->setHistoryId(md5("{$fullName}::{$parameterValues}")); + } + } + + return $test; + } + + /** + * @param class-string|null $class + * @param string|null $method + * @return list + */ + private function loadAnnotations(?string $class, ?string $method): array + { + $annotations = []; + if (!isset($class)) { + return $annotations; + } + + $reader = new LegacyAttributeReader( + new DoctrineAnnotationReader(), + new AttributeReader(), + ); + try { + $classRef = new ReflectionClass($class); + $annotations = [ + ...$annotations, + ...$reader->getClassAnnotations($classRef), + ]; + } catch (Throwable $e) { + throw new LogicException("Annotations not loaded", 0, $e); + } + + if (!isset($method)) { + return $annotations; + } + + try { + $methodRef = new ReflectionMethod($class, $method); + $annotations = [ + ...$annotations, + ...$reader->getMethodAnnotations($methodRef), + ]; + } catch (Throwable $e) { + throw new LogicException("Annotations not loaded", 0, $e); + } + + return $annotations; + } + + private function createStatusDetails(?string $message = null): StatusDetails + { + return new StatusDetails(message: $message); + } +} diff --git a/src/Internal/TestInfo.php b/src/Internal/TestInfo.php new file mode 100644 index 0000000..8819b96 --- /dev/null +++ b/src/Internal/TestInfo.php @@ -0,0 +1,93 @@ + $matches */ + if (1 === $dataLabelMatchResult) { + $classAndMethod = $matches[1] ?? null; + $dataLabel = $matches[2] ?? '?'; + } else { + $classAndMethod = $test; + $dataLabel = null; + } + + [$class, $method] = isset($classAndMethod) + ? array_pad(explode('::', $classAndMethod, 2), 2, null) + : [null, null]; + + /** @psalm-suppress MixedArgument */ + return new self( + test: $test, + class: isset($class) && class_exists($class) ? $class : null, + method: $method, + dataLabel: $dataLabel, + ); + } + + /** + * @param string $test + * @param class-string|null $class + * @param string|null $method + * @param string|null $dataLabel + */ + private function __construct( + private string $test, + private ?string $class, + private ?string $method, + private ?string $dataLabel, + ) { + } + + public function getTest(): string + { + return $this->test; + } + + /** + * @return class-string|null + */ + public function getClass(): ?string + { + return $this->class; + } + + public function getMethod(): ?string + { + return $this->method; + } + + public function getDataLabel(): ?string + { + return $this->dataLabel; + } + + public function getFullName(): ?string + { + return isset($this->class, $this->method) + ? "{$this->class}::{$this->method}" + : null; + } + + public function getName(): string + { + return $this->getFullName() ?? $this->getTest(); + } +} diff --git a/test/report/Generate/AnnotationTest.php b/test/report/Generate/AnnotationTest.php new file mode 100644 index 0000000..b13d5f9 --- /dev/null +++ b/test/report/Generate/AnnotationTest.php @@ -0,0 +1,168 @@ +expectNotToPerformAssertions(); + } + + #[Attribute\Title('Test native title')] + public function testNativeTitleAnnotation(): void + { + $this->expectNotToPerformAssertions(); + } + + /** + * @Title ("Test legacy title") + */ + #[Attribute\Title('Test native title')] + public function testMixedTitleAnnotation(): void + { + $this->expectNotToPerformAssertions(); + } + + /** + * @Description ("Test legacy description with `markdown`", type = DescriptionType::MARKDOWN) + */ + public function testLegacyDescriptionAnnotation(): void + { + $this->expectNotToPerformAssertions(); + } + + #[Attribute\Description('Test native description with `markdown`')] + public function testNativeDescriptionAnnotation(): void + { + $this->expectNotToPerformAssertions(); + } + + /** + * @Description ("Test legacy description with `markdown`", type = DescriptionType::MARKDOWN) + */ + #[Attribute\Description('Test native description with HTML', true)] + public function testMixedDescriptionAnnotation(): void + { + $this->expectNotToPerformAssertions(); + } + + /** + * @Severity (level = SeverityLevel::MINOR) + */ + public function testLegacySeverityAnnotation(): void + { + $this->expectNotToPerformAssertions(); + } + + #[Attribute\Severity(Attribute\Severity::CRITICAL)] + public function testNativeSeverityAnnotation(): void + { + $this->expectNotToPerformAssertions(); + } + + /** + * @Severity (level = SeverityLevel::MINOR) + */ + #[Attribute\Severity(Attribute\Severity::CRITICAL)] + public function testMixedSeverityAnnotation(): void + { + $this->expectNotToPerformAssertions(); + } + + /** + * @Parameter (name = "foo", value = "legacy foo", kind = ParameterKind::ARGUMENT) + */ + public function testLegacyParameterAnnotation(): void + { + $this->expectNotToPerformAssertions(); + } + + #[Attribute\Parameter('foo', 'native foo')] + #[Attribute\Parameter('bar', 'native bar')] + public function testNativeParameterAnnotation(): void + { + $this->expectNotToPerformAssertions(); + } + + /** + * @Parameter (name = "foo", value = "legacy bar", kind = ParameterKind::ARGUMENT) + */ + #[Attribute\Parameter('foo', 'native foo')] + #[Attribute\Parameter('bar', 'native baz')] + public function testMixedParameterAnnotation(): void + { + $this->expectNotToPerformAssertions(); + } + + /** + * @Stories ("Legacy story 1", "Legacy story 2") + */ + public function testLegacyStoriesAnnotation(): void + { + $this->expectNotToPerformAssertions(); + } + + #[Attribute\Story('Native story 1')] + #[Attribute\Story('Native story 2')] + public function testNativeStoriesAnnotation(): void + { + $this->expectNotToPerformAssertions(); + } + + /** + * @Stories ("Legacy story 1", "Mixed story 2") + */ + #[Attribute\Story('Native story 1')] + #[Attribute\Story('Mixed story 2')] + public function testMixedStoriesAnnotation(): void + { + $this->expectNotToPerformAssertions(); + } + + /** + * @Features ("Legacy feature 1", "Legacy feature 2") + */ + public function testLegacyFeaturesAnnotation(): void + { + $this->expectNotToPerformAssertions(); + } + + #[Attribute\Feature('Native feature 1')] + #[Attribute\Feature('Native feature 2')] + public function testNativeFeaturesAnnotation(): void + { + $this->expectNotToPerformAssertions(); + } + + /** + * @Features ("Legacy feature 1", "Mixed feature 2") + */ + #[Attribute\Feature('Native feature 1')] + #[Attribute\Feature('Mixed feature 2')] + public function testMixedFeaturesAnnotation(): void + { + $this->expectNotToPerformAssertions(); + } +} From 04cdb8ef5e7138e4164ef97a559cedd189c83ffd Mon Sep 17 00:00:00 2001 From: Edward Surov Date: Mon, 16 Aug 2021 10:13:18 +0300 Subject: [PATCH 2/7] Rewritten for commons v2 --- .github/workflows/build.yml | 31 +- README.md | 192 +++++----- composer.json | 13 +- phpunit.extension.xml | 19 - phpunit.report.xml | 25 +- phpunit.xml | 9 +- src/AllureExtension.php | 155 ++++++++ src/ExceptionDetailsTrait.php | 18 + src/Extension.php | 239 ------------- src/Internal/AllureAdapter.php | 166 +++++++++ src/Internal/DefaultThreadDetector.php | 23 ++ src/Internal/TestInfo.php | 53 +-- src/Internal/TestRegistry.php | 105 ++++++ src/Internal/TestRegistryInterface.php | 20 ++ src/Internal/TestRunInfo.php | 52 +++ src/Internal/TestUpdater.php | 137 +++++++ src/Internal/TestUpdaterInterface.php | 25 ++ src/Setup/ConfiguratorInterface.php | 26 ++ src/Setup/DefaultConfigurator.php | 55 +++ src/Setup/ThreadDetectorInterface.php | 11 + src/SharedTestState.php | 44 +++ src/SharedTestStateInterface.php | 17 + src/Yandex/Allure/PhpUnit/AllurePhpUnit.php | 304 ---------------- .../Allure/Adapter/AllureAdapterTest.php | 179 ---------- .../Yandex/Allure/Report/Check/ReportTest.php | 337 ------------------ .../Allure/Report/Generate/AnnotationTest.php | 71 ---- .../Allure/Report/Generate/AttachmentTest.php | 29 -- .../Allure/Report/Generate/StepsTest.php | 109 ------ .../Annotation/TitleClassLevelLegacyTest.php | 25 ++ .../Annotation/TitleClassLevelMixedTest.php | 26 ++ .../Annotation/TitleClassLevelNativeTest.php | 22 ++ test/report/Generate/Annotation/TitleTest.php | 48 +++ test/report/Generate/AnnotationTest.php | 48 +-- test/report/Generate/NegativeTest.php | 77 ++++ test/report/Generate/RetriesTest.php | 63 ++++ .../Internal/DefaultThreadDetectorTest.php | 22 ++ 36 files changed, 1299 insertions(+), 1496 deletions(-) delete mode 100644 phpunit.extension.xml create mode 100644 src/AllureExtension.php create mode 100644 src/ExceptionDetailsTrait.php delete mode 100644 src/Extension.php create mode 100644 src/Internal/AllureAdapter.php create mode 100644 src/Internal/DefaultThreadDetector.php create mode 100644 src/Internal/TestRegistry.php create mode 100644 src/Internal/TestRegistryInterface.php create mode 100644 src/Internal/TestRunInfo.php create mode 100644 src/Internal/TestUpdater.php create mode 100644 src/Internal/TestUpdaterInterface.php create mode 100644 src/Setup/ConfiguratorInterface.php create mode 100644 src/Setup/DefaultConfigurator.php create mode 100644 src/Setup/ThreadDetectorInterface.php create mode 100644 src/SharedTestState.php create mode 100644 src/SharedTestStateInterface.php delete mode 100644 src/Yandex/Allure/PhpUnit/AllurePhpUnit.php delete mode 100644 test/Yandex/Allure/Adapter/AllureAdapterTest.php delete mode 100644 test/Yandex/Allure/Report/Check/ReportTest.php delete mode 100644 test/Yandex/Allure/Report/Generate/AnnotationTest.php delete mode 100644 test/Yandex/Allure/Report/Generate/AttachmentTest.php delete mode 100644 test/Yandex/Allure/Report/Generate/StepsTest.php create mode 100644 test/report/Generate/Annotation/TitleClassLevelLegacyTest.php create mode 100644 test/report/Generate/Annotation/TitleClassLevelMixedTest.php create mode 100644 test/report/Generate/Annotation/TitleClassLevelNativeTest.php create mode 100644 test/report/Generate/Annotation/TitleTest.php create mode 100644 test/report/Generate/NegativeTest.php create mode 100644 test/report/Generate/RetriesTest.php create mode 100644 test/unit/Internal/DefaultThreadDetectorTest.php diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 638d429..30746e9 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -9,14 +9,31 @@ on: - '*' jobs: - build80: + tests: + strategy: + matrix: + php-version: + - "8.0" + dependencies: + - lowest + - highest runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: shivammathur/setup-php@v2 + - name: Checkout + uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 with: - php-version: '8.0' - - name: Install - run: composer update - - name: Test + php-version: "${{ matrix.php-version }}" + + - name: Install lowest dependencies + if: ${{ matrix.dependencies == 'lowest' }} + run: composer update --no-interaction --prefer-lowest + + - name: Install highest dependencies + if: ${{ matrix.dependencies == 'highest' }} + run: composer update --no-interaction + + - name: Run tests run: composer test diff --git a/README.md b/README.md index 6308fbb..e383ba0 100644 --- a/README.md +++ b/README.md @@ -19,63 +19,58 @@ This an official PHPUnit adapter for Allure Framework - a flexible, lightweight * [Steps](#divide-test-methods-into-steps) ## What is this for? -The main purpose of this adapter is to accumulate information about your tests and write it out to a set of XML files: one for each test class. Then you can use a standalone command line tool or a plugin for popular continuous integration systems to generate an HTML page showing your tests in a good form. +The main purpose of this adapter is to accumulate information about your tests and write it out to a set of JSON files: one for each test class. Then you can use a standalone command line tool or a plugin for popular continuous integration systems to generate an HTML page showing your tests in a good form. ## Example project Example project is located at: https://github.com/allure-framework/allure-phpunit-example ## How to generate report -This adapter only generates XML files containing information about tests. See [wiki section](https://github.com/allure-framework/allure-core/wiki#generating-report) on how to generate report. +This adapter only generates JSON files containing information about tests. See [wiki section](https://docs.qameta.io/allure/#_reporting) on how to generate report. ## Installation && Usage -**Note:** this adapter supports Allure 1.4.x only. +**Note:** this adapter supports Allure 2.x.x only. In order to use this adapter you need to add a new dependency to your **composer.json** file: ``` { "require": { - "php": ">=7.0.0", - "allure-framework/allure-phpunit": "~1.2.0" + "php": "^8", + "allure-framework/allure-phpunit": "^2" } } ``` Then add Allure test listener in **phpunit.xml** file: ```xml - - + + + - build/allure-results - true - - - someCustomAnnotation - - + + build/allure-results + + Qameta\Allure\PHPUnit\Setup\DefaultConfigurator + - - + + ``` -After running PHPUnit tests a new folder will be created (**build/allure-results** in the example above). This folder will contain generated XML files. See [framework help](https://github.com/allure-framework/allure-core/wiki) for details about how to generate report from XML files. By default generated report will only show a limited set of information but you can use cool Allure features by adding a minimum of test code changes. Read next section for details. +After running PHPUnit tests a new folder will be created (**build/allure-results** in the example above). This folder will contain generated JSON files. See [framework help](https://docs.qameta.io/allure/#_reporting) for details about how to generate report from JSON files. By default generated report will only show a limited set of information but you can use cool Allure features by adding a minimum of test code changes. Read next section for details. ## Main features This adapter comes with a set of PHP annotations and traits allowing to use main Allure features. ### Human-readable test class or test method title -In order to add such title to any test class or [test case](https://github.com/allure-framework/allure-core/wiki/Glossary#test-case) method you need to annotate it with **@Title** annotation: +In order to add such title to any test class or [test case](https://github.com/allure-framework/allure1/wiki/Glossary#test-case) method you need to annotate it with **#[Title]** annotation: ```php namespace Example\Tests; use PHPUnit\Framework\TestCase; -use Yandex\Allure\Adapter\Annotation\Title; +use Qameta\Allure\Attribute\Title; -/** - * @Title("Human-readable test class title") - */ +#[Title("Human-readable test class title")] class SomeTest extends TestCase { - /** - * @Title("Human-readable test method title") - */ - public function testCase() + #[Title("Human-readable test method title")] + public function testCaseMethod(): void { //Some implementation here... } @@ -83,45 +78,38 @@ class SomeTest extends TestCase ``` ### Extended test class or test method description -Similarly you can add detailed description for each test class and [test method](https://github.com/allure-framework/allure-core/wiki/Glossary#test-case). To add such description simply use **@Description** annotation: +Similarly you can add detailed description for each test class and [test method](https://github.com/allure-framework/allure1/wiki/Glossary#test-case). To add such description simply use **#[Description]** annotation: ```php namespace Example\Tests; use PHPUnit\Framework\TestCase; -use Yandex\Allure\Adapter\Annotation\Description; -use Yandex\Allure\Adapter\Model\DescriptionType; +use Qameta\Allure\Attribute\Description; -/** - * @Description("Detailed description for test class") - */ +#[Description("Detailed description for **test** class")] class SomeTest extends TestCase { - /** - * @Description(value = "Detailed description for test class.", type = DescriptionType::HTML) - */ - public function testCase() + #[Description("Detailed description for test class", isHtml: true)] + public function testCaseMethod(): void { //Some implementation here... } } ``` -Description can be added in plain text, HTML or Markdown format - simply assign different **type** value. +Description can be added in Markdown format (which is default one) or in HTML format. For HTML simply pass `true` value for optional `isHtml` argument. ### Set test severity -**@Severity** annotation is used in order to prioritize test methods by severity: +**#[Severity]** annotation is used in order to prioritize test methods by severity: + ```php namespace Example\Tests; use PHPUnit\Framework\TestCase; -use Yandex\Allure\Adapter\Annotation\Severity; -use Yandex\Allure\Adapter\Model\SeverityLevel; +use Qameta\Allure\Attribute\Severity; class SomeTest extends TestCase { - /** - * @Severity(level = SeverityLevel::MINOR) - */ - public function testCase() + #[Severity(Severity::MINOR)] + public function testCaseMethod(): void { //Some implementation here... } @@ -129,47 +117,52 @@ class SomeTest extends TestCase ``` ### Specify test parameters information -In order to add information about test method [parameters](https://github.com/allure-framework/allure-core/wiki/Glossary#parameter) you should use **@Parameter** annotation: +In order to add information about test method [parameters](https://github.com/allure-framework/allure-core/wiki/Glossary#parameter) you should use **#[Parameter]** annotation. You can also use static shortcut if your marameter has dynamic value: + ```php namespace Example\Tests; use PHPUnit\Framework\TestCase; -use Yandex\Allure\Adapter\Annotation\Parameter; -use Yandex\Allure\Adapter\Model\ParameterKind; +use Qameta\Allure\Allure; +use Qameta\Allure\Attribute\Parameter; class SomeTest extends TestCase { - /** - * @Parameter(name = "param1", value = "value1") - * @Parameter(name = "param2", value = "value2", kind = ParameterKind::SYSTEM_PROPERTY) - */ - public function testCase() + #[ + Parameter("param1", "value1"), + Parameter("param2", "value2"), + ] + public function testCaseMethod(): void { //Some implementation here... + Allure::parameter("param3", $someVar); } } ``` ### Map test classes and test methods to features and stories -In some development approaches tests are classified by [stories](https://github.com/allure-framework/allure-core/wiki/Glossary#user-story) and [features](https://github.com/allure-framework/allure-core/wiki/Glossary#feature). If you're using this then you can annotate your test with **@Stories** and **@Features** annotations: +In some development approaches tests are classified by [stories](https://github.com/allure-framework/allure-core/wiki/Glossary#user-story) and [features](https://github.com/allure-framework/allure-core/wiki/Glossary#feature). If you're using this then you can annotate your test with **#[Story]** and **#[Feature]** annotations: ```php namespace Example\Tests; use PHPUnit\Framework\TestCase; -use Yandex\Allure\Adapter\Annotation\Features; -use Yandex\Allure\Adapter\Annotation\Stories; - -/** - * @Stories({"story1", "story2"}) - * @Features({"feature1", "feature2", "feature3"}) - */ +use Qameta\Allure\Attribute\Feature; +use Qameta\Allure\Attribute\Story; + +#[ + Story("story1"), + Story("story2"), + Feature("feature1"), + Feature("feature2"), + Feature("feature3"), +] class SomeTest extends TestCase { - /** - * @Features({"feature2"}) - * @Stories({"story1"}) - */ - public function testCase() + #[ + Story("story3"), + Feature("feature4"), + ] + public function testCaseMethod(): void { //Some implementation here... } @@ -178,82 +171,65 @@ class SomeTest extends TestCase You will then be able to filter tests by specified features and stories in generated Allure report. ### Attach files to report -If you wish to [attach some files](https://github.com/allure-framework/allure-core/wiki/Glossary#attachment) generated during PHPUnit run (screenshots, log files, dumps and so on) to report - then you need to use **AttachmentSupport** trait in your test class: +If you wish to [attach some files](https://github.com/allure-framework/allure-core/wiki/Glossary#attachment) generated during PHPUnit run (screenshots, log files, dumps and so on) to report - then you need to use static shortcuts in your test class: ```php namespace Example\Tests; use PHPUnit\Framework\TestCase; -use Yandex\Allure\Adapter\Support\AttachmentSupport; +use Qameta\Allure\Allure; class SomeTest extends TestCase { - use AttachmentSupport; - - public function testCase() + public function testCaseMethod() { //Some implementation here... - $filePath = $this->outputSomeContentToTemporaryFile(); - $this->addAttachment($filePath, 'Attachment human-readable name', 'text/plain'); + Allure::attachment("Attachment 1", "attachment content", 'text/plain'); + Allure::attachmentFile("Attachment 2", "/path/to/file.png", 'image/png'); //Some implementation here... } - - private function outputSomeContentToTemporaryFile() - { - $tmpPath = tempnam(sys_get_temp_dir(), 'test'); - file_put_contents($tmpPath, 'Some content to be outputted to temporary file.'); - return $tmpPath; - } - } ``` -In order to create an [attachment](https://github.com/allure-framework/allure-core/wiki/Glossary#attachment) simply call **AttachmentSupport::addAttachment()** method. This method accepts attachment type, human-readable name and a string either storing full path to the file we need to attach or file contents. +In order to create an [attachment](https://github.com/allure-framework/allure-core/wiki/Glossary#attachment) simply call **Allure::attachment()** method. This method accepts human-readable name, string content and MIME attachment type. To attach a file, use **Allure::attachmentFile()** method that accepts file name instead of string content. ### Divide test methods into steps -Allure framework also supports very useful feature called [steps](https://github.com/allure-framework/allure-core/wiki/Glossary#test-step). Consider a test method which has complex logic inside and several assertions. When an exception is thrown or one of assertions fails sometimes it's very difficult to determine which one caused the failure. Allure steps allow to divide test method logic into several isolated pieces having independent run statuses such as **passed** or **failed**. This allows to have much more cleaner understanding of what really happens. In order to use steps simply import **StepSupport** trait in your test class: +Allure framework also supports very useful feature called [steps](https://github.com/allure-framework/allure-core/wiki/Glossary#test-step). Consider a test method which has complex logic inside and several assertions. When an exception is thrown or one of assertions fails sometimes it's very difficult to determine which one caused the failure. Allure steps allow dividing test method logic into several isolated pieces having independent run statuses such as **passed** or **failed**. This allows to have much cleaner understanding of what really happens. In order to use steps simply use static shortcuts: + ```php namespace Example\Tests; use PHPUnit\Framework\TestCase; -use Yandex\Allure\Adapter\Support\StepSupport; +use Qameta\Allure\Allure; +use Qameta\Allure\Attribute\Parameter; +use Qameta\Allure\Attribute\Title; +use Qameta\Allure\StepContextInterface; class SomeTest extends TestCase { - use StepSupport; - - public function testCase() + public function testCaseMethod(): void { //Some implementation here... - $this->executeStep("This is step one", function () { - $this->stepOne(); - }); - - $this->executeStep("This is step two", function () { - $this-stepTwo(); - }); - - $this->executeStep("This is step three", function () { - $this->stepThree('someArgument'); - }); - //Some implementation here... - } - - private function stepOne() - { + $x = Allure::runStep( + #[Title('First step')] + function (StepContextInterface $step): string { + $step->parameter('param1', $someValue); + + return 'foo'; + }, + ); + Allure::runStep([$this, 'stepTwo']); //Some implementation here... } + #[ + Title("Second step"), + Parameter("param2", "value2"), + ] private function stepTwo() { //Some implementation here... } - - private function stepThree($argument) - { - //Some implementation here... - } - } ``` The entire test method execution status will depend on every step but information about steps status will be stored separately. diff --git a/composer.json b/composer.json index cde8c51..038ac05 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "remorhaz/allure-phpunit", + "name": "allure-framework/allure-phpunit", "keywords": [ "phpunit", "testing", @@ -35,12 +35,9 @@ }, "require-dev": { "ext-dom": "*", - "mikey179/vfsstream": "^1.6.9" + "brianium/paratest": "^6.3.1" }, "autoload": { - "psr-0": { - "Yandex": "src/" - }, "psr-4": { "Qameta\\Allure\\PHPUnit\\": "src/" } @@ -50,7 +47,7 @@ "Yandex": "test/" }, "psr-4": { - "Qameta\\Allure\\PHPUnit\\Test\\": "test/", + "Qameta\\Allure\\PHPUnit\\Test\\": "test/unit/", "Qameta\\Allure\\PHPUnit\\Test\\Report\\": "test/report/" } }, @@ -58,7 +55,9 @@ "test-unit": "vendor/bin/phpunit", "test-report": [ "rm -rf ./build/allure-results", - "vendor/bin/phpunit --configuration=phpunit.extension.xml --stderr 2> /dev/null; exit 0" + "vendor/bin/paratest --configuration=phpunit.report.xml --testsuite=positive", + "vendor/bin/paratest --configuration=phpunit.report.xml --testsuite=negative; exit 0", + "vendor/bin/paratest --configuration=phpunit.report.xml --testsuite=retries --repeat=3; exit 0" ], "test": [ "@test-unit", diff --git a/phpunit.extension.xml b/phpunit.extension.xml deleted file mode 100644 index 7ebd604..0000000 --- a/phpunit.extension.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - test/report/Generate - - - - - - build/allure-results - - - - diff --git a/phpunit.report.xml b/phpunit.report.xml index 53063fc..d3b5e67 100644 --- a/phpunit.report.xml +++ b/phpunit.report.xml @@ -3,18 +3,21 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd" colors="true" - defaultTestSuite="generate-report"> + defaultTestSuite="positive"> - - test/Yandex/Allure/Report/Generate + + test/report/Generate + test/report/Generate/NegativeTest.php + test/report/Generate/RetriesTest.php + + + test/report/Generate/NegativeTest.php + + + test/report/Generate/RetriesTest.php - - - - build/allure-results - true - - - + + + diff --git a/phpunit.xml b/phpunit.xml index fd3cfd2..f39018f 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -3,13 +3,10 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/7.0/phpunit.xsd" colors="true" - defaultTestSuite="adapter"> + defaultTestSuite="unit"> - - test/Yandex/Allure/Adapter/ - - - test/Yandex/Allure/Report/Check/ + + test/unit/ diff --git a/src/AllureExtension.php b/src/AllureExtension.php new file mode 100644 index 0000000..3af0964 --- /dev/null +++ b/src/AllureExtension.php @@ -0,0 +1,155 @@ +createConfigurator( + $configuratorClass ?? DefaultConfigurator::class, + ...$args, + ); + $configurator->setupAllure($outputDirectory ?? self::DEFAULT_OUTPUT_DIRECTORY); + $this->sharedTestState = $configurator->getSharedTestState() ?? SharedTestState::getInstance(); + $this->adapter = new AllureAdapter( + $configurator->getAllureLifecycle() ?? Allure::getLifecycle(), + $configurator->getResultFactory() ?? Allure::getResultFactory(), + $configurator->getStatusDetector() ?? Allure::getStatusDetector(), + $configurator->getThreadDetector() ?? new DefaultThreadDetector(), + new TestRegistry(), + new TestUpdater(), + ); + } + + private function createConfigurator(string $class, mixed ...$args): ConfiguratorInterface + { + return + class_exists($class) && + is_a($class, ConfiguratorInterface::class, true) + ? new $class(...$args) + : throw new LogicException("Invalid configurator class: {$class}"); + } + + public function executeBeforeTest(string $test): void + { + $this->sharedTestState->reset(); + $this + ->adapter + ->switchToTest($test) + ->createTest() + ->updateInitialInfo() + ->start(); + } + + public function executeAfterTest(string $test, float $time): void + { + $this + ->adapter + ->switchToTest($test) + ->stop() + ->updateRunInfo() + ->write(); + } + + public function executeAfterTestFailure(string $test, string $message, float $time): void + { + $this + ->adapter + ->switchToTest($test) + ->updateStatus($message, Status::failed()); + } + + public function executeAfterTestError(string $test, string $message, float $time): void + { + $this->adapter->switchToTest($test); + $exception = $this->sharedTestState->getLastException(); + isset($exception) + ? $this->adapter->updateDetectedStatus($exception) + : $this->adapter->updateStatus($message, Status::failed()); + } + + public function executeAfterIncompleteTest(string $test, string $message, float $time): void + { + $this + ->adapter + ->switchToTest($test) + ->updateStatus($message, Status::broken()); + } + + public function executeAfterSkippedTest(string $test, string $message, float $time): void + { + $this + ->adapter + ->switchToTest($test) + ->updateStatus($message, Status::skipped()); + } + + public function executeAfterTestWarning(string $test, string $message, float $time): void + { + $this + ->adapter + ->switchToTest($test) + ->updateStatus($message, Status::broken()); + } + + public function executeAfterRiskyTest(string $test, string $message, float $time): void + { + $this + ->adapter + ->switchToTest($test) + ->updateStatus($message, Status::failed()); + } + + public function executeAfterSuccessfulTest(string $test, float $time): void + { + $this + ->adapter + ->switchToTest($test) + ->updateStatus(status: Status::passed()); + } +} diff --git a/src/ExceptionDetailsTrait.php b/src/ExceptionDetailsTrait.php new file mode 100644 index 0000000..0c39e53 --- /dev/null +++ b/src/ExceptionDetailsTrait.php @@ -0,0 +1,18 @@ +setLastException($t); + throw $t; + } +} diff --git a/src/Extension.php b/src/Extension.php deleted file mode 100644 index 4552b7f..0000000 --- a/src/Extension.php +++ /dev/null @@ -1,239 +0,0 @@ -lifecycle = Allure::getLifecycle(); - $this->resultFactory = Allure::getResultFactory(); - } - - public function executeBeforeTest(string $test): void - { - $test = $this->createAnnotatedTest(TestInfo::parse($test)); - $this - ->lifecycle - ->scheduleTest($test); - $this - ->lifecycle - ->startTest($test->getUuid()); - } - - public function executeAfterTest(string $test, float $time): void - { - $testId = $this->lifecycle->getCurrentTest(); - $this->lifecycle->stopTest($testId); - $this->lifecycle->writeTest($testId); - } - - public function executeAfterTestFailure(string $test, string $message, float $time): void - { - $this->lifecycle->updateTest( - fn (TestResult $testResult) => $testResult - ->setStatus(Status::failed()) - ->setStatusDetails($this->createStatusDetails($message)), - ); - } - - public function executeAfterTestError(string $test, string $message, float $time): void - { - $this->lifecycle->updateTest( - fn (TestResult $testResult) => $testResult - ->setStatus(Status::failed()) - ->setStatusDetails($this->createStatusDetails($message)), - ); - } - - public function executeAfterIncompleteTest(string $test, string $message, float $time): void - { - $this->lifecycle->updateTest( - fn (TestResult $testResult) => $testResult - ->setStatus(Status::broken()) - ->setStatusDetails($this->createStatusDetails($message)), - ); - } - - public function executeAfterSkippedTest(string $test, string $message, float $time): void - { - $this->lifecycle->updateTest( - fn (TestResult $testResult) => $testResult - ->setStatus(Status::skipped()) - ->setStatusDetails($this->createStatusDetails($message)), - ); - } - - public function executeAfterTestWarning(string $test, string $message, float $time): void - { - $this->lifecycle->updateTest( - fn (TestResult $testResult) => $testResult - ->setStatus(Status::failed()) - ->setStatusDetails($this->createStatusDetails($message)), - ); - } - - public function executeAfterRiskyTest(string $test, string $message, float $time): void - { - $this->lifecycle->updateTest( - fn (TestResult $testResult) => $testResult - ->setStatus(Status::failed()) - ->setStatusDetails($this->createStatusDetails($message)), - ); - } - - public function executeAfterSuccessfulTest(string $test, float $time): void - { - $this->lifecycle->updateTest( - fn (TestResult $testResult) => $testResult - ->setStatus(Status::passed()) - ->setStatusDetails($this->createStatusDetails()), - ); - } - - private function createAnnotatedTest(TestInfo $info): TestResult - { - $test = $this - ->resultFactory - ->createTest(); - $class = $info->getClass(); - if (isset($class)) { - $test->addLabels(Label::testClass($class)); - } - $method = $info->getMethod(); - if (isset($method)) { - $test->addLabels(Label::testMethod($method)); - } - $parser = new AttributeParser($this->loadAnnotations($class, $method)); - - $fullName = $info->getFullName(); - $test - ->setName($parser->getTitle() ?? $info->getName()) - ->setFullName($fullName) - ->setDescriptionHtml($parser->getDescriptionHtml()) - ->setDescription($parser->getDescription()) - ->addLabels(...$parser->getLabels()) - ->addLinks(...$parser->getLinks()) - ->addParameters(...$parser->getParameters()); - - $dataLabel = $info->getDataLabel(); - if (isset($dataLabel)) { - $test->addParameters(new Parameter('dataset', $dataLabel)); - } - if (isset($fullName)) { - $parameterNames = implode( - '::', - array_map( - fn (Parameter $parameter) => $parameter->getName(), - $test->getParameters(), - ), - ); - $test->setTestCaseId(md5("{$fullName}::{$parameterNames}")); - if (isset($dataLabel)) { - $parameterValues = implode( - '::', - array_map( - fn (Parameter $parameter) => $parameter->getValue() ?? '', - $test->getParameters(), - ), - ); - $test->setHistoryId(md5("{$fullName}::{$parameterValues}")); - } - } - - return $test; - } - - /** - * @param class-string|null $class - * @param string|null $method - * @return list - */ - private function loadAnnotations(?string $class, ?string $method): array - { - $annotations = []; - if (!isset($class)) { - return $annotations; - } - - $reader = new LegacyAttributeReader( - new DoctrineAnnotationReader(), - new AttributeReader(), - ); - try { - $classRef = new ReflectionClass($class); - $annotations = [ - ...$annotations, - ...$reader->getClassAnnotations($classRef), - ]; - } catch (Throwable $e) { - throw new LogicException("Annotations not loaded", 0, $e); - } - - if (!isset($method)) { - return $annotations; - } - - try { - $methodRef = new ReflectionMethod($class, $method); - $annotations = [ - ...$annotations, - ...$reader->getMethodAnnotations($methodRef), - ]; - } catch (Throwable $e) { - throw new LogicException("Annotations not loaded", 0, $e); - } - - return $annotations; - } - - private function createStatusDetails(?string $message = null): StatusDetails - { - return new StatusDetails(message: $message); - } -} diff --git a/src/Internal/AllureAdapter.php b/src/Internal/AllureAdapter.php new file mode 100644 index 0000000..e2a1d00 --- /dev/null +++ b/src/Internal/AllureAdapter.php @@ -0,0 +1,166 @@ +resultFactory->createTest(); + $this->lifecycle->scheduleTest($testResult); + $this->testRegistry->registerTest($testResult, $this->getCurrentTest()); + + return $this; + } + + public function updateInitialInfo(): self + { + $this->lifecycle->updateTest( + fn (TestResult $testResult) => $this->testUpdater->setInfo($testResult, $this->getCurrentTest()), + $this->testRegistry->getTestId($this->getCurrentTest()), + ); + + return $this; + } + + public function start(): self + { + $this->lifecycle->startTest( + $this->testRegistry->getTestId($this->getCurrentTest()), + ); + + return $this; + } + + public function stop(): self + { + $this->lifecycle->stopTest( + $this->testRegistry->getTestId($this->getCurrentTest()), + ); + + return $this; + } + + public function updateRunInfo(): self + { + $this->lifecycle->updateTest( + fn (TestResult $testResult) => $this->testUpdater->setRunInfo( + $testResult, + $this->testRegistry->registerRun($testResult, $this->getCurrentTest()), + ), + $this->testRegistry->getTestId($this->getCurrentTest()), + ); + + return $this; + } + + public function write(): self + { + $this->lifecycle->writeTest( + $this->testRegistry->getTestId($this->getCurrentTest()), + ); + + return $this; + } + + public function updateStatus(?string $message = null, ?Status $status = null): self + { + $this->lifecycle->updateTest( + fn (TestResult $testResult) => $this->testUpdater->setStatus($testResult, $message, $status), + $this->testRegistry->getTestId($this->getCurrentTest()), + ); + + return $this; + } + + public function updateDetectedStatus(Throwable $exception): self + { + $this->lifecycle->updateTest( + fn (TestResult $testResult) => $this->testUpdater->setDetectedStatus( + $testResult, + $this->statusDetector, + $exception, + ), + $this->testRegistry->getTestId($this->getCurrentTest()), + ); + + return $this; + } + + public function switchToTest(string $test): self + { + $thread = $this->threadDetector->getThread(); + $this->lifecycle->switchThread($thread); + + $this->currentTest = $this->buildTestInfo($test, $thread); + + return $this; + } + + private function getCurrentTest(): TestInfo + { + return $this->currentTest ?? throw new RuntimeException("Current test is not set"); + } + + private function buildTestInfo(string $test, ?string $thread = null): TestInfo + { + $dataLabelMatchResult = preg_match( + '#^([^\s]+)\s+with\s+data\s+set\s+"(.*)"\s+\(.+\)$#', + $test, + $matches, + ); + + /** @var list $matches */ + if (1 === $dataLabelMatchResult) { + $classAndMethod = $matches[1] ?? null; + $dataLabel = $matches[2] ?? '?'; + } else { + $classAndMethod = $test; + $dataLabel = null; + } + + [$class, $method] = isset($classAndMethod) + ? array_pad(explode('::', $classAndMethod, 2), 2, null) + : [null, null]; + + /** @psalm-suppress MixedArgument */ + return new TestInfo( + test: $test, + class: isset($class) && class_exists($class) ? $class : null, + method: $method, + dataLabel: $dataLabel, + thread: $thread, + ); + } +} diff --git a/src/Internal/DefaultThreadDetector.php b/src/Internal/DefaultThreadDetector.php new file mode 100644 index 0000000..fc87bca --- /dev/null +++ b/src/Internal/DefaultThreadDetector.php @@ -0,0 +1,23 @@ + $matches */ - if (1 === $dataLabelMatchResult) { - $classAndMethod = $matches[1] ?? null; - $dataLabel = $matches[2] ?? '?'; - } else { - $classAndMethod = $test; - $dataLabel = null; - } - - [$class, $method] = isset($classAndMethod) - ? array_pad(explode('::', $classAndMethod, 2), 2, null) - : [null, null]; - - /** @psalm-suppress MixedArgument */ - return new self( - test: $test, - class: isset($class) && class_exists($class) ? $class : null, - method: $method, - dataLabel: $dataLabel, - ); - } - /** - * @param string $test + * @param string $test * @param class-string|null $class - * @param string|null $method - * @param string|null $dataLabel + * @param string|null $method + * @param string|null $dataLabel + * @param string|null $thread */ - private function __construct( + public function __construct( private string $test, private ?string $class, private ?string $method, private ?string $dataLabel, + private ?string $thread, ) { } @@ -90,4 +60,9 @@ public function getName(): string { return $this->getFullName() ?? $this->getTest(); } + + public function getThread(): ?string + { + return $this->thread; + } } diff --git a/src/Internal/TestRegistry.php b/src/Internal/TestRegistry.php new file mode 100644 index 0000000..cf37e36 --- /dev/null +++ b/src/Internal/TestRegistry.php @@ -0,0 +1,105 @@ + + */ + private array $lastStarts = []; + + /** + * @var array + */ + private array $lastRuns = []; + + public function registerTest(TestResult $testResult, TestInfo $info): string + { + $this->lastStarts[$info->getTest()] = $testResult->getUuid(); + + return $testResult->getUuid(); + } + + public function getTestId(TestInfo $info): string + { + return $this->lastStarts[$info->getTest()] + ?? throw new RuntimeException("Test not registered"); + } + + public function registerRun(TestResult $testResult, TestInfo $info): TestRunInfo + { + $historyId = $this->buildHistoryId($testResult, $info); + + $previousRunInfo = $this->lastRuns[$historyId] ?? null; + $currentRunInfo = new TestRunInfo( + testInfo: $info, + uuid: $testResult->getUuid(), + rerunOf: $previousRunInfo?->getUuid(), + runIndex: 1 + ($previousRunInfo?->getRunIndex() ?? -1), + testCaseId: $this->buildTestCaseId($testResult, $info), + historyId: $historyId, + ); + $this->lastRuns[$historyId] = $currentRunInfo; + + return $currentRunInfo; + } + + /** + * @param TestResult $test + * @return list + */ + private function getIncludedParameters(TestResult $test): array + { + return array_values( + array_filter( + $test->getParameters(), + fn (Parameter $parameter): bool => $parameter->getExcluded() !== true, + ), + ); + } + + private function buildTestCaseId(TestResult $test, TestInfo $info): string + { + $parameterNames = implode( + '::', + array_map( + fn (Parameter $parameter): string => $parameter->getName(), + $this->getIncludedParameters($test), + ), + ); + $fullName = $info->getFullName() ?? ''; + + return md5("{$fullName}::{$parameterNames}"); + } + + private function buildHistoryId(TestResult $test, TestInfo $info): string + { + $parameterValues = implode( + '::', + array_map( + fn (Parameter $parameter) => $parameter->getValue() ?? '', + $this->getIncludedParameters($test), + ), + ); + $fullName = $info->getFullName() ?? ''; + + return md5("{$fullName}::{$parameterValues}"); + } +} diff --git a/src/Internal/TestRegistryInterface.php b/src/Internal/TestRegistryInterface.php new file mode 100644 index 0000000..d0665d4 --- /dev/null +++ b/src/Internal/TestRegistryInterface.php @@ -0,0 +1,20 @@ +testInfo; + } + + public function getUuid(): string + { + return $this->uuid; + } + + public function getRerunOf(): ?string + { + return $this->rerunOf; + } + + public function getRunIndex(): int + { + return $this->runIndex; + } + + public function getTestCaseId(): string + { + return $this->testCaseId; + } + + public function getHistoryId(): string + { + return $this->historyId; + } +} diff --git a/src/Internal/TestUpdater.php b/src/Internal/TestUpdater.php new file mode 100644 index 0000000..a7dfaee --- /dev/null +++ b/src/Internal/TestUpdater.php @@ -0,0 +1,137 @@ +parseAnnotations($info); + + $testResult + ->setName($parser->getTitle() ?? $info->getName()) + ->setFullName($info->getFullName()) + ->setDescriptionHtml($parser->getDescriptionHtml()) + ->setDescription($parser->getDescription()) + ->addLabels( + ...$this->createSystemLabels($info), + ...$parser->getLabels(), + ) + ->addParameters( + ...$this->createSystemParameters($info), + ...$parser->getParameters(), + ) + ->addLinks(...$parser->getLinks()); + } + + /** + * @param TestInfo $info + * @return AttributeParser + */ + private function parseAnnotations(TestInfo $info): AttributeParser + { + $class = $info->getClass(); + if (!isset($class)) { + return new AttributeParser([]); + } + + $annotations = []; + $reader = new LegacyAttributeReader( + new DoctrineAnnotationReader(), + new AttributeReader(), + ); + try { + $classRef = new ReflectionClass($class); + $annotations = [ + ...$annotations, + ...$reader->getClassAnnotations($classRef), + ]; + } catch (Throwable $e) { + throw new LogicException("Annotations not loaded", 0, $e); + } + + $method = $info->getMethod(); + if (!isset($method)) { + return new AttributeParser($annotations); + } + + try { + $methodRef = new ReflectionMethod($class, $method); + $annotations = [ + ...$annotations, + ...$reader->getMethodAnnotations($methodRef), + ]; + } catch (Throwable $e) { + throw new LogicException("Annotations not loaded", 0, $e); + } + + return new AttributeParser($annotations); + } + + /** + * @return list