Bladeren bron

complete unit tests (ongoing)

Olivier Massot 1 jaar geleden
bovenliggende
commit
19533eb338

+ 1 - 1
src/Repository/Person/PersonRepository.php

@@ -17,7 +17,7 @@ use Symfony\Component\Security\Core\User\PasswordUpgraderInterface;
  * @method Person[]    findAll()
  * @method Person[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  */
-final class PersonRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
+class PersonRepository extends ServiceEntityRepository implements PasswordUpgraderInterface
 {
     public function __construct(ManagerRegistry $registry)
     {

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

@@ -90,7 +90,7 @@ class OrganizationFactory
 
             // On vérifie la validité et la disponibilité du sous domaine
             $this->validateSubdomain($organizationCreationRequest->getSubdomain());
-            $this->logger->info("Subdomain is valid and available : " . $organizationCreationRequest->getSubdomain());
+            $this->logger->info("Subdomain is valid and available : '" . $organizationCreationRequest->getSubdomain() . "'");
 
             // On construit l'organisation et ses relations
             $organization = $this->makeOrganizationWithRelations($organizationCreationRequest);
@@ -444,6 +444,7 @@ class OrganizationFactory
      *                                                                                 Person object, or the id of an
      *                                                                                 existing one.
      * @return Access The created Access object.
+     * @throws NumberParseException
      */
     protected function makeAccess(int|OrganizationMemberCreationRequest $organizationMemberCreationRequest): Access
     {

+ 2 - 0
src/Service/Rest/ApiRequestService.php

@@ -72,6 +72,8 @@ class ApiRequestService implements ApiRequestInterface
     }
 
     /**
+     * Complète les options en y ajoutant le body
+     *
      * @param array<mixed> $options
      * @param array<mixed>|string $body
      * @return array<mixed>

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

@@ -2,11 +2,13 @@
 
 namespace App\Tests\Unit\Service\Dolibarr;
 
+use App\Entity\Organization\Organization;
 use App\Service\Dolibarr\DolibarrApiService;
 use PHPUnit\Framework\MockObject\MockObject;
 use PHPUnit\Framework\TestCase;
 use Symfony\Component\HttpKernel\Exception\HttpException;
 use Symfony\Contracts\HttpClient\HttpClientInterface;
+use Symfony\Contracts\HttpClient\ResponseInterface;
 
 class DolibarrApiServiceTest extends TestCase
 {
@@ -439,4 +441,76 @@ class DolibarrApiServiceTest extends TestCase
 
         $dolibarrApiService->getSocietyTagsIds($socId);
     }
+
+    /**
+     * @see DolibarrApiService::createSociety
+     */
+    public function testCreateSociety(): void
+    {
+        $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
+            ->setConstructorArgs([$this->client])
+            ->setMethodsExcept(['createSociety'])
+            ->getMock();
+
+        $organization = $this->getMockBuilder(Organization::class)->getMock();
+        $organization->method('getId')->willReturn(123);
+        $organization->method('getName')->willReturn('Foo');
+
+        $expectedPostBody = [
+            'name' => 'Foo',
+            'client' => 2,
+            'code_client' => -1,
+            'import_key' => 'crm',
+            'array_options' => ['options_2iopen_organization_id' => 123]
+        ];
+
+        $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
+        $response->method('getContent')->willReturn('456');
+
+        $dolibarrApiService
+            ->expects(self::once())
+            ->method('post')
+            ->with("/thirdparties", $expectedPostBody)
+            ->willReturn($response);
+
+        $result = $dolibarrApiService->createSociety($organization);
+
+        $this->assertEquals(456, $result);
+    }
+
+    /**
+     * @see DolibarrApiService::createSociety
+     */
+    public function testCreateSocietyIsClient(): void
+    {
+        $dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)
+            ->setConstructorArgs([$this->client])
+            ->setMethodsExcept(['createSociety'])
+            ->getMock();
+
+        $organization = $this->getMockBuilder(Organization::class)->getMock();
+        $organization->method('getId')->willReturn(123);
+        $organization->method('getName')->willReturn('Foo');
+
+        $expectedPostBody = [
+            'name' => 'Foo',
+            'client' => 1,
+            'code_client' => -1,
+            'import_key' => 'crm',
+            'array_options' => ['options_2iopen_organization_id' => 123]
+        ];
+
+        $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
+        $response->method('getContent')->willReturn('456');
+
+        $dolibarrApiService
+            ->expects(self::once())
+            ->method('post')
+            ->with("/thirdparties", $expectedPostBody)
+            ->willReturn($response);
+
+        $result = $dolibarrApiService->createSociety($organization, true);
+
+        $this->assertEquals(456, $result);
+    }
 }

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

@@ -0,0 +1,211 @@
+<?php
+declare(strict_types=1);
+
+namespace App\Tests\Unit\Service\Organization;
+
+use App\ApiResources\Organization\OrganizationCreationRequest;
+use App\ApiResources\Organization\OrganizationMemberCreationRequest;
+use App\Entity\Access\Access;
+use App\Entity\Core\ContactPoint;
+use App\Entity\Network\NetworkOrganization;
+use App\Entity\Organization\Organization;
+use App\Entity\Organization\OrganizationAddressPostal;
+use App\Entity\Organization\Parameters;
+use App\Entity\Organization\Settings;
+use App\Entity\Organization\Subdomain;
+use App\Entity\Person\PersonAddressPostal;
+use App\Repository\Core\CountryRepository;
+use App\Repository\Organization\OrganizationRepository;
+use App\Repository\Person\PersonRepository;
+use App\Service\Dolibarr\DolibarrApiService;
+use App\Service\Organization\OrganizationFactory;
+use App\Service\Organization\Utils as OrganizationUtils;
+use App\Service\Typo3\BindFileService;
+use App\Service\Typo3\SubdomainService;
+use App\Service\Typo3\Typo3Service;
+use Doctrine\ORM\EntityManagerInterface;
+use PHPUnit\Framework\MockObject\MockObject;
+use PHPUnit\Framework\TestCase;
+use Psr\Log\LoggerInterface;
+
+class TestableOrganizationFactory extends OrganizationFactory {
+    public function isExistingOrganization(OrganizationCreationRequest $organizationCreationRequest): bool
+    {
+        return parent::isExistingOrganization($organizationCreationRequest);
+    }
+
+    public function validateSubdomain(string $subdomainValue): void
+    {
+        parent::validateSubdomain($subdomainValue);
+    }
+
+    public function makeOrganizationWithRelations(
+        OrganizationCreationRequest $organizationCreationRequest
+    ): Organization {
+        return parent::makeOrganizationWithRelations($organizationCreationRequest);
+    }
+
+    public function makeOrganization(OrganizationCreationRequest $organizationCreationRequest): Organization {
+        return parent::makeOrganization($organizationCreationRequest);
+    }
+
+    public function makeParameters(OrganizationCreationRequest $organizationCreationRequest): Parameters {
+        return parent::makeParameters($organizationCreationRequest);
+    }
+
+    public function makeSettings(OrganizationCreationRequest $organizationCreationRequest): Settings {
+        return parent::makeSettings($organizationCreationRequest);
+    }
+
+    public function makePostalAddress(OrganizationCreationRequest $organizationCreationRequest): OrganizationAddressPostal {
+        return parent::makePostalAddress($organizationCreationRequest);
+    }
+
+    public function makeContactPoint(OrganizationCreationRequest $organizationCreationRequest): ContactPoint {
+        return parent::makeContactPoint($organizationCreationRequest);
+    }
+
+    public function makeNetworkOrganization(OrganizationCreationRequest $organizationCreationRequest): NetworkOrganization {
+        return parent::makeNetworkOrganization($organizationCreationRequest);
+    }
+
+    public function makeAdminAccess(OrganizationCreationRequest $organizationCreationRequest): Access {
+        return parent::makeAdminAccess($organizationCreationRequest);
+    }
+
+    public function makeCycles(): array {
+        return parent::makeCycles();
+    }
+
+    public function makeAccess(int|OrganizationMemberCreationRequest $organizationMemberCreationRequest): Access {
+        return parent::makeAccess($organizationMemberCreationRequest);
+    }
+
+    public function makeAccessPostalAddress(OrganizationMemberCreationRequest $organizationMemberCreationRequest): PersonAddressPostal {
+        return parent::makeAccessPostalAddress($organizationMemberCreationRequest);
+    }
+
+    public function makeAccessContactPoint(OrganizationMemberCreationRequest $organizationMemberCreationRequest): ContactPoint {
+        return parent::makeAccessContactPoint($organizationMemberCreationRequest);
+    }
+
+    public function makeSubdomain(OrganizationCreationRequest $organizationCreationRequest): Subdomain {
+        return parent::makeSubdomain($organizationCreationRequest);
+    }
+
+    public function createTypo3Website(Organization $organization): ?int {
+        return parent::createTypo3Website($organization);
+    }
+}
+
+class OrganizationFactoryTest extends TestCase
+{
+    private readonly SubdomainService       $subdomainService;
+    private readonly OrganizationRepository $organizationRepository;
+    private readonly CountryRepository      $countryRepository;
+    private readonly OrganizationUtils      $organizationUtils;
+    private readonly Typo3Service           $typo3Service;
+    private readonly DolibarrApiService     $dolibarrApiService;
+    private readonly EntityManagerInterface $entityManager;
+    private readonly PersonRepository       $personRepository;
+    private readonly BindFileService        $bindFileService;
+    private readonly LoggerInterface        $logger;
+
+    public function setUp(): void
+    {
+        $this->subdomainService = $this->getMockBuilder(SubdomainService::class)->disableOriginalConstructor()->getMock();
+        $this->organizationRepository = $this->getMockBuilder(OrganizationRepository::class)->disableOriginalConstructor()->getMock();
+        $this->countryRepository = $this->getMockBuilder(CountryRepository::class)->disableOriginalConstructor()->getMock();
+        $this->organizationUtils = $this->getMockBuilder(OrganizationUtils::class)->disableOriginalConstructor()->getMock();
+        $this->typo3Service = $this->getMockBuilder(Typo3Service::class)->disableOriginalConstructor()->getMock();
+        $this->dolibarrApiService = $this->getMockBuilder(DolibarrApiService::class)->disableOriginalConstructor()->getMock();
+        $this->entityManager = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
+        $this->personRepository = $this->getMockBuilder(PersonRepository::class)->disableOriginalConstructor()->getMock();
+        $this->bindFileService = $this->getMockBuilder(BindFileService::class)->disableOriginalConstructor()->getMock();
+        $this->logger = $this->getMockBuilder(LoggerInterface::class)->disableOriginalConstructor()->getMock();
+    }
+
+    private function getOrganizationFactoryMockFor(string $methodName): TestableOrganizationFactory | MockObject
+    {
+        $organizationFactory = $this
+            ->getMockBuilder(TestableOrganizationFactory::class)
+            ->setConstructorArgs(
+                [
+                    $this->subdomainService,
+                    $this->organizationRepository,
+                    $this->countryRepository,
+                    $this->organizationUtils,
+                    $this->typo3Service,
+                    $this->dolibarrApiService,
+                    $this->entityManager,
+                    $this->personRepository,
+                    $this->bindFileService
+                ])
+            ->setMethodsExcept(['setLoggerInterface', $methodName])
+            ->getMock();
+
+        $organizationFactory->setLoggerInterface($this->logger);
+
+        return $organizationFactory;
+    }
+
+    public function testCreate(): void {
+        $organizationFactory = $this->getOrganizationFactoryMockFor('create');
+
+        $organizationCreationRequest = $this->getMockBuilder(OrganizationCreationRequest::class)->getMock();
+        $organizationCreationRequest->method('getName')->willReturn('foo');
+        $organizationCreationRequest->method('getSubdomain')->willReturn('subdomain');
+        $organizationCreationRequest->method('isClient')->willReturn(false);
+        $organizationCreationRequest->method('getCreateWebsite')->willReturn(true);
+
+        $this->entityManager->expects(self::once())->method('beginTransaction');
+
+        $organizationFactory->expects(self::once())->method('isExistingOrganization')->willReturn(false);
+        $organizationFactory->expects(self::once())->method('validateSubdomain')->with('subdomain');
+
+        $organization = $this->getMockBuilder(Organization::class)->getMock();
+        $organizationFactory
+            ->expects(self::once())
+            ->method('makeOrganizationWithRelations')
+            ->with($organizationCreationRequest)
+            ->willReturn($organization);
+
+        $this->entityManager->expects(self::once())->method('persist')->with($organization);
+        $this->entityManager->expects(self::once())->method('flush');
+        $this->entityManager->expects(self::once())->method('commit');
+
+        $this->dolibarrApiService
+            ->expects(self::once())
+            ->method('createSociety')
+            ->with($organization, false)
+            ->willReturn(456);
+
+        $this->bindFileService
+            ->expects(self::once())
+            ->method('registerSubdomain')
+            ->with('subdomain');
+
+        $organizationFactory
+            ->expects(self::once())
+            ->method('createTypo3Website')
+            ->with($organization);
+
+        $this->logger
+            ->method('info')
+            ->withConsecutive(
+                ["Start the creation of a new organization named 'foo'"],
+                ["Subdomain is valid and available : 'subdomain'"],
+                ["Organization created with all its relations"],
+                ["New dolibarr structure created (uid : 456)"],
+                ["Organization persisted in the DB"],
+                ["Subdomain registered"],
+            );
+
+        $result = $organizationFactory->create($organizationCreationRequest);
+
+        $this->assertEquals(
+            $organization,
+            $result
+        );
+    }
+}

+ 62 - 0
tests/Unit/Service/Organization/UtilsTest.php

@@ -4,6 +4,7 @@
 
 namespace App\Tests\Unit\Service\Organization;
 
+use App\Entity\Network\NetworkOrganization;
 use App\Entity\Organization\Organization;
 use App\Entity\Organization\Parameters;
 use App\Entity\Organization\Settings;
@@ -653,4 +654,65 @@ class UtilsTest extends TestCase
         $this->assertTrue($organizationUtils->hasModule($organization, 'foo'));
         $this->assertFalse($organizationUtils->hasModule($organization, 'other'));
     }
