浏览代码

fix subdomain service and complete unit tests

Olivier Massot 2 年之前
父节点
当前提交
f8a15a31e0

+ 2 - 0
src/Enum/Utils/EnvironnementVarEnum.php

@@ -6,6 +6,8 @@ namespace App\Enum\Utils;
 use MyCLabs\Enum\Enum;
 
 /**
+ * Liste des variables d'environnement qu'il est possible de requêter via le service Utils/Environnement
+ *
  * @method static APP_ENV()
  */
 class EnvironnementVarEnum extends Enum

+ 2 - 5
src/Service/Typo3/SubdomainService.php

@@ -72,12 +72,9 @@ class SubdomainService
      * @throws \Exception
      */
     public function isReservedSubdomain(string $subdomainValue): bool {
-//        $reservedSubdomains = $this->configUtils->get('subdomains')['reserved'];
-        dd($this->parameterBag->get('modules'));
-
-        $reservedSubdomains = $this->parameterBag->get('opentalent')['reserved_subdomains'];
+        $reservedSubdomains = $this->parameterBag->get('opentalent.subdomains')['reserved'];
         $subRegexes = array_map(
-            function (string $s) { return '(' . trim($s, '^$/\s') . ')'; },
+            function (string $s) { return '(\b' . trim($s, '^$/\s') . '\b)'; },
             $reservedSubdomains
         );
 

+ 3 - 3
src/Service/Utils/Environnement.php

@@ -11,10 +11,10 @@ use RuntimeException;
  */
 class Environnement
 {
-    public function get(string $name): string{
-        if(in_array($name, EnvironnementVarEnum::toArray(), true)){
+    public function get(string $name): string {
+        if(in_array($name, EnvironnementVarEnum::toArray(), true)) {
             return $_ENV[$name];
         }
         throw new RuntimeException(sprintf('Undefined environment variable : %s', $name), 500);
     }
-}
+}

+ 48 - 0
tests/Unit/Service/Typo3/SubdomainServiceTest.php

@@ -125,6 +125,27 @@ class SubdomainServiceTest extends TestCase
         $this->assertFalse($subdomainService->isValidSubdomain(str_repeat('abcdef', 20)));
     }
 
+    public function testIsReservedSubdomain(): void {
+        $subdomainService = $this->makeSubdomainServiceMockFor('isReservedSubdomain');
+
+        $this->parameterBag
+            ->method('get')
+            ->with('opentalent.subdomains')
+            ->willReturn([
+                'reserved' => [
+                    'abcd',
+                    'abc\d+'
+                ]
+            ]);
+
+        $this->assertTrue($subdomainService->isReservedSubdomain('abcd'));
+        $this->assertTrue($subdomainService->isReservedSubdomain('abc123'));
+
+        $this->assertFalse($subdomainService->isReservedSubdomain('abcde'));
+        $this->assertFalse($subdomainService->isReservedSubdomain('abc'));
+        $this->assertFalse($subdomainService->isReservedSubdomain('foo'));
+    }
+
     public function testAddNewSubdomain(): void {
         $subdomainService = $this->makeSubdomainServiceMockFor('addNewSubdomain');
 
@@ -132,6 +153,7 @@ class SubdomainServiceTest extends TestCase
 
         $subdomainService->expects(self::once())->method('isValidSubdomain')->with('sub')->willReturn(True);
         $subdomainService->expects(self::once())->method('canRegisterNewSubdomain')->with($organization)->willReturn(True);
+        $subdomainService->expects(self::once())->method('isReservedSubdomain')->with('sub')->willReturn(false);
         $this->subdomainRepository->expects(self::once())->method('findBy')->with(['subdomain' => 'sub'])->willReturn(0);
 
         $this->entityManager->expects(self::once())->method('persist');
@@ -157,6 +179,7 @@ class SubdomainServiceTest extends TestCase
 
         $subdomainService->expects(self::once())->method('isValidSubdomain')->with('_sub')->willReturn(False);
         $subdomainService->expects(self::never())->method('canRegisterNewSubdomain')->with($organization)->willReturn(True);
+        $subdomainService->expects(self::never())->method('isReservedSubdomain')->with($organization)->willReturn(false);
         $this->subdomainRepository->expects(self::never())->method('findBy')->with(['subdomain' => '_sub'])->willReturn(0);
 
         $this->entityManager->expects(self::never())->method('persist');
@@ -178,6 +201,7 @@ class SubdomainServiceTest extends TestCase
 
         $subdomainService->expects(self::once())->method('isValidSubdomain')->with('_sub')->willReturn(true);
         $subdomainService->expects(self::once())->method('canRegisterNewSubdomain')->with($organization)->willReturn(false);
+        $subdomainService->expects(self::never())->method('isReservedSubdomain')->with($organization)->willReturn(false);
         $this->subdomainRepository->expects(self::never())->method('findBy')->with(['subdomain' => '_sub'])->willReturn(0);
 
         $this->entityManager->expects(self::never())->method('persist');
@@ -191,6 +215,28 @@ class SubdomainServiceTest extends TestCase
         $subdomainService->addNewSubdomain($organization, '_sub');
     }
 
+    public function testAddNewSubdomainIsReserved(): void
+    {
+        $subdomainService = $this->makeSubdomainServiceMockFor('addNewSubdomain');
+
+        $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock();
+
+        $subdomainService->expects(self::once())->method('isValidSubdomain')->with('_sub')->willReturn(true);
+        $subdomainService->expects(self::once())->method('canRegisterNewSubdomain')->with($organization)->willReturn(true);
+        $subdomainService->expects(self::once())->method('isReservedSubdomain')->with('_sub')->willReturn(true);
+        $this->subdomainRepository->expects(self::never())->method('findBy')->with(['subdomain' => '_sub'])->willReturn(0);
+
+        $this->entityManager->expects(self::never())->method('persist');
+        $this->entityManager->expects(self::never())->method('flush');
+        $this->bindFileService->expects(self::never())->method('registerSubdomain')->with('_sub');
+        $subdomainService->expects(self::never())->method('activateSubdomain');
+
+        $this->expectException(\RuntimeException::class);
+        $this->expectExceptionMessage('This subdomain is not available');
+
+        $subdomainService->addNewSubdomain($organization, '_sub');
+    }
+
     public function testAddNewSubdomainExisting(): void
     {
         $subdomainService = $this->makeSubdomainServiceMockFor('addNewSubdomain');
@@ -199,6 +245,7 @@ class SubdomainServiceTest extends TestCase
 
         $subdomainService->expects(self::once())->method('isValidSubdomain')->with('sub')->willReturn(true);
         $subdomainService->expects(self::once())->method('canRegisterNewSubdomain')->with($organization)->willReturn(true);
+        $subdomainService->expects(self::once())->method('isReservedSubdomain')->with('sub')->willReturn(false);
         $this->subdomainRepository->expects(self::once())->method('findBy')->with(['subdomain' => 'sub'])->willReturn(1);
 
         $this->entityManager->expects(self::never())->method('persist');
@@ -220,6 +267,7 @@ class SubdomainServiceTest extends TestCase
 
         $subdomainService->expects(self::once())->method('isValidSubdomain')->with('sub')->willReturn(true);
         $subdomainService->expects(self::once())->method('canRegisterNewSubdomain')->with($organization)->willReturn(true);
+        $subdomainService->expects(self::once())->method('isReservedSubdomain')->with('sub')->willReturn(false);
         $this->subdomainRepository->expects(self::once())->method('findBy')->with(['subdomain' => 'sub'])->willReturn(0);
 
         $subdomainService->expects(self::once())->method('activateSubdomain');

+ 12 - 0
tests/Unit/Service/Utils/ArrayUtilsTest.php

@@ -81,4 +81,16 @@ class ArrayUtilsTest extends TestCase
             )
         );
     }
