소스 검색

fix and complete unit tests

Olivier Massot 11 달 전
부모
커밋
e572e39d4f

+ 2 - 2
src/Service/Organization/OrganizationFactory.php

@@ -644,7 +644,7 @@ class OrganizationFactory
         }
 
         try {
-            $this->switchSocietyToProspect($organization);
+            $this->switchDolibarrSocietyToProspect($organization);
         } catch (\Exception $e) {
             $this->logger->critical("An error happened while updating the Dolibarr society, please proceed manually.");
             $this->logger->debug($e);
@@ -731,7 +731,7 @@ class OrganizationFactory
 //        $this->typo3Service->deleteSite($organization->getId());
     }
 
-    protected function switchSocietyToProspect(Organization $organization): void
+    protected function switchDolibarrSocietyToProspect(Organization $organization): void
     {
         $this->dolibarrApiService->switchSocietyToProspect($organization->getId());
     }

+ 49 - 0
tests/Unit/Service/Dolibarr/DolibarrApiServiceTest.php

@@ -513,4 +513,53 @@ class DolibarrApiServiceTest extends TestCase
 
         $this->assertEquals(456, $result);
     }
+
+    public function testSwitchSocietyToProspect(): void {
+        $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
+            ->setConstructorArgs([$this->client])
+            ->setMethodsExcept(['switchSocietyToProspect'])
+            ->getMock();
+
+        $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
+        $response->method('getStatusCode')->willReturn(200);
+
+        $dolibarrApiService
+            ->method('getSociety')
+            ->with(123)
+            ->willReturn(['id' => 456]);
+
+        $dolibarrApiService
+            ->expects(self::once())
+            ->method('put')
+            ->with('thirdparties/456', ['client' => 2])
+            ->willReturn($response);
+
+        $dolibarrApiService->switchSocietyToProspect(123);
+    }
+
+    public function testSwitchSocietyToProspectWithError(): void {
+        $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
+            ->setConstructorArgs([$this->client])
+            ->setMethodsExcept(['switchSocietyToProspect'])
+            ->getMock();
+
+        $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
+        $response->method('getStatusCode')->willReturn(500);
+
+        $dolibarrApiService
+            ->method('getSociety')
+            ->with(123)
+            ->willReturn(['id' => 456]);
+
+        $dolibarrApiService
+            ->expects(self::once())
+            ->method('put')
+            ->with('thirdparties/456', ['client' => 2])
+            ->willReturn($response);
+
+        $this->expectException(HttpException::class);
+        $this->expectExceptionMessage('Error while updating the society in Dolibarr');
+
+        $dolibarrApiService->switchSocietyToProspect(123);
+    }
 }

+ 23 - 6
tests/Unit/Service/Organization/OrganizationFactoryTest.php

@@ -127,9 +127,9 @@ class TestableOrganizationFactory extends OrganizationFactory {
         parent::deleteTypo3Website($organization);
     }
 
-    public function deleteDolibarrSociety(Organization $organization): void
+    public function switchDolibarrSocietyToProspect(Organization $organization): void
     {
-        parent::deleteDolibarrSociety($organization);
+        parent::switchDolibarrSocietyToProspect($organization);
     }
 
     public function deleteLocalDirectories(Organization $organization): void
@@ -1291,6 +1291,7 @@ class OrganizationFactoryTest extends TestCase
 
         $parameters = $this->getMockBuilder(Parameters::class)->getMock();
         $organization = $this->getMockBuilder(Organization::class)->getMock();
+        $organization->method('getId')->willReturn(123);
         $organization->method('getParameters')->willReturn($parameters);
 
         $this->organizationRepository
@@ -1312,7 +1313,7 @@ class OrganizationFactoryTest extends TestCase
         );
 
         $organizationFactory->expects(self::once())->method('deleteTypo3Website')->with($organization);
-        $organizationFactory->expects(self::once())->method('deleteDolibarrSociety')->with($organization);
+        $organizationFactory->expects(self::once())->method('switchDolibarrSocietyToProspect')->with($organization);
         $organizationFactory->expects(self::once())->method('deleteLocalDirectories')->with($organization);
         $organizationFactory->expects(self::once())->method('deleteDirectoriesV1')->with($organization);
         $organizationFactory->expects(self::once())->method('deleteDirectories59')->with($organization);
@@ -1358,7 +1359,7 @@ class OrganizationFactoryTest extends TestCase
         $this->entityManager->method('remove')->willThrowException(new \Exception('some error'));
 
         $organizationFactory->expects(self::never())->method('deleteTypo3Website');
-        $organizationFactory->expects(self::never())->method('deleteDolibarrSociety');
+        $organizationFactory->expects(self::never())->method('switchDolibarrSocietyToProspect');
         $organizationFactory->expects(self::never())->method('deleteLocalDirectories');
         $organizationFactory->expects(self::never())->method('deleteDirectoriesV1');
         $organizationFactory->expects(self::never())->method('deleteDirectories59');
@@ -1389,6 +1390,7 @@ class OrganizationFactoryTest extends TestCase
 
         $parameters = $this->getMockBuilder(Parameters::class)->getMock();
         $organization = $this->getMockBuilder(Organization::class)->getMock();
+        $organization->method('getId')->willReturn(123);
         $organization->method('getParameters')->willReturn($parameters);
 
         $this->organizationRepository
@@ -1410,7 +1412,7 @@ class OrganizationFactoryTest extends TestCase
         );
 
         $organizationFactory->expects(self::once())->method('deleteTypo3Website')->willThrowException(new \Exception('some error'));
-        $organizationFactory->expects(self::once())->method('deleteDolibarrSociety')->willThrowException(new \Exception('some error'));
+        $organizationFactory->expects(self::once())->method('switchDolibarrSocietyToProspect')->willThrowException(new \Exception('some error'));
         $organizationFactory->expects(self::once())->method('deleteLocalDirectories')->willThrowException(new \Exception('some error'));
         $organizationFactory->expects(self::once())->method('deleteDirectoriesV1')->willThrowException(new \Exception('some error'));
         $organizationFactory->expects(self::once())->method('deleteDirectories59')->willThrowException(new \Exception('some error'));
@@ -1425,7 +1427,7 @@ class OrganizationFactoryTest extends TestCase
             ->method('critical')
         ->withConsecutive(
             ["An error happened while deleting the Typo3 website, please proceed manually."],
-            ["An error happened while deleting the Dolibarr society, please proceed manually."],
+            ["An error happened while updating the Dolibarr society, please proceed manually."],
             ["An error happened while deleting the local directories, please proceed manually."],
             ["An error happened while deleting the V1 directories, please proceed manually."],
             ["An error happened while deleting the 5.9 directories, please proceed manually."],
@@ -1469,4 +1471,19 @@ class OrganizationFactoryTest extends TestCase
 
         $organizationFactory->deleteOrganizationAccesses($organization);
     }
+
+    public function testSwitchDolibarrSocietyToProspect(): void
+    {
+        $organizationFactory = $this->getOrganizationFactoryMockFor('switchDolibarrSocietyToProspect');
+
+        $organization = $this->getMockBuilder(Organization::class)->getMock();
+        $organization->method('getId')->willReturn(123);
+
+        $this->dolibarrApiService
+            ->expects(self::once())
+            ->method('switchSocietyToProspect')
+            ->with(123);
+
+        $organizationFactory->switchDolibarrSocietyToProspect($organization);
+    }
 }