فهرست منبع

fix existing tests

Olivier Massot 1 سال پیش
والد
کامیت
18b981b175

+ 1 - 1
src/Service/Rest/ApiRequestService.php

@@ -76,7 +76,7 @@ class ApiRequestService implements ApiRequestInterface
      * @param array|string $body
      * @return array
      */
-    private function addBodyOption(array $options, array | string $body): array
+    protected function addBodyOption(array $options, array | string $body): array
     {
         $option = is_array($body) ? ['json' => $body] : ['body' => $body];
         return array_merge($options, $option);

+ 45 - 23
tests/Unit/Service/Rest/ApiRequestServiceTest.php

@@ -9,6 +9,15 @@ use Symfony\Component\HttpKernel\Exception\HttpException;
 use Symfony\Contracts\HttpClient\HttpClientInterface;
 use Symfony\Contracts\HttpClient\ResponseInterface;
 
+class TestableApiRequestService extends ApiRequestService {
+    public function addBodyOption(array $options, array|string $body): array
+    {
+        return parent::addBodyOption($options, $body);
+    }
+}
+
+
+
 class ApiRequestServiceTest extends TestCase
 {
     private HttpClientInterface $client;
@@ -19,11 +28,11 @@ class ApiRequestServiceTest extends TestCase
     }
 
     /**
-     * @see ApiRequestService::getJsonContent()
+     * @see TestableApiRequestService::getJsonContent()
      */
     public function testGetJsonContent(): void
     {
-        $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
+        $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
             ->setConstructorArgs([$this->client])
             ->setMethodsExcept(['getJsonContent'])
             ->getMock();
@@ -39,11 +48,11 @@ class ApiRequestServiceTest extends TestCase
     }
 
     /**
-     * @see ApiRequestService::getJsonContent()
+     * @see TestableApiRequestService::getJsonContent()
      */
     public function testGetContent(): void
     {
-        $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
+        $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
             ->setConstructorArgs([$this->client])
             ->setMethodsExcept(['getContent'])
             ->getMock();
@@ -62,11 +71,11 @@ class ApiRequestServiceTest extends TestCase
     }
 
     /**
-     * @see ApiRequestService::getContent()
+     * @see TestableApiRequestService::getContent()
      */
     public function testGetContentWithError(): void
     {
-        $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
+        $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
             ->setConstructorArgs([$this->client])
             ->setMethodsExcept(['getContent'])
             ->getMock();
@@ -80,11 +89,11 @@ class ApiRequestServiceTest extends TestCase
     }
 
     /**
-     * @see ApiRequestService::get()
+     * @see TestableApiRequestService::get()
      */
     public function testGet(): void
     {
-        $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
+        $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
             ->setConstructorArgs([$this->client])
             ->setMethodsExcept(['get'])
             ->getMock();
@@ -102,33 +111,40 @@ class ApiRequestServiceTest extends TestCase
     }
 
     /**
-     * @see ApiRequestService::post()
+     * @see TestableApiRequestService::post()
      */
     public function testPost(): void
     {
-        $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
+        $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
             ->setConstructorArgs([$this->client])
             ->setMethodsExcept(['post'])
             ->getMock();
 
         $response = $this->getMockBuilder(ResponseInterface::class)->disableOriginalConstructor()->getMock();
 
-        $apiRequestService->expects(self::once())
+        $apiRequestService
+            ->expects(self::once())
             ->method('request')
-            ->with('POST', 'path/to/data', [], [])
+            ->with('POST', 'path/to/data', [], ['option' => 2, 'json' => ['foo' => 1]])
             ->willReturn($response);
 
-        $actualResponse = $apiRequestService->post('path/to/data');
+        $apiRequestService
+            ->expects(self::once())
+            ->method('addBodyOption')
+            ->with(['option' => 2, 'json' => 3], ['foo' => 1])
+            ->willReturn(['option' => 2, 'json' => ['foo' => 1]]);
+
+        $actualResponse = $apiRequestService->post('path/to/data', ['foo' => 1], [], ['option' => 2, 'json' => 3]);
 
         $this->assertEquals($response, $actualResponse);
     }
 
     /**
-     * @see ApiRequestService::put()
+     * @see TestableApiRequestService::put()
      */
     public function testPut(): void
     {
-        $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
+        $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
             ->setConstructorArgs([$this->client])
             ->setMethodsExcept(['put'])
             ->getMock();
@@ -137,20 +153,26 @@ class ApiRequestServiceTest extends TestCase
 
         $apiRequestService->expects(self::once())
             ->method('request')
-            ->with('PUT', 'path/to/data', [], [])
+            ->with('PUT', 'path/to/data', [], ['option' => 2, 'body' => 'foo'])
             ->willReturn($response);
 
-        $actualResponse = $apiRequestService->put('path/to/data');
+        $apiRequestService
+            ->expects(self::once())
+            ->method('addBodyOption')
+            ->with([], 'foo')
+            ->willReturn(['option' => 2, 'body' => 'foo']);
+
+        $actualResponse = $apiRequestService->put('path/to/data', 'foo');
 
         $this->assertEquals($response, $actualResponse);
     }
 
     /**
-     * @see ApiRequestService::delete()
+     * @see TestableApiRequestService::delete()
      */
     public function testDelete(): void
     {
-        $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
+        $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
             ->setConstructorArgs([$this->client])
             ->setMethodsExcept(['delete'])
             ->getMock();
@@ -168,11 +190,11 @@ class ApiRequestServiceTest extends TestCase
     }
 
     /**
-     * @see ApiRequestService::request()
+     * @see TestableApiRequestService::request()
      */
     public function testRequest(): void
     {
-        $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
+        $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
             ->setConstructorArgs([$this->client])
             ->setMethodsExcept(['request'])
             ->getMock();
@@ -186,11 +208,11 @@ class ApiRequestServiceTest extends TestCase
     }
 
     /**
-     * @see ApiRequestService::request()
+     * @see TestableApiRequestService::request()
      */
     public function testRequestWithError(): void
     {
-        $apiRequestService = $this->getMockBuilder(ApiRequestService::class)
+        $apiRequestService = $this->getMockBuilder(TestableApiRequestService::class)
             ->setConstructorArgs([$this->client])
             ->setMethodsExcept(['request'])
             ->getMock();

+ 5 - 2
tests/Unit/Service/Security/InternalRequestsServiceTest.php

@@ -5,6 +5,7 @@ namespace App\Tests\Unit\Service\Security;
 use App\Service\Security\InternalRequestsService;
 use PHPUnit\Framework\MockObject\MockObject;
 use PHPUnit\Framework\TestCase;
+use Symfony\Bundle\SecurityBundle\Security;
 
 class TestableInternalRequestsService extends InternalRequestsService
 {
@@ -13,7 +14,7 @@ class TestableInternalRequestsService extends InternalRequestsService
         return parent::isInternalIp($ip);
     }
 
-    public function tokenIsValid(string $token): bool
+    public function tokenIsValid(string|null $token): bool
     {
         return parent::tokenIsValid($token);
     }
@@ -22,15 +23,17 @@ class TestableInternalRequestsService extends InternalRequestsService
 class InternalRequestsServiceTest extends TestCase
 {
     public const internalRequestsToken = 'azerty';
+    private Security $security;
 
     public function setUp(): void
     {
+        $this->security = $this->getMockBuilder(Security::class)->disableOriginalConstructor()->getMock();
     }
 
     private function getInternalRequestsServiceMockFor(string $methodName, ?string $token = null): TestableInternalRequestsService|MockObject
     {
         return $this->getMockBuilder(TestableInternalRequestsService::class)
-            ->setConstructorArgs([$token ?? self::internalRequestsToken])
+            ->setConstructorArgs([$token ?? self::internalRequestsToken, $this->security])
             ->setMethodsExcept([$methodName])
             ->getMock();
     }

+ 4 - 4
tests/Unit/Service/Typo3/SubdomainServiceTest.php

@@ -184,14 +184,11 @@ class SubdomainServiceTest extends TestCase
         $subdomainService->expects(self::once())->method('isReservedSubdomain')->with('sub')->willReturn(false);
         $subdomainService->expects(self::once())->method('isRegistered')->with('sub')->willReturn(false);
 
-        $this->entityManager->expects(self::exactly(2))->method('persist');
+        $this->entityManager->expects(self::once())->method('persist');
         $this->entityManager->expects(self::once())->method('flush');
 
         $this->bindFileService->expects(self::once())->method('registerSubdomain')->with('sub');
 
-        $parameters->expects(self::once())->method('setSubDomain')->with('sub');
-        $parameters->expects(self::once())->method('setOtherWebsite')->with('https://sub.opentalent.fr');
-
         // Subdomain is not activated by default
         $subdomainService->expects(self::never())->method('activateSubdomain');
 
@@ -391,6 +388,9 @@ class SubdomainServiceTest extends TestCase
         $subdomainService->expects(self::never())->method('updateTypo3Website');
         $subdomainService->expects(self::never())->method('sendConfirmationEmail');
 
+//        $parameters->expects(self::once())->method('setSubDomain')->with('sub');
+//        $parameters->expects(self::once())->method('setOtherWebsite')->with('https://sub.opentalent.fr');
+
         $this->expectException(\RuntimeException::class);
         $this->expectExceptionMessage('Can not activate a non-persisted subdomain');