Bläddra i källkod

add tests for BaseApiRepository

Olivier Massot 4 år sedan
förälder
incheckning
e4bd168f21
1 ändrade filer med 196 tillägg och 0 borttagningar
  1. 196 0
      ot_core/Tests/Unit/Domain/Repository/BaseApiRepositoryTest.php

+ 196 - 0
ot_core/Tests/Unit/Domain/Repository/BaseApiRepositoryTest.php

@@ -0,0 +1,196 @@
+<?php
+
+namespace Opentalent\OtCore\Tests\Unit\Repository;
+
+use Nimut\TestingFramework\TestCase\UnitTestCase;
+use Opentalent\OtCore\Domain\Repository\BaseApiRepository;
+use GuzzleHttp\Client;
+use Opentalent\OtCore\Exception\ApiRequestException;
+use Opentalent\OtCore\Tests\Unit\Fixtures\ApiResponseFixtures;
+use TYPO3\CMS\Core\Core\ApplicationContext;
+
+class ConcreteBaseApiRepository extends BaseApiRepository {
+    protected function memberToObject(array $member) { return $member; }
+    public function injectClient($client) { parent::injectClient($client); }
+    public function get($uri, $params = []) { return parent::get($uri, $params); }
+    public function getBody($uri, $params = []) { return parent::getBody($uri, $params); }
+    public function getJsonDecoded($uri, $params = []) { return parent::getJsonDecoded($uri, $params); }
+    public function getApiFirstRecord($uri, $params = []) { return parent::getApiFirstRecord($uri, $params); }
+    public function getApiRecords($uri, $params = []) { return parent::getApiRecords($uri, $params); }
+}
+
+class BaseApiRepositoryTest extends UnitTestCase
+{
+    private $context;
+    /**
+     * @var ApiResponseFixtures
+     */
+    private $fixture;
+    /**
+     * @var ConcreteBaseApiRepository
+     */
+    private $baseApiRepository;
+    /**
+     * @var \Prophecy\Prophecy\ObjectProphecy
+     */
+    private $client;
+
+    public function setUp() {
+        // mock the application context
+        $this->context = $this->prophesize(ApplicationContext::class);
+        $this->context->isProduction()->willReturn(true);
+
+        $this->client = $this->prophesize(Client::class);
+        $this->baseApiRepository = new ConcreteBaseApiRepository($this->client->reveal(), $this->context->reveal());
+
+        $this->fixture = new ApiResponseFixtures();
+    }
+
+    private function injectClientFor($uri) {
+        // mock the Guzzle client
+        $willReturn = $this->fixture->get($uri);
+        $client = $this->prophesize(Client::class);
+        $client->request(BaseApiRepository::HTTP_METHOD, $uri)
+            ->shouldBeCalled()
+            ->willReturn($willReturn);
+        $this->inject($this->baseApiRepository, "client", $client->reveal());
+    }
+
+    /**
+     * get should build a valid url, send a query and
+     * return a Guzzle response object
+     *
+     * @test
+     */
+    public function get() {
+
+        $base_uri = "https://api.opentalent.fr/api/public/organizations";
+        $params = ['itemsPerPage' => 10, 'foo' => 1];
+
+        // uri as it is supposed to be processed by the repo
+        $processed_uri = $base_uri . "?_format=json&itemsPerPage=10&foo=1";
+
+        $this->injectClientFor($processed_uri);
+        $actual = $this->baseApiRepository->get($base_uri, $params);
+
+        $this->assertEquals(200, $actual->getStatusCode());
+    }
+
+    /**
+     * get should build a valid url, send a query and
+     * return a Guzzle response object
+     *
+     * @test
+     */
+    public function getWithNoParams() {
+
+        $base_uri = "https://api.opentalent.fr/api/public/organizations";
+        $params = [];
+
+        // uri as it is supposed to be processed by the repo
+        $processed_uri = $base_uri . "?_format=json&itemsPerPage=8";
+
+        $this->injectClientFor($processed_uri);
+        $actual = $this->baseApiRepository->get($base_uri, $params);
+
+        $this->assertEquals(200, $actual->getStatusCode());
+    }
+
+    /**
+     * get should build a valid url, send a query and
+     * return a Guzzle response object
+     *
+     * @test
+     */
+    public function getInvalidUri()
+    {
+        $base_uri = "a very bad uri";
+        $params = [];
+        $processed_uri = $base_uri . "?_format=json&itemsPerPage=8";
+
+        $client = $this->prophesize(Client::class);
+        $client->request(BaseApiRepository::HTTP_METHOD, $processed_uri)
+            ->shouldBeCalled()
+            ->willThrow(new \GuzzleHttp\Exception\TransferException('error'));
+        $this->inject($this->baseApiRepository, "client", $client->reveal());
+
+        try {
+            $this->baseApiRepository->get($base_uri, $params);
+            throw new \AssertionError("An ApiRequestException should have been thrown");
+        } catch (ApiRequestException $e) {
+            $this->assertEquals('error', $e->getMessage());
+        }
+    }
+
+    /**
+     * getBody should return the response body as a string
+     *
+     * @test
+     */
+    public function getBody() {
+
+        $base_uri = "https://api.opentalent.fr/api/public/organizations";
+        $params = ['itemsPerPage' => 10, 'foo' => 1];
+
+        // uri as it is supposed to be processed by the repo
+        $processed_uri = $base_uri . "?_format=json&itemsPerPage=10&foo=1";
+
+        $this->injectClientFor($processed_uri);
+        $actual = $this->baseApiRepository->getBody($base_uri, $params);
+
+        $this->assertEquals('{"@context": "/api/contexts/PortailOrganization"}', $actual);
+    }
+
+    /**
+     * getBody should return the response body as an array
+     *
+     * @test
+     */
+    public function getJsonDecoded() {
+
+        $base_uri = "https://api.opentalent.fr/api/public/organizations";
+        $params = ['itemsPerPage' => 10, 'foo' => 1];
+
+        // uri as it is supposed to be processed by the repo
+        $processed_uri = $base_uri . "?_format=json&itemsPerPage=10&foo=1";
+
+        $this->injectClientFor($processed_uri);
+        $actual = $this->baseApiRepository->getJsonDecoded($base_uri, $params);
+
+        $this->assertEquals(["@context" => "/api/contexts/PortailOrganization"], $actual);
+    }
+
+    /**
+     * getApiFirstRecord should return the first member of the api response
+     * this member has been processed by the memberToObject method, which does nothing here
+     *
+     * @test
+     */
+    public function getApiFirstRecord() {
+        $base_uri = "https://api.opentalent.fr/api/public/organizations";
+        $params = ['filter[where][id]' => 1];
+        $processed_uri = $base_uri . "?_format=json&filter%5Bwhere%5D%5Bid%5D=1&page=1&totalItems=1&itemsPerPage=8";
+        $this->injectClientFor($processed_uri);
+
+        $actual = $this->baseApiRepository->getApiFirstRecord($base_uri, $params);
+
+        $this->assertEquals('PortailOrganization', $actual['@type']);
+    }
+
+    /**
+     * getApiFirstRecord should return the members of the api response
+     * these members has been processed by the memberToObject method, which does nothing here
+     *
+     * @test
+     */
+    public function getApiRecords() {
+        $base_uri = "https://api.opentalent.fr/api/public/organizations";
+        $params = ['filter[where][id]' => 1];
+        $processed_uri = $base_uri . "?_format=json&filter%5Bwhere%5D%5Bid%5D=1&itemsPerPage=8";
+        $this->injectClientFor($processed_uri);
+
+        $actual = $this->baseApiRepository->getApiRecords($base_uri, $params);
+
+        $this->assertEquals('PortailOrganization', $actual->getMembers()[0]['@type']);
+    }
+}