Commit c5cabc34 authored by Patrick Barroca's avatar Patrick Barroca 😁
Browse files

dev #152400 : remove old electre api

parent 30d7d6b3
Pipeline #16792 passed with stage
in 38 seconds
<?php
namespace Pellicule\Providers;
use \Pellicule\Models\Record;
use \Fig\Http\Message\StatusCodeInterface;
class Electre extends Provider {
const
TOKEN_END_POINT = 'https://electre3staging-idp.bvdep.com',
API_END_POINT = 'https://electre3staging-api.bvdep.com',
ID = 'electre';
protected
$_client_id,
$_client_secret;
public function __construct($data = []) {
if (!array_key_exists('client_id', $data))
return;
$this->_client_id = $data['client_id'];
if (!array_key_exists('client_secret', $data))
return;
$this->_client_secret = $data['client_secret'];
}
public function providerName() {
return 'Electre';
}
public function fetchRecord() {
try {
$response = $this->_getToken($this->_client_id, $this->_client_secret);
} catch(\Exception $exception) {
return $this->_newError(StatusCodeInterface::STATUS_GATEWAY_TIMEOUT,
'no_answer_from_gateway');
}
if ($response->getStatusCode() == StatusCodeInterface::STATUS_BAD_REQUEST)
return $this->_newError(StatusCodeInterface::STATUS_UNAUTHORIZED,
'invalid_credentials');
if ($response->getStatusCode() != StatusCodeInterface::STATUS_OK)
return $this->_newError(StatusCodeInterface::STATUS_BAD_GATEWAY,
'provider_error');
$json_response = $this->_parseJsonResponse($response);
if (! $json_response)
return $this->_newError(StatusCodeInterface::STATUS_UNPROCESSABLE_ENTITY,
'cannot_parse_response');
if (!isset($json_response->access_token))
return $this->_newError(StatusCodeInterface::STATUS_NOT_ACCEPTABLE,
'no_expected_attribute_in_data');
$token = $json_response->access_token;
$this->setHeaders(['Authorization' => 'Bearer '. $token]);
try{
$response = $this->_getAllLinks($this->_getIsbnOrEan());
} catch(\Exception $exception) {
return $this->_newError(StatusCodeInterface::STATUS_GATEWAY_TIMEOUT,
'no_answer_from_gateway');
}
if ($response->getStatusCode() != StatusCodeInterface::STATUS_OK)
return $this->_newError(StatusCodeInterface::STATUS_NOT_FOUND,
'ean_not_found');
$json_response = $this->_parseJsonResponse($response);
if (!$json_response)
return $this->_newError(StatusCodeInterface::STATUS_UNPROCESSABLE_ENTITY,
'cannot_parse_response');
if (!isset($json_response->_links))
return $this->_newError(StatusCodeInterface::STATUS_NOT_ACCEPTABLE,
'no_expected_attribute_in_data');
$links = new \Storm\Model\Collection($json_response->_links);
$medialist =
$links->select(
function($element){
return in_array($element->rel, ['cover']);
})
->collect(function($link)
{
return (new \Pellicule\Models\Media())
->setProvider($this->providerName())
->setProviderHeaders($this->getHeaders())
->setUrl($link->href)
->setType($link->rel)
->setFullsizeFromIdentifier($this->_getIsbnOrEan());
});
$record = (new Record())
->updateAttributes($this->_search_args)
->setMedia($medialist);
return (new FetchRecordResult())->beSuccess($record);
}
protected function _getToken($id, $secret) {
$browser = static::newHttpClient();
$response = $browser->post(static::TOKEN_END_POINT . '/connect/token',
[],
http_build_query(['client_id' => $id,
'client_secret' => $secret,
'grant_type' => 'client_credentials',
'scope' => 'webapi']));
return $response;
}
public function _getAllLinks($ean) {
return static::newHttpClient()->get(static::API_END_POINT . '/v1.0/eans/' . $ean,
$this->getHeaders());
}
}
......@@ -13,7 +13,6 @@ class RemoteProviders {
public function __construct() {
$this->_providers = new Collection([ElectreNg::class,
ElectreRest2::class,
Electre::class,
Orb::class]);
}
......
......@@ -168,241 +168,6 @@ abstract class MediaWithElectreTestCase extends TestCase {
class MediaWithElectreOnIsbnTest extends MediaWithElectreTestCase {
public function setUp() {
parent::setUp();
$payload = base64_encode(json_encode(['provider'=> 'electre',
'client_secret' => 'leeloo',
'client_id' => 'multipass']));
$headers = ['Authorization' => ['Pellicule ' . $payload]];
$this->_response = $this->httpGet('/1.0/media/isbn/2259228194', $headers);
}
/** @test */
public function databaseShouldContainsRecordForIsbn2259228194() {
$this->assertNotNull(Record::findFirstBy(['isbn' => '2259228194']));
}
}
class MediaWithElectreOnEanTest extends MediaWithElectreTestCase {
public function setUp() {
parent::setUp();
$this->_response = $this->httpGet('/1.0/media/ean/2259228194',
['Authorization' => ['Pellicule ' . base64_encode(json_encode(['provider'=> 'electre',
'client_secret'=>'leeloo',
'client_id'=>'multipass']))]]);
}
/** @test */
public function databaseShouldContainsRecordForEan2259228194() {
$this->assertNotNull(Record::findFirstBy(['ean' => '2259228194']));
}
}
class MediaWithElectreWithSearchError extends TestCase {
public function setUp(){
parent::setUp();
Media::setFileSystem($this->mock()->whenCalled('fileExists')->answers(true));
$this->_http_client
->whenCalled('post')
->answers($this->_forgePSR7Response(['content' => '{"access_token":"MyPr3ttyT0k3n","expires_in":3600,"token_type":"Bearer"}']))
->whenCalled('get')
->answers($this->_forgePSR7Response(['status' => '404']));
$this->_response = $this->dispatch('/1.0/media/ean/123456789',
['Authorization' => ['Pellicule ' . base64_encode(json_encode(['provider'=> 'electre',
'client_secret'=>'leeloo',
'client_id'=>'multipass']))]]);
}
/** @test */
public function responseStatusShouldBe404() {
$this->assertEquals('404',$this->_response->getStatusCode());
}
/** @test */
public function responseShouldBeEmptyJSON() {
$this->_response->getBody()->rewind();
$this->assertEquals('{"error":"ean_not_found"}', $this->_response->getBody()->getContents());
}
/** @test */
public function databaseShouldNotContainsRecordForEan123456789() {
$this->assertNull(Record::findFirstBy(['ean' => '123456789']));
}
}
class MediaWithElectreWithInvalidCredentialsTest extends TestCase {
public function setUp(){
parent::setUp();
Media::setFileSystem($this->mock()->whenCalled('fileExists')->answers(true));
$this->_http_client
->whenCalled('post')
->answers($this->_forgePSR7Response(['status'=> '400', 'content' => '{"error": "invalid_client"}']));
$this->_response = $this->dispatch('/1.0/media/ean/123456789',
['Authorization' => ['Pellicule ' . base64_encode(json_encode(['provider'=> 'electre',
'client_secret'=>'1314',
'client_id'=>'multipass']))]]);
}
/** @test */
public function responseStatusShouldBe401() {
$this->assertEquals(401, $this->_response->getStatusCode());
}
/** @test */
public function responseBodyShouldContainsInvalidCredentials() {
$this->assertEquals('{"error":"invalid_credentials"}', $this->_response->getBody()->getContents());
}
}
class MediaWithElectreWithServiceInErrorTest extends TestCase {
/** @test */
public function withError500OnElectreResponseShouldHaveStatus502() {
$this->_http_client
->whenCalled('post')
->answers($this->_forgePSR7Response(['status'=>'500']));
$response = $this->dispatch('/1.0/media/ean/123456789',
['Authorization' => ['Pellicule ' . base64_encode(json_encode(['provider'=> 'electre',
'client_secret'=>'1314',
'client_id'=>'multipass']))]]);
$this->assertEquals(502, $response->getStatusCode());
return $response;
}
/**
* @depends withError500OnElectreResponseShouldHaveStatus502
* @test
*/
public function withError500OnElectreResponseShouldHaveErrorProviderError($response) {
$this->assertEquals('{"error":"provider_error"}', $response->getBody()->getContents());
}
}
class MediaWithElectreWithServiceInTimeOutTest extends TestCase {
public function setUp() {
parent::setUp();
$this->_http_client
->whenCalled('post')
->willDo(function() { throw new \Exception('timeout !!');});
}
/** @test */
public function withTimeOutOnElectreResponseShouldHaveStatus504() {
$response = $this->dispatch('/1.0/media/ean/123456789',
['Authorization' => ['Pellicule ' . base64_encode(json_encode(['provider'=> 'electre',
'client_secret'=>'1314',
'client_id'=>'multipass']))]]);
$this->assertEquals(504, $response->getStatusCode());
return $response;
}
/**
* @depends withTimeOutOnElectreResponseShouldHaveStatus504
* @test
*/
public function withTimeOutOnElectreResponseContentShouldHaveErrorNoAnswerFromGateway($response) {
$this->assertEquals('{"error":"no_answer_from_gateway"}', $response->getBody()->getContents());
}
}
class MediaWithElectreWithIncorrectJSONError extends TestCase {
public function setUp(){
parent::setUp();
$this->_http_client
->whenCalled('post')
->answers($this->_forgePSR7Response(["content" => '{"access_token":"MyPr3ttyT0k3n","expi}']));
$this->_response = $this->dispatch('/1.0/media/ean/123456789',
['Authorization' => ['Pellicule ' . base64_encode(json_encode(['provider'=> 'electre',
'client_secret'=>'leeloo',
'client_id'=>'multipass']))]]);
}
/** @test */
public function responseStatusShouldBe422() {
$this->assertEquals('422',$this->_response->getStatusCode());
}
/** @test */
public function responseJSONErrorShouldBeCannotParseResponse() {
$this->_response->getBody()->rewind();
$this->assertEquals('{"error":"cannot_parse_response"}', $this->_response->getBody()->getContents());
}
}
class MediaWithElectreWithNoAccessTokenError extends TestCase {
public function setUp(){
parent::setUp();
$this->_http_client
->whenCalled('post')
->answers($this->_forgePSR7Response(['content'=>'{"expires_in":3600}']));
$this->_response = $this->dispatch('/1.0/media/ean/123456789',
['Authorization' => ['Pellicule ' . base64_encode(json_encode(['provider'=> 'electre',
'client_secret'=>'leeloo',
'client_id'=>'multipass']))]]);
}
/** @test */
public function responseStatusShouldBe406() {
$this->assertEquals('406',$this->_response->getStatusCode());
}
/** @test */
public function responseJSONShouldBeErrorNoExpectedAttributeInData() {
$this->_response->getBody()->rewind();
$this->assertEquals('{"error":"no_expected_attribute_in_data"}', $this->_response->getBody()->getContents());
}
}
abstract class MediaWithOrbTestCase extends TestCase {
public function setUp(){
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment