Olivier Massot 3 лет назад
Родитель
Сommit
46004caeb4

+ 6 - 8
tests/Service/MailHubTest.php

@@ -61,10 +61,9 @@ class MailHubTest extends TestCase
             $this->accessUtils
         );
 
-        try {
-            $mailerHub->sendAutomaticEmailTo($access, 'subject', 'a_template', []);
-            throw new AssertionError('A RuntimeException should have been thrown but has not');
-        } catch (\RuntimeException $e) {}
+        $this->expectException(\RuntimeException::class);
+
+        $mailerHub->sendAutomaticEmailTo($access, 'subject', 'a_template', []);
     }
 
     public function testSendAutomaticEmailToAdmin() {
@@ -95,9 +94,8 @@ class MailHubTest extends TestCase
             $this->accessUtils
         );
 
-        try {
-            $mailerHub->sendAutomaticEmailToAdmin($organization, 'subject', 'template', []);
-            throw new AssertionError('A RuntimeException should have been thrown but has not');
-        } catch (\RuntimeException $e) {}
+        $this->expectException(\RuntimeException::class);
+
+        $mailerHub->sendAutomaticEmailToAdmin($organization, 'subject', 'template', []);
     }
 }

+ 3 - 4
tests/Service/OnChange/Organization/OnParametersChangeTest.php

@@ -69,10 +69,9 @@ class OnParametersChangeTest extends TestCase
         $parameters->expects(self::once())->method('getDesactivateOpentalentSiteWeb')->willReturn(true);
         $this->networkUtils->expects(self::once())->method('isCMFAndActiveNow')->willReturn(true);
 
-        try {
-            $this->onParametersChange->validate($parameters, $context);
-            throw new AssertionError('OnParametersChange::validate should have thrown an error');
-        } catch (\RuntimeException) {}
+        $this->expectException(\RuntimeException::class);
+
+        $this->onParametersChange->validate($parameters, $context);
     }
 
     public function testBeforeChange(): void

+ 43 - 33
tests/Service/OnChange/Organization/OnSubdomainChangeTest.php

@@ -6,10 +6,13 @@ use App\Entity\Access\Access;
 use App\Entity\Organization\Organization;
 use App\Entity\Organization\Subdomain;
 use App\Message\Command\Typo3\Typo3UpdateCommand;
+use App\Service\Access\Utils as AccessUtils;
 use App\Service\MailHub;
 use App\Service\OnChange\OnChangeContext;
 use App\Service\OnChange\Organization\OnSubdomainChange;
+use App\Service\Organization\Utils as OrganizationUtils;
 use App\Service\Typo3\BindFileService;
+use Doctrine\Common\Collections\ArrayCollection;
 use Doctrine\ORM\EntityManagerInterface;
 use PHPUnit\Framework\TestCase;
 use Symfony\Component\Messenger\Envelope;
