From 2be57685446ef260cc416d7954d1ac5b94a2fd33 Mon Sep 17 00:00:00 2001 From: Damien <damien.burel@maarch.org> Date: Thu, 19 Dec 2019 16:04:33 +0100 Subject: [PATCH] FEAT #12091 TIME 0:00 Contact custom field controller test --- .../ContactCustomFieldControllerTest.php | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 test/unitTests/app/contact/ContactCustomFieldControllerTest.php diff --git a/test/unitTests/app/contact/ContactCustomFieldControllerTest.php b/test/unitTests/app/contact/ContactCustomFieldControllerTest.php new file mode 100644 index 00000000000..f01528f3cf6 --- /dev/null +++ b/test/unitTests/app/contact/ContactCustomFieldControllerTest.php @@ -0,0 +1,110 @@ +<?php + +/** +* Copyright Maarch since 2008 under licence GPLv3. +* See LICENCE.txt file at the root folder for more details. +* This file is part of Maarch software. +* +*/ + +use PHPUnit\Framework\TestCase; + +class ContactCustomFieldControllerTest extends TestCase +{ + private static $id = null; + + public function testCreate() + { + $contactCustomFieldController = new \Contact\controllers\ContactCustomFieldController(); + + // CREATE + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + $args = [ + 'label' => 'mon custom', + 'type' => 'select', + 'values' => ['one', 'two'] + ]; + $fullRequest = \httpRequestCustom::addContentInBody($args, $request); + + $response = $contactCustomFieldController->create($fullRequest, new \Slim\Http\Response()); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + + $this->assertInternalType('int', $responseBody['id']); + + self::$id = $responseBody['id']; + + // Errors + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + $args = [ + 'label' => 'mon custom', + 'type' => 'select', + 'values' => ['one', 'two'] + ]; + $fullRequest = \httpRequestCustom::addContentInBody($args, $request); + + $response = $contactCustomFieldController->create($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + + $this->assertSame('Custom field with this label already exists', $responseBody['errors']); + } + + public function testReadList() + { + $contactCustomFieldController = new \Contact\controllers\ContactCustomFieldController(); + + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + $response = $contactCustomFieldController->get($request, new \Slim\Http\Response()); + $responseBody = json_decode((string)$response->getBody(), true); + + $this->assertNotNull($responseBody['customFields']); + } + + public function testUpdate() + { + $contactCustomFieldController = new \Contact\controllers\ContactCustomFieldController(); + + // UPDATE + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + $args = [ + 'label' => 'mon custom22', + 'values' => ['one', 'two', 'trois'] + ]; + $fullRequest = \httpRequestCustom::addContentInBody($args, $request); + + $response = $contactCustomFieldController->update($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(204, $response->getStatusCode()); + + // Errors + unset($args['label']); + $fullRequest = \httpRequestCustom::addContentInBody($args, $request); + + $response = $contactCustomFieldController->update($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + + $this->assertSame('Body label is empty or not a string', $responseBody['errors']); + } + + public function testDelete() + { + $contactCustomFieldController = new \Contact\controllers\ContactCustomFieldController(); + + // UPDATE + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'DELETE']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + + $response = $contactCustomFieldController->delete($request, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(204, $response->getStatusCode()); + } +} -- GitLab