Skip to content

Commit

Permalink
Add phpunit v11 support (#2196)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek authored Apr 2, 2024
1 parent 0d4dc18 commit 3119820
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 9 deletions.
12 changes: 4 additions & 8 deletions demos/_unit-test/fatal-error.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@

$runOnShutdownFx = static function (\Closure $fx) use ($app) {
// relies on https://github.com/atk4/ui/blob/5.0.0/src/App.php#L1108
$app->onHook(App::HOOK_BEFORE_RENDER, static function () use (&$fx) {
if ($fx !== null) {
try {
$fx();
} finally {
$fx = null;
}
}
$hookIndex = $app->onHook(App::HOOK_BEFORE_RENDER, static function () use ($app, &$hookIndex, $fx) {
$app->removeHook(App::HOOK_BEFORE_RENDER, $hookIndex, true);

$fx();
});
};

Expand Down
2 changes: 1 addition & 1 deletion src/HtmlTemplate/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private function encodeValueToHtml(string $value): string
*/
public function set(string $value): self
{
if (\PHP_MAJOR_VERSION === 7 || (\PHP_MAJOR_VERSION === 8 && \PHP_MAJOR_VERSION <= 1) // @phpstan-ignore-line
if (\PHP_VERSION_ID < 80200
? !preg_match('~~u', $value) // much faster in PHP 8.1 and lower
: !mb_check_encoding($value, 'UTF-8')
) {
Expand Down
2 changes: 2 additions & 0 deletions tests/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Atk4\Ui\Exception\LateOutputError;
use Atk4\Ui\HtmlTemplate;
use Nyholm\Psr7\Factory\Psr17Factory;
use PHPUnit\Framework\Attributes\DataProvider;

class AppTest extends TestCase
{
Expand Down Expand Up @@ -186,6 +187,7 @@ public static function provideUrlCases(): iterable
* @param array<0|string, string|int|false> $page
* @param array<string, string> $extraRequestUrlArgs
*/
#[DataProvider('provideUrlCases')]
public function testUrl(string $requestUrl, array $appStickyGetArguments, array $page, array $extraRequestUrlArgs, string $expectedUrl): void
{
$request = (new Psr17Factory())->createServerRequest('GET', $requestUrl);
Expand Down
2 changes: 2 additions & 0 deletions tests/ButtonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Atk4\Core\Phpunit\TestCase;
use Atk4\Ui\Button;
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;

class ButtonTest extends TestCase
{
Expand All @@ -14,6 +15,7 @@ class ButtonTest extends TestCase
/**
* @doesNotPerformAssertions
*/
#[DoesNotPerformAssertions]
public function testButtonIcon(): void
{
$button = new Button(['Load', 'icon' => 'pause']);
Expand Down
3 changes: 3 additions & 0 deletions tests/DemosHttpNoExitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@

namespace Atk4\Ui\Tests;

use PHPUnit\Framework\Attributes\Group;

/**
* Same as DemosHttpTest, only App::callExit is set to false.
*
* @group demos_http
*/
#[Group('demos_http')]
class DemosHttpNoExitTest extends DemosHttpTest
{
/** @var bool set the app->callExit in demo */
Expand Down
4 changes: 4 additions & 0 deletions tests/DemosHttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Psr7\LazyOpenStream;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use Symfony\Component\Process\Process;

/**
* Same as DemosTest, but using native HTTP to check if output and shutdown handlers work correctly.
*
* @group demos_http
*/
#[Group('demos_http')]
class DemosHttpTest extends DemosTest
{
/** @var Process|null */
Expand Down Expand Up @@ -158,6 +161,7 @@ public static function provideDemoCallbackErrorCases(): iterable
/**
* @dataProvider provideDemoLateOutputErrorCases
*/
#[DataProvider('provideDemoLateOutputErrorCases')]
public function testDemoLateOutputError(string $urlTrigger, string $expectedOutput): void
{
$path = '_unit-test/late-output-error.php?' . Callback::URL_QUERY_TRIGGER_PREFIX . $urlTrigger . '=ajax&'
Expand Down
7 changes: 7 additions & 0 deletions tests/DemosTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use GuzzleHttp\Exception\ServerException;
use GuzzleHttp\Promise\FulfilledPromise;
use GuzzleHttp\Psr7\Request;
use PHPUnit\Framework\Attributes\DataProvider;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

Expand Down Expand Up @@ -290,6 +291,7 @@ public static function provideDemosStatusAndHtmlResponseCases(): iterable
/**
* @dataProvider provideDemosStatusAndHtmlResponseCases
*/
#[DataProvider('provideDemosStatusAndHtmlResponseCases')]
public function testDemosStatusAndHtmlResponse(string $path): void
{
$response = $this->getResponseFromRequest($path);
Expand Down Expand Up @@ -322,6 +324,7 @@ public static function provideDemoGetCases(): iterable
/**
* @dataProvider provideDemoGetCases
*/
#[DataProvider('provideDemoGetCases')]
public function testDemoGet(string $path): void
{
$response = $this->getResponseFromRequest($path);
Expand Down Expand Up @@ -398,6 +401,7 @@ public static function provideDemoJsonResponseCases(): iterable
*
* @dataProvider provideDemoJsonResponseCases
*/
#[DataProvider('provideDemoJsonResponseCases')]
public function testDemoJsonResponse(string $path, ?string $expectedExceptionMessage = null): void
{
if (static::class === self::class) {
Expand Down Expand Up @@ -436,6 +440,7 @@ public static function provideDemoSseResponseCases(): iterable
*
* @dataProvider provideDemoSseResponseCases
*/
#[DataProvider('provideDemoSseResponseCases')]
public function testDemoSseResponse(string $path): void
{
// this test requires SessionTrait, more precisely session_start() which we do not support in non-HTTP testing
Expand Down Expand Up @@ -476,6 +481,7 @@ public static function provideDemoJsonResponsePostCases(): iterable
/**
* @dataProvider provideDemoJsonResponsePostCases
*/
#[DataProvider('provideDemoJsonResponsePostCases')]
public function testDemoJsonResponsePost(string $path, array $postData): void
{
$response = $this->getResponseFromRequest($path, ['form_params' => $postData]);
Expand All @@ -488,6 +494,7 @@ public function testDemoJsonResponsePost(string $path, array $postData): void
*
* @slowThreshold 1500
*/
#[DataProvider('provideDemoCallbackErrorCases')]
public function testDemoCallbackError(string $path, string $expectedExceptionMessage, array $options = []): void
{
if (static::class === self::class) {
Expand Down
2 changes: 2 additions & 0 deletions tests/ListerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Atk4\Ui\HtmlTemplate;
use Atk4\Ui\Lister;
use Atk4\Ui\View;
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;

class ListerTest extends TestCase
{
Expand All @@ -17,6 +18,7 @@ class ListerTest extends TestCase
/**
* @doesNotPerformAssertions
*/
#[DoesNotPerformAssertions]
public function testListerRender(): void
{
$view = new View();
Expand Down
2 changes: 2 additions & 0 deletions tests/PaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Atk4\Core\Phpunit\TestCase;
use Atk4\Ui\Paginator;
use PHPUnit\Framework\Attributes\DataProvider;

class PaginatorTest extends TestCase
{
Expand Down Expand Up @@ -34,6 +35,7 @@ public static function providePaginatorCases(): iterable
/**
* @dataProvider providePaginatorCases
*/
#[DataProvider('providePaginatorCases')]
public function testPaginator(int $page, int $range, int $total, array $expected): void
{
$paginator = new Paginator(['page' => $page, 'range' => $range, 'total' => $total]);
Expand Down
4 changes: 4 additions & 0 deletions tests/PersistenceUiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Atk4\Core\Phpunit\TestCase;
use Atk4\Data\Field;
use Atk4\Ui\Persistence\Ui as UiPersistence;
use PHPUnit\Framework\Attributes\DataProvider;

class PersistenceUiTest extends TestCase
{
Expand All @@ -16,6 +17,8 @@ class PersistenceUiTest extends TestCase
* @dataProvider provideTypecastBidirectionalCases
* @dataProvider provideTypecastLoadOnlyCases
*/
#[DataProvider('provideTypecastBidirectionalCases')]
#[DataProvider('provideTypecastLoadOnlyCases')]
public function testTypecast(array $persistenceSeed, array $fieldSeed, $phpValue, ?string $uiValue, bool $isUiValueNormalized = true): void
{
$p = (new UiPersistence())->setDefaults($persistenceSeed);
Expand Down Expand Up @@ -203,6 +206,7 @@ public static function provideTypecastLoadOnlyCases(): iterable
*
* @dataProvider provideAttributeTypecastCases
*/
#[DataProvider('provideAttributeTypecastCases')]
public function testAttributeTypecast(array $fieldSeed, $phpValue, ?string $uiValue): void
{
$p = new UiPersistence();
Expand Down
4 changes: 4 additions & 0 deletions tests/SessionTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
use Atk4\Ui\App;
use Atk4\Ui\Exception;
use Atk4\Ui\SessionTrait;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;

/**
* @group require_session
*
* @runTestsInSeparateProcesses
*/
#[Group('require_session')]
#[RunTestsInSeparateProcesses]
class SessionTraitTest extends TestCase
{
use CreateAppTrait;
Expand Down
2 changes: 2 additions & 0 deletions tests/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Atk4\Core\Phpunit\TestCase;
use Atk4\Ui\Exception;
use Atk4\Ui\Table;
use PHPUnit\Framework\Attributes\DoesNotPerformAssertions;

class TableTest extends TestCase
{
Expand All @@ -15,6 +16,7 @@ class TableTest extends TestCase
/**
* @doesNotPerformAssertions
*/
#[DoesNotPerformAssertions]
public function testAddColumnWithoutModel(): void
{
$table = new Table();
Expand Down
2 changes: 2 additions & 0 deletions tests/ViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Atk4\Ui\Popup;
use Atk4\Ui\View;
use Atk4\Ui\VirtualPage;
use PHPUnit\Framework\Attributes\DataProvider;

class ViewTest extends TestCase
{
Expand Down Expand Up @@ -146,6 +147,7 @@ public function testSetException(): void
*
* @dataProvider provideSetNotClosureErrorCases
*/
#[DataProvider('provideSetNotClosureErrorCases')]
public function testSetNotClosureError(string $class): void
{
$v = new $class();
Expand Down

0 comments on commit 3119820

Please sign in to comment.