diff --git a/composer.lock b/composer.lock index e9a73e0f74..5a15899be9 100644 --- a/composer.lock +++ b/composer.lock @@ -11968,5 +11968,5 @@ "platform-overrides": { "ext-redis": "4.3" }, - "plugin-api-version": "2.6.0" + "plugin-api-version": "2.3.0" } diff --git a/module/Api/config/module.config.php b/module/Api/config/module.config.php index 84cb120aca..3296696635 100644 --- a/module/Api/config/module.config.php +++ b/module/Api/config/module.config.php @@ -647,11 +647,11 @@ 'OtherLicence' => RepositoryFactory::class, Repository\Document::class => RepositoryFactory::class, 'Correspondence' => RepositoryFactory::class, - 'SystemParameter' => RepositoryFactory::class, + Repository\SystemParameter::class => RepositoryFactory::class, 'FeatureToggle' => RepositoryFactory::class, 'Stay' => RepositoryFactory::class, 'Submission' => RepositoryFactory::class, - 'TaskAllocationRule' => RepositoryFactory::class, + Repository\TaskAllocationRule::class => RepositoryFactory::class, 'TaskAlphaSplit' => RepositoryFactory::class, 'IrfoPartner' => RepositoryFactory::class, 'Transaction' => RepositoryFactory::class, @@ -806,6 +806,8 @@ 'User' => Repository\User::class, 'Document' => Repository\Document::class, 'Cases' => Repository\Cases::class, + 'SystemParameter' => Repository\SystemParameter::class, + 'TaskAllocationRule' => Repository\TaskAllocationRule::class, ], ], \Dvsa\Olcs\Api\Domain\FormControlServiceManagerFactory::CONFIG_KEY => [ diff --git a/module/Api/src/Domain/CommandHandler/Task/CreateTask.php b/module/Api/src/Domain/CommandHandler/Task/CreateTask.php index f020fd21ab..c0092f55c0 100644 --- a/module/Api/src/Domain/CommandHandler/Task/CreateTask.php +++ b/module/Api/src/Domain/CommandHandler/Task/CreateTask.php @@ -1,33 +1,30 @@ - * @author Alex Peshkov - */ final class CreateTask extends AbstractCommandHandler { - protected $repoServiceName = 'Task'; - - protected $extraRepos = ['TaskAllocationRule', 'SystemParameter']; + protected $extraRepos = [ + Repository\SystemParameter::class, + Repository\Task::class, + Repository\TaskAllocationRule::class, + ]; - protected $alphaNumericTranslations = [ + protected array $alphaNumericTranslations = [ 0 => 'Z', 1 => 'O', 2 => 'T', @@ -41,13 +38,12 @@ final class CreateTask extends AbstractCommandHandler ]; /** - * Handle command - * - * @param \Dvsa\Olcs\Api\Domain\Command\Task\CreateTask $command Command - * + * @param \Dvsa\Olcs\Transfer\Command\Task\CreateTask|CommandInterface $command * @return Result + * @throws ORMException + * @throws RuntimeException */ - public function handleCommand(CommandInterface $command) + public function handleCommand(CommandInterface $command): Result { $task = $this->createTaskObject($command); @@ -55,7 +51,7 @@ public function handleCommand(CommandInterface $command) $this->autoAssignTask($task); } - $this->getRepo()->save($task); + $this->getRepo(Repository\Task::class)->save($task); $result = new Result(); $result->addId('task', $task->getId()); @@ -68,13 +64,9 @@ public function handleCommand(CommandInterface $command) } /** - * Auto assign task - * - * @param Task $task Task - * - * @return void + * @throws RuntimeException|ORMException */ - private function autoAssignTask(Task $task) + private function autoAssignTask(Entity\Task\Task $task): void { if ($task->getLicence() !== null) { $rules = $this->getRulesBasedOnLicence($task); @@ -96,37 +88,24 @@ private function autoAssignTask(Task $task) /** * Fall back on system configuration to populate user and team - * - * @param Task $task Task - * - * @return void + * @throws RuntimeException|ORMException */ - private function assignToDefault(Task $task) + private function assignToDefault(Entity\Task\Task $task): void { - /** @var \Dvsa\Olcs\Api\Domain\Repository\SystemParameter $repo */ - $repo = $this->getRepo('SystemParameter'); + $repo = $this->getRepo(Repository\SystemParameter::class); $teamId = $repo->fetchValue('task.default_team'); if ($teamId !== null) { - $task->setAssignedToTeam($this->getRepo()->getReference(Team::class, $teamId)); + $task->setAssignedToTeam($this->getRepo(Repository\Task::class)->getReference(Entity\User\Team::class, $teamId)); } $userId = $repo->fetchValue('task.default_user'); if ($userId !== null) { - $task->setAssignedToUser($this->getRepo()->getReference(User::class, $userId)); + $task->setAssignedToUser($this->getRepo(Repository\Task::class)->getReference(Entity\User\User::class, $userId)); } } - /** - * Assign by rule - * - * @param Task $task Task - * @param TaskAllocationRule $rule TaskAllocationRule - * @param bool $useAlphaSplit Should use Alpha split - * - * @return void - */ - protected function assignByRule(Task $task, TaskAllocationRule $rule, $useAlphaSplit) + protected function assignByRule(Entity\Task\Task $task, Entity\Task\TaskAllocationRule $rule, bool $useAlphaSplit): void { $task->setAssignedToTeam($rule->getTeam()); if ($rule->getUser() !== null) { @@ -136,15 +115,7 @@ protected function assignByRule(Task $task, TaskAllocationRule $rule, $useAlphaS } } - /** - * Assign by alpha split - * - * @param Task $task Task - * @param TaskAllocationRule $rule TaskAllocationRule - * - * @return void - */ - protected function assignByAlphaSplit(Task $task, TaskAllocationRule $rule) + protected function assignByAlphaSplit(Entity\Task\Task $task, Entity\Task\TaskAllocationRule $rule): void { $taskAlphaSplits = $rule->getTaskAlphaSplits(); if ($taskAlphaSplits === null) { @@ -164,28 +135,24 @@ protected function assignByAlphaSplit(Task $task, TaskAllocationRule $rule) } /** - * Get rules based on category - * - * @param Task $task Task - * - * @return array + * @throws RuntimeException */ - protected function getRulesBasedOnCategory(Task $task) + protected function getRulesBasedOnCategory(Entity\Task\Task $task): array { - return $this->getRepo('TaskAllocationRule')->fetchByParameters($task->getCategory()->getId()); + return $this + ->getRepo(Repository\TaskAllocationRule::class) + ->fetchByParametersWithFallbackWhenSubCategoryNotFound( + $task->getCategory()->getId(), + $task->getSubCategory()->getId() + ); } /** - * Get rules based on licence - * - * @param Task $task Task - * - * @return array + * @throws RuntimeException */ - protected function getRulesBasedOnLicence(Task $task) + protected function getRulesBasedOnLicence(Entity\Task\Task $task): array { - /** @var \Dvsa\Olcs\Api\Domain\Repository\TaskAllocationRule $repo */ - $repo = $this->getRepo('TaskAllocationRule'); + $repo = $this->getRepo(Repository\TaskAllocationRule::class); $licence = $task->getLicence(); $app = $task->getApplication(); @@ -196,12 +163,6 @@ protected function getRulesBasedOnLicence(Task $task) ? $licenceTrafficArea->getId() : null ); - $category = $task->getCategory(); - $categoryId = ( - $category !== null - ? $category->getId() - : null - ); // define operator Type $operatorType = null; @@ -222,10 +183,11 @@ protected function getRulesBasedOnLicence(Task $task) } // Goods Licence - if ($operatorType === Licence::LICENCE_CATEGORY_GOODS_VEHICLE) { - $rules = $repo->fetchByParameters( - $categoryId, - Licence::LICENCE_CATEGORY_GOODS_VEHICLE, + if ($operatorType === Entity\Licence\Licence::LICENCE_CATEGORY_GOODS_VEHICLE) { + $rules = $repo->fetchByParametersWithFallbackWhenSubCategoryNotFound( + $task->getCategory()->getId(), + $task->getSubCategory()->getId(), + Entity\Licence\Licence::LICENCE_CATEGORY_GOODS_VEHICLE, $trafficArea, $licence->getOrganisation()->isMlh() ); @@ -236,8 +198,9 @@ protected function getRulesBasedOnLicence(Task $task) // PSV licence or no rules found for Goods Licence // search rules by category, operator type and traffic area - $rules = $repo->fetchByParameters( - $categoryId, + $rules = $repo->fetchByParametersWithFallbackWhenSubCategoryNotFound( + $task->getCategory()->getId(), + $task->getSubCategory()->getId(), $operatorType, $trafficArea ); @@ -246,8 +209,9 @@ protected function getRulesBasedOnLicence(Task $task) } // search rules by category and traffic area - $rules = $repo->fetchByParameters( - $categoryId, + $rules = $repo->fetchByParametersWithFallbackWhenSubCategoryNotFound( + $task->getCategory()->getId(), + $task->getSubCategory()->getId(), null, $trafficArea ); @@ -256,8 +220,9 @@ protected function getRulesBasedOnLicence(Task $task) } // search rules by category and operator type - $rules = $repo->fetchByParameters( - $categoryId, + $rules = $repo->fetchByParametersWithFallbackWhenSubCategoryNotFound( + $task->getCategory()->getId(), + $task->getSubCategory()->getId(), $operatorType ); if (count($rules) >= 1) { @@ -265,37 +230,30 @@ protected function getRulesBasedOnLicence(Task $task) } // search rules by category only - return $repo->fetchByParameters($categoryId); + return $repo->fetchByParametersWithFallbackWhenSubCategoryNotFound($task->getCategory()->getId(), $task->getSubCategory()->getId()); } - /** - * Get letter for alpha split - * - * @param Task $task Task - * - * @return string - */ - protected function getLetterForAlphaSplit(Task $task) + protected function getLetterForAlphaSplit(Entity\Task\Task $task): string { $letter = ''; $organisation = $task->getLicence()->getOrganisation(); switch ($organisation->getType()) { - case Organisation::ORG_TYPE_REGISTERED_COMPANY: - case Organisation::ORG_TYPE_LLP: - case Organisation::ORG_TYPE_OTHER: + case Entity\Organisation\Organisation::ORG_TYPE_REGISTERED_COMPANY: + case Entity\Organisation\Organisation::ORG_TYPE_LLP: + case Entity\Organisation\Organisation::ORG_TYPE_OTHER: $companyName = strtoupper($organisation->getName()); if (strlen($companyName) > 4 && substr($companyName, 0, 4) === 'THE ') { $companyName = substr($companyName, 4); } $letter = substr($companyName, 0, 1); break; - case Organisation::ORG_TYPE_SOLE_TRADER: + case Entity\Organisation\Organisation::ORG_TYPE_SOLE_TRADER: $organisationPerson = $organisation->getOrganisationPersons()->first(); if ($organisationPerson) { $letter = strtoupper(substr($organisationPerson->getPerson()->getFamilyName(), 0, 1)); } break; - case Organisation::ORG_TYPE_PARTNERSHIP: + case Entity\Organisation\Organisation::ORG_TYPE_PARTNERSHIP: $organisationPersons = $organisation->getOrganisationPersons(); $criteria = Criteria::create(); $criteria->orderBy(array('id' => Criteria::ASC)); @@ -312,101 +270,83 @@ protected function getLetterForAlphaSplit(Task $task) } /** - * Create task object - * - * @param \Dvsa\Olcs\Api\Domain\Command\Task\CreateTask $command Command - * + * @param \Dvsa\Olcs\Transfer\Command\Task\CreateTask|CommandInterface $command * @return Task + * @throws ORMException + * @throws RuntimeException + * @throws Exception */ - private function createTaskObject(CommandInterface $command) + private function createTaskObject(CommandInterface $command): Entity\Task\Task { - /** @var \Dvsa\Olcs\Api\Domain\Repository\Task $repo */ - $repo = $this->getRepo(); + /** @var Repository\Task $repo */ + $repo = $this->getRepo(Repository\Task::class); // Required $category = $repo->getCategoryReference($command->getCategory()); $subCategory = $repo->getSubCategoryReference($command->getSubCategory()); - $task = new Task($category, $subCategory); + $task = new Entity\Task\Task($category, $subCategory); // Optional relationships $userId = (int)$command->getAssignedToUser(); - $task->setAssignedToUser($repo->getReference(User::class, $userId)); + $task->setAssignedToUser($repo->getReference(Entity\User\User::class, $userId)); $task->setAssignedToTeam($repo->getTeamReference((int)$command->getAssignedToTeam(), $userId)); if ($command->getApplication() !== null) { - $application = $this->getRepo()->getReference(Application::class, $command->getApplication()); + $application = $repo->getReference(Entity\Application\Application::class, $command->getApplication()); $task->setApplication($application); } if ($command->getLicence() !== null) { - $Licence = $this->getRepo()->getReference(Licence::class, $command->getLicence()); + $Licence = $repo->getReference(Entity\Licence\Licence::class, $command->getLicence()); $task->setLicence($Licence); } if ($command->getBusReg() !== null) { $task->setBusReg( - $this->getRepo()->getReference(\Dvsa\Olcs\Api\Entity\Bus\BusReg::class, $command->getBusReg()) + $repo->getReference(Entity\Bus\BusReg::class, $command->getBusReg()) ); } if ($command->getCase() !== null) { $task->setCase( - $this->getRepo()->getReference(\Dvsa\Olcs\Api\Entity\Cases\Cases::class, $command->getCase()) + $repo->getReference(Entity\Cases\Cases::class, $command->getCase()) ); } if ($command->getSubmission() !== null) { $task->setSubmission( - $this->getRepo()->getReference( - \Dvsa\Olcs\Api\Entity\Submission\Submission::class, - $command->getSubmission() - ) + $repo->getReference(Entity\Submission\Submission::class, $command->getSubmission()) ); } if ($command->getTransportManager() !== null) { $task->setTransportManager( - $this->getRepo()->getReference( - \Dvsa\Olcs\Api\Entity\Tm\TransportManager::class, - $command->getTransportManager() - ) + $repo->getReference(Entity\Tm\TransportManager::class, $command->getTransportManager()) ); } if ($command->getSurrender() !== null) { $task->setSurrender( - $this->getRepo()->getReference( - \Dvsa\Olcs\Api\Entity\Surrender::class, - $command->getSurrender() - ) + $repo->getReference(Entity\Surrender::class, $command->getSurrender()) ); } if ($command->getIrfoOrganisation() !== null) { $task->setIrfoOrganisation( - $this->getRepo()->getReference( - \Dvsa\Olcs\Api\Entity\Organisation\Organisation::class, - $command->getIrfoOrganisation() - ) + $repo->getReference(Entity\Organisation\Organisation::class, $command->getIrfoOrganisation()) ); } if ($command->getIrhpApplication() !== null) { $task->setIrhpApplication( - $this->getRepo()->getReference( - \Dvsa\Olcs\Api\Entity\Permits\IrhpApplication::class, - $command->getIrhpApplication() - ) + $repo->getReference(Entity\Permits\IrhpApplication::class, $command->getIrhpApplication()) ); } if ($command->getAssignedByUser() !== null) { $task->setAssignedByUser( - $this->getRepo()->getReference( - \Dvsa\Olcs\Api\Entity\User\User::class, - $command->getAssignedByUser() - ) + $repo->getReference(Entity\User\User::class, $command->getAssignedByUser()) ); } diff --git a/module/Api/src/Domain/CommandHandler/TaskAllocationRule/Create.php b/module/Api/src/Domain/CommandHandler/TaskAllocationRule/Create.php index 13be0d9571..3f826d984f 100644 --- a/module/Api/src/Domain/CommandHandler/TaskAllocationRule/Create.php +++ b/module/Api/src/Domain/CommandHandler/TaskAllocationRule/Create.php @@ -1,46 +1,38 @@ - */ final class Create extends AbstractCommandHandler { - protected $repoServiceName = 'TaskAllocationRule'; - - public function handleCommand(CommandInterface $command) + use UpdateTaskAllocationRuleTrait; + + protected $extraRepos = [ + Repository\TaskAllocationRule::class + ]; + + /** + * @param $command Cmd + * @throws ORMException + * @throws RuntimeException + */ + public function handleCommand(CommandInterface $command): Result { - /* @var $command Cmd */ - $repo = $this->getRepo(); - - $taskAllocationRule = new \Dvsa\Olcs\Api\Entity\Task\TaskAllocationRule(); - $taskAllocationRule->setCategory( - $repo->getReference(\Dvsa\Olcs\Api\Entity\System\Category::class, $command->getCategory()) - ); - $taskAllocationRule->setTeam( - $repo->getReference(\Dvsa\Olcs\Api\Entity\User\Team::class, $command->getTeam()) - ); - $taskAllocationRule->setUser( - $repo->getReference(\Dvsa\Olcs\Api\Entity\User\User::class, $command->getUser()) - ); - $taskAllocationRule->setGoodsOrPsv($repo->getRefdataReference($command->getGoodsOrPsv())); - $isMlh = null; - if ($command->getIsMlh() === 'Y') { - $isMlh = true; - } elseif ($command->getIsMlh() === 'N') { - $isMlh = false; - } - $taskAllocationRule->setIsMlh($isMlh); - $taskAllocationRule->setTrafficArea( - $repo->getReference(\Dvsa\Olcs\Api\Entity\TrafficArea\TrafficArea::class, $command->getTrafficArea()) - ); + $repo = $this->getRepo(Repository\TaskAllocationRule::class); + + $taskAllocationRule = new Entity\Task\TaskAllocationRule(); + + $taskAllocationRule = $this->updateTaskAllocationRule($taskAllocationRule, $repo, $command); $repo->save($taskAllocationRule); diff --git a/module/Api/src/Domain/CommandHandler/TaskAllocationRule/Update.php b/module/Api/src/Domain/CommandHandler/TaskAllocationRule/Update.php index 6b40827eb4..4ec4a01ab8 100644 --- a/module/Api/src/Domain/CommandHandler/TaskAllocationRule/Update.php +++ b/module/Api/src/Domain/CommandHandler/TaskAllocationRule/Update.php @@ -1,49 +1,41 @@ - */ final class Update extends AbstractCommandHandler { - protected $repoServiceName = 'TaskAllocationRule'; - - public function handleCommand(CommandInterface $command) + use UpdateTaskAllocationRuleTrait; + + protected $extraRepos = [ + Repository\TaskAllocationRule::class + ]; + + /** + * @param Cmd|CommandInterface $command Cmd + * @return Result + * @throws ORMException + * @throws RuntimeException + * @throws NotFoundException + */ + public function handleCommand(CommandInterface $command): Result { - /* @var $command Cmd */ - $repo = $this->getRepo(); - - /* @var $taskAllocationRule \Dvsa\Olcs\Api\Entity\Task\TaskAllocationRule */ - $taskAllocationRule = $repo->fetchUsingId($command, Query::HYDRATE_OBJECT, $command->getVersion()); - - $taskAllocationRule->setCategory( - $repo->getReference(\Dvsa\Olcs\Api\Entity\System\Category::class, $command->getCategory()) - ); - $taskAllocationRule->setTeam( - $repo->getReference(\Dvsa\Olcs\Api\Entity\User\Team::class, $command->getTeam()) - ); - $taskAllocationRule->setUser( - $repo->getReference(\Dvsa\Olcs\Api\Entity\User\User::class, $command->getUser()) - ); - $taskAllocationRule->setGoodsOrPsv($repo->getRefdataReference($command->getGoodsOrPsv())); - $isMlh = null; - if ($command->getIsMlh() === 'Y') { - $isMlh = true; - } elseif ($command->getIsMlh() === 'N') { - $isMlh = false; - } - $taskAllocationRule->setIsMlh($isMlh); - $taskAllocationRule->setTrafficArea( - $repo->getReference(\Dvsa\Olcs\Api\Entity\TrafficArea\TrafficArea::class, $command->getTrafficArea()) - ); + $repo = $this->getRepo(Repository\TaskAllocationRule::class); + + $taskAllocationRule = $repo->fetchById($command->getId(), Query::HYDRATE_OBJECT, $command->getVersion()); + + $taskAllocationRule = $this->updateTaskAllocationRule($taskAllocationRule, $repo, $command); $repo->save($taskAllocationRule); diff --git a/module/Api/src/Domain/CommandHandler/TaskAllocationRule/UpdateTaskAllocationRuleTrait.php b/module/Api/src/Domain/CommandHandler/TaskAllocationRule/UpdateTaskAllocationRuleTrait.php new file mode 100644 index 0000000000..aac6e2ec73 --- /dev/null +++ b/module/Api/src/Domain/CommandHandler/TaskAllocationRule/UpdateTaskAllocationRuleTrait.php @@ -0,0 +1,69 @@ +updateTaskAllocationRuleCommandInstanceCheck($command); + + $entity->setCategory( + $repository->getReference(Entity\System\Category::class, $command->getCategory()) + ); + $entity->setSubCategory( + $repository->getReference(Entity\System\SubCategory::class, $command->getSubCategory()) + ); + $entity->setTeam( + $repository->getReference(Entity\User\Team::class, $command->getTeam()) + ); + $entity->setUser( + $repository->getReference(Entity\User\User::class, $command->getUser()) + ); + $entity->setGoodsOrPsv($repository->getRefdataReference($command->getGoodsOrPsv())); + $isMlh = null; + if ($command->getIsMlh() === 'Y') { + $isMlh = true; + } elseif ($command->getIsMlh() === 'N') { + $isMlh = false; + } + $entity->setIsMlh($isMlh); + $entity->setTrafficArea( + $repository->getReference(Entity\TrafficArea\TrafficArea::class, $command->getTrafficArea()) + ); + + return $entity; + } + + private function updateTaskAllocationRuleCommandInstanceCheck(CommandInterface $command): void + { + $expectedCommands = [ + TaskAllocationRule\Create::class, + TaskAllocationRule\Update::class, + ]; + if (!in_array(get_class($command), $expectedCommands)) { + throw new \RuntimeException(sprintf( + 'Expected instance of: %s', + implode('" or "', $expectedCommands) + )); + } + } +} diff --git a/module/Api/src/Domain/QueryHandler/TaskAllocationRule/Get.php b/module/Api/src/Domain/QueryHandler/TaskAllocationRule/Get.php index f600fd1cf6..1d56407636 100644 --- a/module/Api/src/Domain/QueryHandler/TaskAllocationRule/Get.php +++ b/module/Api/src/Domain/QueryHandler/TaskAllocationRule/Get.php @@ -1,27 +1,35 @@ - */ class Get extends AbstractQueryHandler { - protected $repoServiceName = 'TaskAllocationRule'; + protected $extraRepos = [ + Repository\TaskAllocationRule::class, + ]; - public function handleQuery(QueryInterface $query) + /** + * @param Qry $query + * @throws RuntimeException + */ + public function handleQuery(QueryInterface $query): Result { - $repo = $this->getRepo(); + $repo = $this->getRepo(Repository\TaskAllocationRule::class); return $this->result( $repo->fetchUsingId($query), [ 'category', + 'subCategory', 'team', 'user' => ['contactDetails' => ['person']], 'trafficArea', diff --git a/module/Api/src/Domain/QueryHandler/TaskAllocationRule/GetList.php b/module/Api/src/Domain/QueryHandler/TaskAllocationRule/GetList.php index c769bc9281..fcc81cd221 100644 --- a/module/Api/src/Domain/QueryHandler/TaskAllocationRule/GetList.php +++ b/module/Api/src/Domain/QueryHandler/TaskAllocationRule/GetList.php @@ -1,30 +1,30 @@ - */ class GetList extends AbstractQueryHandler { - protected $repoServiceName = 'TaskAllocationRule'; + protected $extraRepos = [ + Repository\TaskAllocationRule::class, + ]; /** - * @param \Dvsa\Olcs\Transfer\Query\TaskAllocationRule\GetList $query + * @param Qry $query * @return array - * @throws \Dvsa\Olcs\Api\Domain\Exception\RuntimeException + * @throws RuntimeException */ - public function handleQuery(QueryInterface $query) + public function handleQuery(QueryInterface $query): array { - /** @var \Dvsa\Olcs\Api\Domain\Repository\TaskAllocationRule $repo */ - $repo = $this->getRepo(); + $repo = $this->getRepo(Repository\TaskAllocationRule::class); $repo->disableSoftDeleteable(); @@ -46,6 +46,7 @@ public function handleQuery(QueryInterface $query) $result, [ 'category', + 'subCategory', 'team', 'user' => ['contactDetails' => ['person']], 'trafficArea', @@ -62,7 +63,7 @@ public function handleQuery(QueryInterface $query) * * @param Entity\Task\TaskAllocationRule|Entity\Task\TaskAlphaSplit $entity */ - private function cleanDeletedUser($entity) + private function cleanDeletedUser($entity): void { $user = $entity->getUser(); diff --git a/module/Api/src/Domain/Repository/AbstractReadonlyRepository.php b/module/Api/src/Domain/Repository/AbstractReadonlyRepository.php index b715aba7ca..d4bb7eb249 100644 --- a/module/Api/src/Domain/Repository/AbstractReadonlyRepository.php +++ b/module/Api/src/Domain/Repository/AbstractReadonlyRepository.php @@ -5,6 +5,7 @@ use Doctrine\Common\Collections\ArrayCollection; use Doctrine\DBAL\LockMode; use Doctrine\ORM\EntityManagerInterface; +use Doctrine\ORM\Exception\ORMException; use Doctrine\ORM\Mapping\Entity; use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\Query; @@ -565,10 +566,13 @@ public function getSubCategoryReference($id) /** * Get Reference * - * @param string $entityClass Entity class FQN - * @param string|int $id id + * @param class-string $entityClass Entity class FQN + * @param string|int $id id * - * @return null|$entityClass + * @return T|null The entity reference. + * + * @template T + * @throws ORMException */ public function getReference($entityClass, $id) { diff --git a/module/Api/src/Domain/Repository/ReadonlyRepositoryInterface.php b/module/Api/src/Domain/Repository/ReadonlyRepositoryInterface.php index f88fdcc6c2..2ffa308bea 100644 --- a/module/Api/src/Domain/Repository/ReadonlyRepositoryInterface.php +++ b/module/Api/src/Domain/Repository/ReadonlyRepositoryInterface.php @@ -72,10 +72,12 @@ public function getSubCategoryReference($id); /** * Get Reference * - * @param string $entityClass Entity class FQN - * @param string|int $id id + * @param class-string $entityClass Entity class FQN + * @param string|int $id id * - * @return null|$entityClass + * @return T|null Entity reference + * + * @template T */ public function getReference($entityClass, $id); } diff --git a/module/Api/src/Domain/Repository/TaskAllocationRule.php b/module/Api/src/Domain/Repository/TaskAllocationRule.php index 2c90bfdc02..83008aab4e 100644 --- a/module/Api/src/Domain/Repository/TaskAllocationRule.php +++ b/module/Api/src/Domain/Repository/TaskAllocationRule.php @@ -1,78 +1,95 @@ - * @author Alex Peshkov - */ +declare(strict_types=1); namespace Dvsa\Olcs\Api\Domain\Repository; use Doctrine\ORM\Query; +use Doctrine\ORM\QueryBuilder; use Dvsa\Olcs\Api\Entity\Task\TaskAllocationRule as Entity; +use Dvsa\Olcs\Transfer\Query\QueryInterface; -/** - * Task Allocation Rule - * - * @author Rob Caiger - * @author Alex Peshkov - */ class TaskAllocationRule extends AbstractRepository { protected $entity = Entity::class; - /** - * Fetch allocation rules by parameters - * - * @param int $categoryId - * @param string $operatorType - * @param string $ta - * @param bool $isMlh - * @return array - */ - public function fetchByParameters($categoryId, $operatorType = null, $ta = null, $isMlh = null) - { + public function fetchByParameters( + int $categoryId, + ?int $subCategoryId = null, + ?string $operatorType = null, + ?string $ta = null, + ?bool $isMlh = null + ): array { $qb = $this->createQueryBuilder(); - $qb->andWhere($qb->expr()->eq('m.category', ':category')) + $qb + ->andWhere($qb->expr()->eq('m.category', ':category')) ->setParameter('category', $categoryId); + if ($subCategoryId) { + $qb + ->andWhere($qb->expr()->eq('m.subCategory', ':subCategory')) + ->setParameter('subCategory', $subCategoryId); + } else { + $qb + ->andWhere($qb->expr()->isNull('m.subCategory')); + } + if ($operatorType) { - $qb->andWhere($qb->expr()->eq('m.goodsOrPsv', ':operatorType')) + $qb + ->andWhere($qb->expr()->eq('m.goodsOrPsv', ':operatorType')) ->setParameter('operatorType', $operatorType); } else { - $qb->andWhere($qb->expr()->isNull('m.goodsOrPsv')); + $qb + ->andWhere($qb->expr()->isNull('m.goodsOrPsv')); } if ($ta) { - $qb->andWhere($qb->expr()->eq('m.trafficArea', ':trafficArea')) + $qb + ->andWhere($qb->expr()->eq('m.trafficArea', ':trafficArea')) ->setParameter('trafficArea', $ta); } else { - $qb->andWhere($qb->expr()->isNull('m.trafficArea')); + $qb + ->andWhere($qb->expr()->isNull('m.trafficArea')); } if ($isMlh !== null) { - $qb->andWhere($qb->expr()->eq('m.isMlh', ':isMlh')) + $qb + ->andWhere($qb->expr()->eq('m.isMlh', ':isMlh')) ->setParameter('isMlh', $isMlh); } else { - $qb->andWhere($qb->expr()->isNull('m.isMlh')); + $qb + ->andWhere($qb->expr()->isNull('m.isMlh')); } + return $qb->getQuery()->getResult(Query::HYDRATE_OBJECT); } /** - * Build defailt list query - * - * @param \Doctrine\ORM\QueryBuilder $qb - * @param \Dvsa\Olcs\Transfer\Query\QueryInterface $query - * @param array $compositeFields + * Returns matching task allocation rules (as per $this->fetchByParameters); attempting to find a matching + * task allocation rule using provided arguments (including sub-category). Should a rule not be found when + * using sub-category, attempts to find a matching rule without sub-category. */ + public function fetchByParametersWithFallbackWhenSubCategoryNotFound( + int $categoryId, + int $subCategoryId, + ?string $operatorType = null, + ?string $ta = null, + ?bool $isMlh = null + ): array { + $result = $this->fetchByParameters($categoryId, $subCategoryId, $operatorType, $ta, $isMlh); + if ($subCategoryId !== null && count($result) === 0) { + return $this->fetchByParameters($categoryId, null, $operatorType, $ta, $isMlh); + } + + return $result; + } + public function buildDefaultListQuery( - \Doctrine\ORM\QueryBuilder $qb, - \Dvsa\Olcs\Transfer\Query\QueryInterface $query, + QueryBuilder $qb, + QueryInterface $query, $compositeFields = array() - ) { + ): void { // add calculated columns to allow ordering by them parent::buildDefaultListQuery($qb, $query, ['categoryDescription', 'criteria', 'trafficAreaName']); diff --git a/module/Api/src/Entity/Task/AbstractTaskAllocationRule.php b/module/Api/src/Entity/Task/AbstractTaskAllocationRule.php index 67a85b3299..5e4521bc3a 100644 --- a/module/Api/src/Entity/Task/AbstractTaskAllocationRule.php +++ b/module/Api/src/Entity/Task/AbstractTaskAllocationRule.php @@ -23,6 +23,8 @@ * @ORM\HasLifecycleCallbacks * @ORM\Table(name="task_allocation_rule", * indexes={ + * @ORM\Index(name="fk_task_allocation_rule_sub_category_id_sub_category_id", + * columns={"sub_category_id"}), * @ORM\Index(name="ix_task_allocation_rule_category_id", columns={"category_id"}), * @ORM\Index(name="ix_task_allocation_rule_created_by", columns={"created_by"}), * @ORM\Index(name="ix_task_allocation_rule_goods_or_psv", columns={"goods_or_psv"}), @@ -103,6 +105,16 @@ abstract class AbstractTaskAllocationRule implements BundleSerializableInterface */ protected $lastModifiedBy; + /** + * Sub category + * + * @var \Dvsa\Olcs\Api\Entity\System\SubCategory + * + * @ORM\ManyToOne(targetEntity="Dvsa\Olcs\Api\Entity\System\SubCategory", fetch="LAZY") + * @ORM\JoinColumn(name="sub_category_id", referencedColumnName="id", nullable=true) + */ + protected $subCategory; + /** * Team * @@ -323,6 +335,30 @@ public function getLastModifiedBy() return $this->lastModifiedBy; } + /** + * Set the sub category + * + * @param \Dvsa\Olcs\Api\Entity\System\SubCategory $subCategory entity being set as the value + * + * @return TaskAllocationRule + */ + public function setSubCategory($subCategory) + { + $this->subCategory = $subCategory; + + return $this; + } + + /** + * Get the sub category + * + * @return \Dvsa\Olcs\Api\Entity\System\SubCategory + */ + public function getSubCategory() + { + return $this->subCategory; + } + /** * Set the team * diff --git a/test/module/Api/src/Domain/CommandHandler/Task/CreateTaskTest.php b/test/module/Api/src/Domain/CommandHandler/Task/CreateTaskTest.php index 7c83cd2618..058b7dd87e 100644 --- a/test/module/Api/src/Domain/CommandHandler/Task/CreateTaskTest.php +++ b/test/module/Api/src/Domain/CommandHandler/Task/CreateTaskTest.php @@ -52,9 +52,9 @@ public function setUp(): void { $this->sut = new CreateTask(); - $this->mockRepo('Task', m::mock(Repository\Task::class)->makePartial()); - $this->mockRepo('TaskAllocationRule', Repository\TaskAllocationRule::class); - $this->mockRepo('SystemParameter', Repository\SystemParameter::class); + $this->mockRepo(Repository\Task::class, m::mock(Repository\Task::class)->makePartial()); + $this->mockRepo(Repository\TaskAllocationRule::class, Repository\TaskAllocationRule::class); + $this->mockRepo(Repository\SystemParameter::class, Repository\SystemParameter::class); parent::setUp(); @@ -162,7 +162,7 @@ public function testHandleCommandX() $command = Cmd::create($data); - $this->repoMap['Task']->shouldReceive('save') + $this->repoMap[Repository\Task::class]->shouldReceive('save') ->once() ->with(m::type(TaskEntity::class)) ->andReturnUsing( @@ -214,12 +214,12 @@ public function testHandleCommandWithAssignByCategory($rules) { $command = Cmd::create($this->getData(null)); - $this->repoMap['TaskAllocationRule']->shouldReceive('fetchByParameters') - ->with(1) + $this->repoMap[Repository\TaskAllocationRule::class]->shouldReceive('fetchByParametersWithFallbackWhenSubCategoryNotFound') + ->with(1, 2) ->once() ->andReturn($rules); - $this->repoMap['SystemParameter']->shouldReceive('fetchValue') + $this->repoMap[Repository\SystemParameter::class]->shouldReceive('fetchValue') ->with('task.default_team') ->once() ->andReturn(999) @@ -266,8 +266,8 @@ public function testHandleCommandWithAutoAssignmentGoodsLicence() $this->mockLicence(Licence::LICENCE_CATEGORY_GOODS_VEHICLE, true); - $this->repoMap['TaskAllocationRule']->shouldReceive('fetchByParameters') - ->with(1, Licence::LICENCE_CATEGORY_GOODS_VEHICLE, 'B', false) + $this->repoMap[Repository\TaskAllocationRule::class]->shouldReceive('fetchByParametersWithFallbackWhenSubCategoryNotFound') + ->with(1, 2, Licence::LICENCE_CATEGORY_GOODS_VEHICLE, 'B', false) ->once() ->andReturn($this->rules); @@ -289,12 +289,12 @@ public function testHandleCommandWithAutoAssignmentGoodsLicenceRulesNotFound() $this->mockLicence(Licence::LICENCE_CATEGORY_GOODS_VEHICLE, true); - $this->repoMap['TaskAllocationRule']->shouldReceive('fetchByParameters') - ->with(1, Licence::LICENCE_CATEGORY_GOODS_VEHICLE, 'B', false) + $this->repoMap[Repository\TaskAllocationRule::class]->shouldReceive('fetchByParametersWithFallbackWhenSubCategoryNotFound') + ->with(1, 2, Licence::LICENCE_CATEGORY_GOODS_VEHICLE, 'B', false) ->once() ->andReturn([]) - ->shouldReceive('fetchByParameters') - ->with(1, Licence::LICENCE_CATEGORY_GOODS_VEHICLE, 'B') + ->shouldReceive('fetchByParametersWithFallbackWhenSubCategoryNotFound') + ->with(1, 2, Licence::LICENCE_CATEGORY_GOODS_VEHICLE, 'B') ->once() ->andReturn($this->rules) ->getMock(); @@ -317,8 +317,8 @@ public function testHandleCommandWithAutoAssignmentPsvLicence() $this->mockLicence(); - $this->repoMap['TaskAllocationRule']->shouldReceive('fetchByParameters') - ->with(1, Licence::LICENCE_CATEGORY_PSV, 'B') + $this->repoMap[Repository\TaskAllocationRule::class]->shouldReceive('fetchByParametersWithFallbackWhenSubCategoryNotFound') + ->with(1, 2, Licence::LICENCE_CATEGORY_PSV, 'B') ->once() ->andReturn($this->rules); @@ -340,12 +340,12 @@ public function testHandleCommandWithAutoAssignmentPsvLicenceSearchRulesByCatego $this->mockLicence(); - $this->repoMap['TaskAllocationRule']->shouldReceive('fetchByParameters') - ->with(1, Licence::LICENCE_CATEGORY_PSV, 'B') + $this->repoMap[Repository\TaskAllocationRule::class]->shouldReceive('fetchByParametersWithFallbackWhenSubCategoryNotFound') + ->with(1, 2, Licence::LICENCE_CATEGORY_PSV, 'B') ->once() ->andReturn([]) - ->shouldReceive('fetchByParameters') - ->with(1, null, 'B') + ->shouldReceive('fetchByParametersWithFallbackWhenSubCategoryNotFound') + ->with(1, 2, null, 'B') ->once() ->andReturn($this->rules); @@ -367,16 +367,16 @@ public function testHandleCommandWithAutoAssignmentPsvLicenceSearchRulesByCatego $this->mockLicence(); - $this->repoMap['TaskAllocationRule']->shouldReceive('fetchByParameters') - ->with(1, Licence::LICENCE_CATEGORY_PSV, 'B') + $this->repoMap[Repository\TaskAllocationRule::class]->shouldReceive('fetchByParametersWithFallbackWhenSubCategoryNotFound') + ->with(1, 2, Licence::LICENCE_CATEGORY_PSV, 'B') ->once() ->andReturn([]) - ->shouldReceive('fetchByParameters') - ->with(1, null, 'B') + ->shouldReceive('fetchByParametersWithFallbackWhenSubCategoryNotFound') + ->with(1, 2, null, 'B') ->once() ->andReturn([]) - ->shouldReceive('fetchByParameters') - ->with(1, Licence::LICENCE_CATEGORY_PSV) + ->shouldReceive('fetchByParametersWithFallbackWhenSubCategoryNotFound') + ->with(1, 2, Licence::LICENCE_CATEGORY_PSV) ->once() ->andReturn($this->rules); @@ -398,20 +398,20 @@ public function testHandleCommandWithAutoAssignmentPsvLicenceSearchRulesByCatego $this->mockLicence(); - $this->repoMap['TaskAllocationRule']->shouldReceive('fetchByParameters') - ->with(1, Licence::LICENCE_CATEGORY_PSV, 'B') + $this->repoMap[Repository\TaskAllocationRule::class]->shouldReceive('fetchByParametersWithFallbackWhenSubCategoryNotFound') + ->with(1, 2, Licence::LICENCE_CATEGORY_PSV, 'B') ->once() ->andReturn([]) - ->shouldReceive('fetchByParameters') - ->with(1, null, 'B') + ->shouldReceive('fetchByParametersWithFallbackWhenSubCategoryNotFound') + ->with(1, 2, null, 'B') ->once() ->andReturn([]) - ->shouldReceive('fetchByParameters') - ->with(1, Licence::LICENCE_CATEGORY_PSV) + ->shouldReceive('fetchByParametersWithFallbackWhenSubCategoryNotFound') + ->with(1, 2, Licence::LICENCE_CATEGORY_PSV) ->once() ->andReturn([]) - ->shouldReceive('fetchByParameters') - ->with(1) + ->shouldReceive('fetchByParametersWithFallbackWhenSubCategoryNotFound') + ->with(1, 2) ->once() ->andReturn($this->rules); @@ -433,8 +433,8 @@ public function testHandleCommandWithAutoAssignmentGoodsLicenceAlphaSplitNoUser( $this->mockLicence(Licence::LICENCE_CATEGORY_GOODS_VEHICLE, true); - $this->repoMap['TaskAllocationRule']->shouldReceive('fetchByParameters') - ->with(1, Licence::LICENCE_CATEGORY_GOODS_VEHICLE, 'B', false) + $this->repoMap[Repository\TaskAllocationRule::class]->shouldReceive('fetchByParametersWithFallbackWhenSubCategoryNotFound') + ->with(1, 2, Licence::LICENCE_CATEGORY_GOODS_VEHICLE, 'B', false) ->once() ->andReturn($this->rulesForAlphaSplit); @@ -481,8 +481,8 @@ public function testHandleCommandWithAutoAssignmentGoodsLicenceAlphaSplitNoLette ->once() ->getMock(); - $this->repoMap['TaskAllocationRule']->shouldReceive('fetchByParameters') - ->with(1, Licence::LICENCE_CATEGORY_PSV, 'B') + $this->repoMap[Repository\TaskAllocationRule::class]->shouldReceive('fetchByParametersWithFallbackWhenSubCategoryNotFound') + ->with(1, 2, Licence::LICENCE_CATEGORY_PSV, 'B') ->once() ->andReturn($this->rulesForAlphaSplit); @@ -526,8 +526,8 @@ public function testHandleCommandWithAutoAssignmentGoodsLicenceNoAlphaSplitFound ->once() ->getMock(); - $this->repoMap['TaskAllocationRule']->shouldReceive('fetchByParameters') - ->with(1, Licence::LICENCE_CATEGORY_PSV, 'B') + $this->repoMap[Repository\TaskAllocationRule::class]->shouldReceive('fetchByParametersWithFallbackWhenSubCategoryNotFound') + ->with(1, 2, Licence::LICENCE_CATEGORY_PSV, 'B') ->once() ->andReturn($this->rulesForAlphaSplit); @@ -565,8 +565,8 @@ public function testHandleCommandWithAutoAssignmentGoodsLicenceAlphaSplitUnknown ->once() ->getMock(); - $this->repoMap['TaskAllocationRule']->shouldReceive('fetchByParameters') - ->with(1, Licence::LICENCE_CATEGORY_PSV, 'B') + $this->repoMap[Repository\TaskAllocationRule::class]->shouldReceive('fetchByParametersWithFallbackWhenSubCategoryNotFound') + ->with(1, 2, Licence::LICENCE_CATEGORY_PSV, 'B') ->once() ->andReturn($this->rulesForAlphaSplit); @@ -632,8 +632,8 @@ public function testHandleCommandWithAutoAssignmentGoodsLicenceAlphaSplitOrgType ->once() ->getMock(); - $this->repoMap['TaskAllocationRule']->shouldReceive('fetchByParameters') - ->with(1, Licence::LICENCE_CATEGORY_PSV, 'B') + $this->repoMap[Repository\TaskAllocationRule::class]->shouldReceive('fetchByParametersWithFallbackWhenSubCategoryNotFound') + ->with(1, 2, Licence::LICENCE_CATEGORY_PSV, 'B') ->once() ->andReturn($this->rulesForAlphaSplit); @@ -688,7 +688,7 @@ protected function getData($licenceId = 222) */ protected function mockSaveTask($user = null) { - $this->repoMap['Task']->shouldReceive('save') + $this->repoMap[Repository\Task::class]->shouldReceive('save') ->once() ->with(m::type(TaskEntity::class)) ->andReturnUsing( @@ -793,8 +793,8 @@ public function testHandleCommandWithAutoAssignmentGoodsLicenceNoOperatorType() ->once() ->getMock(); - $this->repoMap['TaskAllocationRule']->shouldReceive('fetchByParameters') - ->with(1, Licence::LICENCE_CATEGORY_GOODS_VEHICLE, 'B', false) + $this->repoMap[Repository\TaskAllocationRule::class]->shouldReceive('fetchByParametersWithFallbackWhenSubCategoryNotFound') + ->with(1, 2, Licence::LICENCE_CATEGORY_GOODS_VEHICLE, 'B', false) ->once() ->andReturn($this->rules); diff --git a/test/module/Api/src/Domain/CommandHandler/TaskAllocationRule/CreateTest.php b/test/module/Api/src/Domain/CommandHandler/TaskAllocationRule/CreateTest.php index 0e02d0c913..361abc8929 100644 --- a/test/module/Api/src/Domain/CommandHandler/TaskAllocationRule/CreateTest.php +++ b/test/module/Api/src/Domain/CommandHandler/TaskAllocationRule/CreateTest.php @@ -2,20 +2,16 @@ namespace Dvsa\OlcsTest\Api\Domain\CommandHandler\TaskAllocationRule; -use Mockery as m; use Dvsa\Olcs\Api\Domain\CommandHandler\TaskAllocationRule\Create as CommandHandler; -use Dvsa\Olcs\Transfer\Command\TaskAllocationRule\Create as Cmd; -use Dvsa\OlcsTest\Api\Domain\CommandHandler\CommandHandlerTestCase; -use Dvsa\Olcs\Api\Entity\User\Team as TeamEntity; -use Dvsa\Olcs\Api\Entity\User\User as UserEntity; +use Dvsa\Olcs\Api\Domain\Repository; use Dvsa\Olcs\Api\Entity\System\Category as CategoryEntity; use Dvsa\Olcs\Api\Entity\TrafficArea\TrafficArea as TrafficAreaEntity; +use Dvsa\Olcs\Api\Entity\User\Team as TeamEntity; +use Dvsa\Olcs\Api\Entity\User\User as UserEntity; +use Dvsa\Olcs\Transfer\Command\TaskAllocationRule\Create as Cmd; +use Dvsa\OlcsTest\Api\Domain\CommandHandler\CommandHandlerTestCase; +use Mockery as m; -/** - * TaskAllocationRule CreateTest - * - * @author Mat Evans - */ class CreateTest extends CommandHandlerTestCase { /** @@ -24,7 +20,7 @@ class CreateTest extends CommandHandlerTestCase public function setUp(): void { $this->sut = new CommandHandler(); - $this->mockRepo('TaskAllocationRule', \Dvsa\Olcs\Api\Domain\Repository\TaskAllocationRule::class); + $this->mockRepo(Repository\TaskAllocationRule::class, Repository\TaskAllocationRule::class); parent::setUp(); } @@ -75,7 +71,7 @@ public function testHandleCommandAllParams($goodsOrPsv, $mlh, $expected) ] ); - $this->repoMap['TaskAllocationRule']->shouldReceive('save')->once()->andReturnUsing( + $this->repoMap[Repository\TaskAllocationRule::class]->shouldReceive('save')->once()->andReturnUsing( function (\Dvsa\Olcs\Api\Entity\Task\TaskAllocationRule $tar) use ($goodsOrPsv, $expected) { $this->assertSame($this->references[CategoryEntity::class][1], $tar->getCategory()); $this->assertSame($this->references[TeamEntity::class][2], $tar->getTeam()); @@ -128,7 +124,7 @@ public function testHandleCommandMinParams() ] ); - $this->repoMap['TaskAllocationRule']->shouldReceive('save')->once()->andReturnUsing( + $this->repoMap[Repository\TaskAllocationRule::class]->shouldReceive('save')->once()->andReturnUsing( function (\Dvsa\Olcs\Api\Entity\Task\TaskAllocationRule $tar) { $this->assertSame($this->references[CategoryEntity::class][1], $tar->getCategory()); $this->assertSame($this->references[TeamEntity::class][2], $tar->getTeam()); diff --git a/test/module/Api/src/Domain/CommandHandler/TaskAllocationRule/UpdateTest.php b/test/module/Api/src/Domain/CommandHandler/TaskAllocationRule/UpdateTest.php index 04c7ff2aa0..7c76ae89a5 100644 --- a/test/module/Api/src/Domain/CommandHandler/TaskAllocationRule/UpdateTest.php +++ b/test/module/Api/src/Domain/CommandHandler/TaskAllocationRule/UpdateTest.php @@ -2,20 +2,16 @@ namespace Dvsa\OlcsTest\Api\Domain\CommandHandler\TaskAllocationRule; -use Mockery as m; use Dvsa\Olcs\Api\Domain\CommandHandler\TaskAllocationRule\Update as CommandHandler; -use Dvsa\Olcs\Transfer\Command\TaskAllocationRule\Update as Cmd; -use Dvsa\OlcsTest\Api\Domain\CommandHandler\CommandHandlerTestCase; -use Dvsa\Olcs\Api\Entity\User\Team as TeamEntity; -use Dvsa\Olcs\Api\Entity\User\User as UserEntity; +use Dvsa\Olcs\Api\Domain\Repository; use Dvsa\Olcs\Api\Entity\System\Category as CategoryEntity; use Dvsa\Olcs\Api\Entity\TrafficArea\TrafficArea as TrafficAreaEntity; +use Dvsa\Olcs\Api\Entity\User\Team as TeamEntity; +use Dvsa\Olcs\Api\Entity\User\User as UserEntity; +use Dvsa\Olcs\Transfer\Command\TaskAllocationRule\Update as Cmd; +use Dvsa\OlcsTest\Api\Domain\CommandHandler\CommandHandlerTestCase; +use Mockery as m; -/** - * TaskAllocationRule UpdateTest - * - * @author Mat Evans - */ class UpdateTest extends CommandHandlerTestCase { /** @@ -24,7 +20,7 @@ class UpdateTest extends CommandHandlerTestCase public function setUp(): void { $this->sut = new CommandHandler(); - $this->mockRepo('TaskAllocationRule', \Dvsa\Olcs\Api\Domain\Repository\TaskAllocationRule::class); + $this->mockRepo(Repository\TaskAllocationRule::class, Repository\TaskAllocationRule::class); parent::setUp(); } @@ -80,10 +76,10 @@ public function testHandleCommandAllParams($goodsOrPsv, $mlh, $expected) $tar = new \Dvsa\Olcs\Api\Entity\Task\TaskAllocationRule(); $tar->setId(1304); - $this->repoMap['TaskAllocationRule']->shouldReceive('fetchUsingId') - ->with($command, \Doctrine\ORM\Query::HYDRATE_OBJECT, 42)->once()->andReturn($tar); + $this->repoMap[Repository\TaskAllocationRule::class]->shouldReceive('fetchById') + ->with($command->getId(), \Doctrine\ORM\Query::HYDRATE_OBJECT, 42)->once()->andReturn($tar); - $this->repoMap['TaskAllocationRule']->shouldReceive('save')->once()->andReturnUsing( + $this->repoMap[Repository\TaskAllocationRule::class]->shouldReceive('save')->once()->andReturnUsing( function (\Dvsa\Olcs\Api\Entity\Task\TaskAllocationRule $tar) use ($goodsOrPsv, $expected) { $this->assertSame($this->references[CategoryEntity::class][1], $tar->getCategory()); $this->assertSame($this->references[TeamEntity::class][2], $tar->getTeam()); @@ -140,10 +136,10 @@ public function testHandleCommandMinParams() $tar = new \Dvsa\Olcs\Api\Entity\Task\TaskAllocationRule(); $tar->setId(1304); - $this->repoMap['TaskAllocationRule']->shouldReceive('fetchUsingId') - ->with($command, \Doctrine\ORM\Query::HYDRATE_OBJECT, 42)->once()->andReturn($tar); + $this->repoMap[Repository\TaskAllocationRule::class]->shouldReceive('fetchById') + ->with($command->getId(), \Doctrine\ORM\Query::HYDRATE_OBJECT, 42)->once()->andReturn($tar); - $this->repoMap['TaskAllocationRule']->shouldReceive('save')->once()->andReturnUsing( + $this->repoMap[Repository\TaskAllocationRule::class]->shouldReceive('save')->once()->andReturnUsing( function (\Dvsa\Olcs\Api\Entity\Task\TaskAllocationRule $tar) { $this->assertSame($this->references[CategoryEntity::class][1], $tar->getCategory()); $this->assertSame($this->references[TeamEntity::class][2], $tar->getTeam()); diff --git a/test/module/Api/src/Domain/QueryHandler/TaskAllocationRule/GetListTest.php b/test/module/Api/src/Domain/QueryHandler/TaskAllocationRule/GetListTest.php index 86045810ad..4532f1b96e 100644 --- a/test/module/Api/src/Domain/QueryHandler/TaskAllocationRule/GetListTest.php +++ b/test/module/Api/src/Domain/QueryHandler/TaskAllocationRule/GetListTest.php @@ -3,23 +3,19 @@ namespace Dvsa\OlcsTest\Api\Domain\QueryHandler\TaskAllocationRule; use Dvsa\Olcs\Api\Domain\QueryHandler\TaskAllocationRule\GetList as QueryHandler; +use Dvsa\Olcs\Api\Domain\Repository; use Dvsa\Olcs\Api\Entity; use Dvsa\Olcs\Transfer\Query\TaskAllocationRule\GetList as Query; use Dvsa\OlcsTest\Api\Domain\QueryHandler\QueryHandlerTestCase; use Mockery as m; -/** - * TaskAllocationRule GetListTest - * - * @author Mat Evans - */ class GetListTest extends QueryHandlerTestCase { public function setUp(): void { $this->sut = new QueryHandler(); - $this->mockRepo('TaskAllocationRule', \Dvsa\Olcs\Api\Domain\Repository\TaskAllocationRule::class); + $this->mockRepo(Repository\TaskAllocationRule::class, Repository\TaskAllocationRule::class); parent::setUp(); } @@ -50,6 +46,7 @@ public function testHandleQuery() ->with( [ 'category', + 'subCategory', 'team', 'user' => ['contactDetails' => ['person']], 'trafficArea', @@ -60,7 +57,7 @@ public function testHandleQuery() ->andReturn(['foo' => 'bar']) ->getMock(); - $this->repoMap['TaskAllocationRule'] + $this->repoMap[Repository\TaskAllocationRule::class] ->shouldReceive('fetchList') ->once() ->with($query, \Doctrine\ORM\Query::HYDRATE_OBJECT) diff --git a/test/module/Api/src/Domain/QueryHandler/TaskAllocationRule/GetTest.php b/test/module/Api/src/Domain/QueryHandler/TaskAllocationRule/GetTest.php index 98a1c99501..2192d13764 100644 --- a/test/module/Api/src/Domain/QueryHandler/TaskAllocationRule/GetTest.php +++ b/test/module/Api/src/Domain/QueryHandler/TaskAllocationRule/GetTest.php @@ -2,23 +2,19 @@ namespace Dvsa\OlcsTest\Api\Domain\QueryHandler\TaskAllocationRule; +use Dvsa\Olcs\Api\Domain\QueryHandler\BundleSerializableInterface; use Dvsa\Olcs\Api\Domain\QueryHandler\TaskAllocationRule\Get as QueryHandler; +use Dvsa\Olcs\Api\Domain\Repository; use Dvsa\Olcs\Transfer\Query\TaskAllocationRule\Get as Query; use Dvsa\OlcsTest\Api\Domain\QueryHandler\QueryHandlerTestCase; -use Dvsa\Olcs\Api\Domain\QueryHandler\BundleSerializableInterface; use Mockery as m; -/** - * TaskAllocationRule GetTest - * - * @author Mat Evans - */ class GetTest extends QueryHandlerTestCase { public function setUp(): void { $this->sut = new QueryHandler(); - $this->mockRepo('TaskAllocationRule', \Dvsa\Olcs\Api\Domain\Repository\TaskAllocationRule::class); + $this->mockRepo(Repository\TaskAllocationRule::class, Repository\TaskAllocationRule::class); parent::setUp(); } @@ -31,6 +27,7 @@ public function testHandleQuery() ->with( [ 'category', + 'subCategory', 'team', 'user' => ['contactDetails' => ['person']], 'trafficArea', @@ -41,7 +38,7 @@ public function testHandleQuery() ->andReturn(['foo' => 'bar']) ->getMock(); - $this->repoMap['TaskAllocationRule']->shouldReceive('fetchUsingId')->with($query)->once()->andReturn($mockTas); + $this->repoMap[Repository\TaskAllocationRule::class]->shouldReceive('fetchUsingId')->with($query)->once()->andReturn($mockTas); $this->assertSame( ['foo' => 'bar'], diff --git a/test/module/Api/src/Domain/Repository/TaskAllocationRuleTest.php b/test/module/Api/src/Domain/Repository/TaskAllocationRuleTest.php index c550dfa440..fd8859db27 100644 --- a/test/module/Api/src/Domain/Repository/TaskAllocationRuleTest.php +++ b/test/module/Api/src/Domain/Repository/TaskAllocationRuleTest.php @@ -9,22 +9,18 @@ namespace Dvsa\OlcsTest\Api\Domain\Repository; -use Dvsa\Olcs\Api\Entity\System\Category; +use Doctrine\ORM\EntityRepository; +use Doctrine\ORM\Query; +use Dvsa\Olcs\Api\Domain\Repository\TaskAllocationRule as TaskAllocationRuleRepo; use Dvsa\Olcs\Api\Entity\Task\TaskAllocationRule as Entity; use Dvsa\Olcs\Transfer\Query\QueryInterface; use Mockery as m; -use Dvsa\Olcs\Api\Domain\Repository\TaskAllocationRule as TaskAllocationRuleRepo; -use Doctrine\ORM\Query; -use Doctrine\ORM\EntityRepository; -/** - * Task Allocation Rule Test - * - * @author Rob Caiger - * @author Alex Peshkov - */ class TaskAllocationRuleTest extends RepositoryTestCase { + /** @var TaskAllocationRuleRepo|m\MockInterface */ + protected $sut; + /** * Set up */ @@ -36,33 +32,35 @@ public function setUp(): void /** * Test fetch by parameters * - * @dataProvider paramProvider + * @dataProvider fetchByParametersDataProvider * @param int $category * @param string $operatorType * @param string $trafficArea * @param bool $isMlh * @param string $query */ - public function testFetchByParameters($category, $operatorType, $trafficArea, $isMlh, $query) + public function testFetchByParameters($category, $subCategory, $operatorType, $trafficArea, $isMlh, $query) { $qb = $this->createMockQb('[QUERY]'); $qb->shouldReceive('getQuery->getResult') ->with(Query::HYDRATE_OBJECT) - ->andReturn('RESULT'); + ->once() + ->andReturn(['foo', 'bar']); - /** @var EntityRepository $repo */ $repo = m::mock(EntityRepository::class); $repo->shouldReceive('createQueryBuilder') ->with('m') + ->once() ->andReturn($qb); $this->em->shouldReceive('getRepository') ->with(Entity::class) + ->once() ->andReturn($repo); $this->assertEquals( - 'RESULT', - $this->sut->fetchByParameters($category, $operatorType, $trafficArea, $isMlh) + ['foo', 'bar'], + $this->sut->fetchByParameters($category, $subCategory, $operatorType, $trafficArea, $isMlh) ); $this->assertEquals( $query, @@ -75,32 +73,44 @@ public function testFetchByParameters($category, $operatorType, $trafficArea, $i * * @return array */ - public function paramProvider() + public function fetchByParametersDataProvider(): array { return [ // category, operatorType, trafficArea, isMlh, query [ 111, + 222, 'gv', 'B', 1, - '[QUERY] AND m.category = [[111]] AND m.goodsOrPsv = [[gv]] AND m.trafficArea = [[B]] ' . - 'AND m.isMlh = [[1]]' + '[QUERY] AND m.category = [[111]] AND m.subCategory = [[222]] AND m.goodsOrPsv = [[gv]] AND m.trafficArea = [[B]] ' . + 'AND m.isMlh = [[true]]' ], [ 111, + 222, 'gv', 'B', null, - '[QUERY] AND m.category = [[111]] AND m.goodsOrPsv = [[gv]] AND m.trafficArea = [[B]] ' . + '[QUERY] AND m.category = [[111]] AND m.subCategory = [[222]] AND m.goodsOrPsv = [[gv]] AND m.trafficArea = [[B]] ' . 'AND m.isMlh IS NULL' ], [ 111, + 222, 'gv', null, null, - '[QUERY] AND m.category = [[111]] AND m.goodsOrPsv = [[gv]] AND m.trafficArea IS NULL ' . + '[QUERY] AND m.category = [[111]] AND m.subCategory = [[222]] AND m.goodsOrPsv = [[gv]] AND m.trafficArea IS NULL ' . + 'AND m.isMlh IS NULL' + ], + [ + 111, + 222, + null, + null, + null, + '[QUERY] AND m.category = [[111]] AND m.subCategory = [[222]] AND m.goodsOrPsv IS NULL AND m.trafficArea IS NULL ' . 'AND m.isMlh IS NULL' ], [ @@ -108,12 +118,62 @@ public function paramProvider() null, null, null, - '[QUERY] AND m.category = [[111]] AND m.goodsOrPsv IS NULL AND m.trafficArea IS NULL ' . + null, + '[QUERY] AND m.category = [[111]] AND m.subCategory IS NULL AND m.goodsOrPsv IS NULL AND m.trafficArea IS NULL ' . 'AND m.isMlh IS NULL' ], ]; } + /** + * @dataProvider fetchByParametersAttemptsLookupWithoutSubCategoryWhenCallReturnsNoResultsDataProvider + * @param array $returnValues + * @param bool $expectSubsequentCall + */ + public function testFetchByParametersAttemptsLookupWithoutSubCategoryWhenCallReturnsNoResults(array $returnValues, bool $expectSubsequentCall) + { + $qb = $this->createMockQb('[QUERY]'); + $qb->shouldReceive('getQuery->getResult') + ->with(Query::HYDRATE_OBJECT) + ->times($expectSubsequentCall ? 2 : 1) + ->andReturnValues($returnValues); + + $repo = m::mock(EntityRepository::class); + $repo->shouldReceive('createQueryBuilder') + ->with('m') + ->times($expectSubsequentCall ? 2 : 1) + ->andReturn($qb); + + $this->em->shouldReceive('getRepository') + ->with(Entity::class) + ->times($expectSubsequentCall ? 2 : 1) + ->andReturn($repo); + + $this->assertEquals( + ['foo'], + $this->sut->fetchByParametersWithFallbackWhenSubCategoryNotFound(1, 2) + ); + } + + public function fetchByParametersAttemptsLookupWithoutSubCategoryWhenCallReturnsNoResultsDataProvider(): array + { + return [ + 'Subcategory returns result' => [ + [ + ['foo'], + ], + false + ], + 'Subcategory returns no results' => [ + [ + [], + ['foo'], + ], + true + ], + ]; + } + /** * Test build default list query */