Skip to content

Commit

Permalink
Upgrade to PHPUnit v11 [SLE-196][SLE-14]
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelgfeller committed Mar 19, 2024
1 parent adda351 commit 6de4671
Show file tree
Hide file tree
Showing 56 changed files with 627 additions and 622 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"require-dev": {
"roave/security-advisories": "dev-latest",
"phpunit/phpunit": "^10",
"phpunit/phpunit": "^11",
"selective/test-traits": "^4",
"phpstan/phpstan": "^1",
"jetbrains/phpstorm-attributes": "^1.0",
Expand Down
2 changes: 1 addition & 1 deletion config/container.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

// When testing, 'test' value is true which means the monolog test handler should be used
if (isset($loggerSettings['test']) && $loggerSettings['test'] === true) {
return $logger->pushHandler(new \Monolog\Handler\TestHandler());
return $logger->pushHandler(new Monolog\Handler\TestHandler());
}

// Instantiate logger with rotating file handler
Expand Down
4 changes: 2 additions & 2 deletions config/defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
'database' => 'slim_example_project',
'username' => 'root',
'password' => '',
'driver' => \Cake\Database\Driver\Mysql::class,
'driver' => Cake\Database\Driver\Mysql::class,
'encoding' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
// Enable identifier quoting
Expand Down Expand Up @@ -163,7 +163,7 @@
// Log file location
'path' => $settings['root_dir'] . '/logs',
// Default log level
'level' => \Monolog\Level::Debug,
'level' => Monolog\Level::Debug,
];

// Email settings
Expand Down
2 changes: 1 addition & 1 deletion config/env/env.prod.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@

$settings['db']['database'] = 'samuelgfeller_demo_slim_example_project';

$settings['logger']['level'] = \Monolog\Level::Info;
$settings['logger']['level'] = Monolog\Level::Info;
2 changes: 1 addition & 1 deletion config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
$app->post('/login', \App\Application\Action\Authentication\Ajax\LoginSubmitAction::class)
->setName('login-submit');
$app->get('/logout', \App\Application\Action\Authentication\Page\LogoutPageAction::class)->setName('logout')->add(
\Odan\Session\Middleware\SessionStartMiddleware::class
Odan\Session\Middleware\SessionStartMiddleware::class
);

// Authentication - email verification - token
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public function __construct(
* @param ServerRequestInterface $request
* @param ResponseInterface $response
*
* @return ResponseInterface
* @throws \Throwable
*
* @return ResponseInterface
*/
public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
Expand Down Expand Up @@ -68,7 +68,7 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
// the password-forgotten form is shown instead of the login form.
return $this->templateRenderer->render($response, 'authentication/login.html.php');
} // Validation Exception has to be caught here and not middleware as the token,
// and id have to be added to php view
// and id have to be added to php view
catch (ValidationException $validationException) {
// Render reset-password form with token, and id so that it can be submitted again
$flash->add('error', $validationException->getMessage());
Expand Down
9 changes: 8 additions & 1 deletion src/Application/ErrorHandler/DefaultErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,14 @@ public function __invoke(
if ($exception instanceof \PDOException && str_contains($exception->getMessage(), 'Column not found')) {
echo "Column not existing. Try running `composer schema:generate` in the console and run tests again. \n";
}
// The exception is thrown to have the standard behaviour (important for testing)

// Restore previous error handler when the exception has been thrown to satisfy PHPUnit v11
// It is restored in the post-processing of the NonFatalErrorHandlerMiddleware, but the code doesn't
// reach it when there's an exception (especially needed for tests expecting an exception).
// Related PR: https://github.com/sebastianbergmann/phpunit/pull/5619
restore_error_handler();

// The exception is thrown to have the standard behaviour (important for testing).
throw $exception;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ function ($severity, $message, $file, $line) {
}
);

return $handler->handle($request);
$response = $handler->handle($request);

// Restore previous error handler in post-processing to satisfy PHPUnit 11 that checks for any
// leftover error handlers https://github.com/sebastianbergmann/phpunit/pull/5619
restore_error_handler();

return $response;
}
}
3 changes: 2 additions & 1 deletion src/Domain/Client/Data/ClientReadResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public function __construct(array $clientResultData = [])
}

