فهرست منبع

refactor applicative test and minor fixes

Olivier Massot 2 سال پیش
والد
کامیت
f558431d66

+ 63 - 18
tests/Application/OtWebTestCase.php

@@ -5,6 +5,13 @@ namespace App\Tests\Application;
 use App\Entity\Access\Access;
 use App\Entity\Person\Person;
 use App\Entity\Public\FederationStructure;
+use App\Enum\Organization\LegalEnum;
+use App\Enum\Organization\PrincipalTypeEnum;
+use App\Enum\Organization\SettingsProductEnum;
+use App\Tests\Fixture\Factory\Access\AccessFactory;
+use App\Tests\Fixture\Factory\Organization\OrganizationFactory;
+use App\Tests\Fixture\Factory\Organization\SettingsFactory;
+use App\Tests\Fixture\Factory\Person\PersonFactory;
 use Doctrine\ORM\EntityManagerInterface;
 use Doctrine\Common\DataFixtures\Purger\ORMPurger;
 use Symfony\Bundle\SecurityBundle\Security;
@@ -42,7 +49,7 @@ abstract class OtWebTestCase extends ApiTestCase
         $this->purgeDb();
 
         // Définit les fixtures et flush
-        $this->setFixtures();
+        $this->loadFixture();
         $this->em->flush();
 
         // Instancie le client qui exécutera les requêtes à l'api
@@ -69,14 +76,36 @@ abstract class OtWebTestCase extends ApiTestCase
     }
 
     /**
-     * Create and persist the fixtures (no need to flush, the setup will perform it later)
+     * Create and persist the fixtures (do not flush)
      *
      * @see https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html#same-entities-used-in-these-docs
      *
      * @return void
      */
-    protected function setFixtures(): void {
-        // TODO: préparer un jeu de fixtures par défaut
+    protected function loadFixture(): void {
+        $person = PersonFactory::createOne(
+            [
+                'username' => 'username',
+                'password' => 'password'
+            ]
+        );
+
+        $organization = OrganizationFactory::createOne([
+            'legalStatus' => LegalEnum::ASSOCIATION_LAW_1901()->getValue(),
+            'principalType' => PrincipalTypeEnum::ARTISTIC_EDUCATION_ONLY()->getValue(),
+            'name' => 'My Organization'
+        ]);
+
+        SettingsFactory::createOne([
+            'product' => SettingsProductEnum::ARTIST(),
+            'organization' => $organization
+        ]);
+
+        $this->user = AccessFactory::createOne([
+            'person' => $person,
+            'organization' => $organization,
+            'roles' => ['ROLE_USERS_VIEW']
+        ]);
     }
 
     /**
@@ -97,7 +126,6 @@ abstract class OtWebTestCase extends ApiTestCase
         }
 
         $parameters = ['headers' => $headers];
-
         if ($data) {
             $parameters['json'] = $data;
         }
@@ -175,6 +203,36 @@ abstract class OtWebTestCase extends ApiTestCase
         );
     }
 
+    /**
+     * Login as the given Access user
+     *
+     * @param Proxy|Access $access
+     * @return void
+     * @throws \Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface
+     * @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
+     * @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
+     * @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
+     */
+    protected function loginAs(Proxy | Access $access): void {
+        $person = $access->getPerson();
+
+        $response = $this->post(
+            '/login_check',
+            ['username' => $person->getUsername(), 'password' => $person->getPassword()]
+        );
+        $content = $response->getContent();
+
+        $this->securityToken = json_decode($content)->token;
+        $this->user = $access;
+    }
+
+    /**
+     * Assert that the response has the expected status code and is well formated
+     *
+     * @param string $resourceClass
+     * @param int $expectedStatus
+     * @return void
+     */
     protected function validateCollectionSchema(string $resourceClass, int $expectedStatus = 200): void {
         $this->assertResponseStatusCodeSame($expectedStatus);
 
@@ -189,17 +247,4 @@ abstract class OtWebTestCase extends ApiTestCase
         // >>> Issue with the json typed PublicStructure::addresses properties
         // $this->assertMatchesResourceCollectionJsonSchema($resourceClass);
     }
-
-    protected function loginAs(Proxy | Access $access): void {
-        $person = $access->getPerson();
-
-        $response = $this->post(
-            '/login_check',
-            ['username' => $person->getUsername(), 'password' => $person->getPassword()]
-        );
-        $content = $response->getContent();
-
-        $this->securityToken = json_decode($content)->token;
-        $this->user = $access;
-    }
 }