@@ -17,30 +20,27 @@ use Symfony\Component\Messenger\MessageBusInterface;
 
 class OnSubdomainChangeTest extends TestCase
 {
-    private \App\Service\Organization\Utils $organizationUtils;
-    private \App\Service\Access\Utils $accessUtils;
+    private OrganizationUtils $organizationUtils;
+    private AccessUtils $accessUtils;
     private MailHub $mailHub;
     private BindFileService $bindFileService;
     private MessageBusInterface $messageBus;
-    private EntityManagerInterface $entityManager;
     private OnSubdomainChange $onSubdomainChange;
 
     public function setUp():void
     {
-        $this->organizationUtils = $this->getMockBuilder(\App\Service\Organization\Utils::class)->disableOriginalConstructor()->getMock();
-        $this->accessUtils = $this->getMockBuilder(\App\Service\Access\Utils::class)->disableOriginalConstructor()->getMock();
+        $this->organizationUtils = $this->getMockBuilder(OrganizationUtils::class)->disableOriginalConstructor()->getMock();
+        $this->accessUtils = $this->getMockBuilder(AccessUtils::class)->disableOriginalConstructor()->getMock();
         $this->mailHub = $this->getMockBuilder(MailHub::class)->disableOriginalConstructor()->getMock();
         $this->bindFileService = $this->getMockBuilder(BindFileService::class)->disableOriginalConstructor()->getMock();
         $this->messageBus = $this->getMockBuilder(MessageBusInterface::class)->disableOriginalConstructor()->getMock();
-        $this->entityManager = $this->getMockBuilder(EntityManagerInterface::class)->disableOriginalConstructor()->getMock();
 
         $this->onSubdomainChange = new OnSubdomainChange(
             $this->organizationUtils,
             $this->accessUtils,
             $this->mailHub,
             $this->bindFileService,
-            $this->messageBus,
-            $this->entityManager
+            $this->messageBus
         );
     }
 
@@ -50,7 +50,7 @@ class OnSubdomainChangeTest extends TestCase
         $context->method('isPostRequest')->willReturn(true);
 
         $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
-        $organization->expects(self::once())->method('getSubdomains')->willReturn(new \Doctrine\Common\Collections\ArrayCollection([1,2]));
+        $organization->expects(self::once())->method('getSubdomains')->willReturn(new ArrayCollection([1,2]));
 
         $subdomain = $this->getMockBuilder(Subdomain::class)->disableOriginalConstructor()->getMock();
         $subdomain->expects(self::once())->method('getOrganization')->willReturn($organization);
@@ -75,15 +75,40 @@ class OnSubdomainChangeTest extends TestCase
         $context->method('isPostRequest')->willReturn(true);
 
         $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
-        $organization->expects(self::once())->method('getSubdomains')->willReturn(new \Doctrine\Common\Collections\ArrayCollection([1,2,3]));
+        $organization->expects(self::once())->method('getSubdomains')->willReturn(new ArrayCollection([1,2,3]));
 
         $subdomain = $this->getMockBuilder(Subdomain::class)->disableOriginalConstructor()->getMock();
         $subdomain->expects(self::once())->method('getOrganization')->willReturn($organization);
 
-        try {
-            $this->onSubdomainChange->validate($subdomain, $context);
-            throw new \AssertionError('A validation error should have been thrown');
-        } catch (\RuntimeException) {}
+        $this->expectException(\RuntimeException::class);
+        $this->onSubdomainChange->validate($subdomain, $context);
+    }
+
+    public function testBeforeChangeActivated() {
+        // Le sous-domaine qu'on vient d'activer
+        $subdomain = $this->getMockBuilder(Subdomain::class)->disableOriginalConstructor()->getMock();
+        $subdomain->method('isActive')->willReturn(true);
+
+        // Son état précédent
+        $previousData = $this->getMockBuilder(Subdomain::class)->disableOriginalConstructor()->getMock();
+        $previousData->method('isActive')->willReturn(false);
+
+        // Le sous domaine qui était actif jusqu'ici, et que le OnChange devrait désactiver
+        $otherSubdomain = $this->getMockBuilder(Subdomain::class)->disableOriginalConstructor()->getMock();
+        $otherSubdomain->method('isActive')->willReturn(true);
+        $otherSubdomain->expects(self::once())->method('setActive')->with(false);
+
+        $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
+        $organization->expects(self::once())->method('getSubdomains')->willReturn(new ArrayCollection([$subdomain, $otherSubdomain]));
+
+        $subdomain->method('getOrganization')->willReturn($organization);
+
+        $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
+        $context->method('previousData')->willReturn($previousData);
+        $context->method('isPutRequest')->willReturn(true);
+        $context->method('isPostRequest')->willReturn(false);
+
+        $this->onSubdomainChange->beforeChange($subdomain, $context);
     }
 
     public function testOnChangeNoChange(): void
@@ -95,7 +120,6 @@ class OnSubdomainChangeTest extends TestCase
             ->getMock();
 
         $this->bindFileService->expects(self::never())->method('registerSubdomain');
-        $this->entityManager->expects(self::never())->method('flush');
         $this->messageBus->expects(self::never())->method('dispatch');
         $onChange->expects(self::never())->method('sendEmailAfterSubdomainChange');
 
