Skip to content

Commit

Permalink
Try updating to phpunit 10
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Devlamynck committed Dec 31, 2024
1 parent 8393d4b commit e4c5871
Show file tree
Hide file tree
Showing 12 changed files with 411 additions and 1,039 deletions.
1 change: 0 additions & 1 deletion .github/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: '3.7'
services:
application:
user: '1001:116'
12 changes: 6 additions & 6 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on: [push, pull_request]

jobs:
build-and-test:
runs-on: ubuntu-18.04
runs-on: ubuntu-24.04
steps:
- name: Checkout 🛎️
uses: actions/checkout@v2
Expand All @@ -12,7 +12,7 @@ jobs:

- name: Start docker 🔧
run: |
docker-compose -f docker-compose.yml -f .github/docker-compose.yml up -d
docker compose -f docker-compose.yml -f .github/docker-compose.yml up -d
- name: Cache multiple paths
uses: actions/cache@v2
Expand All @@ -23,15 +23,15 @@ jobs:

- name: Install the dependencies 🔧
run: |
docker-compose exec -T application composer install --prefer-dist --no-interaction --no-progress
docker compose exec -T application composer install --prefer-dist --no-interaction --no-progress
- name: Execute the tests 🔧
run: |
docker-compose exec -T application php -d extension=pcov.so ./bin/phpunit --configuration phpunit.xml.dist --colors=never --coverage-text --log-junit reports/junit.xml --coverage-html reports/coverage --coverage-clover build/logs/clover.xml
docker compose exec -T application php -d extension=pcov.so ./bin/phpunit --configuration phpunit.xml.dist --colors=never --coverage-text --log-junit reports/junit.xml --coverage-html reports/coverage --coverage-clover build/logs/clover.xml
- name: Upload results to Coveralls 🚀
env:
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_SECRET }}
run: |
docker-compose exec -T application composer global require php-coveralls/php-coveralls --prefer-dist --no-interaction --no-progress
docker-compose exec -T -e COVERALLS_REPO_TOKEN="$COVERALLS_REPO_TOKEN" application /home/application/.composer/vendor/bin/php-coveralls --coverage_clover=build/logs/clover.xml -v
docker compose exec -T application composer global require php-coveralls/php-coveralls --prefer-dist --no-interaction --no-progress
docker compose exec -T -e COVERALLS_REPO_TOKEN="$COVERALLS_REPO_TOKEN" application /home/application/.composer/vendor/bin/php-coveralls --coverage_clover=build/logs/clover.xml -v
168 changes: 15 additions & 153 deletions PHPUnitExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,161 +2,23 @@

namespace RichCongress\TestFramework;

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\BeforeFirstTestHook;
use PHPUnit\Runner\AfterLastTestHook;
use PHPUnit\Runner\BeforeTestHook;
use RichCongress\TestFramework\TestHook\TestConfigurationHook;
use RichCongress\TestFramework\TestHook\TestHookInterface;

/**
* Class PHPUnitExtension
*
* @package RichCongress\TestFramework
* @author Nicolas Guilloux <[email protected]>
* @copyright 2014 - 2020 RichCongress (https://www.richcongress.com)
*/
class PHPUnitExtension implements
AfterIncompleteTestHook,
AfterLastTestHook,
AfterRiskyTestHook,
AfterSkippedTestHook,
AfterSuccessfulTestHook,
AfterTestErrorHook,
AfterTestFailureHook,
AfterTestHook,
AfterTestWarningHook,
BeforeFirstTestHook,
BeforeTestHook
use PHPUnit\Event\Test\PreparationStarted;
use PHPUnit\Event\Test\PreparationStartedSubscriber;
use PHPUnit\Runner\Extension\Extension;
use PHPUnit\Runner\Extension\Facade;
use PHPUnit\Runner\Extension\ParameterCollection;
use PHPUnit\TextUI\Configuration\Configuration;
use RichCongress\TestFramework\TestConfiguration\TestConfiguration;