+
+    public function testGetAndCast(): void {
+        $this->assertEquals(
+            123,
+            ArrayUtils::getAndCast(['a' => '123'], 'a', 'int')
+        );
+
+        $this->assertEquals(
+            null,
+            ArrayUtils::getAndCast(['b' => '123'], 'a', 'int')
+        );
+    }
 }

+ 31 - 0
tests/Unit/Service/Utils/EnvironnementTest.php

@@ -0,0 +1,31 @@
+<?php
+
+namespace App\Tests\Unit\Service\Utils;
+
+use App\Enum\Utils\EnvironnementVarEnum;
+use App\Service\Utils\Environnement;
+use PHPUnit\Framework\TestCase;
+use RuntimeException;
+
+class EnvironnementTest extends TestCase
+{
+    public function testGet(): void {
+        $environnement = new Environnement();
+
+        $_ENV[EnvironnementVarEnum::APP_ENV()->getValue()] = 'foo';
+
+        $this->assertEquals(
+            'foo',
+            $environnement->get(EnvironnementVarEnum::APP_ENV()->getValue())
+        );
+    }
+
+    public function testGetInvalid(): void {
+        $environnement = new Environnement();
+
+        $this->expectException(RuntimeException::class);
+        $this->expectExceptionMessage('Undefined environment variable : invalid');
+
+        $environnement->get('invalid');
+    }
+}