[BUGFIX] Fix broken TcaEnhancer unit tests

This commit is contained in:
Markus Klein 2021-02-02 10:16:24 +01:00
parent d180aeb111
commit 1f75390dcd
3 changed files with 30 additions and 7 deletions

View file

@ -26,7 +26,7 @@ class TcaEnhancer
['', 0], ['', 0],
]; ];
$pid = BackendUtility::getTSconfig_pidValue('tt_content', $params['row']['uid'], $params['row']['pid']); $pid = $this->resolvePid($params['row']);
if ($pid > 0) { if ($pid > 0) {
$campaigns = $this->campaignsFactory->createFromPageUid($pid)->getForUser(); $campaigns = $this->campaignsFactory->createFromPageUid($pid)->getForUser();
/** @var Campaign $campaign */ /** @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']);
}
} }

View file

@ -23,8 +23,10 @@ namespace SkillDisplay\SkilldisplayContent\Tests\Unit;
* 02110-1301, USA. * 02110-1301, USA.
*/ */
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait; use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\ObjectProphecy;
use SkillDisplay\PHPToolKit\Api\Campaigns; use SkillDisplay\PHPToolKit\Api\Campaigns;
use SkillDisplay\PHPToolKit\Entity\Campaign; use SkillDisplay\PHPToolKit\Entity\Campaign;
use SkillDisplay\SkilldisplayContent\CampaignsFactory; use SkillDisplay\SkilldisplayContent\CampaignsFactory;
@ -42,6 +44,7 @@ class TcaEnhancerTest extends TestCase
*/ */
public function instanceCanBeCreated(): void public function instanceCanBeCreated(): void
{ {
/** @var CampaignsFactory|ObjectProphecy $campaignsFactory */
$campaignsFactory = $this->prophesize(CampaignsFactory::class); $campaignsFactory = $this->prophesize(CampaignsFactory::class);
$subject = new TcaEnhancer( $subject = new TcaEnhancer(
$campaignsFactory->reveal() $campaignsFactory->reveal()
@ -59,17 +62,23 @@ class TcaEnhancerTest extends TestCase
'row' => [ 'row' => [
'pid' => 10, 'pid' => 10,
], ],
'items' => [],
]; ];
/** @var Campaigns|ObjectProphecy $campaigns */
$campaigns = $this->prophesize(Campaigns::class); $campaigns = $this->prophesize(Campaigns::class);
$campaigns->getForUser()->willReturn([]); $campaigns->getForUser()->willReturn([]);
/** @var CampaignsFactory|ObjectProphecy $campaignsFactory */
$campaignsFactory = $this->prophesize(CampaignsFactory::class); $campaignsFactory = $this->prophesize(CampaignsFactory::class);
$campaignsFactory->createFromPageUid(10)->willReturn($campaigns->reveal()); $campaignsFactory->createFromPageUid(10)->willReturn($campaigns->reveal());
$subject = new TcaEnhancer( /** @var TcaEnhancer|MockObject $subject */
$campaignsFactory->reveal() $subject = $this->getMockBuilder(TcaEnhancer::class)
); ->onlyMethods(['resolvePid'])
->setConstructorArgs([$campaignsFactory->reveal()])
->getMock();
$subject->expects(self::any())->method('resolvePid')->willReturn(10);
$subject->getCampaignsForTCA($parameters); $subject->getCampaignsForTCA($parameters);
self::assertArrayHasKey('items', $parameters); self::assertArrayHasKey('items', $parameters);
@ -87,27 +96,35 @@ class TcaEnhancerTest extends TestCase
'row' => [ 'row' => [
'pid' => 10, 'pid' => 10,
], ],
'items' => [],
]; ];
/** @var Campaign|ObjectProphecy $campaign1 */
$campaign1 = $this->prophesize(Campaign::class); $campaign1 = $this->prophesize(Campaign::class);
$campaign1->getTitle()->willReturn('Campaign Title 1'); $campaign1->getTitle()->willReturn('Campaign Title 1');
$campaign1->getId()->willReturn(10); $campaign1->getId()->willReturn(10);
/** @var Campaign|ObjectProphecy $campaign2 */
$campaign2 = $this->prophesize(Campaign::class); $campaign2 = $this->prophesize(Campaign::class);
$campaign2->getTitle()->willReturn('Campaign Title 2'); $campaign2->getTitle()->willReturn('Campaign Title 2');
$campaign2->getId()->willReturn(20); $campaign2->getId()->willReturn(20);
/** @var Campaigns|ObjectProphecy $campaigns */
$campaigns = $this->prophesize(Campaigns::class); $campaigns = $this->prophesize(Campaigns::class);
$campaigns->getForUser()->willReturn([ $campaigns->getForUser()->willReturn([
$campaign1->reveal(), $campaign1->reveal(),
$campaign2->reveal(), $campaign2->reveal(),
]); ]);
/** @var CampaignsFactory|ObjectProphecy $campaignsFactory */
$campaignsFactory = $this->prophesize(CampaignsFactory::class); $campaignsFactory = $this->prophesize(CampaignsFactory::class);
$campaignsFactory->createFromPageUid(10)->willReturn($campaigns->reveal()); $campaignsFactory->createFromPageUid(10)->willReturn($campaigns->reveal());
$subject = new TcaEnhancer( /** @var TcaEnhancer|MockObject $subject */
$campaignsFactory->reveal() $subject = $this->getMockBuilder(TcaEnhancer::class)
); ->onlyMethods(['resolvePid'])
->setConstructorArgs([$campaignsFactory->reveal()])
->getMock();
$subject->expects(self::any())->method('resolvePid')->willReturn(10);
$subject->getCampaignsForTCA($parameters); $subject->getCampaignsForTCA($parameters);
self::assertArrayHasKey('items', $parameters); self::assertArrayHasKey('items', $parameters);

View file

@ -37,6 +37,7 @@
"require-dev": { "require-dev": {
"squizlabs/php_codesniffer": "^3.5", "squizlabs/php_codesniffer": "^3.5",
"phpstan/phpstan": "^0.12.18", "phpstan/phpstan": "^0.12.18",
"phpstan/phpstan-phpunit": "*",
"phpstan/extension-installer": "^1.0", "phpstan/extension-installer": "^1.0",
"maglnet/composer-require-checker": "^2.1", "maglnet/composer-require-checker": "^2.1",
"saschaegerer/phpstan-typo3": "^0.13.1", "saschaegerer/phpstan-typo3": "^0.13.1",