+
+    /**
+     * @see OrganizationUtils::getActiveNetworkOrganization()
+     */
+    public function testGetActiveNetworkOrganization(): void
+    {
+        $organizationUtils = $this->getOrganizationUtilsMockFor('getActiveNetworkOrganization');
+
+        $networkOrganization1 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
+        $networkOrganization2 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
+        $networkOrganization3 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
+
+        $this
+            ->networkUtils
+            ->expects(self::exactly(2))
+            ->method('isNetworkOrganizationActiveNow')
+            ->willReturnMap([
+                [$networkOrganization1, false],
+                [$networkOrganization2, true],
+                [$networkOrganization3, false],
+            ]);
+
+        $organization = $this->getMockBuilder(Organization::class)->getMock();
+        $organization
+            ->method('getNetworkOrganizations')
+            ->willReturn(new ArrayCollection([$networkOrganization1, $networkOrganization2, $networkOrganization3]));
+
+        $result = $organizationUtils->getActiveNetworkOrganization($organization);
+
+        $this->assertEquals($networkOrganization2, $result);
+    }
+
+    /**
+     * @see OrganizationUtils::getActiveNetworkOrganization()
+     */
+    public function testGetActiveNetworkOrganizationNoActive(): void
+    {
+        $organizationUtils = $this->getOrganizationUtilsMockFor('getActiveNetworkOrganization');
+
+        $networkOrganization1 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
+        $networkOrganization2 = $this->getMockBuilder(NetworkOrganization::class)->getMock();
+
+        $this
+            ->networkUtils
+            ->expects(self::exactly(2))
+            ->method('isNetworkOrganizationActiveNow')
+            ->willReturnMap([
+                [$networkOrganization1, false],
+                [$networkOrganization2, false],
+            ]);
+
+        $organization = $this->getMockBuilder(Organization::class)->getMock();
+        $organization
+            ->method('getNetworkOrganizations')
+            ->willReturn(new ArrayCollection([$networkOrganization1, $networkOrganization2]));
+
+        $result = $organizationUtils->getActiveNetworkOrganization($organization);
+
+        $this->assertEquals(null, $result);
+    }
+
 }

