Parcourir la source

complete tests for Person

Olivier Massot il y a 2 ans
Parent
commit
7d2bfd952f
2 fichiers modifiés avec 85 ajouts et 15 suppressions
  1. 42 4
      tests/Application/OtWebTestCase.php
  2. 43 11
      tests/Application/Person/PersonTest.php

+ 42 - 4
tests/Application/OtWebTestCase.php

@@ -113,7 +113,7 @@ abstract class OtWebTestCase extends ApiTestCase
      * Send a GET request and return the response parsed content
      *
      * @param string $route
-     * @param array<mixed> $parameters
+     * @param array<mixed> $headers
      * @return ResponseInterface
      */
     protected function get(string $route, array $headers = []): ResponseInterface {
@@ -125,11 +125,29 @@ abstract class OtWebTestCase extends ApiTestCase
         );
     }
 
+    /**
+     * Send a PUT request and return the response parsed content
+     *
+     * @param string $route
+     * @param array<mixed> $data
+     * @param array<mixed> $headers
+     * @return ResponseInterface
+     */
+    protected function put(string $route, array $data, array $headers = []): ResponseInterface {
+        return $this->request(
+            Request::METHOD_PUT,
+            $route,
+            $data,
+            $headers
+        );
+    }
+
     /**
      * Send a POST request and return the response parsed content
      *
      * @param string $route
-     * @param array<mixed> $parameters
+     * @param array<mixed> $data
+     * @param array<mixed> $headers
      * @return ResponseInterface
      */
     protected function post(string $route, array $data, array $headers = []): ResponseInterface {
@@ -141,8 +159,28 @@ abstract class OtWebTestCase extends ApiTestCase
         );
     }
 
-    protected function validateCollectionSchema(string $resourceClass): void {
-        $this->assertResponseIsSuccessful();
+    /**
+     * Send a DELETE request and return the response parsed content
+     *
+     * @param string $route
+     * @param array<mixed> $headers
+     * @return ResponseInterface
+     */
+    protected function delete(string $route, array $headers = []): ResponseInterface {
+        return $this->request(
+            Request::METHOD_DELETE,
+            $route,
+            null,
+            $headers
+        );
+    }
+
+    protected function validateCollectionSchema(string $resourceClass, int $expectedStatus = 200): void {
+        $this->assertResponseStatusCodeSame($expectedStatus);
+
+        if ($expectedStatus == 200) {
+            $this->assertResponseIsSuccessful();
+        }
 
         // Asserts that the returned content type is JSON-LD (the default)
         $this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');

+ 43 - 11
tests/Application/Person/PersonTest.php

@@ -1,6 +1,7 @@
 <?php
 
 namespace App\Tests\Application\Person;
+use App\Entity\Person\Person;
 use App\Entity\Access\Access;
 use App\Entity\Organization\Organization;
 use App\Entity\Organization\Settings;
@@ -8,7 +9,6 @@ use App\Entity\Public\PublicEvent;
 use App\Enum\Organization\PrincipalTypeEnum;
 use App\Enum\Organization\SettingsProductEnum;
 use App\Tests\Application\OtWebTestCase;
-use App\Entity\Person;
 use App\Tests\Fixture\Factory\Access\AccessFactory;
 use App\Tests\Fixture\Factory\Organization\OrganizationFactory;
 use App\Tests\Fixture\Factory\Organization\SettingsFactory;
@@ -21,9 +21,7 @@ use App\Enum\Organization\LegalEnum;
 class PersonTest extends OtWebTestCase
 {
     private Proxy | Person $person;
-    private Proxy | Organization $organization;
     private Proxy | Access $access;
-    private Proxy | Settings $settings;
 
     protected function setFixtures(): void
     {
@@ -34,20 +32,20 @@ class PersonTest extends OtWebTestCase
             ]
         );
 
-        $this->organization = OrganizationFactory::createOne([
+        $organization = OrganizationFactory::createOne([
             'legalStatus' => LegalEnum::ASSOCIATION_LAW_1901()->getValue(),
             'principalType' => PrincipalTypeEnum::ARTISTIC_EDUCATION_ONLY()->getValue(),
             'name' => 'My Organization'
         ]);
 
-        $this->settings = SettingsFactory::createOne([
+        SettingsFactory::createOne([
             'product' => SettingsProductEnum::ARTIST(),
-            'organization' => $this->organization
+            'organization' => $organization
         ]);
 
         $this->access = AccessFactory::createOne([
             'person' => $this->person,
-            'organization' => $this->organization,
+            'organization' => $organization,
             'roles' => ['ROLE_USERS_VIEW']
         ]);
     }
@@ -68,19 +66,53 @@ class PersonTest extends OtWebTestCase
         ]);
     }
 
+    public function testPersonGetHasNoRole(): void {
+        $this->access->setRoles([]);
+        $this->access->save();
+
+        $this->loginAs($this->access);
+
+        $this->get('/api/people/' . $this->person->getId());
+
+        $this->validateCollectionSchema(Person::class, 403);
+
+        $this->assertJsonContains([
+            "hydra:description" => "Access Denied."
+        ]);
+    }
+
     public function testPersonGetCollection(): void {
-        // TODO: get collection is not permitted
+        $this->loginAs($this->access);
+
+        $this->get('/api/peoples');
+
+        $this->assertResponseStatusCodeSame(404);
     }
 
     public function testPersonPut(): void {
-        // TODO: put is not permitted
+        $this->loginAs($this->access);
+
+        $this->put('/api/people/' . $this->person->getId(), []);
+
+        // Expects : 405 Method Not Allowed
+        $this->assertResponseStatusCodeSame(405);
     }
 
     public function testPersonPost(): void {
-        // TODO: post is not permitted
+        $this->loginAs($this->access);
+
+        $this->post('/api/people/' . $this->person->getId(), []);
+
+        // Expects : 405 Method Not Allowed
+        $this->assertResponseStatusCodeSame(405);
     }
 
     public function testPersonDelete(): void {
-        // TODO: delete is not permitted
+        $this->loginAs($this->access);
+
+        $this->delete('/api/people/' . $this->person->getId());
+
+        // Expects : 405 Method Not Allowed
+        $this->assertResponseStatusCodeSame(405);
     }
 }