From 1f75390dcde646b317cbf94235633efe8c6821e5 Mon Sep 17 00:00:00 2001 From: Markus Klein Date: Tue, 2 Feb 2021 10:16:24 +0100 Subject: [PATCH] [BUGFIX] Fix broken TcaEnhancer unit tests --- Classes/TcaEnhancer.php | 7 ++++++- Tests/Unit/TcaEnhancerTest.php | 29 +++++++++++++++++++++++------ composer.json | 1 + 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/Classes/TcaEnhancer.php b/Classes/TcaEnhancer.php index 8de0175..0b37441 100644 --- a/Classes/TcaEnhancer.php +++ b/Classes/TcaEnhancer.php @@ -26,7 +26,7 @@ class TcaEnhancer ['', 0], ]; - $pid = BackendUtility::getTSconfig_pidValue('tt_content', $params['row']['uid'], $params['row']['pid']); + $pid = $this->resolvePid($params['row']); if ($pid > 0) { $campaigns = $this->campaignsFactory->createFromPageUid($pid)->getForUser(); /** @var Campaign $campaign */ @@ -38,4 +38,9 @@ class TcaEnhancer } } } + + protected function resolvePid(array $row): ?int + { + return BackendUtility::getTSconfig_pidValue('tt_content', $row['uid'], $row['pid']); + } } diff --git a/Tests/Unit/TcaEnhancerTest.php b/Tests/Unit/TcaEnhancerTest.php index d0dee34..71c5f09 100644 --- a/Tests/Unit/TcaEnhancerTest.php +++ b/Tests/Unit/TcaEnhancerTest.php @@ -23,8 +23,10 @@ namespace SkillDisplay\SkilldisplayContent\Tests\Unit; * 02110-1301, USA. */ +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Prophecy\PhpUnit\ProphecyTrait; +use Prophecy\Prophecy\ObjectProphecy; use SkillDisplay\PHPToolKit\Api\Campaigns; use SkillDisplay\PHPToolKit\Entity\Campaign; use SkillDisplay\SkilldisplayContent\CampaignsFactory; @@ -42,6 +44,7 @@ class TcaEnhancerTest extends TestCase */ public function instanceCanBeCreated(): void { + /** @var CampaignsFactory|ObjectProphecy $campaignsFactory */ $campaignsFactory = $this->prophesize(CampaignsFactory::class); $subject = new TcaEnhancer( $campaignsFactory->reveal() @@ -59,17 +62,23 @@ class TcaEnhancerTest extends TestCase 'row' => [ 'pid' => 10, ], + 'items' => [], ]; + /** @var Campaigns|ObjectProphecy $campaigns */ $campaigns = $this->prophesize(Campaigns::class); $campaigns->getForUser()->willReturn([]); + /** @var CampaignsFactory|ObjectProphecy $campaignsFactory */ $campaignsFactory = $this->prophesize(CampaignsFactory::class); $campaignsFactory->createFromPageUid(10)->willReturn($campaigns->reveal()); - $subject = new TcaEnhancer( - $campaignsFactory->reveal() - ); + /** @var TcaEnhancer|MockObject $subject */ + $subject = $this->getMockBuilder(TcaEnhancer::class) + ->onlyMethods(['resolvePid']) + ->setConstructorArgs([$campaignsFactory->reveal()]) + ->getMock(); + $subject->expects(self::any())->method('resolvePid')->willReturn(10); $subject->getCampaignsForTCA($parameters); self::assertArrayHasKey('items', $parameters); @@ -87,27 +96,35 @@ class TcaEnhancerTest extends TestCase 'row' => [ 'pid' => 10, ], + 'items' => [], ]; + /** @var Campaign|ObjectProphecy $campaign1 */ $campaign1 = $this->prophesize(Campaign::class); $campaign1->getTitle()->willReturn('Campaign Title 1'); $campaign1->getId()->willReturn(10); + /** @var Campaign|ObjectProphecy $campaign2 */ $campaign2 = $this->prophesize(Campaign::class); $campaign2->getTitle()->willReturn('Campaign Title 2'); $campaign2->getId()->willReturn(20); + /** @var Campaigns|ObjectProphecy $campaigns */ $campaigns = $this->prophesize(Campaigns::class); $campaigns->getForUser()->willReturn([ $campaign1->reveal(), $campaign2->reveal(), ]); + /** @var CampaignsFactory|ObjectProphecy $campaignsFactory */ $campaignsFactory = $this->prophesize(CampaignsFactory::class); $campaignsFactory->createFromPageUid(10)->willReturn($campaigns->reveal()); - $subject = new TcaEnhancer( - $campaignsFactory->reveal() - ); + /** @var TcaEnhancer|MockObject $subject */ + $subject = $this->getMockBuilder(TcaEnhancer::class) + ->onlyMethods(['resolvePid']) + ->setConstructorArgs([$campaignsFactory->reveal()]) + ->getMock(); + $subject->expects(self::any())->method('resolvePid')->willReturn(10); $subject->getCampaignsForTCA($parameters); self::assertArrayHasKey('items', $parameters); diff --git a/composer.json b/composer.json index a7f188b..f4f5f2e 100644 --- a/composer.json +++ b/composer.json @@ -37,6 +37,7 @@ "require-dev": { "squizlabs/php_codesniffer": "^3.5", "phpstan/phpstan": "^0.12.18", + "phpstan/phpstan-phpunit": "*", "phpstan/extension-installer": "^1.0", "maglnet/composer-require-checker": "^2.1", "saschaegerer/phpstan-typo3": "^0.13.1",