From 358905b099aac37e89e933f9392b7663338c988a Mon Sep 17 00:00:00 2001 From: Rafal Samek Date: Tue, 26 Sep 2023 14:52:56 +0200 Subject: [PATCH] [ScheduleService] Unit tests fixed, ScheduleService incomplete --- src/Components/HttpClientInterface.php | 2 +- src/Domains/Schedule/ScheduleItemEntity.php | 61 +++++++++++++ .../Schedule/ScheduleItemInterface.php | 8 +- src/Domains/Schedule/ScheduleRepository.php | 22 ++++- src/Domains/Schedule/ScheduleService.php | 42 +++++++++ src/Domains/Task/TaskEntity.php | 91 +++++++++++++++++++ src/Domains/Task/TaskFactory.php | 21 ++++- src/Domains/Task/TaskRepository.php | 10 +- src/Domains/Task/TaskStorage.php | 5 +- tests/ScheduleCest.php | 49 +++++++++- tests/TaskCest.php | 19 +++- 11 files changed, 313 insertions(+), 17 deletions(-) create mode 100644 src/Domains/Schedule/ScheduleItemEntity.php create mode 100644 src/Domains/Schedule/ScheduleService.php diff --git a/src/Components/HttpClientInterface.php b/src/Components/HttpClientInterface.php index f3ea554..bc38e15 100644 --- a/src/Components/HttpClientInterface.php +++ b/src/Components/HttpClientInterface.php @@ -10,5 +10,5 @@ interface HttpClientInterface * @param string $uri * @return array */ - public function request(string $method, string $uri):array; + public function request(string $method, string $uri): array; } \ No newline at end of file diff --git a/src/Domains/Schedule/ScheduleItemEntity.php b/src/Domains/Schedule/ScheduleItemEntity.php new file mode 100644 index 0000000..0d3e3ef --- /dev/null +++ b/src/Domains/Schedule/ScheduleItemEntity.php @@ -0,0 +1,61 @@ +scheduleId; + } + + public function setScheduleId(int $scheduleId): ScheduleItemEntity + { + $this->scheduleId = $scheduleId; + return $this; + } + + public function getStartTime(): int + { + return $this->startTime; + } + + public function setStartTime(int $startTime): ScheduleItemEntity + { + $this->startTime = $startTime; + return $this; + } + + public function getEndTime(): int + { + return $this->endTime; + } + + public function setEndTime(int $endTime): ScheduleItemEntity + { + $this->endTime = $endTime; + return $this; + } + + public function getType(): string + { + return $this->type; + } + + public function setType(string $type): ScheduleItemEntity + { + $this->type = $type; + return $this; + } +} \ No newline at end of file diff --git a/src/Domains/Schedule/ScheduleItemInterface.php b/src/Domains/Schedule/ScheduleItemInterface.php index d7cb063..d985707 100644 --- a/src/Domains/Schedule/ScheduleItemInterface.php +++ b/src/Domains/Schedule/ScheduleItemInterface.php @@ -8,20 +8,20 @@ interface ScheduleItemInterface /** * @return int */ - public function getScheduleId():int; + public function getScheduleId(): int; /** * @return int */ - public function getStartTime():int; + public function getStartTime(): int; /** * @return int */ - public function getEndTime():int; + public function getEndTime(): int; /** * @return string */ - public function getType():string; + public function getType(): string; } \ No newline at end of file diff --git a/src/Domains/Schedule/ScheduleRepository.php b/src/Domains/Schedule/ScheduleRepository.php index ac74ec5..4d6180d 100644 --- a/src/Domains/Schedule/ScheduleRepository.php +++ b/src/Domains/Schedule/ScheduleRepository.php @@ -2,25 +2,41 @@ namespace Tymeshift\PhpTest\Domains\Schedule; -use Tymeshift\PhpTest\Domains\Schedule\ScheduleStorage; +use Tymeshift\PhpTest\Exceptions\StorageDataMissingException; use Tymeshift\PhpTest\Interfaces\EntityInterface; use Tymeshift\PhpTest\Interfaces\FactoryInterface; class ScheduleRepository { + private const SCHEDULE_DOES_NOT_EXIST = 'Schedule with id %s does not exist.'; + + /** + * @var ScheduleStorage + */ private $storage; + /** + * @var ScheduleFactory + */ private $factory; - public function __construct(ScheduleStorage $storage, FactoryInterface $factory) + public function __construct(ScheduleStorage $storage, ScheduleFactory $factory) { $this->storage = $storage; $this->factory = $factory; } - public function getById(int $id):EntityInterface + /** + * @throws StorageDataMissingException + */ + public function getById(int $id): EntityInterface { $data = $this->storage->getById($id); + if (empty($data)) { + throw new StorageDataMissingException( + sprintf(self::SCHEDULE_DOES_NOT_EXIST, $id) + ); + } return $this->factory->createEntity($data); } } \ No newline at end of file diff --git a/src/Domains/Schedule/ScheduleService.php b/src/Domains/Schedule/ScheduleService.php new file mode 100644 index 0000000..417be3f --- /dev/null +++ b/src/Domains/Schedule/ScheduleService.php @@ -0,0 +1,42 @@ +scheduleRepository = $scheduleRepository; + $this->taskRepository = $taskRepository; + } + + /** + * @return ScheduleItemInterface[] + * @throws StorageDataMissingException + * @throws InvalidCollectionDataProvidedException + */ + public function setTasks(int $scheduleId): array + { + $schedule = $this->scheduleRepository->getById($scheduleId); + $taskCollection = $this->taskRepository->getByScheduleId($scheduleId); + foreach ($taskCollection as $task) { + + } + } +} \ No newline at end of file diff --git a/src/Domains/Task/TaskEntity.php b/src/Domains/Task/TaskEntity.php index 4b076fa..7e350bb 100644 --- a/src/Domains/Task/TaskEntity.php +++ b/src/Domains/Task/TaskEntity.php @@ -4,8 +4,99 @@ namespace Tymeshift\PhpTest\Domains\Task; use Tymeshift\PhpTest\Interfaces\EntityInterface; +use DateTime; class TaskEntity implements EntityInterface { + /** + * @var int + */ + private int $id; + /** + * @var int + */ + private int $scheduleId; + + /** + * @var DateTime + */ + private DateTime $startTime; + + /** + * @var int + */ + private int $duration; + + /** + * @return int + */ + public function getId(): int + { + return $this->id; + } + + /** + * @param int $id + * @return TaskEntity + */ + public function setId(int $id): TaskEntity + { + $this->id = $id; + return $this; + } + + /** + * @return int + */ + public function getScheduleId(): int + { + return $this->scheduleId; + } + + /** + * @param int $scheduleId + * @return TaskEntity + */ + public function setScheduleId(int $scheduleId): TaskEntity + { + $this->scheduleId = $scheduleId; + return $this; + } + + /** + * @return DateTime + */ + public function getStartTime(): DateTime + { + return $this->startTime; + } + + /** + * @param DateTime $startTime + * @return TaskEntity + */ + public function setStartTime(DateTime $startTime): TaskEntity + { + $this->startTime = $startTime; + return $this; + } + + /** + * @return int + */ + public function getDuration(): int + { + return $this->duration; + } + + /** + * @param int $duration + * @return TaskEntity + */ + public function setDuration(int $duration): TaskEntity + { + $this->duration = $duration; + return $this; + } } \ No newline at end of file diff --git a/src/Domains/Task/TaskFactory.php b/src/Domains/Task/TaskFactory.php index 2ccfa22..eb4aab8 100644 --- a/src/Domains/Task/TaskFactory.php +++ b/src/Domains/Task/TaskFactory.php @@ -13,13 +13,30 @@ class TaskFactory implements FactoryInterface public function createEntity(array $data): EntityInterface { - // TODO: Implement createEntity() method. + $entity = new TaskEntity(); + if (isset($data['id']) && is_int($data['id'])) { + $entity->setId($data['id']); + } + + if (isset($data['schedule_id']) && is_int($data['schedule_id'])) { + $entity->setScheduleId($data['schedule_id']); + } + + if (isset($data['start_time']) && is_int($data['start_time'])) { + $entity->setStartTime((new \DateTime())->setTimestamp($data['start_time'])); + } + + if (isset($data['duration']) && is_int($data['duration'])) { + $entity->setDuration($data['duration']); + } + + return $entity; } /** * @throws InvalidCollectionDataProvidedException */ - public function createCollection(array $data):CollectionInterface + public function createCollection(array $data): CollectionInterface { return (new TaskCollection())->createFromArray($data, $this); } diff --git a/src/Domains/Task/TaskRepository.php b/src/Domains/Task/TaskRepository.php index cd56671..1abd1f6 100644 --- a/src/Domains/Task/TaskRepository.php +++ b/src/Domains/Task/TaskRepository.php @@ -3,6 +3,7 @@ namespace Tymeshift\PhpTest\Domains\Task; +use Tymeshift\PhpTest\Exceptions\InvalidCollectionDataProvidedException; use Tymeshift\PhpTest\Interfaces\EntityCollection; use Tymeshift\PhpTest\Interfaces\EntityInterface; use Tymeshift\PhpTest\Interfaces\RepositoryInterface; @@ -30,9 +31,14 @@ public function getById(int $id): EntityInterface // TODO: Implement getById() method. } - public function getByScheduleId(int $scheduleId):TaskCollection + /** + * @throws InvalidCollectionDataProvidedException + */ + public function getByScheduleId(int $scheduleId): TaskCollection { - + return $this->factory->createCollection( + $this->storage->getByScheduleId($scheduleId) + ); } public function getByIds(array $ids): TaskCollection diff --git a/src/Domains/Task/TaskStorage.php b/src/Domains/Task/TaskStorage.php index 9488da7..1aaa290 100644 --- a/src/Domains/Task/TaskStorage.php +++ b/src/Domains/Task/TaskStorage.php @@ -16,7 +16,10 @@ public function __construct(HttpClientInterface $httpClient) public function getByScheduleId(int $id): array { - + return $this->client->request( + 'GET', + 'https://tymeshift.com/get-by-schedule-id/' . $id + ); } public function getByIds(array $ids): array diff --git a/tests/ScheduleCest.php b/tests/ScheduleCest.php index 4776fec..7b56e78 100644 --- a/tests/ScheduleCest.php +++ b/tests/ScheduleCest.php @@ -4,14 +4,20 @@ use Codeception\Example; use Mockery\MockInterface; +use Tymeshift\PhpTest\Components\HttpClientInterface; +use Tymeshift\PhpTest\Domains\Schedule\ScheduleService; +use Tymeshift\PhpTest\Domains\Schedule\ScheduleItemInterface; use Tymeshift\PhpTest\Domains\Schedule\ScheduleRepository; use Tymeshift\PhpTest\Domains\Schedule\ScheduleFactory; use Tymeshift\PhpTest\Domains\Schedule\ScheduleStorage; +use Tymeshift\PhpTest\Exceptions\InvalidCollectionDataProvidedException; use Tymeshift\PhpTest\Exceptions\StorageDataMissingException; +use Tymeshift\PhpTest\Domains\Task\TaskRepository; +use Tymeshift\PhpTest\Domains\Task\TaskStorage; +use Tymeshift\PhpTest\Domains\Task\TaskFactory; class ScheduleCest { - /** * @var MockInterface|ScheduleStorage */ @@ -68,6 +74,36 @@ public function testGetByIdFail(\UnitTester $tester) }); } + /** + * @dataProvider tasksDataProvider + * @throws InvalidCollectionDataProvidedException + * @throws StorageDataMissingException + */ + public function testService(Example $example, \UnitTester $tester) + { + $httpClientMock = \Mockery::mock(HttpClientInterface::class); + + $data = (array) $example->getIterator(); + $httpClientMock->shouldReceive('request') + ->once() + ->with('GET', 'https://tymeshift.com/get-by-schedule-id/1') + ->andReturn($data); + + $taskStorage = new TaskStorage($httpClientMock); + $taskRepository = new TaskRepository($taskStorage, new TaskFactory()); + + $scheduleService = new ScheduleService( + $this->scheduleRepository, + $taskRepository + ); + + $schedule = $scheduleService->setTasks(1); + $tester->assertContainsOnlyInstancesOf( + ScheduleItemInterface::class, + $schedule->getScheduleItems() + ); + } + /** * @return array[] */ @@ -77,4 +113,15 @@ protected function scheduleProvider() ['id' => 1, 'start_time' => 1631232000, 'end_time' => 1631232000 + 86400, 'name' => 'Test'], ]; } + + public function tasksDataProvider(): array + { + return [ + [ + ["id" => 123, "schedule_id" => 1, "start_time" => 0, "duration" => 3600], + ["id" => 431, "schedule_id" => 1, "start_time" => 3600, "duration" => 650], + ["id" => 332, "schedule_id" => 1, "start_time" => 5600, "duration" => 3600], + ] + ]; + } } \ No newline at end of file diff --git a/tests/TaskCest.php b/tests/TaskCest.php index d4ae159..d9fc082 100644 --- a/tests/TaskCest.php +++ b/tests/TaskCest.php @@ -11,6 +11,7 @@ use Tymeshift\PhpTest\Domains\Task\TaskFactory; use Tymeshift\PhpTest\Domains\Task\TaskRepository; use Tymeshift\PhpTest\Domains\Task\TaskStorage; +use Tymeshift\PhpTest\Exceptions\InvalidCollectionDataProvidedException; class TaskCest { @@ -20,11 +21,16 @@ class TaskCest */ private $taskRepository; + /** + * @var Mock|HttpClientInterface + */ + private $httpClientMock; + public function _before() { - $httpClientMock = \Mockery::mock(HttpClientInterface::class); - $storage = new TaskStorage($httpClientMock); + $this->httpClientMock = \Mockery::mock(HttpClientInterface::class); + $storage = new TaskStorage($this->httpClientMock); $this->taskRepository = new TaskRepository($storage, new TaskFactory()); } @@ -36,9 +42,16 @@ public function _after() /** * @dataProvider tasksDataProvider + * @throws InvalidCollectionDataProvidedException */ public function testGetTasks(Example $example, \UnitTester $tester) { + $data = (array) $example->getIterator(); + $this->httpClientMock->shouldReceive('request') + ->once() + ->with('GET', 'https://tymeshift.com/get-by-schedule-id/1') + ->andReturn($data); + $tasks = $this->taskRepository->getByScheduleId(1); $tester->assertInstanceOf(TaskCollection::class, $tasks); } @@ -50,7 +63,7 @@ public function testGetTasksFailed(\UnitTester $tester) }); } - public function tasksDataProvider() + public function tasksDataProvider(): array { return [ [