Last active
July 13, 2020 17:37
-
-
Save tomasnorre/efbe45a1193d3e6f0545e985a55648b1 to your computer and use it in GitHub Desktop.
How to best write Unit tests for this ?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hi, | |
How would you write unit-test for this function? addFlushedPagesToCrawlerQueue() | |
With a functional test I would check the queue size, if the expected number of pages was added to the queue, | |
but not sure how to deal with this in unit-tests. | |
Could you please give me some pointer. | |
I have added DataHandlerHookTests.php with an idea, but as I always expect null, I would not know which return route it has taken. Therefore... Bad Tests.. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace AOE\Crawler\Hooks; | |
use AOE\Crawler\Api\CrawlerApi; | |
use AOE\Crawler\Domain\Repository\QueueRepository; | |
use TYPO3\CMS\Core\Utility\GeneralUtility; | |
use TYPO3\CMS\Extbase\Object\ObjectManager; | |
class DataHandlerHook | |
{ | |
/** | |
* @param array $parameters | |
* @param \TYPO3\CMS\Core\DataHandling\DataHandler $dataHandler | |
*/ | |
public function addFlushedPagesToCrawlerQueue(array $parameters, \TYPO3\CMS\Core\DataHandling\DataHandler $dataHandler): void | |
{ | |
if ($dataHandler->BE_USER->workspace > 0 && !$this->isWorkspacePublishAction($dataHandler->cmdmap)) { | |
return; | |
} | |
$pageIdsToBeFlushedFromCache = $parameters['pageIdArray']; | |
if (empty($pageIdsToBeFlushedFromCache)) { | |
return; | |
} | |
foreach ($pageIdsToBeFlushedFromCache as $pageId) { | |
$pageId = (int)$pageId; | |
if ($pageId < 1) { | |
continue; | |
} | |
if ($this->getQueueRepository()->isPageInQueue($pageId)) { | |
continue; | |
} | |
$this->getCrawlerApi()->addPageToQueue($pageId); | |
} | |
} | |
/** | |
* Checks if a workspace record is being published | |
* | |
* Example $commandMap structure as provided by TYPO3: | |
* 'pages' => array( | |
* 123 => array( | |
* 'version' => array( | |
* 'action' => 'swap' | |
* [...] | |
* 'tt_content' => array( | |
* 456 => array( | |
* 'version' => array( | |
* 'action' => 'swap' | |
* [...] | |
* | |
* @param array $commandMap | |
* @return bool | |
*/ | |
protected function isWorkspacePublishAction(array $commandMap): bool | |
{ | |
$isWorkspacePublishAction = false; | |
foreach ($commandMap as $tableCommandMap) { | |
if (!is_array($tableCommandMap)) { | |
continue; | |
} | |
foreach ($tableCommandMap as $singleCommandMap) { | |
if (!is_array($singleCommandMap)) { | |
continue; | |
} | |
if (!$this->isSwapAction($singleCommandMap)) { | |
continue; | |
} | |
$isWorkspacePublishAction = true; | |
return $isWorkspacePublishAction; | |
} | |
} | |
return $isWorkspacePublishAction; | |
} | |
/** | |
* Checks if a page is being swapped with it's workspace overlay | |
* | |
* @param array $singleCommandMap | |
* @return bool | |
*/ | |
private function isSwapAction(array $singleCommandMap): bool | |
{ | |
$isSwapAction = false; | |
if ( | |
isset($singleCommandMap['version']) | |
&& is_array($singleCommandMap['version']) | |
&& isset($singleCommandMap['version']['action']) | |
&& 'swap' === $singleCommandMap['version']['action'] | |
) { | |
$isSwapAction = true; | |
} | |
return $isSwapAction; | |
} | |
private function getQueueRepository(): QueueRepository | |
{ | |
return GeneralUtility::makeInstance(ObjectManager::class)->get(QueueRepository::class); | |
} | |
private function getCrawlerApi(): CrawlerApi | |
{ | |
return GeneralUtility::makeInstance(ObjectManager::class)->get(CrawlerApi::class); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace AOE\Crawler\Tests\Unit\Hook; | |
use AOE\Crawler\Hooks\DataHandlerHook; | |
use Nimut\TestingFramework\TestCase\UnitTestCase; | |
use TYPO3\CMS\Core\DataHandling\DataHandler; | |
class DataHandlerHookTest extends UnitTestCase | |
{ | |
/** | |
* @test | |
* @dataProvider addFlushedPagesToCrawlerQueueTestDataProvider | |
* | |
* @param mixed $expected | |
*/ | |
public function addFlushedPagesToCrawlerQueueTest(array $params, int $WSId, bool $WSPublishAction, $expected): void | |
{ | |
$dataHandler = self::createMock(DataHandler::class); | |
$subject = self::createPartialMock(DataHandlerHook::class, ['isWorkspacePublishAction']); | |
$subject->method('isWorkspacePublishAction')->willReturn($WSPublishAction); | |
self::assertSame( | |
$expected, | |
$subject->addFlushedPagesToCrawlerQueue($params, $dataHandler) | |
); | |
} | |
public function addFlushedPagesToCrawlerQueueTestDataProvider(): array | |
{ | |
return [ | |
'WorkspaceID < 1' => [ | |
'params' => [], | |
'workspace' => -1, | |
'isWorkspacePublishAction' => true, | |
'expected' => null, | |
], | |
'WorkspaceID > 0 and workspace is publish action' => [ | |
'params' => [], | |
'workspace' => 1, | |
'isWorkspacePublishAction' => true, | |
'expected' => null, | |
], | |
'PageIdArray does not exist' => [ | |
'params' => [], | |
'workspace' => 1, | |
'isWorkspacePublishAction' => false, | |
'expected' => null, | |
], | |
'PageIdArray exist but is empty' => [ | |
'params' => ['pageIdArray' => []], | |
'workspace' => 1, | |
'isWorkspacePublishAction' => false, | |
'expected' => null, | |
], | |
'PageIdArray exists, but have 0 as pageId' => [ | |
'params' => ['pageIdArray' => [0]], | |
'workspace' => 1, | |
'isWorkspacePublishAction' => false, | |
'expected' => null, | |
], | |
'PageIdArray exists, have 1 as pageId, pageId 1 is in queue' => [ | |
'params' => ['pageIdArray' => []], | |
'workspace' => 1, | |
'isWorkspacePublishAction' => false, | |
'expected' => null, | |
], | |
'PageIdArray exists, have 1 as pageId, pageId 1 is not in queue' => [ | |
'params' => ['pageIdArray' => []], | |
'workspace' => 1, | |
'isWorkspacePublishAction' => false, | |
'expected' => null, | |
], | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment