Sfoglia il codice sorgente

complete unit tests revision

Olivier Massot 3 anni fa
parent
commit
5a0dfc15aa

+ 1 - 1
tests/Service/Dolibarr/DolibarrApiServiceTest.php

@@ -35,7 +35,7 @@ class DolibarrApiServiceTest extends TestCase
             ->with("thirdparties", [ "limit" => "1", "sqlfilters" => "ref_int=" . $organizationId])
             ->willReturn([['id' => 1]]); // dummy non-empty data
 
-        $society = $dolibarrApiService->getSociety(1);
+        $society = $dolibarrApiService->getSociety($organizationId);
 
         $this->assertEquals(['id' => 1], $society);
     }

+ 46 - 38
tests/Service/MailHubTest.php

@@ -1,5 +1,9 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
 
+use App\Entity\Access\Access;
+use App\Entity\Core\ContactPoint;
+use App\Entity\Person\Person;
+use App\Service\Access\Utils as AccessUtils;
 use App\Service\Core\ContactPointUtils;
 use App\Service\MailHub;
 use PHPUnit\Framework\TestCase;
@@ -11,88 +15,92 @@ class MailHubTest extends TestCase
     private MailerInterface $mailer;
     private string $opentalentNoReplyEmailAddress;
     private ContactPointUtils $contactPointUtils;
-    private \App\Service\Access\Utils $accessUtils;
+    private AccessUtils $accessUtils;
 
     public function setUp(): void {
         $this->mailer = $this->getMockBuilder(MailerInterface::class)->disableOriginalConstructor()->getMock();
         $this->opentalentNoReplyEmailAddress = 'noreply@opentalent.fr';
         $this->contactPointUtils = $this->getMockBuilder(ContactPointUtils::class)->disableOriginalConstructor()->getMock();
-        $this->accessUtils = $this->getMockBuilder(\App\Service\Access\Utils::class)->disableOriginalConstructor()->getMock();
+        $this->accessUtils = $this->getMockBuilder(AccessUtils::class)->disableOriginalConstructor()->getMock();
     }
 
     public function testSendAutomaticEmailTo(): void
     {
-        $contactPoint = $this->getMockBuilder(\App\Entity\Core\ContactPoint::class)->disableOriginalConstructor()->getMock();
+        $mailerHub = $this->getMockBuilder(MailHub::class)
+            ->setConstructorArgs([$this->mailer, $this->opentalentNoReplyEmailAddress, $this->contactPointUtils, $this->accessUtils])
+            ->setMethodsExcept(['sendAutomaticEmailTo'])
+            ->getMock();
+
+        $contactPoint = $this->getMockBuilder(ContactPoint::class)->disableOriginalConstructor()->getMock();
         $contactPoint->method('getEmail')->willReturn('mail@domain.net');
 
-        $person = $this->getMockBuilder(\App\Entity\Person\Person::class)->disableOriginalConstructor()->getMock();
+        $person = $this->getMockBuilder(Person::class)->disableOriginalConstructor()->getMock();
         $person->method('getFullName')->willReturn('Don Diego de la Vega');
         $person->method('getUsername')->willReturn('zorro2000');
 
-        $access = $this->getMockBuilder(\App\Entity\Access\Access::class)->disableOriginalConstructor()->getMock();
+        $access = $this->getMockBuilder(Access::class)->disableOriginalConstructor()->getMock();
         $access->method('getPerson')->willReturn($person);
 
         $this->contactPointUtils->expects(self::once())->method('getPersonContactPointPrincipal')->willReturn($contactPoint);
 
-        $this->mailer->expects(self::once())
+        $this->mailer
+            ->expects(self::once())
             ->method('send')
             ->with(self::isInstanceOf(TemplatedEmail::class));
 
-        $mailerHub = new MailHub(
-            $this->mailer,
-            $this->opentalentNoReplyEmailAddress,
-            $this->contactPointUtils,
-            $this->accessUtils
-        );
-
         $mailerHub->sendAutomaticEmailTo($access, 'subject', 'a_template', []);
     }
 
     public function testSendAutomaticEmailToButNoAddress(): void
     {
-        $access = $this->getMockBuilder(\App\Entity\Access\Access::class)->disableOriginalConstructor()->getMock();
+        $mailerHub = $this->getMockBuilder(MailHub::class)
+            ->setConstructorArgs([$this->mailer, $this->opentalentNoReplyEmailAddress, $this->contactPointUtils, $this->accessUtils])
+            ->setMethodsExcept(['sendAutomaticEmailTo'])
+            ->getMock();
 
-        $this->contactPointUtils->expects(self::once())->method('getPersonContactPointPrincipal')->willReturn(null);
+        $access = $this->getMockBuilder(Access::class)->disableOriginalConstructor()->getMock();
 
-        $mailerHub = new MailHub(
-            $this->mailer,
-            $this->opentalentNoReplyEmailAddress,
-            $this->contactPointUtils,
-            $this->accessUtils
-        );
+        $this->contactPointUtils->expects(self::once())->method('getPersonContactPointPrincipal')->willReturn(null);
 
         $this->expectException(\RuntimeException::class);
 
         $mailerHub->sendAutomaticEmailTo($access, 'subject', 'a_template', []);
     }
 