/**
* Define how json_encode() should serialize the object
* Define how json_encode() should serialize the object.
*
* @return array in the format expected by the frontend
*/
public function jsonSerialize(): array
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function buildWhereArrayWithFilterParams(array $filterParams = ['deleted_
* @param string $column column reference
* @param string|int|array|null $value value reference
*/
private function adaptColumnValueToQueryBuilder(string &$column, null|string|int|array &$value): void
private function adaptColumnValueToQueryBuilder(string &$column, string|int|array|null &$value): void
{
// If empty string it means that value should be null
if ($value === '') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public function isGrantedToCreate(array $userValues): bool
*/
public function userRoleIsGranted(
string|int|null $newUserRoleId,
null|string|int $userRoleIdOfUserToMutate,
string|int|null $userRoleIdOfUserToMutate,
?int $authenticatedUserRoleHierarchy = null,
?array $userRoleHierarchies = null,
): bool {
Expand Down
2 changes: 1 addition & 1 deletion src/Domain/UserActivity/Service/UserActivityFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(
*
* @return array
*/
public function findUserActivityReport(null|array|int|string $userIds): array
public function findUserActivityReport(array|int|string|null $userIds): array
{
if ($userIds) {
return $this->findUserActivitiesGroupedByDate($userIds);
Expand Down
2 changes: 1 addition & 1 deletion src/Infrastructure/Console/SqlSchemaGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public function generateSqlSchema(): int
*
* @param string $sql The sql
*
* @return PDOStatement The statement
* @throws UnexpectedValueException
*
* @return PDOStatement The statement
*/
private function query(string $sql): PDOStatement
{
Expand Down
6 changes: 3 additions & 3 deletions src/Infrastructure/Service/LocaleConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(Settings $settings)
*
* @return false|string the new locale string, or false on failure
*/
public function setLanguage(string|null|false $locale, string $domain = 'messages'): bool|string
public function setLanguage(string|false|null $locale, string $domain = 'messages'): bool|string
{
$codeset = 'UTF-8';
$directory = $this->localeSettings['translations_path'];
Expand Down Expand Up @@ -83,7 +83,7 @@ public function getLanguageCodeForPath(): string
*
* @return string
*/
private function getAvailableLocale(null|false|string $locale): string
private function getAvailableLocale(false|string|null $locale): string
{
$availableLocales = $this->localeSettings['available'];

Expand Down Expand Up @@ -116,7 +116,7 @@ private function getAvailableLocale(null|false|string $locale): string
*
* @return string|null e.g. 'en'
*/
private function getLanguageCodeFromLocale(string|null|false $locale): ?string
private function getLanguageCodeFromLocale(string|false|null $locale): ?string
{
// If locale has hyphen instead of underscore, replace it
if ($locale && str_contains($locale, '-')) {
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixture/FixtureInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/
interface FixtureInterface
{
// Attributes are public but php doesn't support class properties in interfaces so getters are needed
// Attributes are public, but php doesn't support class properties in interfaces, so getters are needed
public function getTable(): string;

public function getRecords(): array;
Expand Down
27 changes: 6 additions & 21 deletions tests/Integration/Authentication/AccountUnlockActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
use App\Domain\Authentication\Data\UserVerificationData;
use App\Domain\User\Enum\UserStatus;
use App\Test\Fixture\UserFixture;
use App\Test\Provider\Authentication\UserVerificationProvider;
use App\Test\Traits\AppTestTrait;
use App\Test\Traits\FixtureTestTrait;
use Fig\Http\Message\StatusCodeInterface;
use Odan\Session\SessionInterface;
use PHPUnit\Framework\Attributes\DataProviderExternal;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Selective\TestTrait\Traits\DatabaseTestTrait;
use Selective\TestTrait\Traits\HttpTestTrait;
use Selective\TestTrait\Traits\MailerTestTrait;
Expand All @@ -36,12 +36,8 @@ class AccountUnlockActionTest extends TestCase

/**
* Test that with given correct token the account status is set to active.
*
* @dataProvider \App\Test\Provider\Authentication\UserVerificationProvider::userVerificationProvider()
*
* @param UserVerificationData $verification
* @param string $clearTextToken
*/
#[DataProviderExternal(UserVerificationProvider::class, 'userVerificationProvider')]
public function testAccountUnlockAction(UserVerificationData $verification, string $clearTextToken): void
{
// Insert locked user
Expand Down Expand Up @@ -83,12 +79,8 @@ public function testAccountUnlockAction(UserVerificationData $verification, stri
/**
* Test that with given used, invalid or expired token the account cannot be unlocked
* This is a very important test for security.
*
* @dataProvider \App\Test\Provider\Authentication\UserVerificationProvider::userVerificationInvalidTokenProvider()
*
* @param UserVerificationData $verification
* @param string $clearTextToken
*/
#[DataProviderExternal(UserVerificationProvider::class, 'userVerificationInvalidTokenProvider')]
public function testAccountUnlockActionInvalidExpiredToken(
UserVerificationData $verification,
string $clearTextToken
Expand Down Expand Up @@ -131,17 +123,10 @@ public function testAccountUnlockActionInvalidExpiredToken(
}

/**
* Test that if user has status already on active he gets redirected
* Test that if the user has status already on active he gets redirected
* but not authenticated.
*
* @dataProvider \App\Test\Provider\Authentication\UserVerificationProvider::userVerificationProvider()
*
* @param UserVerificationData $verification
* @param string $clearTextToken
*
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
#[DataProviderExternal(UserVerificationProvider::class, 'userVerificationProvider')]
public function testAccountUnlockActionAlreadyUnlocked(
UserVerificationData $verification,
string $clearTextToken
Expand Down
7 changes: 3 additions & 4 deletions tests/Integration/Authentication/LoginSubmitActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use App\Test\Traits\FixtureTestTrait;
use Fig\Http\Message\StatusCodeInterface;
use Odan\Session\SessionInterface;
use PHPUnit\Framework\Attributes\DataProviderExternal;
use PHPUnit\Framework\TestCase;
use Selective\TestTrait\Traits\DatabaseTestTrait;
use Selective\TestTrait\Traits\HttpTestTrait;
Expand Down Expand Up @@ -109,11 +110,10 @@ public function testLoginSubmitActionWrongPassword(): void
/**
* Test login with invalid values that must not pass validation.
*
* @dataProvider \App\Test\Provider\Authentication\AuthenticationProvider::invalidLoginCredentialsProvider()
*
* @param array $invalidLoginValues valid credentials
* @param string $errorMessage validation message that should be in response body
*/
#[DataProviderExternal(\App\Test\Provider\Authentication\AuthenticationProvider::class, 'invalidLoginCredentialsProvider')]
public function testLoginSubmitActionInvalidValues(array $invalidLoginValues, string $errorMessage): void
{
$this->insertFixtureWithAttributes(new UserFixture());
Expand Down Expand Up @@ -142,11 +142,10 @@ public function testLoginSubmitActionInvalidValues(array $invalidLoginValues, st
* Test login with user status unverified.
* When account is unverified, a verification link is sent to the user via the email.
*
* @dataProvider \App\Test\Provider\Authentication\AuthenticationProvider::nonActiveAuthenticationRequestCases()
*
* @param UserStatus $status
* @param string $partialEmailBody
*/
#[DataProviderExternal(\App\Test\Provider\Authentication\AuthenticationProvider::class, 'nonActiveAuthenticationRequestCases')]
public function testLoginSubmitActionNotActiveAccount(UserStatus $status, string $partialEmailBody): void
{
$loginValues = ['password' => '12345678', 'email' => 'user@example.com'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
use App\Domain\Authentication\Data\UserVerificationData;
use App\Domain\User\Enum\UserStatus;
use App\Test\Fixture\UserFixture;
use App\Test\Provider\Authentication\UserVerificationProvider;
use App\Test\Traits\AppTestTrait;
use App\Test\Traits\FixtureTestTrait;
use Fig\Http\Message\StatusCodeInterface;
use PHPUnit\Framework\Attributes\DataProviderExternal;
use PHPUnit\Framework\TestCase;
use Selective\TestTrait\Traits\DatabaseTestTrait;
use Selective\TestTrait\Traits\HttpJsonTestTrait;
Expand All @@ -33,11 +35,11 @@ class PasswordResetSubmitActionTest extends TestCase
/**
* Request to reset password with token.
*
* @dataProvider \App\Test\Provider\Authentication\UserVerificationProvider::userVerificationProvider()
*
* @param UserVerificationData $verification
* @param string $clearTextToken
*/
#[DataProviderExternal(UserVerificationProvider::class, 'userVerificationProvider')]
#[DataProviderExternal(UserVerificationProvider::class, 'userVerificationProvider')]
public function testResetPasswordSubmit(UserVerificationData $verification, string $clearTextToken): void
{
$newPassword = 'new password';
Expand Down Expand Up @@ -73,11 +75,10 @@ public function testResetPasswordSubmit(UserVerificationData $verification, stri
/**
* Test password submit reset with invalid, used or expired token.
*
* @dataProvider \App\Test\Provider\Authentication\UserVerificationProvider::userVerificationInvalidTokenProvider()
*
* @param UserVerificationData $verification
* @param string $clearTextToken
*/
#[DataProviderExternal(UserVerificationProvider::class, 'userVerificationInvalidTokenProvider')]
public function testResetPasswordSubmitInvalidToken(
UserVerificationData $verification,
string $clearTextToken
Expand Down Expand Up @@ -128,11 +129,10 @@ public function testResetPasswordSubmitInvalidToken(
/**
* Test that backend validation fails when new passwords are invalid.
*
* @dataProvider \App\Test\Provider\Authentication\UserVerificationProvider::userVerificationProvider()
*
* @param UserVerificationData $verification
* @param string $clearTextToken
*/
#[DataProviderExternal(UserVerificationProvider::class, 'userVerificationProvider')]
public function testResetPasswordSubmitInvalidData(
UserVerificationData $verification,
string $clearTextToken
Expand Down
10 changes: 4 additions & 6 deletions tests/Integration/Authentication/RegisterVerifyActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Test\Traits\FixtureTestTrait;
use Fig\Http\Message\StatusCodeInterface;
use Odan\Session\SessionInterface;
use PHPUnit\Framework\Attributes\DataProviderExternal;
use PHPUnit\Framework\TestCase;
use Selective\TestTrait\Traits\DatabaseTestTrait;
use Selective\TestTrait\Traits\HttpTestTrait;
Expand All @@ -34,11 +35,10 @@ class RegisterVerifyActionTest extends TestCase
/**
* Test that with given correct token the account status is set to active.
*
* @dataProvider \App\Test\Provider\Authentication\UserVerificationProvider::userVerificationProvider()
*
* @param UserVerificationData $verification
* @param string $clearTextToken
*/
#[DataProviderExternal(\App\Test\Provider\Authentication\UserVerificationProvider::class, 'userVerificationProvider')]
public function testRegisterVerification(UserVerificationData $verification, string $clearTextToken): void
{
// User needed to insert verification (taking first record from userFixture)
Expand Down Expand Up @@ -80,11 +80,10 @@ public function testRegisterVerification(UserVerificationData $verification, str
/**
* Check that user gets redirect to the home or wanted page and most importantly: that no error is thrown.
*
* @dataProvider \App\Test\Provider\Authentication\UserVerificationProvider::userVerificationProvider()
*
* @param UserVerificationData $verification
* @param string $clearTextToken
*/
#[DataProviderExternal(\App\Test\Provider\Authentication\UserVerificationProvider::class, 'userVerificationProvider')]
public function testRegisterVerificationAlreadyVerified(
UserVerificationData $verification,
string $clearTextToken
Expand Down Expand Up @@ -130,11 +129,10 @@ public function testRegisterVerificationAlreadyVerified(
* Link in email contains the verification db entry id and if this id is incorrect (token not found)
* according exception should be thrown and user redirected to register page.
*
* @dataProvider \App\Test\Provider\Authentication\UserVerificationProvider::userVerificationInvalidTokenProvider()
*
* @param UserVerificationData $verification
* @param string $clearTextToken
*/
#[DataProviderExternal(\App\Test\Provider\Authentication\UserVerificationProvider::class, 'userVerificationInvalidTokenProvider')]
public function testRegisterVerificationInvalidUsedExpiredToken(
UserVerificationData $verification,
string $clearTextToken
Expand Down
Loading

0 comments on commit 6de4671

Please sign in to comment.