class PHPUnitExtension implements Extension
{
/** @var array|string[] */
public static $supportedHooks = [
TestConfigurationHook::class
];

/** @var array */
protected $hooks = [
'executeAfterIncompleteTest' => [],
'executeAfterLastTest' => [],
'executeAfterRiskyTest' => [],
'executeAfterSkippedTest' => [],
'executeAfterSuccessfulTest' => [],
'executeAfterTest' => [],
'executeAfterTestError' => [],
'executeAfterTestFailure' => [],
'executeAfterTestWarning' => [],
'executeBeforeFirstTest' => [],
'executeBeforeTest' => [],
];

/**
* PHPUnitExtension constructor.
*
* @param string ...$hooksClass
*/
public function __construct(string ...$hooksClass)
public function bootstrap(Configuration $configuration, Facade $facade, ParameterCollection $parameters): void
{
$supportedHooks = array_merge($hooksClass, static::$supportedHooks);

foreach ($supportedHooks as $class) {
/** @var TestHookInterface $hook */
$hook = new $class();
$priorities = $hook->getExecutionPriorities();

foreach (array_keys($this->hooks) as $method) {
$this->hooks[$method][] = [
'hook' => $hook,
'priority' => $priorities[$method] ?? 0,
];
$facade->registerSubscriber(new class implements PreparationStartedSubscriber {
public function notify(PreparationStarted $event): void
{
TestConfiguration::registerTestConfig($event->test());
}
}

foreach ($this->hooks as $method => $hooksParams) {
usort(
$hooksParams,
static function (array $a, array $b) {
$aPriority = $a['priority'] ?? 0;
$bPriority = $b['priority'] ?? 0;

if ($aPriority === $bPriority) {
return 0;
}

return ($aPriority < $bPriority) ? 1 : -1;
}
);

$this->hooks[$method] = $hooksParams;
}
}

public function executeAfterIncompleteTest(string $test, string $message, float $time): void
{
$this->callHooks(__FUNCTION__, $test, $message, $time);
}

public function executeAfterLastTest(): void
{
$this->callHooks(__FUNCTION__);
}

public function executeAfterRiskyTest(string $test, string $message, float $time): void
{
$this->callHooks(__FUNCTION__, $test, $message, $time);
}

public function executeAfterSkippedTest(string $test, string $message, float $time): void
{
$this->callHooks(__FUNCTION__, $test, $message, $time);
}

public function executeAfterSuccessfulTest(string $test, float $time): void
{
$this->callHooks(__FUNCTION__, $test, $time);
}

public function executeAfterTestError(string $test, string $message, float $time): void
{
$this->callHooks(__FUNCTION__, $test, $message, $time);
}

public function executeAfterTestFailure(string $test, string $message, float $time): void
{
$this->callHooks(__FUNCTION__, $test, $message, $time);
}

public function executeAfterTest(string $test, float $time): void
{
$this->callHooks(__FUNCTION__, $test, $time);
}

public function executeAfterTestWarning(string $test, string $message, float $time): void
{
$this->callHooks(__FUNCTION__, $test, $message, $time);
}

public function executeBeforeFirstTest(): void
{
$this->callHooks(__FUNCTION__);
}

public function executeBeforeTest(string $test): void
{
$this->callHooks(__FUNCTION__, $test);
}

public function callHooks(string $method, ...$arguments): void
{
foreach ($this->hooks[$method] as $hookParams) {
$callback = [$hookParams['hook'], $method];
\call_user_func_array($callback, $arguments);
}
});
}
}
15 changes: 15 additions & 0 deletions TestConfiguration/TestConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace RichCongress\TestFramework\TestConfiguration;

use PHPUnit\Event\Code\Test;
use PHPUnit\Event\Code\TestMethod;
use PHPUnit\Event\Test\AfterTestMethodFinished;
use RichCongress\TestFramework\TestConfiguration\Attribute\TestConfig;

/**
Expand All @@ -24,6 +27,18 @@ private function __construct()
// Avoid instantiation
}

public static function registerTestConfig(Test $test): void
{
if (!$test instanceof TestMethod) {
throw new \RuntimeException("test-framework only supports tests in class extending phpunit's TestCase. Other test kinds such as phpt are not supported yet.");
}

$testConfig = TestConfigurationExtractor::getRecursively($test->className(), $test->methodName())
?? new TestConfig();

TestConfiguration::setCurrentTestConfig($testConfig);
}

public static function setCurrentTestConfig(TestConfig $testConfig): void
{
self::$currentTestConfig = $testConfig;
Expand Down
141 changes: 0 additions & 141 deletions TestHook/AbstractTestHook.php

This file was deleted.

Loading

0 comments on commit e4c5871

Please sign in to comment.