浏览代码

complete unit tests

Olivier Massot 1 年之前
父节点
当前提交
743b4dca7a
共有 1 个文件被更改,包括 139 次插入0 次删除
  1. 139 0
      tests/Unit/Service/Organization/OrganizationFactoryTest.php

+ 139 - 0
tests/Unit/Service/Organization/OrganizationFactoryTest.php

@@ -39,6 +39,7 @@ use App\Service\Typo3\BindFileService;
 use App\Service\Typo3\SubdomainService;
 use App\Service\Typo3\Typo3Service;
 use App\Service\Utils\DatesUtils;
+use Doctrine\Common\Collections\ArrayCollection;
 use Doctrine\ORM\EntityManagerInterface;
 use Elastica\Param;
 use PHPUnit\Framework\MockObject\MockObject;
@@ -1327,7 +1328,145 @@ class OrganizationFactoryTest extends TestCase
             $organizationDeletionRequest,
             $result
         );
+    }
+
+    public function testDeleteWithRollback(): void
+    {
+        $organizationFactory = $this->getOrganizationFactoryMockFor('delete');
+
+        $organizationDeletionRequest = $this->getMockBuilder(OrganizationDeletionRequest::class)->getMock();
+
+        $organizationDeletionRequest->method('getOrganizationId')->willReturn(123);
+
+        $parameters = $this->getMockBuilder(Parameters::class)->getMock();
+        $organization = $this->getMockBuilder(Organization::class)->getMock();
+        $organization->method('getParameters')->willReturn($parameters);
+
+        $this->organizationRepository
+            ->expects(self::once())
+            ->method('find')
+            ->with(123)
+            ->willReturn($organization);
+
+        $this->entityManager->expects(self::once())->method('beginTransaction');
+        $this->entityManager->expects(self::never())->method('flush');
+        $this->entityManager->expects(self::never())->method('commit');
+        $this->entityManager->expects(self::once())->method('rollback');
+
+        $organizationFactory->expects(self::once())->method('deleteOrganizationAccesses')->with($organization);
 
+        $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('deleteLocalDirectories');
+        $organizationFactory->expects(self::never())->method('deleteDirectoriesV1');
+        $organizationFactory->expects(self::never())->method('deleteDirectories59');
+
+        $organizationDeletionRequest
+            ->expects(self::never())
+            ->method('setStatus');
+
+        $this->logger
+            ->expects(self::once())
+            ->method('critical')
+            ->with($this->callback(function($arg) {
+                return is_string($arg) && str_contains($arg, 'An error happened, operation cancelled') && str_contains($arg, 'some error');
+            }));
+
+        $this->expectException(\Exception::class);
+
+        $organizationFactory->delete($organizationDeletionRequest);
     }
 
+    public function testDeleteWithNonBlockingErrors(): void
+    {
+        $organizationFactory = $this->getOrganizationFactoryMockFor('delete');
+
+        $organizationDeletionRequest = $this->getMockBuilder(OrganizationDeletionRequest::class)->getMock();
+
+        $organizationDeletionRequest->method('getOrganizationId')->willReturn(123);
+
+        $parameters = $this->getMockBuilder(Parameters::class)->getMock();
+        $organization = $this->getMockBuilder(Organization::class)->getMock();
+        $organization->method('getParameters')->willReturn($parameters);
+
+        $this->organizationRepository
+            ->expects(self::once())
+            ->method('find')
+            ->with(123)
+            ->willReturn($organization);
+
+        $this->entityManager->expects(self::once())->method('beginTransaction');
+        $this->entityManager->expects(self::once())->method('flush');
+        $this->entityManager->expects(self::once())->method('commit');
+        $this->entityManager->expects(self::never())->method('rollback');
+
+        $organizationFactory->expects(self::once())->method('deleteOrganizationAccesses')->with($organization);
+
+        $this->entityManager->expects(self::exactly(2))->method('remove')->withConsecutive(
+            [$parameters],
+            [$organization]
+        );
+
+        $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('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'));
+
+        $organizationDeletionRequest
+            ->expects(self::once())
+            ->method('setStatus')
+            ->with(OrganizationDeletionRequest::STATUS_OK_WITH_ERRORS);
+
+        $this->logger
+            ->expects(self::exactly(5))
+            ->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 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."],
+        );
+
+        $result = $organizationFactory->delete($organizationDeletionRequest);
+
+        $this->assertEquals(
+            $organizationDeletionRequest,
+            $result
+        );
+    }
+
+    public function testDeleteOrganizationAccesses(): void {
+        $organizationFactory = $this->getOrganizationFactoryMockFor('deleteOrganizationAccesses');
+
+        $organization = $this->getMockBuilder(Organization::class)->getMock();
+
+        $access1 = $this->getMockBuilder(Access::class)->getMock();
+        $access2 = $this->getMockBuilder(Access::class)->getMock();
+        $access_other = $this->getMockBuilder(Access::class)->getMock();
+
+        $person1 = $this->getMockBuilder(Person::class)->getMock();
+        $person1->method('getAccesses')->willReturn(new ArrayCollection([$access1]));
+        $access1->method('getPerson')->willReturn($person1);
+
+        $person2 = $this->getMockBuilder(Person::class)->getMock();
+        $person2->method('getAccesses')->willReturn(new ArrayCollection([$access2, $access_other]));
+        $access2->method('getPerson')->willReturn($person2);
+
+        $organization->method('getAccesses')->willReturn(new ArrayCollection([$access1, $access2]));
+
+        $this->entityManager
+            ->expects(self::exactly(3))
+            ->method('remove')
+            ->withConsecutive(
+                [$person1],
+                [$access1],
+                [$access2],
+            );
+
+        $organizationFactory->deleteOrganizationAccesses($organization);
+    }
 }