From d52116eb5a4e38198ee1b49119f0cc6fb2a607c0 Mon Sep 17 00:00:00 2001 From: Tom Udding Date: Fri, 26 Jan 2024 15:01:49 +0100 Subject: [PATCH] Fix PHPUnit 10 issues PHPUnit 10 removed some methods which were marked as `@internal`. On top of that, a lot of other issues presented itself (which were already incorrect in the past). This fixes that by moving the base `Controller` and `Mapper` tests to a trait. --- module/Activity/test/ControllerTest.php | 7 ++- .../test/Mapper/ActivityMapperTest.php | 13 +++-- .../test/AutomaticControllerTest.php | 26 +++++++-- ...rollerTest.php => BaseControllerTrait.php} | 11 ++-- ...BaseMapperTest.php => BaseMapperTrait.php} | 30 ++++++---- module/Company/test/ControllerTest.php | 7 ++- module/Decision/test/ControllerTest.php | 7 ++- module/Education/test/ControllerTest.php | 7 ++- module/Frontpage/test/ControllerTest.php | 7 ++- module/Photo/test/ControllerTest.php | 7 ++- module/User/test/ControllerTest.php | 7 ++- phpunit.xml | 55 +++++++++---------- psalm/psalm-baseline.xml | 24 +------- 13 files changed, 113 insertions(+), 95 deletions(-) rename module/Application/test/{BaseControllerTest.php => BaseControllerTrait.php} (97%) rename module/Application/test/Mapper/{BaseMapperTest.php => BaseMapperTrait.php} (93%) diff --git a/module/Activity/test/ControllerTest.php b/module/Activity/test/ControllerTest.php index 00f92010e5..58459a7d2b 100644 --- a/module/Activity/test/ControllerTest.php +++ b/module/Activity/test/ControllerTest.php @@ -4,10 +4,13 @@ namespace ActivityTest; -use ApplicationTest\BaseControllerTest; +use ApplicationTest\BaseControllerTrait; +use Laminas\Test\PHPUnit\Controller\AbstractHttpControllerTestCase; -class ControllerTest extends BaseControllerTest +class ControllerTest extends AbstractHttpControllerTestCase { + use BaseControllerTrait; + public function testActivityActionCanBeAccessed(): void { $this->dispatch('/activity'); diff --git a/module/Activity/test/Mapper/ActivityMapperTest.php b/module/Activity/test/Mapper/ActivityMapperTest.php index c39074ad78..5f7f362c1e 100644 --- a/module/Activity/test/Mapper/ActivityMapperTest.php +++ b/module/Activity/test/Mapper/ActivityMapperTest.php @@ -7,24 +7,25 @@ use Activity\Mapper\Activity as ActivityMapper; use Activity\Model\Activity; use Activity\Model\ActivityLocalisedText; -use Application\Mapper\BaseMapper; -use ApplicationTest\Mapper\BaseMapperTest; +use ApplicationTest\Mapper\BaseMapperTrait; use DateTime; use Decision\Model\Enums\MembershipTypes; use Decision\Model\Member; +use PHPUnit\Framework\TestCase; use User\Model\User; -class ActivityMapperTest extends BaseMapperTest +class ActivityMapperTest extends TestCase { - /** @var ActivityMapper */ - protected BaseMapper $mapper; + /** @use BaseMapperTrait */ + use BaseMapperTrait; + protected User $user; protected Member $member; private ActivityLocalisedText $localisedText; public function setUp(): void { - parent::setUp(); + $this->setUpEntityManager(); $this->mapper = $this->serviceManager->get('activity_mapper_activity'); diff --git a/module/Application/test/AutomaticControllerTest.php b/module/Application/test/AutomaticControllerTest.php index 8e5e6dc72c..d50f5585c3 100644 --- a/module/Application/test/AutomaticControllerTest.php +++ b/module/Application/test/AutomaticControllerTest.php @@ -13,15 +13,21 @@ use Laminas\Router\Http\Regex; use Laminas\Router\Http\Segment; use Laminas\Router\PriorityList; +use Laminas\Test\PHPUnit\Controller\AbstractHttpControllerTestCase; use RuntimeException; use Throwable; use function is_string; use function serialize; use function sprintf; +use function trigger_error; -class AutomaticControllerTest extends BaseControllerTest +use const E_USER_WARNING; + +class AutomaticControllerTest extends AbstractHttpControllerTestCase { + use BaseControllerTrait; + public function testAllRoutes(): void { /** @var LanguageAwareTreeRouteStack $router */ @@ -106,15 +112,25 @@ protected function parseSegment(Segment|Part $element): void $url = $element->assemble($params); $this->parseUrl($url); } catch (InvalidArgumentException $exception) { - $this->addWarning( + trigger_error( // phpcs:ignore Generic.Files.LineLength.TooLong -- user-visible strings should not be split 'Skipping one or multiple route segments/parts because required parameters could not be generated automatically.', + E_USER_WARNING, + ); + trigger_error( + $exception->getMessage(), + E_USER_WARNING, ); - $this->addWarning($exception->getMessage()); try { - $this->addWarning(serialize($element)); + trigger_error( + serialize($element), + E_USER_WARNING, + ); } catch (Throwable) { - $this->addWarning('More details could not be provided through serialization.'); + trigger_error( + 'More details could not be provided through serialization.', + E_USER_WARNING, + ); // A part is not always serializable. } } diff --git a/module/Application/test/BaseControllerTest.php b/module/Application/test/BaseControllerTrait.php similarity index 97% rename from module/Application/test/BaseControllerTest.php rename to module/Application/test/BaseControllerTrait.php index 9c9f4f2ca9..ded733c2f8 100644 --- a/module/Application/test/BaseControllerTest.php +++ b/module/Application/test/BaseControllerTrait.php @@ -15,7 +15,6 @@ use Laminas\Mvc\ApplicationInterface; use Laminas\Mvc\Service\ServiceManagerConfig; use Laminas\ServiceManager\ServiceManager; -use Laminas\Test\PHPUnit\Controller\AbstractHttpControllerTestCase; use PHPUnit\Framework\MockObject\MockObject; use User\Authentication\AuthenticationService; use User\Model\CompanyUser; @@ -27,7 +26,7 @@ use function array_merge; use function array_unique; -abstract class BaseControllerTest extends AbstractHttpControllerTestCase +trait BaseControllerTrait { protected ServiceManager $serviceManager; @@ -229,8 +228,8 @@ private function setUpMockIdentity(string $role): CompanyUser|User|null $this->setUpMockNewCompanyUser(); $this->setUpMockCompanyUser(); - $this->companyMapper->method('find')->willReturnMap([[$this::COMPANY_ID], $this->company]); - $this->companyUserMapper->method('find')->willReturnMap([[$this::COMPANY_ID], $this->companyUser]); + $this->companyMapper->method('find')->willReturnMap([[$this::COMPANY_ID], [$this->company]]); + $this->companyUserMapper->method('find')->willReturnMap([[$this::COMPANY_ID], [$this->companyUser]]); return $this->companyUser; } @@ -252,8 +251,8 @@ private function setUpMockIdentity(string $role): CompanyUser|User|null $roleModel->setLidnr($this->user); } - $this->userMapper->method('find')->willReturnMap([[$this::LIDNR], $this->user]); - $this->memberMapper->method('findByLidnr')->willReturnMap([[$this::LIDNR], $this->member]); + $this->userMapper->method('find')->willReturnMap([[$this::LIDNR], [$this->user]]); + $this->memberMapper->method('findByLidnr')->willReturnMap([[$this::LIDNR], [$this->member]]); return $this->user; } diff --git a/module/Application/test/Mapper/BaseMapperTest.php b/module/Application/test/Mapper/BaseMapperTrait.php similarity index 93% rename from module/Application/test/Mapper/BaseMapperTest.php rename to module/Application/test/Mapper/BaseMapperTrait.php index a5f0e8d9b4..9b5abc79d2 100644 --- a/module/Application/test/Mapper/BaseMapperTest.php +++ b/module/Application/test/Mapper/BaseMapperTrait.php @@ -12,35 +12,38 @@ use Laminas\Mvc\Service\ServiceManagerConfig; use Laminas\ServiceManager\ServiceManager; use Laminas\Stdlib\Exception\LogicException; -use PHPUnit\Framework\TestCase; use RuntimeException; use function array_merge; use function array_unique; +use function trigger_error; -abstract class BaseMapperTest extends TestCase +use const E_USER_WARNING; + +/** + * @psalm-template TMapper of BaseMapper + * @psalm-template TObject of object + */ +trait BaseMapperTrait { /** @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingTraversableTypeHintSpecification */ protected array $applicationConfig; protected ?Application $application = null; protected ServiceManager $serviceManager; protected EntityManager $entityManager; + /** @psalm-var TMapper $mapper */ protected BaseMapper $mapper; + /** @psalm-var TObject $object */ protected object $object; - public function setUp(): void + private function setUpEntityManager(): void { $this->setApplicationConfig(TestConfigProvider::getConfig()); $this->getApplication(); $this->entityManager = $this->serviceManager->get('doctrine.entitymanager.orm_default'); } - protected function getId(object $object): mixed - { - throw new RuntimeException('Not implemented'); - } - - public function getApplication(): Application + private function getApplication(): Application { if ($this->application) { return $this->application; @@ -127,6 +130,11 @@ private function bootstrapApplication( return $serviceManager->get('Application')->bootstrap($listeners); } + protected function getId(object $object): mixed + { + throw new RuntimeException('Not implemented'); + } + public function testGetEntityManager(): void { $this->mapper->getEntityManager(); @@ -202,8 +210,8 @@ public function testRemoveById(): void $this->expectNotToPerformAssertions(); } catch (RuntimeException $e) { if ('Not implemented' !== $e->getMessage()) { - $this->addWarning($e->getMessage()); - $this->addWarning($e->getTraceAsString()); + trigger_error($e->getMessage(), E_USER_WARNING); + trigger_error($e->getTraceAsString(), E_USER_WARNING); $this->fail('testRemoveById threw an unexpected exception.'); } else { $this->expectNotToPerformAssertions(); diff --git a/module/Company/test/ControllerTest.php b/module/Company/test/ControllerTest.php index 9b2b714226..3a75a59554 100644 --- a/module/Company/test/ControllerTest.php +++ b/module/Company/test/ControllerTest.php @@ -4,10 +4,13 @@ namespace CompanyTest; -use ApplicationTest\BaseControllerTest; +use ApplicationTest\BaseControllerTrait; +use Laminas\Test\PHPUnit\Controller\AbstractHttpControllerTestCase; -class ControllerTest extends BaseControllerTest +class ControllerTest extends AbstractHttpControllerTestCase { + use BaseControllerTrait; + public function testCareerActionCanBeAccessed(): void { $this->dispatch('/career'); diff --git a/module/Decision/test/ControllerTest.php b/module/Decision/test/ControllerTest.php index 7abfaedeb5..6e018b1575 100644 --- a/module/Decision/test/ControllerTest.php +++ b/module/Decision/test/ControllerTest.php @@ -4,11 +4,14 @@ namespace DecisionTest; -use ApplicationTest\BaseControllerTest; +use ApplicationTest\BaseControllerTrait; use Laminas\Http\Request; +use Laminas\Test\PHPUnit\Controller\AbstractHttpControllerTestCase; -class ControllerTest extends BaseControllerTest +class ControllerTest extends AbstractHttpControllerTestCase { + use BaseControllerTrait; + public function testMemberActionCanBeAccessedAsUser(): void { $this->setUpWithRole(); diff --git a/module/Education/test/ControllerTest.php b/module/Education/test/ControllerTest.php index 146ca72752..647814d325 100644 --- a/module/Education/test/ControllerTest.php +++ b/module/Education/test/ControllerTest.php @@ -4,10 +4,13 @@ namespace EducationTest; -use ApplicationTest\BaseControllerTest; +use ApplicationTest\BaseControllerTrait; +use Laminas\Test\PHPUnit\Controller\AbstractHttpControllerTestCase; -class ControllerTest extends BaseControllerTest +class ControllerTest extends AbstractHttpControllerTestCase { + use BaseControllerTrait; + public function testEducationActionCanBeAccessed(): void { $this->dispatch('/education'); diff --git a/module/Frontpage/test/ControllerTest.php b/module/Frontpage/test/ControllerTest.php index 67bc8b97aa..05e62a9208 100644 --- a/module/Frontpage/test/ControllerTest.php +++ b/module/Frontpage/test/ControllerTest.php @@ -4,10 +4,13 @@ namespace FrontpageTest; -use ApplicationTest\BaseControllerTest; +use ApplicationTest\BaseControllerTrait; +use Laminas\Test\PHPUnit\Controller\AbstractHttpControllerTestCase; -class ControllerTest extends BaseControllerTest +class ControllerTest extends AbstractHttpControllerTestCase { + use BaseControllerTrait; + /** * @psalm-suppress UnevaluatedCode */ diff --git a/module/Photo/test/ControllerTest.php b/module/Photo/test/ControllerTest.php index d2c0ffcfaa..369f0631f3 100644 --- a/module/Photo/test/ControllerTest.php +++ b/module/Photo/test/ControllerTest.php @@ -4,10 +4,13 @@ namespace PhotoTest; -use ApplicationTest\BaseControllerTest; +use ApplicationTest\BaseControllerTrait; +use Laminas\Test\PHPUnit\Controller\AbstractHttpControllerTestCase; -class ControllerTest extends BaseControllerTest +class ControllerTest extends AbstractHttpControllerTestCase { + use BaseControllerTrait; + public function testPhotoActionIsForbidden(): void { $this->dispatch('/photo'); diff --git a/module/User/test/ControllerTest.php b/module/User/test/ControllerTest.php index 5d103dc0f3..0fc682cd8a 100644 --- a/module/User/test/ControllerTest.php +++ b/module/User/test/ControllerTest.php @@ -4,10 +4,13 @@ namespace UserTest; -use ApplicationTest\BaseControllerTest; +use ApplicationTest\BaseControllerTrait; +use Laminas\Test\PHPUnit\Controller\AbstractHttpControllerTestCase; -class ControllerTest extends BaseControllerTest +class ControllerTest extends AbstractHttpControllerTestCase { + use BaseControllerTrait; + public function testCompanyLoginActionCanBeAccessed(): void { $this->dispatch('/user/login/member'); diff --git a/phpunit.xml b/phpunit.xml index 5c0fdc7c4d..56ecf2db89 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,32 +1,27 @@ - - - - ./module/Activity/src - ./module/Application/src - ./module/Company/src - ./module/Decision/src - ./module/Education/src - ./module/Frontpage/src - ./module/Photo/src - ./module/User/src - - - - - - ./module/Activity/test/ - ./module/Application/test/ - ./module/Company/test/ - ./module/Decision/test/ - ./module/Education/test/ - ./module/Frontpage/test/ - ./module/Photo/test/ - ./module/User/test/ - - + + + + ./module/Activity/test/ + ./module/Application/test/ + ./module/Company/test/ + ./module/Decision/test/ + ./module/Education/test/ + ./module/Frontpage/test/ + ./module/Photo/test/ + ./module/User/test/ + + + + + ./module/Activity/src + ./module/Application/src + ./module/Company/src + ./module/Decision/src + ./module/Education/src + ./module/Frontpage/src + ./module/Photo/src + ./module/User/src + + diff --git a/psalm/psalm-baseline.xml b/psalm/psalm-baseline.xml index 8f70986094..01e9fedb21 100644 --- a/psalm/psalm-baseline.xml +++ b/psalm/psalm-baseline.xml @@ -20,15 +20,7 @@ $this->remoteAddress - - - [[$this::COMPANY_ID], $this->companyUser] - [[$this::COMPANY_ID], $this->company] - [[$this::LIDNR], $this->member] - [[$this::LIDNR], $this->user] - - - + getEntityManager @@ -48,20 +40,6 @@ $file - - - function (MeetingDocumentModel $document) use ($ordering): void { - $position = $ordering->indexOf($document->getId()); - - $document->setDisplayPosition($position); - - $this->meetingDocumentMapper->persist($document); - } - static function (MeetingDocumentModel $document) { - return $document->getId(); - } - - $this->data[$field . 'En']