diff --git a/phpunit.xml b/phpunit.xml new file mode 100755 index 0000000..fbbc05a --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + + tests + + + + + ./src/ + + + diff --git a/phpunit.xml.dist b/phpunit.xml.dist old mode 100644 new mode 100755 index 5592466..4df1557 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,9 +1,10 @@ + xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" + bootstrap="tests/TestBootstrap.php" + cacheResult="false" + executionOrder="random"> diff --git a/tests/ClassInstantiableTest.php b/tests/ClassInstantiableTest.php old mode 100644 new mode 100755 diff --git a/tests/Service/LengowAccessTest.php b/tests/Service/LengowAccessTest.php new file mode 100644 index 0000000..0eaa8d0 --- /dev/null +++ b/tests/Service/LengowAccessTest.php @@ -0,0 +1,165 @@ +lengowConfiguration = $this->createMock(LengowConfiguration::class); + $this->salesChannelRepository = new StaticEntityRepository([]); + $this->lengowAccess = new LengowAccess($this->lengowConfiguration, $this->salesChannelRepository); + } + + public function testCheckSalesChannelWithInvalidUuid(): void + { + $result = $this->lengowAccess->checkSalesChannel('invalid-uuid'); + $this->assertNull($result); + } + + public function testCheckSalesChannelWithValidUuidButNoMatch(): void + { + $salesChannelId = Uuid::randomHex(); + $this->salesChannelRepository = new StaticEntityRepository([ + new EntitySearchResult( + 'sales_channel', + 0, + new EntityCollection([]), + null, + new Criteria(), + Context::createDefaultContext() + ) + ]); + + $this->lengowAccess = new LengowAccess($this->lengowConfiguration, $this->salesChannelRepository); + + $result = $this->lengowAccess->checkSalesChannel($salesChannelId); + $this->assertNull($result); + } + + public function testCheckSalesChannelWithValidUuidAndMatch(): void + { + $salesChannelId = Uuid::randomHex(); + $salesChannel = $this->createMock(SalesChannelEntity::class); + $salesChannel->method('getName')->willReturn('Test Channel'); + $salesChannel->method('getId')->willReturn($salesChannelId); + + $this->salesChannelRepository = new StaticEntityRepository([ + new EntitySearchResult( + 'sales_channel', + 1, + new EntityCollection([$salesChannel]), + null, + new Criteria(), + Context::createDefaultContext() + ) + ]); + + $this->lengowAccess = new LengowAccess($this->lengowConfiguration, $this->salesChannelRepository); + + $result = $this->lengowAccess->checkSalesChannel($salesChannelId); + $this->assertEquals('Test Channel', $result); + } + public function testCheckWebserviceAccessWithInvalidIpAndToken(): void + { + $_SERVER['REMOTE_ADDR'] = 'invalid-ip'; + + $lengowAccess = $this->getMockBuilder(LengowAccess::class) + ->setConstructorArgs([$this->lengowConfiguration, $this->salesChannelRepository]) + ->onlyMethods(['checkIp']) + ->getMock(); + + $lengowAccess->method('checkIp')->willReturn(false); + + $result = $lengowAccess->checkWebserviceAccess('invalid-token'); + $this->assertFalse($result); + } + + public function testCheckWebserviceAccessWithValidIp(): void + { + $_SERVER['REMOTE_ADDR'] = '10.0.4.150'; + $result = $this->lengowAccess->checkWebserviceAccess(); + $this->assertTrue($result); + } + + public function testCheckIpWithInvalidIp(): void + { + $result = $this->lengowAccess->checkIp('invalid-ip'); + $this->assertFalse($result); + } + + public function testCheckIpWithValidIp(): void + { + $result = $this->lengowAccess->checkIp('10.0.4.150'); + $this->assertTrue($result); + } + + public function testGetAuthorizedIps(): void + { + $customIps = ['185.61.176.142', '46.19.183.204']; + + $mockConfig = $this->createMock(LengowConfiguration::class); + + $mockConfig->method('get') + ->willReturnOnConsecutiveCalls( + $customIps, + true + ); + + $lengowAccess = new LengowAccess($mockConfig, $this->salesChannelRepository); + + $result = $lengowAccess->getAuthorizedIps(); + + foreach ($customIps as $ip) { + $this->assertContains($ip, $result); + } + } + + public function testCheckTokenWithValidToken(): void + { + $salesChannelId = Uuid::randomHex(); + $tokenFromConfig = 'valid-token'; + $tokenToCheck = 'valid-token'; + + $mockConfig = $this->createMock(LengowConfiguration::class); + $mockConfig->method('getToken') + ->willReturn($tokenFromConfig); + + $lengowAccess = new LengowAccess($mockConfig, $this->salesChannelRepository); + + $result = $lengowAccess->checkToken($tokenToCheck, $salesChannelId); + + $this->assertTrue($result); + } + + public function testCheckTokenWithInvalidToken(): void + { + $salesChannelId = Uuid::randomHex(); + $tokenFromConfig = 'valid-token'; + $tokenToCheck = 'invalid-token'; + + $mockConfig = $this->createMock(LengowConfiguration::class); + $mockConfig->method('getToken') + ->willReturn($tokenFromConfig); + + $lengowAccess = new LengowAccess($mockConfig, $this->salesChannelRepository); + $result = $lengowAccess->checkToken($tokenToCheck, $salesChannelId); + + $this->assertFalse($result); + } + +} diff --git a/tests/Service/LengowActionTest.php b/tests/Service/LengowActionTest.php new file mode 100644 index 0000000..0f862cc --- /dev/null +++ b/tests/Service/LengowActionTest.php @@ -0,0 +1,766 @@ +lengowActionRepository = $this->createMock(EntityRepository::class); + $this->lengowLog = $this->createMock(LengowLog::class); + $this->lengowConnector = $this->createMock(LengowConnector::class); + $this->lengowConfiguration = $this->createMock(LengowConfiguration::class); + } + + public function testCreateAction(): void + { + $this->lengowActionRepository->expects($this->once()) + ->method('create') + ->willReturnCallback(function ($data, $context) { + // Simulate successful creation + return $this->createMock(EntityWrittenContainerEvent::class); + }); + + $lengowAction = new LengowAction( + $this->lengowActionRepository, + $this->lengowLog, + $this->lengowConnector, + $this->lengowConfiguration + ); + + $validData = [ + LengowActionDefinition::FIELD_ORDER_ID => Uuid::randomHex(), + LengowActionDefinition::FIELD_ACTION_ID => Uuid::randomHex(), + LengowActionDefinition::FIELD_ACTION_TYPE => LengowAction::TYPE_SHIP, + LengowActionDefinition::FIELD_PARAMETERS => [], + ]; + + $result = $lengowAction->create($validData); + $this->assertTrue($result); + } + + public function testCreateActionMissingRequiredFields(): void + { + $this->lengowActionRepository->expects($this->never()) + ->method('create'); + + $lengowAction = new LengowAction( + $this->lengowActionRepository, + $this->lengowLog, + $this->lengowConnector, + $this->lengowConfiguration + ); + + $invalidData = [ + LengowActionDefinition::FIELD_ORDER_ID => Uuid::randomHex(), + LengowActionDefinition::FIELD_ACTION_ID => Uuid::randomHex(), + LengowActionDefinition::FIELD_PARAMETERS => [], + ]; + + $result = $lengowAction->create($invalidData); + $this->assertFalse($result); + } + + /** + * @expectedException Exception + */ + public function testCreateActionRepositoryException(): void + { + $this->lengowActionRepository->expects($this->once()) + ->method('create') + ->willThrowException(new \Exception('Mocked repository exception')); + + $this->lengowLog->expects($this->once()) + ->method('write'); + + $lengowAction = new LengowAction( + $this->lengowActionRepository, + $this->lengowLog, + $this->lengowConnector, + $this->lengowConfiguration + ); + + $validData = [ + LengowActionDefinition::FIELD_ORDER_ID => Uuid::randomHex(), + LengowActionDefinition::FIELD_ACTION_ID => Uuid::randomHex(), + LengowActionDefinition::FIELD_ACTION_TYPE => LengowAction::TYPE_SHIP, + LengowActionDefinition::FIELD_PARAMETERS => [], + ]; + + $result = $lengowAction->create($validData); + $this->assertFalse($result); + } + + public function testUpdateAction(): void + { + $this->lengowActionRepository->expects($this->once()) + ->method('update') + ->willReturnCallback(function ($data, $context) { + return $this->createMock(EntityWrittenContainerEvent::class); + }); + + $lengowAction = new LengowAction( + $this->lengowActionRepository, + $this->lengowLog, + $this->lengowConnector, + $this->lengowConfiguration + ); + + $actionId = Uuid::randomHex(); + $validData = [ + LengowActionDefinition::FIELD_ACTION_TYPE => LengowAction::TYPE_SHIP, + ]; + + $result = $lengowAction->update($actionId, $validData); + $this->assertTrue($result); + } + + public function testUpdateActionWithUnauthorizedFields(): void + { + $this->lengowActionRepository->expects($this->once()) + ->method('update') + ->willReturnCallback(function ($data, $context) { + return $this->createMock(EntityWrittenContainerEvent::class); + }); + + $lengowAction = new LengowAction( + $this->lengowActionRepository, + $this->lengowLog, + $this->lengowConnector, + $this->lengowConfiguration + ); + + $actionId = Uuid::randomHex(); + $invalidData = [ + LengowActionDefinition::FIELD_ACTION_ID => Uuid::randomHex(), // Assuming this field is not authorized to be updated + ]; + + $result = $lengowAction->update($actionId, $invalidData); + $this->assertTrue($result); + } + + /** + * @expectedException Exception + */ + public function testUpdateActionRepositoryException(): void + { + $this->lengowActionRepository->expects($this->once()) + ->method('update') + ->willThrowException(new \Exception('Mocked repository exception')); + + $this->lengowLog->expects($this->once()) + ->method('write'); + + $lengowAction = new LengowAction( + $this->lengowActionRepository, + $this->lengowLog, + $this->lengowConnector, + $this->lengowConfiguration + ); + + $actionId = Uuid::randomHex(); + $validData = [ + LengowActionDefinition::FIELD_ACTION_TYPE => LengowAction::TYPE_SHIP, + ]; + + $result = $lengowAction->update($actionId, $validData); + $this->assertFalse($result); + } + + public function testGetActionByApiActionId(): void + { + $apiActionId = 123; + + $mockLengowActionEntity = $this->createMock(LengowActionEntity::class); + + $mockLengowActionCollection = $this->createMock(LengowActionCollection::class); + $mockLengowActionCollection->method('first')->willReturn($mockLengowActionEntity); + $mockLengowActionCollection->method('count')->willReturn(1); + + $mockEntitySearchResult = $this->createMock(EntitySearchResult::class); + $mockEntitySearchResult->method('getEntities')->willReturn($mockLengowActionCollection); + + $this->lengowActionRepository->expects($this->once()) + ->method('search') + ->willReturn($mockEntitySearchResult); + + $lengowAction = new LengowAction( + $this->lengowActionRepository, + $this->lengowLog, + $this->lengowConnector, + $this->lengowConfiguration + ); + + $result = $lengowAction->getActionByApiActionId($apiActionId); + $this->assertInstanceOf(LengowActionEntity::class, $result); + } + + public function testGetActiveActions(): void + { + $mockLengowActionEntity = $this->createMock(LengowActionEntity::class); + + $mockLengowActionCollection = $this->createMock(LengowActionCollection::class); + $mockLengowActionCollection->method('first')->willReturn($mockLengowActionEntity); + $mockLengowActionCollection->method('count')->willReturn(1); + + $mockEntitySearchResult = $this->createMock(EntitySearchResult::class); + $mockEntitySearchResult->method('getEntities')->willReturn($mockLengowActionCollection); + + $this->lengowActionRepository->expects($this->once()) + ->method('search') + ->willReturn($mockEntitySearchResult); + + $lengowAction = new LengowAction( + $this->lengowActionRepository, + $this->lengowLog, + $this->lengowConnector, + $this->lengowConfiguration + ); + + $result = $lengowAction->getActiveActions(); + $this->assertInstanceOf(LengowActionCollection::class, $result); + } + + public function testGetActionsByOrderId(): void + { + $orderId = Uuid::randomHex(); + + $mockLengowActionEntity = $this->createMock(LengowActionEntity::class); + + $mockLengowActionCollection = $this->createMock(LengowActionCollection::class); + $mockLengowActionCollection->method('first')->willReturn($mockLengowActionEntity); + $mockLengowActionCollection->method('count')->willReturn(1); + + $mockEntitySearchResult = $this->createMock(EntitySearchResult::class); + $mockEntitySearchResult->method('getEntities')->willReturn($mockLengowActionCollection); + + $this->lengowActionRepository->expects($this->once()) + ->method('search') + ->willReturn($mockEntitySearchResult); + + $lengowAction = new LengowAction( + $this->lengowActionRepository, + $this->lengowLog, + $this->lengowConnector, + $this->lengowConfiguration + ); + + $result = $lengowAction->getActionsByOrderId($orderId); + $this->assertInstanceOf(LengowActionCollection::class, $result); + } + + public function testGetActiveActionsByOrderId(): void + { + $orderId = Uuid::randomHex(); + + $mockLengowActionEntity = $this->createMock(LengowActionEntity::class); + + $mockLengowActionCollection = $this->createMock(LengowActionCollection::class); + $mockLengowActionCollection->method('first')->willReturn($mockLengowActionEntity); + $mockLengowActionCollection->method('count')->willReturn(1); + + $mockEntitySearchResult = $this->createMock(EntitySearchResult::class); + $mockEntitySearchResult->method('getEntities')->willReturn($mockLengowActionCollection); + + $this->lengowActionRepository->expects($this->once()) + ->method('search') + ->willReturn($mockEntitySearchResult); + + $lengowAction = new LengowAction( + $this->lengowActionRepository, + $this->lengowLog, + $this->lengowConnector, + $this->lengowConfiguration + ); + + $result = $lengowAction->getActionsByOrderId($orderId, true); + $this->assertInstanceOf(LengowActionCollection::class, $result); + } + + public function testGetOldActions(): void + { + $intervalTime = 3600; + + $mockLengowActionEntity = $this->createMock(LengowActionEntity::class); + + $mockLengowActionCollection = $this->createMock(LengowActionCollection::class); + $mockLengowActionCollection->method('first')->willReturn($mockLengowActionEntity); + $mockLengowActionCollection->method('count')->willReturn(1); + + $mockEntitySearchResult = $this->createMock(EntitySearchResult::class); + $mockEntitySearchResult->method('getEntities')->willReturn($mockLengowActionCollection); + + $this->lengowActionRepository->expects($this->once()) + ->method('search') + ->willReturn($mockEntitySearchResult); + + $lengowAction = new LengowAction( + $this->lengowActionRepository, + $this->lengowLog, + $this->lengowConnector, + $this->lengowConfiguration + ); + + $result = $lengowAction->getOldActions($intervalTime); + $this->assertInstanceOf(LengowActionCollection::class, $result); + } + + public function testGetLastOrderActionType(): void + { + $orderId = Uuid::randomHex(); + + $mockLengowActionEntity = $this->createMock(LengowActionEntity::class); + $mockLengowActionEntity->method('getActionType')->willReturn('ship'); + + $mockLengowActionCollection = $this->createMock(LengowActionCollection::class); + $mockLengowActionCollection->method('last')->willReturn($mockLengowActionEntity); + $mockLengowActionCollection->method('count')->willReturn(1); + + $mockEntitySearchResult = $this->createMock(EntitySearchResult::class); + $mockEntitySearchResult->method('getEntities')->willReturn($mockLengowActionCollection); + + $this->lengowActionRepository->expects($this->once()) + ->method('search') + ->willReturn($mockEntitySearchResult); + + $lengowAction = new LengowAction( + $this->lengowActionRepository, + $this->lengowLog, + $this->lengowConnector, + $this->lengowConfiguration + ); + + $result = $lengowAction->getLastOrderActionType($orderId); + $this->assertEquals('ship', $result); + } + + public function testGetLastOrderActionTypeReturnsNull(): void + { + $orderId = Uuid::randomHex(); + + $mockLengowActionCollection = $this->createMock(LengowActionCollection::class); + $mockLengowActionCollection->method('count')->willReturn(0); + + $mockEntitySearchResult = $this->createMock(EntitySearchResult::class); + $mockEntitySearchResult->method('getEntities')->willReturn($mockLengowActionCollection); + + $this->lengowActionRepository->expects($this->once()) + ->method('search') + ->willReturn($mockEntitySearchResult); + + $lengowAction = new LengowAction( + $this->lengowActionRepository, + $this->lengowLog, + $this->lengowConnector, + $this->lengowConfiguration + ); + + $result = $lengowAction->getLastOrderActionType($orderId); + $this->assertNull($result); + } + + public function testCanSendAction(): void + { + $params = [ + 'action_type' => 'ship', + 'line' => '123', + ]; + $orderId = Uuid::randomHex(); + + $mockOrderEntity = $this->createMock(OrderEntity::class); + $mockOrderEntity->method('getId')->willReturn($orderId); + + $mockLengowActionEntity = $this->createMock(LengowActionEntity::class); + $mockLengowActionEntity->method('getState')->willReturn(LengowAction::STATE_NEW); + $mockLengowActionEntity->method('getRetry')->willReturn(0); + + $mockLengowActionCollection = $this->createMock(LengowActionCollection::class); + $mockLengowActionCollection->method('first')->willReturn($mockLengowActionEntity); + $mockLengowActionCollection->method('count')->willReturn(1); + + $mockEntitySearchResult = $this->createMock(EntitySearchResult::class); + $mockEntitySearchResult->method('getEntities')->willReturn($mockLengowActionCollection); + + $this->lengowActionRepository->expects($this->once()) + ->method('search') + ->willReturn($mockEntitySearchResult); + + $this->lengowConnector->expects($this->once()) + ->method('queryApi') + ->willReturn((object) ['count' => 1, 'results' => [(object) ['id' => 1]]]); + + $lengowAction = new LengowAction( + $this->lengowActionRepository, + $this->lengowLog, + $this->lengowConnector, + $this->lengowConfiguration + ); + + $result = $lengowAction->canSendAction($params, $mockOrderEntity); + $this->assertFalse($result); + } + + public function testCanSendActionUnsetParams(): void + { + $params = [ + 'action_type' => 'ship', + 'line' => '123', + 'marketplace_order_id' => 'amazon_fr', + LengowAction::ARG_SHIPPING_DATE => '2022-01-01', + LengowAction::ARG_DELIVERY_DATE => '2022-01-02', + ]; + $orderId = Uuid::randomHex(); + + $mockOrderEntity = $this->createMock(OrderEntity::class); + $mockOrderEntity->method('getId')->willReturn($orderId); + + $this->lengowConnector->expects($this->once()) + ->method('queryApi') + ->willReturn((object) ['count' => 0]); + + $lengowAction = new LengowAction( + $this->lengowActionRepository, + $this->lengowLog, + $this->lengowConnector, + $this->lengowConfiguration + ); + + $result = $lengowAction->canSendAction($params, $mockOrderEntity); + $this->assertTrue($result); + } + + public function testCanSendActionThrowsException(): void + { + $this->expectException(LengowException::class); + + $params = [ + 'action_type' => 'ship', + 'line' => '123', + ]; + $orderId = Uuid::randomHex(); + + $mockOrderEntity = $this->createMock(OrderEntity::class); + $mockOrderEntity->method('getId')->willReturn($orderId); + + $this->lengowConnector->expects($this->once()) + ->method('queryApi') + ->willReturn((object) ['error' => (object) ['message' => 'Mocked error message']]); + + $lengowAction = new LengowAction( + $this->lengowActionRepository, + $this->lengowLog, + $this->lengowConnector, + $this->lengowConfiguration + ); + + $lengowAction->canSendAction($params, $mockOrderEntity); + } + + public function testCanSendActionReturnsTrue(): void + { + $params = [ + 'action_type' => 'ship', + 'line' => '123', + ]; + $orderId = Uuid::randomHex(); + + $mockOrderEntity = $this->createMock(OrderEntity::class); + $mockOrderEntity->method('getId')->willReturn($orderId); + + $this->lengowConnector->expects($this->once()) + ->method('queryApi') + ->willReturn((object) ['count' => 0]); + + $lengowAction = new LengowAction( + $this->lengowActionRepository, + $this->lengowLog, + $this->lengowConnector, + $this->lengowConfiguration + ); + + $result = $lengowAction->canSendAction($params, $mockOrderEntity); + $this->assertTrue($result); + } + + public function testCanSendActionCreatesAction(): void + { + $params = [ + 'action_type' => 'ship', + 'line' => '123', + 'marketplace_order_id' => 'amazon_fr', + ]; + $orderId = Uuid::randomHex(); + + $mockOrderEntity = $this->createMock(OrderEntity::class); + $mockOrderEntity->method('getId')->willReturn($orderId); + + $this->lengowConnector->expects($this->once()) + ->method('queryApi') + ->willReturn((object) ['count' => 1, 'results' => [(object) ['id' => 1]]]); + + $this->lengowActionRepository->expects($this->once()) + ->method('search') + ->willReturn($this->createMock(EntitySearchResult::class)); + + $this->lengowActionRepository->expects($this->once()) + ->method('create') + ->willReturn($this->createMock(EntityWrittenContainerEvent::class)); + + $lengowAction = new LengowAction( + $this->lengowActionRepository, + $this->lengowLog, + $this->lengowConnector, + $this->lengowConfiguration + ); + + $result = $lengowAction->canSendAction($params, $mockOrderEntity); + $this->assertFalse($result); + } + + public function testSendAction(): void + { + $params = [ + LengowAction::ARG_ACTION_TYPE => LengowAction::TYPE_SHIP, + LengowAction::ARG_LINE => '123', + LengowAction::ARG_CARRIER => 'DHL', + LengowAction::ARG_CARRIER_NAME => 'DHL Express', + LengowAction::ARG_SHIPPING_METHOD => 'Express', + LengowAction::ARG_TRACKING_NUMBER => '123456789', + LengowAction::ARG_TRACKING_URL => 'http://tracking.example.com', + LengowAction::ARG_SHIPPING_PRICE => '10.00', + LengowAction::ARG_SHIPPING_DATE => '2022-01-01', + LengowAction::ARG_DELIVERY_DATE => '2022-01-02', + LengowImport::ARG_MARKETPLACE_ORDER_ID => 'MO123456', + ]; + + $orderEntity = $this->createMock(OrderEntity::class); + $lengowOrderEntity = $this->createMock(LengowOrderEntity::class); + + $this->lengowConnector->expects($this->once()) + ->method('queryApi') + ->with( + $this->equalTo(LengowConnector::POST), + $this->equalTo(LengowConnector::API_ORDER_ACTION), + $this->equalTo($params) + ) + ->willReturn((object) ['id' => 1]); + + $lengowAction = new LengowAction( + $this->lengowActionRepository, + $this->lengowLog, + $this->lengowConnector, + $this->lengowConfiguration + ); + + $lengowAction->sendAction($params, $orderEntity, $lengowOrderEntity); + } + + public function testSendActionWithoutIdInResult(): void + { + $params = [ + LengowAction::ARG_ACTION_TYPE => LengowAction::TYPE_SHIP, + LengowAction::ARG_LINE => '123', + LengowAction::ARG_CARRIER => 'DHL', + LengowAction::ARG_CARRIER_NAME => 'DHL Express', + LengowAction::ARG_SHIPPING_METHOD => 'Express', + LengowAction::ARG_TRACKING_NUMBER => '123456789', + LengowAction::ARG_TRACKING_URL => 'http://tracking.example.com', + LengowAction::ARG_SHIPPING_PRICE => '10.00', + LengowAction::ARG_SHIPPING_DATE => '2022-01-01', + LengowAction::ARG_DELIVERY_DATE => '2022-01-02', + LengowImport::ARG_MARKETPLACE_ORDER_ID => 'MO123456', + ]; + + $orderEntity = $this->createMock(OrderEntity::class); + $lengowOrderEntity = $this->createMock(LengowOrderEntity::class); + + $this->lengowConnector->expects($this->once()) + ->method('queryApi') + ->with( + $this->equalTo(LengowConnector::POST), + $this->equalTo(LengowConnector::API_ORDER_ACTION), + $this->equalTo($params) + ) + ->willReturn((object) ['result' => 'No id']); + + $this->lengowLog->expects($this->once()) + ->method('encodeMessage') + ->with( + $this->equalTo('lengow_log.exception.action_not_created'), + $this->equalTo(['error_message' => json_encode(['result' => 'No id'])]) + ); + + $lengowAction = new LengowAction( + $this->lengowActionRepository, + $this->lengowLog, + $this->lengowConnector, + $this->lengowConfiguration + ); + + $this->expectException(LengowException::class); + + $lengowAction->sendAction($params, $orderEntity, $lengowOrderEntity); + } + + public function testSendActionWithNullResult(): void + { + $params = [ + LengowAction::ARG_ACTION_TYPE => LengowAction::TYPE_SHIP, + LengowAction::ARG_LINE => '123', + LengowAction::ARG_CARRIER => 'DHL', + LengowAction::ARG_CARRIER_NAME => 'DHL Express', + LengowAction::ARG_SHIPPING_METHOD => 'Express', + LengowAction::ARG_TRACKING_NUMBER => '123456789', + LengowAction::ARG_TRACKING_URL => 'http://tracking.example.com', + LengowAction::ARG_SHIPPING_PRICE => '10.00', + LengowAction::ARG_SHIPPING_DATE => '2022-01-01', + LengowAction::ARG_DELIVERY_DATE => '2022-01-02', + LengowImport::ARG_MARKETPLACE_ORDER_ID => 'MO123456', + ]; + + $orderEntity = $this->createMock(OrderEntity::class); + $lengowOrderEntity = $this->createMock(LengowOrderEntity::class); + + $this->lengowConnector->expects($this->once()) + ->method('queryApi') + ->with( + $this->equalTo(LengowConnector::POST), + $this->equalTo(LengowConnector::API_ORDER_ACTION), + $this->equalTo($params) + ) + ->willReturn(null); + + $this->lengowLog->expects($this->once()) + ->method('encodeMessage') + ->with( + $this->equalTo('lengow_log.exception.action_not_created_api') + ); + + $lengowAction = new LengowAction( + $this->lengowActionRepository, + $this->lengowLog, + $this->lengowConnector, + $this->lengowConfiguration + ); + + $this->expectException(LengowException::class); + + $lengowAction->sendAction($params, $orderEntity, $lengowOrderEntity); + } + + public function testFinishActions(): void + { + $orderId = 'testOrderId'; + $actionType = LengowAction::TYPE_SHIP; + + // Mocking LengowActionEntity + $lengowActionEntity = $this->createMock(LengowActionEntity::class); + $lengowActionEntity->method('getId')->willReturn('testActionId'); + + // Mocking ActionCollection to return the LengowActionEntity + $lengowActionCollection = $this->getMockBuilder(ActionCollection::class) + ->disableOriginalConstructor() + ->onlyMethods(['getElements', 'getIterator']) + ->getMock(); + $lengowActionCollection->method('getElements')->willReturn([$lengowActionEntity]); + $lengowActionCollection->method('getIterator')->willReturn(new ArrayIterator([$lengowActionEntity])); + + // Mocking EntitySearchResult to return the mocked ActionCollection + $entitySearchResult = $this->createMock(EntitySearchResult::class); + $entitySearchResult->method('getEntities')->willReturn($lengowActionCollection); + + // Mocking the repository to return the mocked EntitySearchResult + $this->lengowActionRepository->expects($this->once()) + ->method('search') + ->willReturn($entitySearchResult); + + // Creating a mock of EntityWrittenContainerEvent + $entityWrittenContainerEvent = $this->createMock(EntityWrittenContainerEvent::class); + + // Mocking the update method to return true for success check + $lengowAction = $this->getMockBuilder(LengowAction::class) + ->setConstructorArgs([$this->lengowActionRepository, $this->lengowLog, $this->lengowConnector, $this->lengowConfiguration]) + ->onlyMethods(['update']) + ->getMock(); + $lengowAction->expects($this->once()) + ->method('update') + ->with('testActionId', [LengowActionDefinition::FIELD_STATE => LengowAction::STATE_FINISH]) + ->willReturn(true); + + // Calling finishActions + $result = $lengowAction->finishActions($orderId, $actionType); + + // Asserting that the result is true + $this->assertTrue($result); + } + + public function testFinishActionsFailure(): void + { + $orderId = 'testOrderId'; + $actionType = LengowAction::TYPE_SHIP; + + // Mocking LengowActionEntity + $lengowActionEntity = $this->createMock(LengowActionEntity::class); + $lengowActionEntity->method('getId')->willReturn('testActionId'); + + // Mocking ActionCollection to return the LengowActionEntity + $lengowActionCollection = $this->getMockBuilder(ActionCollection::class) + ->disableOriginalConstructor() + ->onlyMethods(['getElements', 'getIterator']) + ->getMock(); + $lengowActionCollection->method('getElements')->willReturn([$lengowActionEntity]); + $lengowActionCollection->method('getIterator')->willReturn(new ArrayIterator([$lengowActionEntity])); + + // Mocking EntitySearchResult to return the mocked ActionCollection + $entitySearchResult = $this->createMock(EntitySearchResult::class); + $entitySearchResult->method('getEntities')->willReturn($lengowActionCollection); + + // Mocking the repository to return the mocked EntitySearchResult + $this->lengowActionRepository->expects($this->once()) + ->method('search') + ->willReturn($entitySearchResult); + + // Creating a mock of EntityWrittenContainerEvent + $entityWrittenContainerEvent = $this->createMock(EntityWrittenContainerEvent::class); + + // Mocking the update method to return true for success check + $lengowAction = $this->getMockBuilder(LengowAction::class) + ->setConstructorArgs([$this->lengowActionRepository, $this->lengowLog, $this->lengowConnector, $this->lengowConfiguration]) + ->onlyMethods(['update']) + ->getMock(); + $lengowAction->expects($this->once()) + ->method('update') + ->with('testActionId', [LengowActionDefinition::FIELD_STATE => LengowAction::STATE_FINISH]) + ->willReturn(false); + + // Calling finishActions + $result = $lengowAction->finishActions($orderId, $actionType); + + // Asserting that the result is true + $this->assertFalse($result); + } + +} diff --git a/tests/TestBootstrap.php b/tests/TestBootstrap.php new file mode 100644 index 0000000..83a1633 --- /dev/null +++ b/tests/TestBootstrap.php @@ -0,0 +1,12 @@ +addCallingPlugin() + ->addActivePlugins('LengowConnector') + ->setForceInstallPlugins(true) + ->bootstrap() + ->getClassLoader(); + +$loader->addPsr4('LengowConnector\\tests\\', __DIR__);