Skip to content

Commit

Permalink
Extension rewritten for PHPUnit 10 (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
remorhaz committed Sep 18, 2023
1 parent 17d2a8a commit a279254
Show file tree
Hide file tree
Showing 45 changed files with 1,334 additions and 617 deletions.
18 changes: 4 additions & 14 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ jobs:
fail-fast: false
matrix:
php-version:
- "8.0"
- "8.1"
- "8.2"
- "8.3"
os:
- ubuntu-latest
- windows-latest
Expand All @@ -29,7 +29,7 @@ jobs:
- "--prefer-lowest"
steps:
- name: Checkout
uses: actions/checkout@v3.5.0
uses: actions/checkout@v3

- name: Validate composer.json and composer.lock
run: composer validate
Expand All @@ -49,19 +49,9 @@ jobs:
${{ matrix.composer-options }}

- name: Run tests
if: ${{ matrix.os != 'windows-latest' && matrix.php-version != '8.2' }}
if: ${{ matrix.os != 'windows-latest' }}
run: composer test

- name: Run tests (windows)
if: ${{ matrix.os == 'windows-latest' && matrix.php-version != '8.2' }}
run: composer test-windows

- name: Run tests (experimental)
if: ${{ matrix.os != 'windows-latest' && matrix.php-version == '8.2' }}
continue-on-error: true
run: composer test

- name: Run tests (windows, experimental)
if: ${{ matrix.os == 'windows-latest' && matrix.php-version == '8.2' }}
continue-on-error: true
if: ${{ matrix.os == 'windows-latest' }}
run: composer test-windows
25 changes: 11 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,18 @@ In order to use this adapter you need to add a new dependency to your **composer
```
{
"require": {
"php": "^8",
"allure-framework/allure-phpunit": "^2"
"php": "^8.1",
"allure-framework/allure-phpunit": "^3"
}
}
```
Then add Allure test listener in **phpunit.xml** file:
```xml
<extensions>
<extension class="Qameta\Allure\PHPUnit\AllureExtension">
<!-- Optional arguments block; omit it if you want to use default values -->
<arguments>
<!-- Path to config file (default is config/allure.config.php) -->
<string>config/allure.config.php</string>
</arguments>
</extension>
<bootstrap class="Qameta\Allure\PHPUnit\AllureExtension">
<!-- Path to config file (default is config/allure.config.php) -->
<parameter name="config" value="config/allure.config.php" />
</bootstrap>
</extensions>
```
Config is common PHP file that should return an array:
Expand Down Expand Up @@ -84,17 +81,17 @@ After running PHPUnit tests a new folder will be created (**build/allure-results
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/allure1/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 **#[DisplayName]** annotation:
```php
namespace Example\Tests;

use PHPUnit\Framework\TestCase;
use Qameta\Allure\Attribute\Title;
use Qameta\Allure\Attribute\DisplayName;

#[Title("Human-readable test class title")]
#[DisplayName("Human-readable test class title")]
class SomeTest extends TestCase
{
#[Title("Human-readable test method title")]
#[DisplayName("Human-readable test method title")]
public function testCaseMethod(): void
{
//Some implementation here...
Expand Down Expand Up @@ -251,7 +248,7 @@ class SomeTest extends TestCase
Title("Second step"),
Parameter("param2", "value2"),
]
private function stepTwo()
private function stepTwo(): void
{
//Some implementation here...
}
Expand Down
25 changes: 11 additions & 14 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,19 @@
"source": "https://github.com/allure-framework/allure-phpunit"
},
"require": {
"php": "^8",
"php": "~8.1 || ~8.2 || ~8.3",
"allure-framework/allure-php-commons": "^2",
"phpunit/phpunit": "^9"
"phpunit/phpunit": "^10"
},
"require-dev": {
"brianium/paratest": "^6.8",
"brianium/paratest": "^7",
"psalm/plugin-phpunit": "^0.18.4",
"squizlabs/php_codesniffer": "^3.7.1",
"vimeo/psalm": "^5.4"
"squizlabs/php_codesniffer": "^3.7.2",
"vimeo/psalm": "^5.15"
},
"conflict": {
"amphp/byte-stream": "<1.5.1"
"amphp/byte-stream": "<1.5.1",
"brianium/paratest": "<7.0.3"
},
"autoload": {
"psr-4": {
Expand All @@ -63,27 +64,23 @@
"test-report": [
"@clear-allure-results",
"vendor/bin/paratest --processes=3 --configuration=phpunit.report.xml --testsuite=positive",
"vendor/bin/paratest --processes=3 --configuration=phpunit.report.xml --testsuite=negative; exit 0",
"vendor/bin/paratest --processes=3 --configuration=phpunit.report.xml --testsuite=retries --repeat=3; exit 0"
"vendor/bin/paratest --processes=3 --configuration=phpunit.report.xml --testsuite=negative; exit 0"
],
"test-report-windows": [
"@clear-allure-results",
"vendor/bin/paratest --processes=3 --configuration=phpunit.report.xml --testsuite=positive",
"vendor/bin/paratest --processes=3 --configuration=phpunit.report.xml --testsuite=negative & exit 0",
"vendor/bin/paratest --processes=3 --configuration=phpunit.report.xml --testsuite=retries --repeat=3 & exit 0"
"vendor/bin/paratest --processes=3 --configuration=phpunit.report.xml --testsuite=negative & exit 0"
],
"test-psalm": "vendor/bin/psalm --shepherd",
"test": [
"@test-cs",
"@test-unit",
"@test-report",
"@test-psalm"
"@test-report"
],
"test-windows": [
"@test-cs",
"@test-unit",
"@test-report-windows",
"@test-psalm"
"@test-report-windows"
]
}
}
8 changes: 2 additions & 6 deletions phpunit.report.xml
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
colors="true"
defaultTestSuite="positive">
<testsuites>
<testsuite name="positive">
<directory>test/report/Generate</directory>
<exclude>test/report/Generate/NegativeTest.php</exclude>
<exclude>test/report/Generate/RetriesTest.php</exclude>
</testsuite>
<testsuite name="negative">
<file>test/report/Generate/NegativeTest.php</file>
</testsuite>
<testsuite name="retries">
<file>test/report/Generate/RetriesTest.php</file>
</testsuite>
</testsuites>
<extensions>
<extension class="Qameta\Allure\PHPUnit\AllureExtension" />
<bootstrap class="Qameta\Allure\PHPUnit\AllureExtension" />
</extensions>
</phpunit>
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
colors="true"
defaultTestSuite="unit">
<testsuites>
<testsuite name="unit">
<directory>test/unit/</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
Expand Down
4 changes: 4 additions & 0 deletions psalm.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
findUnusedBaselineEntry="true"
findUnusedCode="false"
findUnusedPsalmSuppress="false"
findUnusedVariablesAndParams="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd">
Expand Down
140 changes: 27 additions & 113 deletions src/AllureExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@

namespace Qameta\Allure\PHPUnit;

use PHPUnit\Runner\AfterIncompleteTestHook;
use PHPUnit\Runner\AfterRiskyTestHook;
use PHPUnit\Runner\AfterSkippedTestHook;
use PHPUnit\Runner\AfterSuccessfulTestHook;
use PHPUnit\Runner\AfterTestErrorHook;
use PHPUnit\Runner\AfterTestFailureHook;
use PHPUnit\Runner\AfterTestHook;
use PHPUnit\Runner\AfterTestWarningHook;
use PHPUnit\Runner\BeforeTestHook;
use PHPUnit\Runner\Extension\Extension;
use PHPUnit\Runner\Extension\Facade;
use PHPUnit\Runner\Extension\ParameterCollection;
use PHPUnit\TextUI\Configuration\Configuration;
use Qameta\Allure\Allure;
use Qameta\Allure\Model\LinkType;
use Qameta\Allure\Model\Status;
use Qameta\Allure\PHPUnit\Internal\Config;
use Qameta\Allure\PHPUnit\Internal\ConfigInterface;
use Qameta\Allure\PHPUnit\Internal\DefaultThreadDetector;
Expand All @@ -29,37 +23,20 @@

use const DIRECTORY_SEPARATOR;

final class AllureExtension implements
BeforeTestHook,
AfterTestHook,
AfterTestFailureHook,
AfterTestErrorHook,
AfterIncompleteTestHook,
AfterSkippedTestHook,
AfterTestWarningHook,
AfterRiskyTestHook,
AfterSuccessfulTestHook
final class AllureExtension implements Extension
{
private const DEFAULT_OUTPUT_DIRECTORY = 'build' . DIRECTORY_SEPARATOR . 'allure-results';

private const DEFAULT_CONFIG_FILE = 'config' . DIRECTORY_SEPARATOR . 'allure.config.php';

private TestLifecycleInterface $testLifecycle;

public function __construct(
string|array|ConfigInterface|TestLifecycleInterface|null $configOrTestLifecycle = null,
private readonly ?TestLifecycleInterface $testLifecycle = null,
) {
$this->testLifecycle = $configOrTestLifecycle instanceof TestLifecycleInterface
? $configOrTestLifecycle
: $this->createTestLifecycle($configOrTestLifecycle);
}

private function createTestLifecycle(string|array|ConfigInterface|null $configSource): TestLifecycleInterface
private function createTestLifecycle(?string $configSource): TestLifecycleInterface
{
$config = $configSource instanceof ConfigInterface
? $configSource
: $this->loadConfig($configSource);

$config = new Config($this->loadConfigData($configSource));
$this->setupAllure($config);

return new TestLifecycle(
Expand Down Expand Up @@ -95,15 +72,6 @@ private function setupAllure(ConfigInterface $config): void
}
}

private function loadConfig(string|array|null $configSource): ConfigInterface
{
return new Config(
is_array($configSource)
? $configSource
: $this->loadConfigData($configSource),
);
}

private function loadConfigData(?string $configFile): array
{
$fileShouldExist = isset($configFile);
Expand All @@ -121,80 +89,26 @@ private function loadConfigData(?string $configFile): array

return [];
}
public function executeBeforeTest(string $test): void
{
$this
->testLifecycle
->switchTo($test)
->reset()
->create()
->updateInfo()
->start();
}

public function executeAfterTest(string $test, float $time): void
{
$this
->testLifecycle
->switchTo($test)
->stop()
->updateRunInfo()
->write();
}

public function executeAfterTestFailure(string $test, string $message, float $time): void
{
$this
->testLifecycle
->switchTo($test)
->updateDetectedStatus($message, Status::failed(), Status::failed());
}

public function executeAfterTestError(string $test, string $message, float $time): void
{
$this
->testLifecycle
->switchTo($test)
->updateDetectedStatus($message, Status::broken());
}

public function executeAfterIncompleteTest(string $test, string $message, float $time): void
{
$this
->testLifecycle
->switchTo($test)
->updateStatus($message, Status::broken());
}

public function executeAfterSkippedTest(string $test, string $message, float $time): void
{
$this
->testLifecycle
->switchTo($test)
->updateStatus($message, Status::skipped());
}

public function executeAfterTestWarning(string $test, string $message, float $time): void
public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void
{
$this
->testLifecycle
->switchTo($test)
->updateStatus($message, Status::broken());
}

public function executeAfterRiskyTest(string $test, string $message, float $time): void
{
$this
->testLifecycle
->switchTo($test)
->updateStatus($message, Status::failed());
}

public function executeAfterSuccessfulTest(string $test, float $time): void
{
$this
->testLifecycle
->switchTo($test)
->updateStatus(status: Status::passed());
$configSource = $parameters->has('config')
? $parameters->get('config')
: null;

$testLifecycle = $this->testLifecycle ?? $this->createTestLifecycle($configSource);

$facade->registerSubscribers(
new Event\TestPreparationStartedSubscriber($testLifecycle),
new Event\TestPreparedSubscriber($testLifecycle),
new Event\TestFinishedSubscriber($testLifecycle),
new Event\TestFailedSubscriber($testLifecycle),
new Event\TestErroredSubscriber($testLifecycle),
new Event\TestMarkedIncompleteSubscriber($testLifecycle),
new Event\TestSkippedSubscriber($testLifecycle),
new Event\TestWarningTriggeredSubscriber($testLifecycle),
new Event\TestConsideredRiskySubscriber($testLifecycle),
new Event\TestPassedSubscriber($testLifecycle),
);
}
}
Loading

0 comments on commit a279254

Please sign in to comment.