From d0e973cb261b2560b7cf2fd79ccf1029ee995d8c Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Tue, 2 Nov 2021 21:57:09 +0100 Subject: [PATCH] add unit tests --- Controller/ApiController.php | 18 +- Models/Employee.php | 39 +- Models/EmployeeEducationHistory.php | 81 +--- Models/EmployeeHistory.php | 154 +------- Models/EmployeeWorkHistory.php | 79 +--- Models/NullEmployeeEducationHistory.php | 12 + Models/NullEmployeeHistory.php | 12 + Models/NullEmployeeWorkHistory.php | 12 + Models/StaffList.php | 80 ---- tests/Controller/ApiControllerTest.php | 374 ++++++++++++++++++ tests/Models/EmployeeEducationHistoryTest.php | 75 ++++ tests/Models/EmployeeHistoryMapperTest.php | 2 +- tests/Models/EmployeeMapperTest.php | 1 + tests/Models/EmployeeTest.php | 99 ++++- tests/Models/EmployeeWorkHistoryTest.php | 69 ++++ tests/Models/EmplyeeHistoryTest.php | 47 ++- .../NullEmployeeEducationHistoryTest.php | 42 ++ tests/Models/NullEmployeeHistoryTest.php | 42 ++ tests/Models/NullEmployeeTest.php | 42 ++ tests/Models/NullEmployeeWorkHistoryTest.php | 42 ++ tests/phpunit_default.xml | 2 +- 21 files changed, 906 insertions(+), 418 deletions(-) delete mode 100755 Models/StaffList.php create mode 100644 tests/Controller/ApiControllerTest.php create mode 100644 tests/Models/EmployeeEducationHistoryTest.php create mode 100644 tests/Models/EmployeeWorkHistoryTest.php create mode 100644 tests/Models/NullEmployeeEducationHistoryTest.php create mode 100644 tests/Models/NullEmployeeHistoryTest.php create mode 100644 tests/Models/NullEmployeeTest.php create mode 100644 tests/Models/NullEmployeeWorkHistoryTest.php diff --git a/Controller/ApiController.php b/Controller/ApiController.php index f3606d2..26e2b36 100755 --- a/Controller/ApiController.php +++ b/Controller/ApiController.php @@ -269,13 +269,13 @@ final class ApiController extends Controller private function createEmployeeHistoryFromRequest(RequestAbstract $request) : EmployeeHistory { $history = new EmployeeHistory((int) ($request->getData('employee') ?? 0)); - $history->setUnit((int) ($request->getData('unit') ?? 0)); - $history->setDepartment((int) ($request->getData('department') ?? 0)); - $history->setPosition((int) ($request->getData('position') ?? 0)); - $history->setStart(new \DateTime($request->getData('start') ?? 'now')); + $history->unit = (int) ($request->getData('unit') ?? 0); + $history->department = (int) ($request->getData('department') ?? 0); + $history->position = (int) ($request->getData('position') ?? 0); + $history->start = new \DateTime($request->getData('start') ?? 'now'); if (!empty($request->getData('end'))) { - $history->setEnd(new \DateTime($request->getData('end'))); + $history->end = new \DateTime($request->getData('end')); } return $history; @@ -343,7 +343,7 @@ final class ApiController extends Controller private function createEmployeeWorkHistoryFromRequest(RequestAbstract $request) : EmployeeWorkHistory { $history = new EmployeeWorkHistory((int) ($request->getData('employee') ?? 0)); - $history->setStart(new \DateTime($request->getData('start') ?? 'now')); + $history->start = new \DateTime($request->getData('start') ?? 'now'); $history->jobTitle = $request->getData('title'); $history->address->name = $request->getData('name'); $history->address->address = $request->getData('address') ?? ''; @@ -354,7 +354,7 @@ final class ApiController extends Controller $history->address->setType(AddressType::WORK); if (!empty($request->getData('end'))) { - $history->setEnd(new \DateTime($request->getData('end'))); + $history->end = new \DateTime($request->getData('end')); } return $history; @@ -422,7 +422,7 @@ final class ApiController extends Controller private function createEmployeeEducationHistoryFromRequest(RequestAbstract $request) : EmployeeEducationHistory { $history = new EmployeeEducationHistory((int) ($request->getData('employee') ?? 0)); - $history->setStart(new \DateTime($request->getData('start') ?? 'now')); + $history->start = new \DateTime($request->getData('start') ?? 'now'); $history->educationTitle = $request->getData('title'); $history->score = $request->getData('score') ?? ''; $history->passed = (bool) ($request->getData('passed') ?? true); @@ -435,7 +435,7 @@ final class ApiController extends Controller $history->address->setType(AddressType::EDUCATION); if (!empty($request->getData('end'))) { - $history->setEnd(new \DateTime($request->getData('end'))); + $history->end = new \DateTime($request->getData('end')); } return $history; diff --git a/Models/Employee.php b/Models/Employee.php index ef4ca22..5caa6cf 100755 --- a/Models/Employee.php +++ b/Models/Employee.php @@ -202,6 +202,20 @@ class Employee implements \JsonSerializable, ArrayableInterface return empty($this->companyHistory) ? new NullEmployeeHistory() : \end($this->companyHistory); } + /** + * Add company history to employee + * + * @param mixed $history Company history + * + * @return void + * + * @since 1.0.0 + */ + public function addEducationHistory($history) : void + { + $this->educationHistory[] = $history; + } + /** * Get employee company education history. * @@ -226,6 +240,20 @@ class Employee implements \JsonSerializable, ArrayableInterface return empty($this->educationHistory) ? new NullEmployeeEducationHistory() : \end($this->educationHistory); } + /** + * Add company history to employee + * + * @param mixed $history Company history + * + * @return void + * + * @since 1.0.0 + */ + public function addWorkHistory($history) : void + { + $this->workHistory[] = $history; + } + /** * Get employee company work. * @@ -258,17 +286,12 @@ class Employee implements \JsonSerializable, ArrayableInterface return [ 'id' => $this->id, 'profile' => $this->profile, + 'history' => $this->companyHistory, + 'workHistory' => $this->workHistory, + 'educationHistory' => $this->educationHistory, ]; } - /** - * {@inheritdoc} - */ - public function __toString() - { - return (string) \json_encode($this->toArray()); - } - /** * {@inheritdoc} */ diff --git a/Models/EmployeeEducationHistory.php b/Models/EmployeeEducationHistory.php index d942938..dfe5c9c 100755 --- a/Models/EmployeeEducationHistory.php +++ b/Models/EmployeeEducationHistory.php @@ -42,7 +42,7 @@ class EmployeeEducationHistory implements \JsonSerializable, ArrayableInterface * @var int|Employee * @since 1.0.0 */ - private $employee = 0; + public $employee = 0; public Address $address; @@ -66,7 +66,7 @@ class EmployeeEducationHistory implements \JsonSerializable, ArrayableInterface * @var \DateTime * @since 1.0.0 */ - private \DateTime $start; + public \DateTime $start; /** * End date @@ -74,7 +74,7 @@ class EmployeeEducationHistory implements \JsonSerializable, ArrayableInterface * @var null|\DateTime * @since 1.0.0 */ - private ?\DateTime $end = null; + public ?\DateTime $end = null; /** * Constructor. @@ -102,70 +102,6 @@ class EmployeeEducationHistory implements \JsonSerializable, ArrayableInterface return $this->id; } - /** - * Get the employee this history belongs to - * - * @return int|Employee - * - * @since 1.0.0 - */ - public function getEmployee() - { - return empty($this->employee) ? new NullEmployee() : $this->employee; - } - - /** - * Get start date - * - * @return \DateTime - * - * @since 1.0.0 - */ - public function getStart() : \DateTime - { - return $this->start; - } - - /** - * Set start date - * - * @param \DateTime $start Start date - * - * @return void - * - * @since 1.0.0 - */ - public function setStart(\DateTime $start) : void - { - $this->start = $start; - } - - /** - * Get end date - * - * @return null|\DateTime - * - * @since 1.0.0 - */ - public function getEnd() : ?\DateTime - { - return $this->end; - } - - /** - * Set end date - * - * @param \DateTime $end End date - * - * @return void - * - * @since 1.0.0 - */ - public function setEnd(\DateTime $end) : void - { - $this->end = $end; - } - /** * {@inheritdoc} */ @@ -174,19 +110,14 @@ class EmployeeEducationHistory implements \JsonSerializable, ArrayableInterface return [ 'id' => $this->id, 'employee' => !\is_int($this->employee) ? $this->employee->getId() : $this->employee, + 'educationTitle' => $this->educationTitle, + 'passed' => $this->passed, + 'score' => $this->score, 'start' => $this->start, 'end' => $this->end, ]; } - /** - * {@inheritdoc} - */ - public function __toString() - { - return (string) \json_encode($this->toArray()); - } - /** * {@inheritdoc} */ diff --git a/Models/EmployeeHistory.php b/Models/EmployeeHistory.php index 9879f11..13d594c 100755 --- a/Models/EmployeeHistory.php +++ b/Models/EmployeeHistory.php @@ -87,7 +87,7 @@ class EmployeeHistory implements \JsonSerializable, ArrayableInterface * @var \DateTime * @since 1.0.0 */ - private \DateTime $start; + public \DateTime $start; /** * End date @@ -95,7 +95,7 @@ class EmployeeHistory implements \JsonSerializable, ArrayableInterface * @var null|\DateTime * @since 1.0.0 */ - private ?\DateTime $end = null; + public ?\DateTime $end = null; /** * Constructor. @@ -122,148 +122,6 @@ class EmployeeHistory implements \JsonSerializable, ArrayableInterface return $this->id; } - /** - * Get the employee this history belongs to - * - * @return int|Employee - * - * @since 1.0.0 - */ - public function getEmployee() - { - return empty($this->employee) ? new NullEmployee() : $this->employee; - } - - /** - * Get the position - * - * @return int|Position - * - * @since 1.0.0 - */ - public function getPosition() - { - return empty($this->position) ? new NullPosition() : $this->position; - } - - /** - * Set the position - * - * @param int|Position $position Position - * - * @return void - * - * @since 1.0.0 - */ - public function setPosition($position) : void - { - $this->position = $position; - } - - /** - * Get the unit - * - * @return int|Unit - * - * @since 1.0.0 - */ - public function getUnit() - { - return empty($this->unit) ? new NullUnit() : $this->unit; - } - - /** - * Set the unit - * - * @param int|Unit $unit Unit - * - * @return void - * - * @since 1.0.0 - */ - public function setUnit($unit) : void - { - $this->unit = $unit; - } - - /** - * Get the department - * - * @return int|Department - * - * @since 1.0.0 - */ - public function getDepartment() - { - return empty($this->department) ? new NullDepartment() : $this->department; - } - - /** - * Set the department - * - * @param int|Department $department Department - * - * @return void - * - * @since 1.0.0 - */ - public function setDepartment($department) : void - { - $this->department = $department; - } - - /** - * Get start date - * - * @return \DateTime - * - * @since 1.0.0 - */ - public function getStart() : \DateTime - { - return $this->start; - } - - /** - * Set start date - * - * @param \DateTime $start Start date - * - * @return void - * - * @since 1.0.0 - */ - public function setStart(\DateTime $start) : void - { - $this->start = $start; - } - - /** - * Get end date - * - * @return null|\DateTime - * - * @since 1.0.0 - */ - public function getEnd() : ?\DateTime - { - return $this->end; - } - - /** - * Set end date - * - * @param \DateTime $end End date - * - * @return void - * - * @since 1.0.0 - */ - public function setEnd(\DateTime $end) : void - { - $this->end = $end; - } - /** * {@inheritdoc} */ @@ -280,14 +138,6 @@ class EmployeeHistory implements \JsonSerializable, ArrayableInterface ]; } - /** - * {@inheritdoc} - */ - public function __toString() - { - return (string) \json_encode($this->toArray()); - } - /** * {@inheritdoc} */ diff --git a/Models/EmployeeWorkHistory.php b/Models/EmployeeWorkHistory.php index eacc652..e8456ea 100755 --- a/Models/EmployeeWorkHistory.php +++ b/Models/EmployeeWorkHistory.php @@ -42,7 +42,7 @@ class EmployeeWorkHistory implements \JsonSerializable, ArrayableInterface * @var int|Employee * @since 1.0.0 */ - private $employee = 0; + public $employee = 0; public Address $address; @@ -62,7 +62,7 @@ class EmployeeWorkHistory implements \JsonSerializable, ArrayableInterface * @var \DateTime * @since 1.0.0 */ - private \DateTime $start; + public \DateTime $start; /** * End date @@ -70,7 +70,7 @@ class EmployeeWorkHistory implements \JsonSerializable, ArrayableInterface * @var null|\DateTime * @since 1.0.0 */ - private ?\DateTime $end = null; + public ?\DateTime $end = null; /** * Constructor. @@ -98,70 +98,6 @@ class EmployeeWorkHistory implements \JsonSerializable, ArrayableInterface return $this->id; } - /** - * Get the employee this history belongs to - * - * @return int|Employee - * - * @since 1.0.0 - */ - public function getEmployee() - { - return empty($this->employee) ? new NullEmployee() : $this->employee; - } - - /** - * Get start date - * - * @return \DateTime - * - * @since 1.0.0 - */ - public function getStart() : \DateTime - { - return $this->start; - } - - /** - * Set start date - * - * @param \DateTime $start Start date - * - * @return void - * - * @since 1.0.0 - */ - public function setStart(\DateTime $start) : void - { - $this->start = $start; - } - - /** - * Get end date - * - * @return null|\DateTime - * - * @since 1.0.0 - */ - public function getEnd() : ?\DateTime - { - return $this->end; - } - - /** - * Set end date - * - * @param \DateTime $end End date - * - * @return void - * - * @since 1.0.0 - */ - public function setEnd(\DateTime $end) : void - { - $this->end = $end; - } - /** * {@inheritdoc} */ @@ -170,19 +106,12 @@ class EmployeeWorkHistory implements \JsonSerializable, ArrayableInterface return [ 'id' => $this->id, 'employee' => !\is_int($this->employee) ? $this->employee->getId() : $this->employee, + 'jobTitle' => $this->jobTitle, 'start' => $this->start, 'end' => $this->end, ]; } - /** - * {@inheritdoc} - */ - public function __toString() - { - return (string) \json_encode($this->toArray()); - } - /** * {@inheritdoc} */ diff --git a/Models/NullEmployeeEducationHistory.php b/Models/NullEmployeeEducationHistory.php index f3b05a6..5664afa 100755 --- a/Models/NullEmployeeEducationHistory.php +++ b/Models/NullEmployeeEducationHistory.php @@ -24,4 +24,16 @@ namespace Modules\HumanResourceManagement\Models; */ final class NullEmployeeEducationHistory extends EmployeeEducationHistory { + /** + * Constructor + * + * @param int $id Model id + * + * @since 1.0.0 + */ + public function __construct(int $id = 0) + { + $this->id = $id; + parent::__construct(); + } } diff --git a/Models/NullEmployeeHistory.php b/Models/NullEmployeeHistory.php index 6204e86..6dcd324 100755 --- a/Models/NullEmployeeHistory.php +++ b/Models/NullEmployeeHistory.php @@ -24,4 +24,16 @@ namespace Modules\HumanResourceManagement\Models; */ final class NullEmployeeHistory extends EmployeeHistory { + /** + * Constructor + * + * @param int $id Model id + * + * @since 1.0.0 + */ + public function __construct(int $id = 0) + { + $this->id = $id; + parent::__construct(); + } } diff --git a/Models/NullEmployeeWorkHistory.php b/Models/NullEmployeeWorkHistory.php index f06d411..ab32a11 100755 --- a/Models/NullEmployeeWorkHistory.php +++ b/Models/NullEmployeeWorkHistory.php @@ -24,4 +24,16 @@ namespace Modules\HumanResourceManagement\Models; */ final class NullEmployeeWorkHistory extends EmployeeWorkHistory { + /** + * Constructor + * + * @param int $id Model id + * + * @since 1.0.0 + */ + public function __construct(int $id = 0) + { + $this->id = $id; + parent::__construct(); + } } diff --git a/Models/StaffList.php b/Models/StaffList.php deleted file mode 100755 index 8ade44e..0000000 --- a/Models/StaffList.php +++ /dev/null @@ -1,80 +0,0 @@ -dbPool = $dbPool; - } - - /** - * Get all staff members. - * - * This function gets all accounts in a range - * - * @param array $filter Filter for search results - * @param int $offset Offset for first account - * @param int $limit Limit for results - * - * @return array - * - * @since 1.0.0 - */ - public function getList($filter = null, $offset = 0, $limit = 100) : array - { - $result = []; - - return $result; - } - - /** - * Get task stats. - * - * @return array - * - * @since 1.0.0 - */ - public function getStats() : array - { - return []; - } -} diff --git a/tests/Controller/ApiControllerTest.php b/tests/Controller/ApiControllerTest.php new file mode 100644 index 0000000..a2900fe --- /dev/null +++ b/tests/Controller/ApiControllerTest.php @@ -0,0 +1,374 @@ +name = 'HRMgmtDepartmentTest'; + $department->description = 'Description'; + $department->unit = new NullUnit(1); + DepartmentMapper::create($department); + + $position = new Position(); + $position->name = 'HRMgmtPositionTest'; + $position->description = 'Description'; + PositionMapper::create($position); + } + + /** + * {@inheritdoc} + */ + protected function setUp() : void + { + $this->app = new class() extends ApplicationAbstract + { + protected string $appName = 'Api'; + }; + + $this->app->dbPool = $GLOBALS['dbpool']; + $this->app->orgId = 1; + $this->app->accountManager = new AccountManager($GLOBALS['session']); + $this->app->appSettings = new CoreSettings(); + $this->app->moduleManager = new ModuleManager($this->app, __DIR__ . '/../../../../Modules/'); + $this->app->dispatcher = new Dispatcher($this->app); + $this->app->eventManager = new EventManager($this->app->dispatcher); + $this->app->eventManager->importFromFile(__DIR__ . '/../../../../Web/Api/Hooks.php'); + $this->app->sessionManager = new HttpSession(36000); + + $account = new Account(); + TestUtils::setMember($account, 'id', 1); + + $permission = new AccountPermission(); + $permission->setUnit(1); + $permission->setApp('api'); + $permission->setPermission( + PermissionType::READ + | PermissionType::CREATE + | PermissionType::MODIFY + | PermissionType::DELETE + | PermissionType::PERMISSION + ); + + $account->addPermission($permission); + + $this->app->accountManager->add($account); + $this->app->router = new WebRouter(); + + $this->module = $this->app->moduleManager->get('HumanResourceManagement'); + + TestUtils::setMember($this->module, 'app', $this->app); + } + + /** + * @covers Modules\HumanResourceManagement\Controller\ApiController + * @group module + */ + public function testEmployeeFromAccountCreate() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('profiles', '1'); + + // can create multiple accounts if profiles is a list of ids e.g. 1,2,3 + $this->module->apiEmployeeCreate($request, $response); + self::assertGreaterThan(0, self::$employee = $response->get('')['response'][0]->getId()); + } + + /** + * @covers Modules\HumanResourceManagement\Controller\ApiController + * @group module + */ + public function testNewEmployeeCreate() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('name1', 'NewEmployee'); + + $this->module->apiEmployeeCreate($request, $response); + self::assertGreaterThan(0, self::$employee = $response->get('')['response']->getId()); + } + + /** + * @covers Modules\HumanResourceManagement\Controller\ApiController + * @group module + */ + public function testApiEmployeeCreateInvalidData() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('invalid', '1'); + + $this->module->apiEmployeeCreate($request, $response); + self::assertEquals(RequestStatusCode::R_400, $response->header->status); + } + + /** + * @covers Modules\HumanResourceManagement\Controller\ApiController + * @group module + */ + public function testEmployeeCreateFromAccountInvalidData() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('invalid', '1'); + + $this->module->apiEmployeeFromAccountCreate($request, $response); + self::assertEquals(RequestStatusCode::R_400, $response->header->status); + } + + /** + * @covers Modules\HumanResourceManagement\Controller\ApiController + * @group module + */ + public function testNewEmployeeCreateInvalidData() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('invalid', '1'); + + $this->module->apiEmployeeNewCreate($request, $response); + self::assertEquals(RequestStatusCode::R_400, $response->header->status); + } + + /** + * @depends testEmployeeFromAccountCreate + * @covers Modules\HumanResourceManagement\Controller\ApiController + * @group module + */ + public function testEmployeeHistoryCreate() : void + { + $start = DateTime::generateDateTime( + (new \DateTime())->setTimestamp(\time() - \mt_rand(31622400 * 5, 31622400 * 10)), + (new \DateTime())->setTimestamp(\time() - \mt_rand(31622400 * 1, 31622400 * 4)) + ); + + $end = DateTime::generateDateTime( + $start, + (new \DateTime())->setTimestamp($start->getTimestamp() + \mt_rand(1, 31622400)) + ); + + for ($i = 0; $i < 3; ++$i) { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('employee', self::$employee); + $request->setData('start', $start->format('Y-m-d')); + $request->setData('end', $i + 1 < 3 ? $end->format('Y-m-d') : null); + $request->setData('unit', 1); + $request->setData('department', 1); + $request->setData('position', 1); + $this->module->apiEmployeeHistoryCreate($request, $response); + self::assertGreaterThan(0, $response->get('')['response']->getId()); + + $start = clone $end; + $end = DateTime::generateDateTime( + $start, + (new \DateTime())->setTimestamp($start->getTimestamp() + \mt_rand(1, 31622400)) + ); + } + } + + /** + * @covers Modules\HumanResourceManagement\Controller\ApiController + * @group module + */ + public function testEmployeeHistoryCreateInvalidData() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('invalid', '1'); + + $this->module->apiEmployeeHistoryCreate($request, $response); + self::assertEquals(RequestStatusCode::R_400, $response->header->status); + } + + /** + * @depends testEmployeeFromAccountCreate + * @covers Modules\HumanResourceManagement\Controller\ApiController + * @group module + */ + public function testEmployeeWorkHistoryCreate() : void + { + $start = DateTime::generateDateTime( + (new \DateTime())->setTimestamp(\time() - \mt_rand(31622400 * 5, 31622400 * 10)), + (new \DateTime())->setTimestamp(\time() - \mt_rand(31622400 * 1, 31622400 * 4)) + ); + + $end = DateTime::generateDateTime( + $start, + (new \DateTime())->setTimestamp($start->getTimestamp() + \mt_rand(1, 31622400)) + ); + + for ($i = 0; $i < 3; ++$i) { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('employee', self::$employee); + $request->setData('start', $start->format('Y-m-d')); + $request->setData('end', $i + 1 < 3 ? $end->format('Y-m-d') : null); + $request->setData('title', 'Title: ' . $i); + $request->setData('name', 'Address Name'); + $request->setData('address', 'Some test address'); + $request->setData('postal', \str_pad((string) \mt_rand(1000, 99999), 5, '0', \STR_PAD_LEFT)); + $request->setData('city', 'TestCity'); + $request->setData('country', ISO3166TwoEnum::getRandom()); + $request->setData('state', ''); + + $this->module->apiEmployeeWorkHistoryCreate($request, $response); + self::assertGreaterThan(0, $response->get('')['response']->getId()); + + $start = clone $end; + $end = DateTime::generateDateTime( + $start, + (new \DateTime())->setTimestamp($start->getTimestamp() + \mt_rand(1, 31622400)) + ); + } + } + + /** + * @covers Modules\HumanResourceManagement\Controller\ApiController + * @group module + */ + public function testEmployeeWorkHistoryCreateInvalidData() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('invalid', '1'); + + $this->module->apiEmployeeWorkHistoryCreate($request, $response); + self::assertEquals(RequestStatusCode::R_400, $response->header->status); + } + + /** + * @depends testEmployeeFromAccountCreate + * @covers Modules\HumanResourceManagement\Controller\ApiController + * @group module + */ + public function testEmployeeEducationHistoryCreate() : void + { + $start = DateTime::generateDateTime( + (new \DateTime())->setTimestamp(\time() - \mt_rand(31622400 * 5, 31622400 * 10)), + (new \DateTime())->setTimestamp(\time() - \mt_rand(31622400 * 1, 31622400 * 4)) + ); + + $end = DateTime::generateDateTime( + $start, + (new \DateTime())->setTimestamp($start->getTimestamp() + \mt_rand(1, 31622400)) + ); + + for ($i = 0; $i < 3; ++$i) { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('employee', self::$employee); + $request->setData('start', $start->format('Y-m-d')); + $request->setData('end', $i + 1 < 3 ? $end->format('Y-m-d') : null); + $request->setData('title', 'Title: ' . $i); + $request->setData('score', (string) \mt_rand(0, 100)); + $request->setData('name', 'Address Name'); + $request->setData('address', 'Some test address'); + $request->setData('postal', \str_pad((string) \mt_rand(1000, 99999), 5, '0', \STR_PAD_LEFT)); + $request->setData('city', 'TestCity'); + $request->setData('country', ISO3166TwoEnum::getRandom()); + $request->setData('state', ''); + + $this->module->apiEmployeeEducationHistoryCreate($request, $response); + self::assertGreaterThan(0, $response->get('')['response']->getId()); + + $start = clone $end; + $end = DateTime::generateDateTime( + $start, + (new \DateTime())->setTimestamp($start->getTimestamp() + \mt_rand(1, 31622400)) + ); + } + } + + /** + * @covers Modules\HumanResourceManagement\Controller\ApiController + * @group module + */ + public function testEmployeeEducationHistoryCreateInvalidData() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('')); + + $request->header->account = 1; + $request->setData('invalid', '1'); + + $this->module->apiEmployeeEducationHistoryCreate($request, $response); + self::assertEquals(RequestStatusCode::R_400, $response->header->status); + } +} diff --git a/tests/Models/EmployeeEducationHistoryTest.php b/tests/Models/EmployeeEducationHistoryTest.php new file mode 100644 index 0000000..07a6b6e --- /dev/null +++ b/tests/Models/EmployeeEducationHistoryTest.php @@ -0,0 +1,75 @@ +history = new EmployeeEducationHistory(); + } + /** + * @covers Modules\HumanResourceManagement\Models\EmployeeEducationHistory + * @group module + */ + public function testDefault() : void + { + self::assertEquals(0, $this->history->getId()); + self::assertNull($this->history->end); + self::assertEquals(0, $this->history->employee); + self::assertEquals('', $this->history->educationTitle); + self::assertTrue($this->history->passed); + self::assertEquals('', $this->history->score); + self::assertInstanceOf('\DateTime', $this->history->start); + self::assertInstanceOf('\Modules\Admin\Models\Address', $this->history->address); + } + + /** + * @covers Modules\HumanResourceManagement\Models\EmployeeEducationHistory + * @group module + */ + public function testSerialize() : void + { + $this->history->employee = 2; + $this->history->educationTitle = 'title'; + $this->history->score = '69'; + $this->history->passed = false; + + $serialized = $this->history->jsonSerialize(); + unset($serialized['start']); + + self::assertEquals( + [ + 'id' => 0, + 'employee' => 2, + 'educationTitle' => 'title', + 'passed' => false, + 'score' => '69', + 'end' => null, + ], + $serialized + ); + } +} diff --git a/tests/Models/EmployeeHistoryMapperTest.php b/tests/Models/EmployeeHistoryMapperTest.php index 8de3bc0..5c19b14 100755 --- a/tests/Models/EmployeeHistoryMapperTest.php +++ b/tests/Models/EmployeeHistoryMapperTest.php @@ -39,6 +39,6 @@ final class EmployeeHistoryMapperTest extends \PHPUnit\Framework\TestCase self::assertEquals($id, $history->getId()); $historyR = EmployeeHistoryMapper::get($history->getId()); - self::assertEquals($history->getEmployee()->getId(), $historyR->getEmployee()->getId()); + self::assertEquals($history->employee->getId(), $historyR->employee->getId()); } } diff --git a/tests/Models/EmployeeMapperTest.php b/tests/Models/EmployeeMapperTest.php index f8e7f85..5c9fdd8 100755 --- a/tests/Models/EmployeeMapperTest.php +++ b/tests/Models/EmployeeMapperTest.php @@ -48,5 +48,6 @@ final class EmployeeMapperTest extends \PHPUnit\Framework\TestCase $employeeR = EmployeeMapper::get($employee->getId()); self::assertEquals($employee->profile->getId(), $employeeR->profile->getId()); + self::assertGreaterThan(0, EmployeeMapper::getFromAccount(1)->getId()); } } diff --git a/tests/Models/EmployeeTest.php b/tests/Models/EmployeeTest.php index cfa38b7..2f1514a 100755 --- a/tests/Models/EmployeeTest.php +++ b/tests/Models/EmployeeTest.php @@ -14,28 +14,109 @@ declare(strict_types=1); namespace Modules\HumanResourceManagement\tests\Models; +use Modules\Profile\Models\Profile; +use Modules\Profile\Models\NullProfile; use Modules\HumanResourceManagement\Models\Employee; +use Modules\HumanResourceManagement\Models\EmployeeHistory; +use Modules\HumanResourceManagement\Models\EmployeeWorkHistory; +use Modules\HumanResourceManagement\Models\EmployeeEducationHistory; /** * @internal */ final class EmployeeTest extends \PHPUnit\Framework\TestCase { + private Employee $employee; + + /** + * {@inheritdoc} + */ + protected function setUp() : void + { + $this->employee = new Employee(); + } + /** * @covers Modules\HumanResourceManagement\Models\Employee * @group module */ public function testDefault() : void { - $employee = new Employee(); + self::assertEquals(0, $this->employee->getId()); + self::assertGreaterThan(0, \strlen($this->employee->getSemiPrivateHash())); + self::assertFalse($this->employee->compareSemiPrivateHash('123')); + self::assertInstanceOf('\Modules\Media\Models\NullMedia', $this->employee->image); + self::assertInstanceOf('\Modules\HumanResourceManagement\Models\NullEmployeeHistory', $this->employee->getNewestHistory()); + self::assertEquals([], $this->employee->getHistory()); + self::assertEquals([], $this->employee->getEducationHistory()); + self::assertEquals([], $this->employee->getWorkHistory()); + } - self::assertEquals(0, $employee->getId()); - self::assertGreaterThan(0, \strlen($employee->getSemiPrivateHash())); - self::assertFalse($employee->compareSemiPrivateHash('123')); - self::assertInstanceOf('\Modules\Media\Models\NullMedia', $employee->image); - self::assertInstanceOf('\Modules\HumanResourceManagement\Models\NullEmployeeHistory', $employee->getNewestHistory()); - self::assertEquals([], $employee->getHistory()); - self::assertEquals([], $employee->getEducationHistory()); - self::assertEquals([], $employee->getWorkHistory()); + /** + * @covers Modules\HumanResourceManagement\Models\Employee + * @group module + */ + public function testPrivateHashInputOutput() : void + { + $temp = $this->employee->getSemiPrivateHash(); + self::assertTrue($this->employee->compareSemiPrivateHash($temp)); + $this->employee->updateSemiPrivateHash(); + self::assertFalse($this->employee->compareSemiPrivateHash($temp)); + } + + /** + * @covers Modules\HumanResourceManagement\Models\Employee + * @group module + */ + public function testHistoryInputOutput() : void + { + $this->employee->addHistory($a = new EmployeeHistory()); + $this->employee->addHistory($b = new EmployeeHistory()); + self::assertCount(2, $this->employee->getHistory()); + self::assertEquals($b, $this->employee->getNewestHistory()); + } + + /** + * @covers Modules\HumanResourceManagement\Models\Employee + * @group module + */ + public function testWorkHistoryInputOutput() : void + { + $this->employee->addWorkHistory($a = new EmployeeWorkHistory()); + $this->employee->addWorkHistory($b = new EmployeeWorkHistory()); + self::assertCount(2, $this->employee->getWorkHistory()); + self::assertEquals($b, $this->employee->getNewestWorkHistory()); + } + + /** + * @covers Modules\HumanResourceManagement\Models\Employee + * @group module + */ + public function testEducationHistoryInputOutput() : void + { + $this->employee->addEducationHistory($a = new EmployeeEducationHistory()); + $this->employee->addEducationHistory($b = new EmployeeEducationHistory()); + self::assertCount(2, $this->employee->getEducationHistory()); + self::assertEquals($b, $this->employee->getNewestEducationHistory()); + } + + /** + * @covers Modules\HumanResourceManagement\Models\Employee + * @group module + */ + public function testSerialize() : void + { + $serialized = $this->employee->jsonSerialize(); + unset($serialized['profile']); + + self::assertEquals( + [ + 'id' => 0, + 'history' => [], + 'workHistory' => [], + 'educationHistory' => [], + ], + $serialized + ); } } diff --git a/tests/Models/EmployeeWorkHistoryTest.php b/tests/Models/EmployeeWorkHistoryTest.php new file mode 100644 index 0000000..6aa73c9 --- /dev/null +++ b/tests/Models/EmployeeWorkHistoryTest.php @@ -0,0 +1,69 @@ +history = new EmployeeWorkHistory(); + } + /** + * @covers Modules\HumanResourceManagement\Models\EmployeeWorkHistory + * @group module + */ + public function testDefault() : void + { + self::assertEquals(0, $this->history->getId()); + self::assertNull($this->history->end); + self::assertEquals(0, $this->history->employee); + self::assertEquals('', $this->history->jobTitle); + self::assertInstanceOf('\DateTime', $this->history->start); + self::assertInstanceOf('\Modules\Admin\Models\Address', $this->history->address); + } + + /** + * @covers Modules\HumanResourceManagement\Models\EmployeeWorkHistory + * @group module + */ + public function testSerialize() : void + { + $this->history->employee = 2; + $this->history->jobTitle = 'title'; + + $serialized = $this->history->jsonSerialize(); + unset($serialized['start']); + + self::assertEquals( + [ + 'id' => 0, + 'employee' => 2, + 'jobTitle' => 'title', + 'end' => null, + ], + $serialized + ); + } +} diff --git a/tests/Models/EmplyeeHistoryTest.php b/tests/Models/EmplyeeHistoryTest.php index 5b4d2a9..c27e3a7 100755 --- a/tests/Models/EmplyeeHistoryTest.php +++ b/tests/Models/EmplyeeHistoryTest.php @@ -21,20 +21,51 @@ use Modules\HumanResourceManagement\Models\EmployeeHistory; */ final class EmployeeHistoryTest extends \PHPUnit\Framework\TestCase { + private EmployeeHistory $history; + + /** + * {@inheritdoc} + */ + protected function setUp() : void + { + $this->history = new EmployeeHistory(); + } /** * @covers Modules\HumanResourceManagement\Models\EmployeeHistory * @group module */ public function testDefault() : void { - $history = new EmployeeHistory(); + self::assertEquals(0, $this->history->getId()); + self::assertNull($this->history->end); + self::assertEquals(0, $this->history->employee); + self::assertNull($this->history->position); + self::assertNull($this->history->unit); + self::assertNull($this->history->department); + self::assertInstanceOf('\DateTime', $this->history->start); + } - self::assertEquals(0, $history->getId()); - self::assertNull($history->getEnd()); - self::assertInstanceOf('\Modules\HumanResourceManagement\Models\NullEmployee', $history->getEmployee()); - self::assertInstanceOf('\Modules\Organization\Models\NullPosition', $history->getPosition()); - self::assertInstanceOf('\Modules\Organization\Models\NullUnit', $history->getUnit()); - self::assertInstanceOf('\Modules\Organization\Models\NullDepartment', $history->getDepartment()); - self::assertInstanceOf('\DateTime', $history->getStart()); + /** + * @covers Modules\HumanResourceManagement\Models\EmployeeHistory + * @group module + */ + public function testSerialize() : void + { + $this->history->employee = 2; + + $serialized = $this->history->jsonSerialize(); + unset($serialized['start']); + + self::assertEquals( + [ + 'id' => 0, + 'employee' => 2, + 'unit' => null, + 'department' => null, + 'position' => null, + 'end' => null, + ], + $serialized + ); } } diff --git a/tests/Models/NullEmployeeEducationHistoryTest.php b/tests/Models/NullEmployeeEducationHistoryTest.php new file mode 100644 index 0000000..de1cd0c --- /dev/null +++ b/tests/Models/NullEmployeeEducationHistoryTest.php @@ -0,0 +1,42 @@ +getId()); + } +} diff --git a/tests/Models/NullEmployeeHistoryTest.php b/tests/Models/NullEmployeeHistoryTest.php new file mode 100644 index 0000000..de32e55 --- /dev/null +++ b/tests/Models/NullEmployeeHistoryTest.php @@ -0,0 +1,42 @@ +getId()); + } +} diff --git a/tests/Models/NullEmployeeTest.php b/tests/Models/NullEmployeeTest.php new file mode 100644 index 0000000..fc72885 --- /dev/null +++ b/tests/Models/NullEmployeeTest.php @@ -0,0 +1,42 @@ +getId()); + } +} diff --git a/tests/Models/NullEmployeeWorkHistoryTest.php b/tests/Models/NullEmployeeWorkHistoryTest.php new file mode 100644 index 0000000..bcefeba --- /dev/null +++ b/tests/Models/NullEmployeeWorkHistoryTest.php @@ -0,0 +1,42 @@ +getId()); + } +} diff --git a/tests/phpunit_default.xml b/tests/phpunit_default.xml index 722365c..9208c99 100755 --- a/tests/phpunit_default.xml +++ b/tests/phpunit_default.xml @@ -1,5 +1,5 @@ - + *vendor*