expectException(InvalidArgumentException::class); new TaskLog([]); } public function testGetBrokerInstantiatingTheTaskLogBrokerAndReturningItWithTheRequiredInterface() { $logBrokerMock = $this->getMockBuilder(TaskLogBrokerInterface::class) ->disableOriginalConstructor() ->setMethods(['setServiceLocator']) ->getMockForAbstractClass(); $logBrokerMock->expects($this->once()) ->method('setServiceLocator'); $taskLogMock = $this->getMockBuilder(TaskLog::class) ->disableOriginalConstructor() ->setMethods(['getOption', 'getServiceLocator']) ->getMock(); $taskLogMock->expects($this->once()) ->method('getOption') ->willReturn($logBrokerMock); $serviceMangerMock = $this->createMock(ServiceLocatorInterface::class); $taskLogMock->expects($this->once()) ->method('getServiceLocator') ->willReturn($serviceMangerMock); $brokerCaller = function () { return $this->getBroker(); }; $bound = $brokerCaller->bindTo($taskLogMock, $taskLogMock); $this->assertInstanceOf(TaskLogBrokerInterface::class, $bound()); } public function testAddWhenWrongStatusIsSuppliedThenErrorMessageShouldBeLogged() { $taskMock = $this->getMockForAbstractClass(AbstractTask::class, [], "", false); $taskLogMock = $this->getMockBuilder(TaskLog::class) ->disableOriginalConstructor() ->setMethods(['logError']) ->getMock(); $taskLogMock->expects($this->atLeastOnce()) ->method('logError'); $taskLogMock->add($taskMock, 'fake_status'); } public function testAddWhenStatusIsOkayThenTaskShouldBeAddedByBroker() { $taskMock = $this->getMockForAbstractClass(AbstractTask::class, [], "", false); $logBrokerMock = $this->getMockForAbstractClass(TaskLogBrokerInterface::class); $logBrokerMock->expects($this->once()) ->method('add'); $taskLogMock = $this->getMockBuilder(TaskLog::class) ->disableOriginalConstructor() ->setMethods(['getBroker']) ->getMock(); $taskLogMock->expects($this->once()) ->method('getBroker') ->willReturn($logBrokerMock); ; $taskLogMock->add($taskMock, 'enqueued'); } public function testSetStatusWhenNewAndPrevStatusIsOkayThenStatusShouldBeUpdatedByBroker() { $logBrokerMock = $this->getMockForAbstractClass(TaskLogBrokerInterface::class); $logBrokerMock->expects($this->once()) ->method('updateStatus'); $taskLogMock = $this->getMockBuilder(TaskLog::class) ->disableOriginalConstructor() ->setMethods(['getBroker', 'validateStatus']) ->getMock(); $taskLogMock->expects($this->once()) ->method('getBroker') ->willReturn($logBrokerMock); $taskLogMock->expects($this->exactly(2)) ->method('validateStatus'); $taskLogMock->setStatus('fakeId', 'dequeued', 'running'); } public function testGetStatusWhenTaskExistItReturnsItsStatus() { $expectedStatus = 'dequeued'; $logBrokerMock = $this->getMockForAbstractClass(TaskLogBrokerInterface::class); $logBrokerMock->expects($this->once()) ->method('getStatus') ->willReturn($expectedStatus); $taskLogMock = $this->getMockBuilder(TaskLog::class) ->disableOriginalConstructor() ->setMethods(['getBroker', 'validateStatus']) ->getMock(); $taskLogMock->expects($this->once()) ->method('getBroker') ->willReturn($logBrokerMock); $this->assertEquals($expectedStatus, $taskLogMock->getStatus('existingTaskId')); } public function testFindAvailableByUser() { $model = $this->getTaskLogMock(); $this->assertInstanceOf(TaskLogCollection::class, $model->findAvailableByUser('userId')); } public function testGetByIdAndUser() { $model = $this->getTaskLogMock(); $this->assertInstanceOf(TaskLogEntity::class, $model->getByIdAndUser('taskId', 'userId')); } public function testGetByIdAndUserNotFound() { $this->expectException(common_exception_NotFound::class); $model = $this->getTaskLogMock(true); $this->assertInstanceOf(TaskLogEntity::class, $model->getByIdAndUser('some task id not found', 'userId')); } public function testGetStats() { $model = $this->getTaskLogMock(); $this->assertInstanceOf(TasksLogsStats::class, $model->getStats('userId')); } public function testArchive() { $model = $this->getTaskLogMock(); $this->assertTrue($model->archive($model->getByIdAndUser('taskId', 'userId'))); } public function testArchiveTaskNotFound() { $this->expectException(common_exception_NotFound::class); $model = $this->getTaskLogMock(true); $this->assertTrue($model->archive($model->getByIdAndUser('taskId', 'userId'))); } public function testArchiveNotPossibleIfTaskIsRunning() { $this->expectException(Exception::class); $model = $this->getTaskLogMock(false, false, true); $this->assertTrue($model->archive($model->getByIdAndUser('taskId', 'userId'))); } public function testCancel() { $model = $this->getTaskLogMock(); $this->assertTrue($model->cancel($model->getByIdAndUser('taskId', 'userId'))); } public function testCancelTaskNotFound() { $this->expectException(common_exception_NotFound::class); $model = $this->getTaskLogMock(true); $this->assertTrue($model->cancel($model->getByIdAndUser('taskId', 'userId'))); } public function testCancelNotPossibleIfTaskIsRunning() { $this->expectException(Exception::class); $model = $this->getTaskLogMock(false, false, true, false); $this->assertTrue($model->cancel($model->getByIdAndUser('taskId', 'userId'))); } /** * @param bool $notFound * @param bool $shouldArchive * @param bool $taskRunning * @return MockObject|TaskLogInterface */ protected function getTaskLogMock($notFound = false, $shouldArchive = true, $taskRunning = false, $shouldCancel = true) { $taskLogMock = $this->getMockBuilder(TaskLog::class)->disableOriginalConstructor()->getMock(); $collectionMock = $this->getMockBuilder(TaskLogCollection::class)->disableOriginalConstructor()->getMock(); $entity = $this->getMockBuilder(TaskLogEntity::class)->disableOriginalConstructor()->getMock(); $statsMock = $this->getMockBuilder(TasksLogsStats::class)->disableOriginalConstructor()->getMock(); $taskLogMock ->method('findAvailableByUser') ->willReturn($collectionMock); $taskLogMock ->method('getStats') ->willReturn($statsMock); if ($taskRunning) { $taskLogMock ->method('getByIdAndUser') ->willThrowException(new Exception()); } else { $taskLogMock ->method('archive') ->willReturn($shouldArchive); $taskLogMock ->method('cancel') ->willReturn($shouldCancel); } if ($notFound) { $taskLogMock ->method('getByIdAndUser') ->willThrowException(new common_exception_NotFound()); } else { $taskLogMock ->method('getByIdAndUser') ->willReturn($entity); } return $taskLogMock; } public function testTaskCategoryWithExactName() { $model = new TaskLog([ 'task_log_broker' => $broker = new RdsTaskLogBroker('fakePersistence', 'fake') ]); $model->linkTaskToCategory('Test\FakeClassName', 'import'); $this->assertSame('import', $model->getCategoryForTask('Test\FakeClassName')); } public function testTaskCategoryWithSubClass() { $model = new TaskLog([ 'task_log_broker' => $broker = new RdsTaskLogBroker('fakePersistence', 'fake') ]); $model->linkTaskToCategory(StubTaskParent::class, 'export'); $this->assertSame('export', $model->getCategoryForTask(StubTaskChild::class)); } public function testTaskCategoryForUnknown() { $model = new TaskLog([ 'task_log_broker' => $broker = new RdsTaskLogBroker('fakePersistence', 'fake') ]); $model->linkTaskToCategory('Fake\Classname', 'export'); $this->assertSame('unknown', $model->getCategoryForTask('ClassName\Which\Not\Added\Ever')); } } class StubTaskChild extends StubTaskParent { } abstract class StubTaskParent { }