From 3e99c6736115d37b1526626920a52f990ed19c83 Mon Sep 17 00:00:00 2001 From: Michael Mas Date: Thu, 13 Jun 2024 12:39:46 +0200 Subject: [PATCH 1/7] ECP-84: Fix PHP unit config of plugin and add first test service --- phpunit.xml | 20 +++++ phpunit.xml.dist | 7 +- tests/ LengowAccessTest.php | 0 tests/ClassInstantiableTest.php | 0 tests/LengowAccessTest.php | 140 ++++++++++++++++++++++++++++++++ tests/TestBootstrap.php | 12 +++ 6 files changed, 176 insertions(+), 3 deletions(-) create mode 100755 phpunit.xml mode change 100644 => 100755 phpunit.xml.dist create mode 100644 tests/ LengowAccessTest.php mode change 100644 => 100755 tests/ClassInstantiableTest.php create mode 100644 tests/LengowAccessTest.php create mode 100644 tests/TestBootstrap.php 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/ LengowAccessTest.php b/tests/ LengowAccessTest.php new file mode 100644 index 0000000..e69de29 diff --git a/tests/ClassInstantiableTest.php b/tests/ClassInstantiableTest.php old mode 100644 new mode 100755 diff --git a/tests/LengowAccessTest.php b/tests/LengowAccessTest.php new file mode 100644 index 0000000..fe3c035 --- /dev/null +++ b/tests/LengowAccessTest.php @@ -0,0 +1,140 @@ +entityRepositoryMock = $this->createMock(EntityRepository::class); + $this->lengowConfiguration = $this->createMock(LengowConfiguration::class); + $this->salesChannelRepository = $this->createMock(EntityRepository::class); + $this->lengowAccess = new LengowAccess($this->lengowConfiguration, $this->salesChannelRepository); + } + + public function testCheckSalesChannelWithInvalidId(): void + { + $result = $this->lengowAccess->checkSalesChannel('invalid-uuid'); + $this->assertNull($result); + } + + public function testCheckSalesChannelWithValidId(): void + { + // Define the expected sales channel ID and name + $salesChannelId = '9f4906f1e8b14b7d86d5b7f4ebd3c57d'; + $expectedName = 'Test Channel'; + + $salesChannelEntity = new SalesChannelEntity(); + $salesChannelEntity->setName($expectedName); + $salesChannelEntity->setUniqueIdentifier($salesChannelId); + + // Create an EntityCollection containing the mock SalesChannelEntity + $entityCollection = new EntityCollection([$salesChannelEntity]); + + // Create an EntitySearchResult with the entity name (empty string for entity), total count, entity collection, criteria, and context + $searchResult = new EntitySearchResult( + 'sales_channel', // Empty string for entity name + $entityCollection->count(), // Total count of entities + $entityCollection, // Entity collection + null, // Facets (can be null if not used) + new Criteria(), // Criteria + Context::createDefaultContext() // Default context + ); + + $result = $searchResult->first()->getName(); + + // Assert that the result matches the expected name + $this->assertEquals($expectedName, $result); + } + + public function testCheckWebserviceAccessWithAuthorizedIp(): void + { + $_SERVER['REMOTE_ADDR'] = '10.0.4.150'; + $this->lengowConfiguration->method('get') + ->willReturn(false); + + $result = $this->lengowAccess->checkWebserviceAccess(null, null); + $this->assertTrue($result); + } + + public function testCheckWebserviceAccessWithValidToken(): void + { + $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; + $this->lengowConfiguration->method('get') + ->willReturn(false); + $this->lengowConfiguration->method('getToken') + ->willReturn('valid-token'); + + $result = $this->lengowAccess->checkWebserviceAccess('valid-token', null); + $this->assertTrue($result); + } + + public function testCheckIpWithUnauthorizedIp(): void + { + $result = $this->lengowAccess->checkIp('127.0.0.1'); + $this->assertFalse($result); + } + + public function testCheckIpWithAuthorizedIp(): void + { + $result = $this->lengowAccess->checkIp('10.0.4.150'); + $this->assertTrue($result); + } + + public function testGetAuthorizedIps(): void + { + $this->lengowConfiguration->method('get') + ->willReturnMap([ + [LengowConfiguration::AUTHORIZED_IPS, []], + [LengowConfiguration::AUTHORIZED_IP_ENABLED, true] + ]); + + $result = $this->lengowAccess->getAuthorizedIps(); + $this->assertContains('10.0.4.150', $result); + } + + public function testCheckTokenWithInvalidToken(): void + { + $this->lengowConfiguration->method('getToken') + ->willReturn('valid-token'); + + $result = $this->lengowAccess->checkToken('invalid-token'); + $this->assertFalse($result); + } + + public function testCheckTokenWithValidToken(): void + { + $this->lengowConfiguration->method('getToken') + ->willReturn('valid-token'); + + $result = $this->lengowAccess->checkToken('valid-token'); + $this->assertTrue($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__); From 8d7bf22dea79b4add858c497ad96090bad63aac3 Mon Sep 17 00:00:00 2001 From: Michael Mas Date: Thu, 13 Jun 2024 12:43:47 +0200 Subject: [PATCH 2/7] ECP-84: delete wrong named file wicth blank space --- tests/ LengowAccessTest.php | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 tests/ LengowAccessTest.php diff --git a/tests/ LengowAccessTest.php b/tests/ LengowAccessTest.php deleted file mode 100644 index e69de29..0000000 From 0863ff6bdb949ed23f2a0249dbacb8c8d360aa0a Mon Sep 17 00:00:00 2001 From: Michael Mas Date: Thu, 13 Jun 2024 12:45:36 +0200 Subject: [PATCH 3/7] ECP-84:delete comment --- tests/LengowAccessTest.php | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/tests/LengowAccessTest.php b/tests/LengowAccessTest.php index fe3c035..271de8f 100644 --- a/tests/LengowAccessTest.php +++ b/tests/LengowAccessTest.php @@ -47,7 +47,6 @@ public function testCheckSalesChannelWithInvalidId(): void public function testCheckSalesChannelWithValidId(): void { - // Define the expected sales channel ID and name $salesChannelId = '9f4906f1e8b14b7d86d5b7f4ebd3c57d'; $expectedName = 'Test Channel'; @@ -55,17 +54,15 @@ public function testCheckSalesChannelWithValidId(): void $salesChannelEntity->setName($expectedName); $salesChannelEntity->setUniqueIdentifier($salesChannelId); - // Create an EntityCollection containing the mock SalesChannelEntity $entityCollection = new EntityCollection([$salesChannelEntity]); - // Create an EntitySearchResult with the entity name (empty string for entity), total count, entity collection, criteria, and context $searchResult = new EntitySearchResult( - 'sales_channel', // Empty string for entity name - $entityCollection->count(), // Total count of entities - $entityCollection, // Entity collection - null, // Facets (can be null if not used) - new Criteria(), // Criteria - Context::createDefaultContext() // Default context + 'sales_channel', + $entityCollection->count(), + $entityCollection, + null, + new Criteria(), + Context::createDefaultContext() ); $result = $searchResult->first()->getName(); From f21857f193ee3609763c9a5563361025ebe4e639 Mon Sep 17 00:00:00 2001 From: Michael Mas Date: Thu, 13 Jun 2024 17:00:15 +0200 Subject: [PATCH 4/7] ECP-84: tests for LengowAction and add folder --- tests/{ => Service}/LengowAccessTest.php | 0 tests/Service/LengowActionTest.php | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename tests/{ => Service}/LengowAccessTest.php (100%) create mode 100644 tests/Service/LengowActionTest.php diff --git a/tests/LengowAccessTest.php b/tests/Service/LengowAccessTest.php similarity index 100% rename from tests/LengowAccessTest.php rename to tests/Service/LengowAccessTest.php diff --git a/tests/Service/LengowActionTest.php b/tests/Service/LengowActionTest.php new file mode 100644 index 0000000..e69de29 From e05cdba3eff82901214e5fca8858736e3df77580 Mon Sep 17 00:00:00 2001 From: Michael Mas Date: Mon, 17 Jun 2024 11:18:15 +0200 Subject: [PATCH 5/7] ECP-84: reach to 100% coverage of unit test for LengowAccessTest --- tests/Service/LengowAccessTest.php | 183 +++++++++++++++++------------ 1 file changed, 110 insertions(+), 73 deletions(-) diff --git a/tests/Service/LengowAccessTest.php b/tests/Service/LengowAccessTest.php index 271de8f..3d7ae91 100644 --- a/tests/Service/LengowAccessTest.php +++ b/tests/Service/LengowAccessTest.php @@ -1,105 +1,108 @@ entityRepositoryMock = $this->createMock(EntityRepository::class); $this->lengowConfiguration = $this->createMock(LengowConfiguration::class); - $this->salesChannelRepository = $this->createMock(EntityRepository::class); + $this->salesChannelRepository = new StaticEntityRepository([]); $this->lengowAccess = new LengowAccess($this->lengowConfiguration, $this->salesChannelRepository); } - public function testCheckSalesChannelWithInvalidId(): void + public function testCheckSalesChannelWithInvalidUuid(): void { $result = $this->lengowAccess->checkSalesChannel('invalid-uuid'); $this->assertNull($result); } - public function testCheckSalesChannelWithValidId(): void + public function testCheckSalesChannelWithValidUuidButNoMatch(): void { - $salesChannelId = '9f4906f1e8b14b7d86d5b7f4ebd3c57d'; - $expectedName = 'Test Channel'; + $salesChannelId = Uuid::randomHex(); + $this->salesChannelRepository = new StaticEntityRepository([ + new EntitySearchResult( + 'sales_channel', + 0, + new EntityCollection([]), + null, + new Criteria(), + Context::createDefaultContext() + ) + ]); - $salesChannelEntity = new SalesChannelEntity(); - $salesChannelEntity->setName($expectedName); - $salesChannelEntity->setUniqueIdentifier($salesChannelId); + $this->lengowAccess = new LengowAccess($this->lengowConfiguration, $this->salesChannelRepository); - $entityCollection = new EntityCollection([$salesChannelEntity]); + $result = $this->lengowAccess->checkSalesChannel($salesChannelId); + $this->assertNull($result); + } - $searchResult = new EntitySearchResult( - 'sales_channel', - $entityCollection->count(), - $entityCollection, - null, - new Criteria(), - Context::createDefaultContext() - ); + 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() + ) + ]); - $result = $searchResult->first()->getName(); + $this->lengowAccess = new LengowAccess($this->lengowConfiguration, $this->salesChannelRepository); - // Assert that the result matches the expected name - $this->assertEquals($expectedName, $result); + $result = $this->lengowAccess->checkSalesChannel($salesChannelId); + $this->assertEquals('Test Channel', $result); } - - public function testCheckWebserviceAccessWithAuthorizedIp(): void + public function testCheckWebserviceAccessWithInvalidIpAndToken(): void { - $_SERVER['REMOTE_ADDR'] = '10.0.4.150'; - $this->lengowConfiguration->method('get') - ->willReturn(false); + $_SERVER['REMOTE_ADDR'] = 'invalid-ip'; - $result = $this->lengowAccess->checkWebserviceAccess(null, null); - $this->assertTrue($result); + $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 testCheckWebserviceAccessWithValidToken(): void + public function testCheckWebserviceAccessWithValidIp(): void { - $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; - $this->lengowConfiguration->method('get') - ->willReturn(false); - $this->lengowConfiguration->method('getToken') - ->willReturn('valid-token'); - - $result = $this->lengowAccess->checkWebserviceAccess('valid-token', null); + $_SERVER['REMOTE_ADDR'] = '10.0.4.150'; + $result = $this->lengowAccess->checkWebserviceAccess(); $this->assertTrue($result); } - public function testCheckIpWithUnauthorizedIp(): void + public function testCheckIpWithInvalidIp(): void { - $result = $this->lengowAccess->checkIp('127.0.0.1'); + $result = $this->lengowAccess->checkIp('invalid-ip'); $this->assertFalse($result); } - public function testCheckIpWithAuthorizedIp(): void + public function testCheckIpWithValidIp(): void { $result = $this->lengowAccess->checkIp('10.0.4.150'); $this->assertTrue($result); @@ -107,31 +110,65 @@ public function testCheckIpWithAuthorizedIp(): void public function testGetAuthorizedIps(): void { - $this->lengowConfiguration->method('get') - ->willReturnMap([ - [LengowConfiguration::AUTHORIZED_IPS, []], - [LengowConfiguration::AUTHORIZED_IP_ENABLED, true] - ]); - - $result = $this->lengowAccess->getAuthorizedIps(); - $this->assertContains('10.0.4.150', $result); - } + $customIps = ['185.61.176.142', '46.19.183.204']; - public function testCheckTokenWithInvalidToken(): void - { - $this->lengowConfiguration->method('getToken') - ->willReturn('valid-token'); + $mockConfig = $this->createMock(LengowConfiguration::class); - $result = $this->lengowAccess->checkToken('invalid-token'); - $this->assertFalse($result); + $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 { - $this->lengowConfiguration->method('getToken') - ->willReturn('valid-token'); + $salesChannelId = Uuid::randomHex(); + $tokenFromConfig = 'valid-token'; + $tokenToCheck = 'valid-token'; + + // Créer un mock pour LengowConfiguration + $mockConfig = $this->createMock(LengowConfiguration::class); + $mockConfig->method('getToken') + ->willReturn($tokenFromConfig); - $result = $this->lengowAccess->checkToken('valid-token'); + // Créer une instance de LengowAccess avec le mock de LengowConfiguration + $lengowAccess = new LengowAccess($mockConfig, $this->salesChannelRepository); + + // Appeler la méthode à tester avec un token valide + $result = $lengowAccess->checkToken($tokenToCheck, $salesChannelId); + + // Vérifier que la méthode retourne true lorsque les tokens correspondent $this->assertTrue($result); } + + public function testCheckTokenWithInvalidToken(): void + { + $salesChannelId = Uuid::randomHex(); + $tokenFromConfig = 'valid-token'; + $tokenToCheck = 'invalid-token'; + + // Créer un mock pour LengowConfiguration + $mockConfig = $this->createMock(LengowConfiguration::class); + $mockConfig->method('getToken') + ->willReturn($tokenFromConfig); + + // Créer une instance de LengowAccess avec le mock de LengowConfiguration + $lengowAccess = new LengowAccess($mockConfig, $this->salesChannelRepository); + + // Appeler la méthode à tester avec un token invalide + $result = $lengowAccess->checkToken($tokenToCheck, $salesChannelId); + + // Vérifier que la méthode retourne false lorsque les tokens ne correspondent pas + $this->assertFalse($result); + } + } From 285476b06f8f4630f541f713704bbda165031654 Mon Sep 17 00:00:00 2001 From: Michael Mas Date: Mon, 17 Jun 2024 11:33:11 +0200 Subject: [PATCH 6/7] ECP-84: delete comment --- tests/Service/LengowAccessTest.php | 9 --------- 1 file changed, 9 deletions(-) diff --git a/tests/Service/LengowAccessTest.php b/tests/Service/LengowAccessTest.php index 3d7ae91..0eaa8d0 100644 --- a/tests/Service/LengowAccessTest.php +++ b/tests/Service/LengowAccessTest.php @@ -135,18 +135,14 @@ public function testCheckTokenWithValidToken(): void $tokenFromConfig = 'valid-token'; $tokenToCheck = 'valid-token'; - // Créer un mock pour LengowConfiguration $mockConfig = $this->createMock(LengowConfiguration::class); $mockConfig->method('getToken') ->willReturn($tokenFromConfig); - // Créer une instance de LengowAccess avec le mock de LengowConfiguration $lengowAccess = new LengowAccess($mockConfig, $this->salesChannelRepository); - // Appeler la méthode à tester avec un token valide $result = $lengowAccess->checkToken($tokenToCheck, $salesChannelId); - // Vérifier que la méthode retourne true lorsque les tokens correspondent $this->assertTrue($result); } @@ -156,18 +152,13 @@ public function testCheckTokenWithInvalidToken(): void $tokenFromConfig = 'valid-token'; $tokenToCheck = 'invalid-token'; - // Créer un mock pour LengowConfiguration $mockConfig = $this->createMock(LengowConfiguration::class); $mockConfig->method('getToken') ->willReturn($tokenFromConfig); - // Créer une instance de LengowAccess avec le mock de LengowConfiguration $lengowAccess = new LengowAccess($mockConfig, $this->salesChannelRepository); - - // Appeler la méthode à tester avec un token invalide $result = $lengowAccess->checkToken($tokenToCheck, $salesChannelId); - // Vérifier que la méthode retourne false lorsque les tokens ne correspondent pas $this->assertFalse($result); } From e3bbcdaa86f885fb3c4a4bbd665115688bea5b3d Mon Sep 17 00:00:00 2001 From: Michael Mas Date: Tue, 18 Jun 2024 10:36:00 +0200 Subject: [PATCH 7/7] ECP-84: LengowActionTest full coverage for test --- tests/Service/LengowActionTest.php | 766 +++++++++++++++++++++++++++++ 1 file changed, 766 insertions(+) diff --git a/tests/Service/LengowActionTest.php b/tests/Service/LengowActionTest.php index e69de29..0f862cc 100644 --- a/tests/Service/LengowActionTest.php +++ 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); + } + +}