From 4543e9be255f979fe67ea824342635a4145c5be1 Mon Sep 17 00:00:00 2001 From: Christian Kuhn <lolli@schwarzbu.ch> Date: Tue, 27 Feb 2024 16:13:53 +0100 Subject: [PATCH] [TASK] Avoid phpunit static TestCase->never() and once() The methods are no longer static in phpunit 11 and should not be used in data providers. We split a data provider to deal with this. The final switch to not call these statically will be done when phpunit is raised, since it otherwise collides with cgl checks and current phpunit 10. Resolves: #103216 Releases: main Change-Id: I90c787d1c8b68e0bf02be0e4f056ef6ebb8ca9ef Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/83148 Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch> Tested-by: core-ci <typo3@b13.com> Tested-by: Anja Leichsenring <aleichsenring@ab-softlab.de> Tested-by: Oliver Klee <typo3-coding@oliverklee.de> Reviewed-by: Anja Leichsenring <aleichsenring@ab-softlab.de> Reviewed-by: Oliver Klee <typo3-coding@oliverklee.de> Tested-by: Christian Kuhn <lolli@schwarzbu.ch> --- .../ContentObjectRendererTest.php | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php b/typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php index 83e1c1533ece..f80b9f57d703 100644 --- a/typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php +++ b/typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php @@ -21,8 +21,6 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\Exception; use PHPUnit\Framework\MockObject\MockObject; -use PHPUnit\Framework\MockObject\Rule\InvocationOrder; -use PHPUnit\Framework\MockObject\Rule\InvokedCount; use Psr\EventDispatcher\EventDispatcherInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Log\NullLogger; @@ -1511,46 +1509,54 @@ final class ContentObjectRendererTest extends UnitTestCase self::assertSame('someTitle', $this->subject->getData('db:tt_content:106:title')); } - public static function getDataWithTypeDbDataProvider(): array + public static function getDataWithTypeDbReturnsEmptyStringOnInvalidIdentifiersDataProvider(): array { return [ 'identifier with missing table, uid and column' => [ 'identifier' => 'db', - 'expectsMethodCall' => self::never(), ], 'identifier with empty table, missing uid and column' => [ 'identifier' => 'db:', - 'expectsMethodCall' => self::never(), ], 'identifier with missing table and column' => [ 'identifier' => 'db:tt_content', - 'expectsMethodCall' => self::never(), ], 'identifier with empty table and missing uid and column' => [ 'identifier' => 'db:tt_content:', - 'expectsMethodCall' => self::never(), ], + ]; + } + + #[DataProvider('getDataWithTypeDbReturnsEmptyStringOnInvalidIdentifiersDataProvider')] + #[Test] + public function getDataWithTypeDbReturnsEmptyStringOnInvalidIdentifiers(string $identifier): void + { + $pageInformation = new PageInformation(); + $pageInformation->setPageRecord([]); + $request = new ServerRequest('https://example.com'); + $request = $request->withAttribute('frontend.page.information', $pageInformation); + $this->subject->setRequest($request); + self::assertSame('', $this->subject->getData($identifier)); + } + + public static function getDataWithTypeDbReturnsEmptyStringOnInvalidIdentifiersCallsPageRepositoryOnceDataProvider(): array + { + return [ 'identifier with empty uid and missing column' => [ 'identifier' => 'db:tt_content:106', - 'expectsMethodCall' => self::once(), ], 'identifier with empty uid and column' => [ 'identifier' => 'db:tt_content:106:', - 'expectsMethodCall' => self::once(), ], 'identifier with empty uid and not existing column' => [ 'identifier' => 'db:tt_content:106:not_existing_column', - 'expectsMethodCall' => self::once(), ], ]; } - /** - * Checks if getData() works with type "db" - */ - #[DataProvider('getDataWithTypeDbDataProvider')] + #[DataProvider('getDataWithTypeDbReturnsEmptyStringOnInvalidIdentifiersCallsPageRepositoryOnceDataProvider')] #[Test] - public function getDataWithTypeDbReturnsEmptyStringOnInvalidIdentifiers(string $identifier, InvocationOrder $expectsMethodCall): void + public function getDataWithTypeDbReturnsEmptyStringOnInvalidIdentifiersCallsPageRepositoryOnce(string $identifier): void { $pageInformation = new PageInformation(); $pageInformation->setPageRecord([]); @@ -1559,16 +1565,11 @@ final class ContentObjectRendererTest extends UnitTestCase $this->subject->setRequest($request); $dummyRecord = ['uid' => 5, 'title' => 'someTitle']; $pageRepository = $this->createMock(PageRepository::class); - if ($expectsMethodCall instanceof InvokedCount && !$expectsMethodCall->isNever()) { - GeneralUtility::addInstance(PageRepository::class, $pageRepository); - } - $pageRepository->expects($expectsMethodCall)->method('getRawRecord')->with('tt_content', '106')->willReturn($dummyRecord); + GeneralUtility::addInstance(PageRepository::class, $pageRepository); + $pageRepository->expects(self::once())->method('getRawRecord')->with('tt_content', '106')->willReturn($dummyRecord); self::assertSame('', $this->subject->getData($identifier)); } - /** - * Checks if getData() works with type "lll" - */ #[Test] public function getDataWithTypeLll(): void { -- GitLab