+ 55 - 0
tests/Unit/Service/Rest/ApiRequestServiceTest.php

@@ -110,6 +110,61 @@ class ApiRequestServiceTest extends TestCase
         $this->assertEquals($response, $actualResponse);
     }
 
+    public function testAddBodyOptionBodyIsString()
+    {
+        $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
+            ->setConstructorArgs([$this->client])
+            ->setMethodsExcept(['addBodyOption'])
+            ->getMock();
+
+        $options = ['some_option' => 'some_value'];
+        $body = 'foo';
+
+        $result = $apiRequestService->addBodyOption($options, $body);
+
+        $this->assertEquals(
+            ['some_option' => 'some_value', 'body' => 'foo'],
+            $result
+        );
+    }
+
+    public function testAddBodyOptionBodyIsArray()
+    {
+        $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
+            ->setConstructorArgs([$this->client])
+            ->setMethodsExcept(['addBodyOption'])
+            ->getMock();
+
+        $options = ['some_option' => 'some_value'];
+        $body = ['foo' => 'bar'];
+
+        $result = $apiRequestService->addBodyOption($options, $body);
+
+        $this->assertEquals(
+            ['some_option' => 'some_value', 'json' => ['foo' => 'bar']],
+            $result
+        );
+    }
+
+
+    public function testAddBodyOptionKeyExists()
+    {
+        $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
+            ->setConstructorArgs([$this->client])
+            ->setMethodsExcept(['addBodyOption'])
+            ->getMock();
+
+        $options = ['some_option' => 'some_value', 'body' => 'exists'];
+        $body = 'foo';
+
+        $result = $apiRequestService->addBodyOption($options, $body);
+
+        $this->assertEquals(
+            ['some_option' => 'some_value', 'body' => 'foo'],
+            $result
+        );
+    }
+
     /**
      * @see TestableApiRequestService::post()
      */