-    public function testSendAutomaticEmailToAdmin() {
+    public function testSendAutomaticEmailToAdmin(): void
+    {
+        $mailerHub = $this->getMockBuilder(MailHub::class)
+            ->setConstructorArgs([$this->mailer, $this->opentalentNoReplyEmailAddress, $this->contactPointUtils, $this->accessUtils])
+            ->setMethodsExcept(['sendAutomaticEmailToAdmin'])
+            ->getMock();
 
         $organization = $this->getMockBuilder(\App\Entity\Organization\Organization::class)->disableOriginalConstructor()->getMock();
-        $admin = $this->getMockBuilder(\App\Entity\Access\Access::class)->disableOriginalConstructor()->getMock();
+        $admin = $this->getMockBuilder(Access::class)->disableOriginalConstructor()->getMock();
 
         $this->accessUtils->expects(self::once())->method('findAdminFor')->with($organization)->willReturn($admin);
 
-        $mailerHub = $this->getMockBuilder(MailHub::class)
-            ->onlyMethods(['sendAutomaticEmailTo'])
-            ->setConstructorArgs([$this->mailer, $this->opentalentNoReplyEmailAddress, $this->contactPointUtils, $this->accessUtils])
-            ->getMock();
-        $mailerHub->expects(self::once())->method('sendAutomaticEmailTo')->with($admin, 'subject', 'template', []);
+        $mailerHub
+            ->expects(self::once())
+            ->method('sendAutomaticEmailTo')
+            ->with($admin, 'subject', 'template', []);
 
         $mailerHub->sendAutomaticEmailToAdmin($organization, 'subject', 'template', []);
     }
 
-    public function testSendAutomaticEmailToAdminButNoAdmin() {
+    public function testSendAutomaticEmailToAdminButNoAdmin(): void
+    {
+        $mailerHub = $this->getMockBuilder(MailHub::class)
+            ->setConstructorArgs([$this->mailer, $this->opentalentNoReplyEmailAddress, $this->contactPointUtils, $this->accessUtils])
+            ->setMethodsExcept(['sendAutomaticEmailToAdmin'])
+            ->getMock();
 
         $organization = $this->getMockBuilder(\App\Entity\Organization\Organization::class)->disableOriginalConstructor()->getMock();
-        $this->accessUtils->expects(self::once())->method('findAdminFor')->with($organization)->willReturn(null);
-
-        $mailerHub = new MailHub(
-            $this->mailer,
-            $this->opentalentNoReplyEmailAddress,
-            $this->contactPointUtils,
-            $this->accessUtils
-        );
+
+        $this->accessUtils
+            ->expects(self::once())
+            ->method('findAdminFor')
+            ->with($organization)
+            ->willReturn(null);
 
         $this->expectException(\RuntimeException::class);
 

+ 6 - 9
tests/Service/Security/SwitchUserTest.php

@@ -13,15 +13,13 @@ class SwitchUserTest extends TestCase
      */
     public function testIsAllowedToSwitch(): void
     {
-        $switchUser = $this->getMockBuilder(SwitchUser::class)
-            ->setMethodsExcept(['isAllowedToSwitch'])
-            ->getMock();
+        // Cannot mock SwitchUser because of https://github.com/sebastianbergmann/phpunit/issues/3886
+        $switchUser = new SwitchUser();
 
         $children = $this->getMockBuilder(Access::class)->getMock();
+
         $user = $this->getMockBuilder(Access::class)->getMock();
-        $user
-            ->expects($this->once())
-            ->method('getChildren')
+        $user->method('getChildren')
             ->willReturn(new ArrayCollection([$children]));
 
         $this->assertTrue($switchUser->isAllowedToSwitch($user, $children));
@@ -32,9 +30,8 @@ class SwitchUserTest extends TestCase
      */
     public function testIsNotAllowedToSwitch(): void
     {
-        $switchUser = $this->getMockBuilder(SwitchUser::class)
-            ->setMethodsExcept(['isAllowedToSwitch'])
-            ->getMock();
+        // Cannot mock SwitchUser because of https://github.com/sebastianbergmann/phpunit/issues/3886
+        $switchUser = new SwitchUser();
 
         $children = $this->getMockBuilder(Access::class)->getMock();
         $user = $this->getMockBuilder(Access::class)->getMock();

+ 42 - 14
tests/Service/Utils/SiretTest.php

@@ -1,32 +1,60 @@
-<?php
+<?php /** @noinspection PhpUnhandledExceptionInspection */
+
 namespace App\Tests\Service\Utils;
 
 use App\Service\Utils\Siret;
 use PHPUnit\Framework\TestCase;
 use Symfony\Component\HttpClient\MockHttpClient;
 use Symfony\Component\HttpClient\Response\MockResponse;
+use Symfony\Contracts\HttpClient\HttpClientInterface;
+use Symfony\Contracts\HttpClient\ResponseInterface;
 
 class SiretTest extends TestCase
 {
+    private HttpClientInterface $client;
+
+    public function setUp(): void
+    {
+        $this->client = $this->getMockBuilder(HttpClientInterface::class)
+            ->disableOriginalConstructor()
+            ->getMock();
+    }
+
     /**
-     * @see Siret::isSiretIsCorrect()
+     * @see Siret::isSiretCorrect()
      */
-    public function testIsSiretIsCorrect():void
+    public function testIsSiretCorrect():void
     {
-        $responses = [new MockResponse('...', ['http_code' => 200])];
-        $client = new MockHttpClient($responses, 'https://entreprise.data.gouv.fr/api/sirene/v3/etablissements/');
-        $siret = new Siret($client);
-        $this->assertTrue($siret->isSiretIsCorrect('50465312200052'));
+        $siret = $this->getMockBuilder(Siret::class)
+            ->setConstructorArgs([$this->client])
+            ->setMethodsExcept(['isSiretCorrect'])
+            ->getMock();
+
+        $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
+        $response->method('getStatusCode')->willReturn(200);
+
+        $this->client->expects(self::once())->method('request')->willReturn($response);
+
+        $this->assertTrue(
+            $siret->isSiretCorrect('50465312200052')
+        );
     }
 
     /**
-     * @see Siret::isSiretIsCorrect()
+     * @see Siret::isSiretCorrect()
      */
-    public function testIsNotSiretIsCorrect():void
+    public function testIsNotSiretCorrect():void
     {
-        $responses = [new MockResponse('...', ['http_code' => 404])];
-        $client = new MockHttpClient($responses, 'https://entreprise.data.gouv.fr/api/sirene/v3/etablissements/');
-        $siret = new Siret($client);
-        $this->assertFalse($siret->isSiretIsCorrect('50465312200052'));
+        $siret = $this->getMockBuilder(Siret::class)
+            ->setConstructorArgs([$this->client])
+            ->setMethodsExcept(['isSiretCorrect'])
+            ->getMock();
+
+        $response = $this->getMockBuilder(ResponseInterface::class)->getMock();
+        $response->method('getStatusCode')->willReturn(404);
+
+        $this->client->expects(self::once())->method('request')->willReturn($response);
+
+        $this->assertFalse($siret->isSiretCorrect('50465312200052'));
     }
-}
+}