From f76e9138d9a7c3239dddd1f00ff64b0b7430b1c0 Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Fri, 15 Nov 2019 15:42:14 +0100
Subject: [PATCH] FEAT #11691 TIME 0:20 Unit tests

---
 .../controllers/AttachmentController.php      |  1 +
 .../attachment/AttachmentControllerTest.php   | 73 +++++++++++++++++++
 2 files changed, 74 insertions(+)

diff --git a/src/app/attachment/controllers/AttachmentController.php b/src/app/attachment/controllers/AttachmentController.php
index 88c6ba0aa36..6bfd3be95df 100755
--- a/src/app/attachment/controllers/AttachmentController.php
+++ b/src/app/attachment/controllers/AttachmentController.php
@@ -171,6 +171,7 @@ class AttachmentController
             return $response->withStatus(400)->withJson(['errors' => $control['errors']]);
         }
 
+        $body['id'] = $args['id'];
         $isStored = StoreController::storeAttachment($body);
         if (empty($isStored) || !empty($isStored['errors'])) {
             return $response->withStatus(500)->withJson(['errors' => '[AttachmentController update] ' . $isStored['errors']]);
diff --git a/test/unitTests/app/attachment/AttachmentControllerTest.php b/test/unitTests/app/attachment/AttachmentControllerTest.php
index 5b80681df32..3ff608d12b7 100644
--- a/test/unitTests/app/attachment/AttachmentControllerTest.php
+++ b/test/unitTests/app/attachment/AttachmentControllerTest.php
@@ -40,6 +40,12 @@ class AttachmentControllerTest extends TestCase
         self::$id = $responseBody->id;
         $this->assertInternalType('int', self::$id);
 
+        $response     = $attachmentController->create($fullRequest, new \Slim\Http\Response());
+        $this->assertSame(400, $response->getStatusCode());
+        $response = (array)json_decode((string)$response->getBody());
+
+        $this->assertSame('Body type is empty or not a string', $response['errors']);
+
 
         //  READ
         $res = \Attachment\models\AttachmentModel::getById(['id' => self::$id, 'select' => ['*']]);
@@ -60,4 +66,71 @@ class AttachmentControllerTest extends TestCase
         $this->assertNotNull($res['filesize']);
         $this->assertNull($res['origin_id']);
     }
+
+    public function testUpdate()
+    {
+        $attachmentController = new \Attachment\controllers\AttachmentController();
+
+        //  UPDATE
+        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']);
+        $request        = \Slim\Http\Request::createFromEnvironment($environment);
+
+        $aArgs = [
+            'title'         => 'La plus chétive cabane renferme plus de vertus que les palais des rois.',
+            'type'          => 'response_project',
+        ];
+
+        $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
+
+        $response     = $attachmentController->update($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]);
+        $this->assertSame(204, $response->getStatusCode());
+
+        unset($aArgs['type']);
+        $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
+
+        $response     = $attachmentController->update($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]);
+        $this->assertSame(400, $response->getStatusCode());
+        $response = (array)json_decode((string)$response->getBody());
+
+        $this->assertSame('Body type is empty or not a string', $response['errors']);
+
+        //  READ
+        $response = $attachmentController->getById($request, new \Slim\Http\Response(), ['id' => self::$id]);
+        $res = (array)json_decode((string)$response->getBody());
+        $this->assertInternalType('array', $res);
+
+        $this->assertSame($aArgs['title'], $res['title']);
+        $this->assertSame($aArgs['type'], $res['type']);
+        $this->assertSame('A_TRA', $res['status']);
+        $this->assertSame(1, $res['relation']);
+    }
+
+    public function testDelete()
+    {
+        $attachmentController = new \Attachment\controllers\AttachmentController();
+
+        //  DELETE
+        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'DELETE']);
+        $request        = \Slim\Http\Request::createFromEnvironment($environment);
+
+        $response     = $attachmentController->delete($request, new \Slim\Http\Response(), ['id' => self::$id]);
+        $this->assertSame(204, $response->getStatusCode());
+
+        //  DELETE
+        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'DELETE']);
+        $request        = \Slim\Http\Request::createFromEnvironment($environment);
+
+        $response     = $attachmentController->delete($request, new \Slim\Http\Response(), ['id' => self::$id]);
+        $res = (array)json_decode((string)$response->getBody());
+        $this->assertSame(400, $response->getStatusCode());
+
+        $this->assertSame('Attachment does not exist', $res['errors']);
+
+        //  READ
+        $response = $attachmentController->getById($request, new \Slim\Http\Response(), ['id' => self::$id]);
+        $res = (array)json_decode((string)$response->getBody());
+        $this->assertSame(400, $response->getStatusCode());
+
+        $this->assertSame('Attachment does not exist', $res['errors']);
+    }
 }
-- 
GitLab