+ 39 - 0
tests/Unit/Service/Security/InternalRequestsServiceTest.php

@@ -2,10 +2,12 @@
 
 namespace App\Tests\Unit\Service\Security;
 
+use App\Entity\Access\Access;
 use App\Service\Security\InternalRequestsService;
 use PHPUnit\Framework\MockObject\MockObject;
 use PHPUnit\Framework\TestCase;
 use Symfony\Bundle\SecurityBundle\Security;
+use Symfony\Component\Security\Core\User\UserInterface;
 
 class TestableInternalRequestsService extends InternalRequestsService
 {
@@ -74,6 +76,43 @@ class InternalRequestsServiceTest extends TestCase
         $this->assertFalse($internalRequestsService->tokenIsValid(''));
     }
 
+    public function testIsSuperAdmin(): void
+    {
+        $internalRequestsService = $this->getInternalRequestsServiceMockFor('isSuperAdmin', '');
+
+        $user = $this->getMockBuilder(Access::class)->getMock();
+        $user->method('getSuperAdminAccess')->willReturn(true);
+        $this->security->expects($this->once())->method('getUser')->willReturn($user);
+
+        $this->assertTrue(
+            $internalRequestsService->isSuperAdmin()
+        );
+    }
+
+    public function testIsSuperAdminIsNot(): void
+    {
+        $internalRequestsService = $this->getInternalRequestsServiceMockFor('isSuperAdmin', '');
+
+        $user = $this->getMockBuilder(Access::class)->getMock();
+        $user->method('getSuperAdminAccess')->willReturn(false);
+        $this->security->expects($this->once())->method('getUser')->willReturn($user);
+
+        $this->assertFalse(
+            $internalRequestsService->isSuperAdmin()
+        );
+    }
+
+    public function testIsSuperAdminNoUser(): void
+    {
+        $internalRequestsService = $this->getInternalRequestsServiceMockFor('isSuperAdmin', '');
+
+        $this->security->expects($this->once())->method('getUser')->willReturn(null);
+
+        $this->assertFalse(
+            $internalRequestsService->isSuperAdmin()
+        );
+    }
+
     public function testIsAllowed(): void
     {
         $internalRequestsService = $this->getInternalRequestsServiceMockFor('isAllowed');