Skip to content

Commit

Permalink
Fix PHPUnit 10 issues
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tomudding committed Jan 26, 2024
1 parent 5fa41e8 commit d52116e
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 95 deletions.
7 changes: 5 additions & 2 deletions module/Activity/test/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
13 changes: 7 additions & 6 deletions module/Activity/test/Mapper/ActivityMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<ActivityMapper, Activity> */
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');

Expand Down
26 changes: 21 additions & 5 deletions module/Application/test/AutomaticControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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.
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,7 +26,7 @@
use function array_merge;
use function array_unique;

abstract class BaseControllerTest extends AbstractHttpControllerTestCase
trait BaseControllerTrait
{
protected ServiceManager $serviceManager;

Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 5 additions & 2 deletions module/Company/test/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
7 changes: 5 additions & 2 deletions module/Decision/test/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
7 changes: 5 additions & 2 deletions module/Education/test/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
7 changes: 5 additions & 2 deletions module/Frontpage/test/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
7 changes: 5 additions & 2 deletions module/Photo/test/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
7 changes: 5 additions & 2 deletions module/User/test/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
55 changes: 25 additions & 30 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,32 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="./bootstrap.php"
colors="true">
<coverage processUncoveredFiles="true" pathCoverage="true">
<include>
<directory suffix=".php">./module/Activity/src</directory>
<directory suffix=".php">./module/Application/src</directory>
<directory suffix=".php">./module/Company/src</directory>
<directory suffix=".php">./module/Decision/src</directory>
<directory suffix=".php">./module/Education/src</directory>
<directory suffix=".php">./module/Frontpage/src</directory>
<directory suffix=".php">./module/Photo/src</directory>
<directory suffix=".php">./module/User/src</directory>
</include>
</coverage>

<testsuites>
<testsuite name="gewisweb">
<directory>./module/Activity/test/</directory>
<directory>./module/Application/test/</directory>
<directory>./module/Company/test/</directory>
<directory>./module/Decision/test/</directory>
<directory>./module/Education/test/</directory>
<directory>./module/Frontpage/test/</directory>
<directory>./module/Photo/test/</directory>
<directory>./module/User/test/</directory>
</testsuite>
</testsuites>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" bootstrap="./bootstrap.php" colors="true" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="gewisweb">
<directory>./module/Activity/test/</directory>
<directory>./module/Application/test/</directory>
<directory>./module/Company/test/</directory>
<directory>./module/Decision/test/</directory>
<directory>./module/Education/test/</directory>
<directory>./module/Frontpage/test/</directory>
<directory>./module/Photo/test/</directory>
<directory>./module/User/test/</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./module/Activity/src</directory>
<directory suffix=".php">./module/Application/src</directory>
<directory suffix=".php">./module/Company/src</directory>
<directory suffix=".php">./module/Decision/src</directory>
<directory suffix=".php">./module/Education/src</directory>
<directory suffix=".php">./module/Frontpage/src</directory>
<directory suffix=".php">./module/Photo/src</directory>
<directory suffix=".php">./module/User/src</directory>
</include>
</source>
</phpunit>
Loading

0 comments on commit d52116e

Please sign in to comment.