+ 17 - 45
tests/Application/Person/PersonTest.php

@@ -1,6 +1,6 @@
 <?php
-
 namespace App\Tests\Application\Person;
+
 use App\Entity\Person\Person;
 use App\Entity\Access\Access;
 use App\Entity\Organization\Organization;
@@ -20,59 +20,31 @@ use App\Enum\Organization\LegalEnum;
 
 class PersonTest extends OtWebTestCase
 {
-    private Proxy | Person $person;
-    private Proxy | Access $access;
-
-    protected function setFixtures(): void
-    {
-        $this->person = PersonFactory::createOne(
-            [
-                'username' => 'username',
-                'password' => 'password'
-            ]
-        );
-
-        $organization = OrganizationFactory::createOne([
-            'legalStatus' => LegalEnum::ASSOCIATION_LAW_1901()->getValue(),
-            'principalType' => PrincipalTypeEnum::ARTISTIC_EDUCATION_ONLY()->getValue(),
-            'name' => 'My Organization'
-        ]);
-
-        SettingsFactory::createOne([
-            'product' => SettingsProductEnum::ARTIST(),
-            'organization' => $organization
-        ]);
-
-        $this->access = AccessFactory::createOne([
-            'person' => $this->person,
-            'organization' => $organization,
-            'roles' => ['ROLE_USERS_VIEW']
-        ]);
-    }
-
     public function testPersonGet(): void
     {
-        $this->loginAs($this->access);
+        $this->loginAs($this->user);
 
-        $this->get('/api/people/' . $this->person->getId());
+        $this->get('/api/people/' . $this->user->getPerson()->getId());
 
         $this->validateCollectionSchema(Person::class);
 
         $this->assertJsonContains([
             '@context' => '/api/contexts/Person',
-            '@id' => '/api/people/' . $this->person->getId(),
+            '@id' => '/api/people/' . $this->user->getPerson()->getId(),
             '@type' => 'Person',
             'username' => 'username'
         ]);
     }
 
     public function testPersonGetHasNoRole(): void {
-        $this->access->setRoles([]);
-        $this->access->save();
 
-        $this->loginAs($this->access);
+        // User has not the required role
+        $this->user->setRoles([]);
+        $this->user->save();
+
+        $this->loginAs($this->user);
 
-        $this->get('/api/people/' . $this->person->getId());
+        $this->get('/api/people/' . $this->user->getPerson()->getId());
 
         $this->validateCollectionSchema(Person::class, 403);
 
@@ -82,7 +54,7 @@ class PersonTest extends OtWebTestCase
     }
 
     public function testPersonGetCollection(): void {
-        $this->loginAs($this->access);
+        $this->loginAs($this->user);
 
         $this->get('/api/peoples');
 
@@ -90,27 +62,27 @@ class PersonTest extends OtWebTestCase
     }
 
     public function testPersonPut(): void {
-        $this->loginAs($this->access);
+        $this->loginAs($this->user);
 
-        $this->put('/api/people/' . $this->person->getId(), []);
+        $this->put('/api/people/' . $this->user->getPerson()->getId(), []);
 
         // Expects : 405 Method Not Allowed
         $this->assertResponseStatusCodeSame(405);
     }
 
     public function testPersonPost(): void {
-        $this->loginAs($this->access);
+        $this->loginAs($this->user);
 
-        $this->post('/api/people/' . $this->person->getId(), []);
+        $this->post('/api/people/' . $this->user->getPerson()->getId(), []);
 
         // Expects : 405 Method Not Allowed
         $this->assertResponseStatusCodeSame(405);
     }
 
     public function testPersonDelete(): void {
-        $this->loginAs($this->access);
+        $this->loginAs($this->user);
 
-        $this->delete('/api/people/' . $this->person->getId());
+        $this->delete('/api/people/' . $this->user->getPerson()->getId());
 
         // Expects : 405 Method Not Allowed
         $this->assertResponseStatusCodeSame(405);

+ 1 - 1
tests/Unit/Service/Access/AccessProfileCreatorTest.php

@@ -382,7 +382,7 @@ class AccessProfileCreatorTest extends TestCase
         $this->assertEquals('MR', $accessProfile->getGender());
         $this->assertEquals(2020, $accessProfile->getActivityYear());
         $this->assertEquals(123, $accessProfile->getAvatarId());
-        $this->assertEquals(false, $accessProfile->getIsSuperAdminAccess());
+        $this->assertFalse($accessProfile->getIsSuperAdminAccess());
         $this->assertEquals($this->organizationProfile, $accessProfile->getOrganization());
     }
 }

