From bc9eca06df9c2f25bc3fe3054fd45e82acd5fbc7 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 28 Mar 2020 16:27:03 +0100 Subject: [PATCH] test path fixes --- Models/AccountRelation.php | 17 ++++++++------- Models/GroupRelation.php | 17 ++++++++------- Models/TaskElement.php | 32 +++++++++++----------------- Models/TaskMapper.php | 26 ++++++++++++++++------ tests/BackendControllerTest.php | 2 -- tests/ControllerTest.php | 2 -- tests/Models/AccountRelationTest.php | 9 ++++---- tests/Models/GroupRelationTest.php | 9 ++++---- 8 files changed, 60 insertions(+), 54 deletions(-) diff --git a/Models/AccountRelation.php b/Models/AccountRelation.php index 1e5b324..dc10e15 100644 --- a/Models/AccountRelation.php +++ b/Models/AccountRelation.php @@ -15,6 +15,7 @@ declare(strict_types=1); namespace Modules\Tasks\Models; use Modules\Admin\Models\Account; +use Modules\Admin\Models\NullAccount; /** * Task relation to account @@ -29,33 +30,33 @@ class AccountRelation extends RelationAbstract /** * Relation object * - * @var int|Account + * @var Account * @since 1.0.0 */ - private $relation = null; + private Account $relation; /** * Constructor. * - * @param int|Account $account Account - * @param int $duty Duty type + * @param null|Account $account Account + * @param int $duty Duty type * * @since 1.0.0 */ - public function __construct($account = 0, int $duty = DutyType::TO) + public function __construct(Account $account = null, int $duty = DutyType::TO) { - $this->relation = $account; + $this->relation = $account ?? new NullAccount(); $this->duty = $duty; } /** * Get relation object. * - * @return int|Account + * @return Account * * @since 1.0.0 */ - public function getRelation() + public function getRelation() : Account { return $this->relation; } diff --git a/Models/GroupRelation.php b/Models/GroupRelation.php index b4056bf..4e1cc41 100644 --- a/Models/GroupRelation.php +++ b/Models/GroupRelation.php @@ -15,6 +15,7 @@ declare(strict_types=1); namespace Modules\Tasks\Models; use Modules\Admin\Models\Group; +use Modules\Admin\Models\NullGroup; /** * Task relation to group @@ -29,33 +30,33 @@ class GroupRelation extends RelationAbstract /** * Relation object * - * @var int|Group + * @var Group * @since 1.0.0 */ - private $relation = null; + private Group $relation; /** * Constructor. * - * @param int|Group $group Group - * @param int $duty Duty type + * @param null|Group $group Group + * @param int $duty Duty type * * @since 1.0.0 */ - public function __construct($group = 0, int $duty = DutyType::TO) + public function __construct(Group $group = null, int $duty = DutyType::TO) { - $this->relation = $group; + $this->relation = $group ?? new NullGroup(); $this->duty = $duty; } /** * Get relation object. * - * @return int|Group + * @return Group * * @since 1.0.0 */ - public function getRelation() + public function getRelation() : Group { return $this->relation; } diff --git a/Models/TaskElement.php b/Models/TaskElement.php index 17967cd..4043465 100644 --- a/Models/TaskElement.php +++ b/Models/TaskElement.php @@ -358,10 +358,8 @@ class TaskElement implements \JsonSerializable public function isToAccount(int $id) : bool { foreach ($this->accRelation as $acc) { - if (($acc->getDuty() === DutyType::TO - && ($acc->getRelation() instanceof Account) && $acc->getRelation()->getId() === $id) - || ($acc->getDuty() === DutyType::TO - && $acc->getRelation() === $id) + if ($acc->getDuty() === DutyType::TO + && $acc->getRelation()->getId() === $id ) { return true; } @@ -382,10 +380,8 @@ class TaskElement implements \JsonSerializable public function isToGroup(int $id) : bool { foreach ($this->grpRelation as $grp) { - if (($grp->getDuty() === DutyType::TO - && ($grp->getRelation() instanceof Group) && $grp->getRelation()->getId() === $id) - || ($grp->getDuty() === DutyType::TO - && $grp->getRelation() === $id) + if ($grp->getDuty() === DutyType::TO + && $grp->getRelation()->getId() === $id ) { return true; } @@ -406,10 +402,8 @@ class TaskElement implements \JsonSerializable public function isCCAccount(int $id) : bool { foreach ($this->accRelation as $acc) { - if (($acc->getDuty() === DutyType::CC - && ($acc->getRelation() instanceof Account) && $acc->getRelation()->getId() === $id) - || ($acc->getDuty() === DutyType::CC - && $acc->getRelation() === $id) + if ($acc->getDuty() === DutyType::CC + && $acc->getRelation()->getId() === $id ) { return true; } @@ -430,10 +424,8 @@ class TaskElement implements \JsonSerializable public function isCCGroup(int $id) : bool { foreach ($this->grpRelation as $grp) { - if (($grp->getDuty() === DutyType::CC - && ($grp->getRelation() instanceof Group) && $grp->getRelation()->getId() === $id) - || ($grp->getDuty() === DutyType::CC - && $grp->getRelation() === $id) + if ($grp->getDuty() === DutyType::CC + && $grp->getRelation()->getId() === $id ) { return true; } @@ -474,7 +466,7 @@ class TaskElement implements \JsonSerializable $groupId = $group->getId(); foreach ($this->grpRelation as $grp) { - $grpId = !\is_int($grp->getRelation()) ? $grp->getRelation()->getId() : $grp->getRelation(); + $grpId = $grp->getRelation()->getId(); if ($grpId === $groupId && $grp->getDuty() === DutyType::TO) { return; @@ -498,7 +490,7 @@ class TaskElement implements \JsonSerializable $accountId = $account->getId(); foreach ($this->accRelation as $acc) { - $accId = !\is_int($acc->getRelation()) ? $acc->getRelation()->getId() : $acc->getRelation(); + $accId = $acc->getRelation()->getId(); if ($accId === $accountId && $acc->getDuty() === DutyType::TO) { return; @@ -566,7 +558,7 @@ class TaskElement implements \JsonSerializable $groupId = $group->getId(); foreach ($this->grpRelation as $grp) { - $grpId = !\is_int($grp->getRelation()) ? $grp->getRelation()->getId() : $grp->getRelation(); + $grpId = $grp->getRelation()->getId(); if ($grpId === $groupId && $grp->getDuty() === DutyType::CC) { return; @@ -590,7 +582,7 @@ class TaskElement implements \JsonSerializable $accountId = $account->getId(); foreach ($this->accRelation as $acc) { - $accId = !\is_int($acc->getRelation()) ? $acc->getRelation()->getId() : $acc->getRelation(); + $accId = $acc->getRelation()->getId(); if ($accId === $accountId && $acc->getDuty() === DutyType::CC) { return; diff --git a/Models/TaskMapper.php b/Models/TaskMapper.php index b73d26b..19b0509 100644 --- a/Models/TaskMapper.php +++ b/Models/TaskMapper.php @@ -19,6 +19,7 @@ use Modules\Calendar\Models\ScheduleMapper; use Modules\Media\Models\MediaMapper; use phpOMS\DataStorage\Database\DataMapperAbstract; use phpOMS\DataStorage\Database\Query\Builder; +use phpOMS\DataStorage\Database\RelationType; /** * Mapper class. @@ -131,6 +132,14 @@ final class TaskMapper extends DataMapperAbstract */ protected static string $primaryField = 'task_id'; + /** + * {@inheritdoc} + */ + public static function get($primaryKey, int $relations = RelationType::ALL, int $depth = 3, string $ref = null, Builder $query = null) + { + return parent::get($primaryKey, $relations, $depth, $ref, $query); + } + /** * Get open tasks by createdBy * @@ -142,9 +151,10 @@ final class TaskMapper extends DataMapperAbstract */ public static function getOpenCreatedBy(int $user) : array { + $depth = 3; $query = self::getQuery(); - $query->where(self::$table . '.task_created_by', '=', $user) - ->where(self::$table . '.task_status', '=', TaskStatus::OPEN); + $query->where(self::$table . '_' . $depth . '.task_created_by', '=', $user) + ->where(self::$table . '_' . $depth . '.task_status', '=', TaskStatus::OPEN); return self::getAllByQuery($query); } @@ -160,12 +170,13 @@ final class TaskMapper extends DataMapperAbstract */ public static function getOpenTo(int $user) : array { + $depth = 3; $query = self::getQuery(); $query->innerJoin(TaskElementMapper::getTable()) ->on(self::$table . '.task_id', '=', TaskElementMapper::getTable() . '.task_element_task') ->innerJoin(AccountRelationMapper::getTable()) ->on(TaskElementMapper::getTable() . '.task_element_id', '=', AccountRelationMapper::getTable() . '.task_account_task_element') - ->where(self::$table . '.task_status', '=', TaskStatus::OPEN) + ->where(self::$table . '_' . $depth . '.task_status', '=', TaskStatus::OPEN) ->andWhere(AccountRelationMapper::getTable() . '.task_account_account', '=', $user) ->andWhere(AccountRelationMapper::getTable() . '.task_account_duty', '=', DutyType::TO); @@ -183,12 +194,13 @@ final class TaskMapper extends DataMapperAbstract */ public static function getOpenAny(int $user) : array { + $depth = 3; $query = self::getQuery(); $query->innerJoin(TaskElementMapper::getTable()) ->on(self::$table . '.task_id', '=', TaskElementMapper::getTable() . '.task_element_task') ->innerJoin(AccountRelationMapper::getTable()) ->on(TaskElementMapper::getTable() . '.task_element_id', '=', AccountRelationMapper::getTable() . '.task_account_task_element') - ->where(self::$table . '.task_status', '=', TaskStatus::OPEN) + ->where(self::$table . '_' . $depth . '.task_status', '=', TaskStatus::OPEN) ->andWhere(AccountRelationMapper::getTable() . '.task_account_account', '=', $user); return self::getAllByQuery($query); @@ -205,12 +217,13 @@ final class TaskMapper extends DataMapperAbstract */ public static function getOpenCC(int $user) : array { + $depth = 3; $query = self::getQuery(); $query->innerJoin(TaskElementMapper::getTable()) ->on(self::$table . '.task_id', '=', TaskElementMapper::getTable() . '.task_element_task') ->innerJoin(AccountRelationMapper::getTable()) ->on(TaskElementMapper::getTable() . '.task_element_id', '=', AccountRelationMapper::getTable() . '.task_account_task_element') - ->where(self::$table . '.task_status', '=', TaskStatus::OPEN) + ->where(self::$table . '_' . $depth . '.task_status', '=', TaskStatus::OPEN) ->andWhere(AccountRelationMapper::getTable() . '.task_account_account', '=', $user) ->andWhere(AccountRelationMapper::getTable() . '.task_account_duty', '=', DutyType::CC); @@ -228,8 +241,9 @@ final class TaskMapper extends DataMapperAbstract */ public static function getCreatedBy(int $user) : array { + $depth = 3; $query = self::getQuery(); - $query->where(self::$table . '.task_created_by', '=', $user); + $query->where(self::$table . '_' . $depth . '.task_created_by', '=', $user); return self::getAllByQuery($query); } diff --git a/tests/BackendControllerTest.php b/tests/BackendControllerTest.php index 4660e31..8aad83b 100644 --- a/tests/BackendControllerTest.php +++ b/tests/BackendControllerTest.php @@ -14,8 +14,6 @@ declare(strict_types=1); namespace Modules\Task\tests; -require_once __DIR__ . '/../../tests/Autoloader.php'; - use Model\CoreSettings; use Modules\Admin\Models\AccountPermission; diff --git a/tests/ControllerTest.php b/tests/ControllerTest.php index 8ce91b7..3abcf0a 100644 --- a/tests/ControllerTest.php +++ b/tests/ControllerTest.php @@ -14,8 +14,6 @@ declare(strict_types=1); namespace Modules\Task\tests; -require_once __DIR__ . '/../../tests/Autoloader.php'; - use Model\CoreSettings; use Modules\Admin\Models\AccountPermission; use Modules\Tasks\Models\TaskPriority; diff --git a/tests/Models/AccountRelationTest.php b/tests/Models/AccountRelationTest.php index f59717f..c40670f 100644 --- a/tests/Models/AccountRelationTest.php +++ b/tests/Models/AccountRelationTest.php @@ -14,6 +14,7 @@ declare(strict_types=1); namespace Modules\Tasks\tests\Models; +use Modules\Admin\Models\NullAccount; use Modules\Tasks\Models\AccountRelation; use Modules\Tasks\Models\DutyType; @@ -26,20 +27,20 @@ class AccountRelationTest extends \PHPUnit\Framework\TestCase { $obj = new AccountRelation(); self::assertEquals(0, $obj->getId()); - self::assertEquals(0, $obj->getRelation()); + self::assertEquals(0, $obj->getRelation()->getId()); self::assertEquals(DutyType::TO, $obj->getDuty()); } public function testSetGet() : void { - $obj = new AccountRelation(1, DutyType::CC); - self::assertEquals(1, $obj->getRelation()); + $obj = new AccountRelation($a = new NullAccount(1), DutyType::CC); + self::assertEquals(1, $obj->getRelation()->getId()); self::assertEquals(DutyType::CC, $obj->getDuty()); self::assertEquals([ 'id' => 0, 'duty' => DutyType::CC, - 'relation' => 1, + 'relation' => $a, ], $obj->toArray()); self::assertEquals($obj->toArray(), $obj->jsonSerialize()); diff --git a/tests/Models/GroupRelationTest.php b/tests/Models/GroupRelationTest.php index d1aa555..39cd5a1 100644 --- a/tests/Models/GroupRelationTest.php +++ b/tests/Models/GroupRelationTest.php @@ -14,6 +14,7 @@ declare(strict_types=1); namespace Modules\Tasks\tests\Models; +use Modules\Admin\Models\NullGroup; use Modules\Tasks\Models\DutyType; use Modules\Tasks\Models\GroupRelation; @@ -26,20 +27,20 @@ class GroupRelationTest extends \PHPUnit\Framework\TestCase { $obj = new GroupRelation(); self::assertEquals(0, $obj->getId()); - self::assertEquals(0, $obj->getRelation()); + self::assertEquals(0, $obj->getRelation()->getId()); self::assertEquals(DutyType::TO, $obj->getDuty()); } public function testSetGet() : void { - $obj = new GroupRelation(1, DutyType::CC); - self::assertEquals(1, $obj->getRelation()); + $obj = new GroupRelation($g = new NullGroup(1), DutyType::CC); + self::assertEquals(1, $obj->getRelation()->getId()); self::assertEquals(DutyType::CC, $obj->getDuty()); self::assertEquals([ 'id' => 0, 'duty' => DutyType::CC, - 'relation' => 1, + 'relation' => $g, ], $obj->toArray()); self::assertEquals($obj->toArray(), $obj->jsonSerialize());