Explorar el Código

add fixtures and fix base method

Olivier Massot hace 4 años
padre
commit
825e6ee852

+ 35 - 10
ot_core/Classes/Domain/Repository/BaseApiRepository.php

@@ -8,14 +8,14 @@ use Opentalent\OtCore\Exception\ApiRequestException;
 use Psr\Http\Message\ResponseInterface;
 use Psr\Log\LoggerAwareInterface;
 use Psr\Log\LoggerAwareTrait;
+use TYPO3\CMS\Core\Core\ApplicationContext;
 use TYPO3\CMS\Extbase\Object\ObjectManagerInterface;
-use TYPO3\CMS\Extbase\Persistence\Repository;
 
 /**
  *  Base class for repositories based on the Opentalent API
  *
  */
-abstract class BaseApiRepository extends Repository implements LoggerAwareInterface
+abstract class BaseApiRepository implements LoggerAwareInterface
 {
     use LoggerAwareTrait;
 
@@ -24,13 +24,38 @@ abstract class BaseApiRepository extends Repository implements LoggerAwareInterf
     const HTTP_METHOD = 'GET';
     const DEFAULT_ITEMS_PER_PAGE = 8;
 
-    private $client;
-    private $context;
+    protected $client;
+    protected $context;
 
-    public function __construct(ObjectManagerInterface $objectManager) {
-        parent::__construct($objectManager);
-        $this->client = new Client(['base_uri' => static::BASE_URI]);
-        $this->context = \TYPO3\CMS\Core\Core\Environment::getContext();
+    /**
+     * BaseApiRepository constructor.
+     *
+     * @param ObjectManagerInterface $objectManager
+     * @param Client|null $client  [For tests only]
+     * @param ApplicationContext|null $context  [For tests only]
+     */
+    public function __construct(
+        ?Client $client = null,
+        ?ApplicationContext $context = null
+    ) {
+        if ($client === null) {
+            $this->client = new Client(['base_uri' => static::BASE_URI]);
+        } else {
+            $this->client = $client;
+        }
+        if ($context === null) {
+            $this->context = \TYPO3\CMS\Core\Core\Environment::getContext();
+        } else {
+            $this->context = $context;
+        }
+    }
+
+    /**
+     * [FOR TESTS ONLY]
+     * @param Client $client
+     */
+    protected function injectClient(Client $client) {
+        $this->client = $client;
     }
 
     /**
@@ -43,7 +68,7 @@ abstract class BaseApiRepository extends Repository implements LoggerAwareInterf
      * @throws ApiRequestException
      */
     protected function getApiRecords(string $uri, $params = []) {
-        $body = $this->getJson($uri, $params);
+        $body = $this->getJsonDecoded($uri, $params);
 
         $page = (int)($params['page'] ?? 1);
 
@@ -100,7 +125,7 @@ abstract class BaseApiRepository extends Repository implements LoggerAwareInterf
      * @return array
      * @throws ApiRequestException
      */
-    protected function getJson(string $uri, $params = [])
+    protected function getJsonDecoded(string $uri, $params = [])
     {
         return json_decode($this->getBody($uri, $params),true);
     }

+ 121 - 0
ot_core/Tests/Unit/Fixtures/ApiResponseFixtures.php

@@ -0,0 +1,121 @@
+<?php
+
+
+namespace Opentalent\OtCore\Tests\Unit\Fixtures;
+
+
+use TYPO3\CMS\Core\Http\HtmlResponse;
+use TYPO3\CMS\Core\Http\JsonResponse;
+use TYPO3\CMS\Core\Http\Response;
+
+class ApiResponseFixtures
+{
+    private $responses = [
+        'stub' => '{"@context": "/api/contexts/PortailOrganization"}',
+        'org-1' => '
+        {
+            "@context": "/api/contexts/PortailOrganization",
+            "@id": "/api/public/organizations?_format=json&filter[where][id]=1",
+            "@type": "hydra:PagedCollection",
+            "hydra:totalItems": 1,
+            "hydra:itemsPerPage": 30,
+            "hydra:firstPage": "/api/public/organizations?_format=json&filter%5Bwhere%5D%5Bid%5D=1",
+            "hydra:lastPage": "/api/public/organizations?_format=json&filter%5Bwhere%5D%5Bid%5D=1",
+            "hydra:member": [
+                {
+                    "@id": "/api/public/organizations/1",
+                    "@type": "PortailOrganization",
+                    "subDomain": "org",
+                    "name": "Name",
+                    "slug": "org-1",
+                    "principalType": "ARTISTIC_PRACTICE_ONLY",
+                    "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi faucibus nulla et nulla maximus tempor. Nullam eros nunc, sodales nec turpis vel, semper consectetur eros. Vivamus id porttitor turpis. Cras at urna faucibus, cursus eros ac, eleifend enim. Maecenas id leo id ante lacinia tempor. Cras est eros, pellentesque et luctus non, tempor in turpis. Maecenas interdum, nisi eu posuere vestibulum, ex erat accumsan sem, in tempor sem mi scelerisque sapien. ",
+                    "categories": [
+                        "1MC"
+                    ],
+                    "addressCity": "CITY",
+                    "postalCode": "00000",
+                    "streetAddress": "street",
+                    "latitude": 1.2345,
+                    "longitude": 1.2345,
+                    "country": "France",
+                    "logo": "https://api.opentalent.fr/_internal/secure/files/1",
+                    "parentName": "Network",
+                    "parentSubDomain": "network"
+                }],
+        "hydra:search": {}
+        }',
+        'event-2' => '
+        {
+            "@context": "/api/contexts/PortailEvent",
+            "@id": "/api/public/events?_format=json&filter[where][id]=2",
+            "@type": "hydra:PagedCollection",
+            "hydra:totalItems": 1,
+            "hydra:itemsPerPage": 30,
+            "hydra:firstPage": "/api/public/events?_format=json&filter%5Bwhere%5D%5Bid%5D=2",
+            "hydra:lastPage": "/api/public/events?_format=json&filter%5Bwhere%5D%5Bid%5D=2",
+            "hydra:member": [
+                {
+                    "@id": "/api/public/events/2",
+                    "@type": "PortailEvent",
+                    "organizationId": 1,
+                    "subDomain": "org",
+                    "name": "The Event",
+                    "description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi faucibus nulla et nulla maximus tempor. Nullam eros nunc, sodales nec turpis vel, semper consectetur eros. Vivamus id porttitor turpis. Cras at urna faucibus, cursus eros ac, eleifend enim. Maecenas id leo id ante lacinia tempor. Cras est eros, pellentesque et luctus non, tempor in turpis. Maecenas interdum, nisi eu posuere vestibulum, ex erat accumsan sem, in tempor sem mi scelerisque sapien. ",
+                    "categories": [],
+                    "url": "http://www.org.fr/",
+                    "rule": null,
+                    "datetimeStart": "2021-01-01T17:00:00+00:00",
+                    "datetimeEnd": "2021-01-01T18:00:00+00:00",
+                    "dates": null,
+                    "placeName": "Place",
+                    "placeDescription": null,
+                    "placeFloorSize": null,
+                    "placeCapacity": null,
+                    "city": "City",
+                    "postalCode": "00000",
+                    "streetAddress": "street",
+                    "longitude": 1.2345,
+                    "latitude": 1.2345,
+                    "roomName": null,
+                    "roomDescription": null,
+                    "roomLocalisation": null,
+                    "roomCapacity": null,
+                    "roomFloorSize": null,
+                    "zupId": null,
+                    "deepLink": null,
+                    "image": "https://api.opentalent.fr/_internal/secure/files/2",
+                    "priceMini": null,
+                    "meetingSchedule": [],
+                    "api": true,
+                    "parentName": "Network",
+                    "parentSubDomain": "network"
+                }
+        ],
+        "hydra:search":{}
+        }'
+    ];
+
+    private $map = [
+        'https://api.opentalent.fr/api/public/organizations?_format=json&itemsPerPage=10&foo=1' => 'stub',
+        'https://api.opentalent.fr/api/public/organizations?_format=json&itemsPerPage=8' => 'stub',
+        'https://api.opentalent.fr/api/public/organizations?_format=json&filter%5Bwhere%5D%5Bid%5D=1&page=1&totalItems=1&itemsPerPage=8' => 'org-1',
+        'https://api.opentalent.fr/api/public/organizations?_format=json&filter%5Bwhere%5D%5Bid%5D=1&itemsPerPage=8' => 'org-1',
+        'https://api.opentalent.fr/api/public/events?_format=json&itemsPerPage=8&filter[where][id]=2' => 'event-2'
+    ];
+
+    public function get($url) {
+        if (array_key_exists($url, $this->map)) {
+            $query = $this->map[$url];
+            $response = new HtmlResponse(
+                $this->responses[$query],
+                200,
+                []
+            );
+            return $response;
+        } else {
+            return null;
+        }
+    }
+
+}