+ 1 - 1
tests/Unit/Service/Dolibarr/DolibarrAccountCreatorTest.php

@@ -203,6 +203,6 @@ class DolibarrAccountCreatorTest extends TestCase
         $this->assertEquals('foo', $dolibarrBill->getRef());
         $this->assertEquals(100.0, $dolibarrBill->getTaxExcludedAmount());
         $this->assertEquals(120.0, $dolibarrBill->getTaxIncludedAmount());
-        $this->assertEquals(false, $dolibarrBill->getPaid());
+        $this->assertFalse($dolibarrBill->getPaid());
     }
 }

+ 1 - 1
tests/Unit/Service/Export/LicenceCmfExporterTest.php

@@ -157,7 +157,7 @@ class LicenceCmfExporterTest extends TestCase
         $qrCode = $this->getMockBuilder(File::class)->getMock();
 
         $this->assertEquals('my_network', $licence->getFederationName());
-        $this->assertEquals(true, $licence->isOrganizationLicence());
+        $this->assertTrue($licence->isOrganizationLicence());
         $this->assertEquals(2020, $licence->getYear());
         $this->assertEquals('931572', $licence->getColor());
         $this->assertEquals($qrCode, $licence->getQrCode());

+ 1 - 1
tests/Unit/Service/Export/Model/LicenceCmfTest.php

@@ -64,7 +64,7 @@ class LicenceCmfTest extends TestCase
 
         $this->assertEquals(1, $model->getId());
         $this->assertEquals(2020, $model->getYear());
-        $this->assertEquals(true, $model->isOrganizationLicence());
+        $this->assertTrue($model->isOrganizationLicence());
         $this->assertEquals('foo', $model->getOrganizationName());
         $this->assertEquals('123', $model->getOrganizationIdentifier());
         $this->assertEquals('bar', $model->getFederationName());

+ 2 - 2
tests/Unit/Service/File/Storage/LocalStorageTest.php

@@ -162,7 +162,7 @@ class LocalStorageTest extends TestCase
         $this->assertEquals('file.ext', $file->getName());
         $this->assertEquals(null, $file->getSlug());
         $this->assertEquals(FileTypeEnum::LICENCE_CMF()->getValue(), $file->getType());
-        $this->assertEquals(true, $file->getIsTemporaryFile());
+        $this->assertTrue($file->getIsTemporaryFile());
         $this->assertEquals('ONLY_ORGANIZATION', $file->getVisibility());
         $this->assertEquals('application/pdf', $file->getMimeType());
         $this->assertEquals(123, $file->getCreatedBy());
@@ -193,7 +193,7 @@ class LocalStorageTest extends TestCase
         $this->assertEquals($owner, $file->getPerson());
         $this->assertEquals('file.txt', $file->getName());
         $this->assertEquals(FileTypeEnum::NONE()->getValue(), $file->getType());
-        $this->assertEquals(false, $file->getIsTemporaryFile());
+        $this->assertFalse($file->getIsTemporaryFile());
         $this->assertEquals('NOBODY', $file->getVisibility());
         $this->assertEquals('text/plain', $file->getMimeType());
     }

+ 1 - 1
tests/Unit/Service/MercureHubTest.php

@@ -43,7 +43,7 @@ class MercureHubTest extends TestCase
 
         $this->assertEquals(['access/1'], $update->getTopics());
         $this->assertEquals("{'foo': 1}", $update->getData());
-        $this->assertEquals(true, $update->isPrivate());
+        $this->assertTrue($update->isPrivate());
     }
 
     /**