@@ -107,7 +131,6 @@ class OnSubdomainChangeTest extends TestCase
 
     public function testOnChangeActivated(): void {
         $this->bindFileService->expects(self::never())->method('registerSubdomain');
-        $this->entityManager->expects(self::once())->method('flush');
         $this->messageBus
             ->expects(self::once())
             ->method('dispatch')
@@ -118,7 +141,7 @@ class OnSubdomainChangeTest extends TestCase
             ->getMockBuilder(OnSubdomainChange::class)
             ->onlyMethods(['sendEmailAfterSubdomainChange'])
             ->setConstructorArgs(
-                [$this->organizationUtils, $this->accessUtils, $this->mailHub, $this->bindFileService, $this->messageBus, $this->entityManager]
+                [$this->organizationUtils, $this->accessUtils, $this->mailHub, $this->bindFileService, $this->messageBus]
             )
             ->getMock();
         $onChange->expects(self::once())->method('sendEmailAfterSubdomainChange');
@@ -131,16 +154,10 @@ class OnSubdomainChangeTest extends TestCase
         $previousData = $this->getMockBuilder(Subdomain::class)->disableOriginalConstructor()->getMock();
         $previousData->method('isActive')->willReturn(false);
 
-        // Le sous domaine qui était actif jusqu'ici, et que le OnChange devrait désactiver
-        $otherSubdomain = $this->getMockBuilder(Subdomain::class)->disableOriginalConstructor()->getMock();
-        $otherSubdomain->method('isActive')->willReturn(true);
-        $otherSubdomain->expects(self::once())->method('setActive')->with(false);
-
         $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
         $organization->method('getId')->willReturn(1);
-        $organization->expects(self::once())->method('getSubdomains')->willReturn(new \Doctrine\Common\Collections\ArrayCollection([$subdomain, $otherSubdomain]));
 
-        $subdomain->expects(self::exactly(2))->method('getOrganization')->willReturn($organization);
+        $subdomain->method('getOrganization')->willReturn($organization);
 
         $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
         $context->method('previousData')->willReturn($previousData);
@@ -152,7 +169,6 @@ class OnSubdomainChangeTest extends TestCase
 
     public function testOnChangeCreated(): void {
         $this->bindFileService->expects(self::once())->method('registerSubdomain');
-        $this->entityManager->expects(self::once())->method('flush');
         $this->messageBus
             ->expects(self::once())
             ->method('dispatch')
@@ -163,7 +179,7 @@ class OnSubdomainChangeTest extends TestCase
             ->getMockBuilder(OnSubdomainChange::class)
             ->onlyMethods(['sendEmailAfterSubdomainChange'])
             ->setConstructorArgs(
-                [$this->organizationUtils, $this->accessUtils, $this->mailHub, $this->bindFileService, $this->messageBus, $this->entityManager]
+                [$this->organizationUtils, $this->accessUtils, $this->mailHub, $this->bindFileService, $this->messageBus]
             )
             ->getMock();
         $onChange->expects(self::once())->method('sendEmailAfterSubdomainChange');
@@ -172,16 +188,10 @@ class OnSubdomainChangeTest extends TestCase
         $subdomain = $this->getMockBuilder(Subdomain::class)->disableOriginalConstructor()->getMock();
         $subdomain->method('isActive')->willReturn(true);
 
-        // Le sous domaine qui était actif jusqu'ici, et que le OnChange devrait désactiver
-        $otherSubdomain = $this->getMockBuilder(Subdomain::class)->disableOriginalConstructor()->getMock();
-        $otherSubdomain->method('isActive')->willReturn(true);
-        $otherSubdomain->expects(self::once())->method('setActive')->with(false);
-
         $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
         $organization->method('getId')->willReturn(1);
-        $organization->expects(self::once())->method('getSubdomains')->willReturn(new \Doctrine\Common\Collections\ArrayCollection([$subdomain, $otherSubdomain]));
 
-        $subdomain->expects(self::exactly(2))->method('getOrganization')->willReturn($organization);
+        $subdomain->method('getOrganization')->willReturn($organization);
 
         $context = $this->getMockBuilder(OnChangeContext::class)->disableOriginalConstructor()->getMock();
         $context->method('previousData')->willReturn(null);

+ 5 - 12
tests/Service/Rest/ApiRequestServiceTest.php

@@ -4,6 +4,7 @@ namespace App\Tests\Service;
 
 use App\Service\Rest\ApiRequestService;
 use AssertionError;
+use Elastica\Transport\Http;
 use PHPUnit\Framework\TestCase;
 use Symfony\Component\HttpClient\Exception\TransportException;
 use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -63,12 +64,8 @@ class ApiRequestServiceTest extends TestCase
             ->method('get')
             ->willThrowException(new TransportException());
 
-        try {
-            $apiRequestService->getContent('path/to/data');
-            throw new AssertionError('An HttpException should have been thrown, but has not');
-        } catch (HttpException $e) {
-            $this->assertEquals(500, $e->getStatusCode());
-        }
+        $this->expectException(HttpException::class);
+        $apiRequestService->getContent('path/to/data');
     }
 
     public function testGet() {
@@ -161,11 +158,7 @@ class ApiRequestServiceTest extends TestCase
 
         $apiRequestService = new ApiRequestService($this->client);
 
-        try {
-            $apiRequestService->request('GET', 'path/to/data');
-            throw new AssertionError('An HttpException should have been thrown, but has not');
-        } catch (HttpException $e) {
-            $this->assertEquals(500, $e->getStatusCode());
-        }
+        $this->expectException(HttpException::class);
+        $apiRequestService->request('GET', 'path/to/data');
     }
 }