|
@@ -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
|
|
|
|
|
+ );
|
|
|
|
|
+ }
|
|
|
|
|
+}
|