Commit 1fabd1e5 authored by Laurent's avatar Laurent
Browse files

Adding Orb to External Providers

Changing Name from Decitre to Orb
Changing sql file to add expected columns
parent 485528fe
Pipeline #9192 passed with stage
in 1 minute and 20 seconds
......@@ -18,6 +18,9 @@ create table if not exists `media` (
`type` varchar(255) not null,
`url` varchar(255) not null,
`provider` varchar(255) not null,
`original` varchar(255) not null,
`thumbnail` varchar(255) not null,
`fullsize` varchar(255) not null,
`created_at` datetime not null,
`updated_at` datetime not null,
primary key (`id`),
......
......@@ -6,7 +6,7 @@ use Slim\Psr7\Request;
use Slim\Psr7\Response;
use Fig\Http\Message\StatusCodeInterface;
use Pellicule\Models\Record;
use Pellicule\Providers\Electre;
use Pellicule\Providers\Provider;
use Pellicule\Providers\FetchRecordResult;
......@@ -47,7 +47,8 @@ class Media {
if (!$credentials)
return (new FetchRecordResult())->beError(StatusCodeInterface::STATUS_PROXY_AUTHENTICATION_REQUIRED, 'no_valid_authentication_information_provided');
return (new Electre($credentials[0]))->fetchRecord($this->_processParams($args));
$provider = Provider::newProvider($credentials[0]);
return $provider->fetchRecord($this->_processParams($args));
}
......
......@@ -9,7 +9,7 @@ use \Slim\Psr7\Request;
use \Pellicule\Models\Record;
use \Fig\Http\Message\StatusCodeInterface;
class Decitre extends Provider {
class Orb extends Provider {
const
END_POINT = 'https://api.base-orb.fr';
......@@ -31,7 +31,7 @@ class Decitre extends Provider {
public function providerName() {
return 'Decitre';
return 'Orb';
}
......@@ -75,7 +75,8 @@ class Decitre extends Provider {
->setProvider($this->providerName())
->setType($imagetype_translate[$image_type])
->setUrl($images[$image_type]['original']['src'])
->setOriginal($images[$image_type]['original']['src']);
->setFullsize($images[$image_type]['original']['src'])
->setThumbnail($images[$image_type]['thumbnail']['src']);
}
$record = (new Record())
......@@ -86,10 +87,5 @@ class Decitre extends Provider {
}
protected function _getIsbnOrEan($search_args) {
if (isset($search_args['ean']))
return $search_args['ean'];
return $search_args['isbn'];
}
}
......@@ -29,8 +29,27 @@ abstract class Provider {
}
static public function newProvider($credentials) {
if ($credentials['provider'] == 'electre')
return new Electre($credentials);
if ($credentials['provider'] == 'orb')
return new Orb($credentials);
return null;
}
protected function _parseJsonResponse($response){
$response->getBody()->rewind();
return json_decode($response->getBody()->getContents());
}
protected function _getIsbnOrEan($search_args) {
if (isset($search_args['ean']))
return $search_args['ean'];
return $search_args['isbn'];
}
}
\ No newline at end of file
......@@ -6,6 +6,7 @@ use Pellicule\AppFactory;
use Pellicule\Models\Record;
use Pellicule\Models\Media;
use Pellicule\Providers\Electre;
use Pellicule\Providers\Orb;
class MediaGetTest extends TestCase {
......@@ -445,3 +446,62 @@ class MediaWithElectreWithNoAccessTokenError extends TestCase {
$this->assertEquals('{"error":"no_expected_attribute_in_data"}', $this->_response->getBody()->getContents());
}
}
abstract class MediaWithOrbTestCase extends TestCase {
public function setUp(){
parent::setUp();
$mock_browser = (new BrowserMockBuilder())
->expectGet(['url' => 'https://api.base-orb.fr/v1/products?sort=ean_asc&eans=2259228234',
'headers' => ['Authorization' => 'Basic '.base64_encode('leeloo:multipass'),
'Accept-Encoding'=> 'deflate, gzip']],
['content' => zlib_encode('{"data":[{"ean13":"2259228234","images":{"front":{"original":{"src":"https:\/\/products-images.di-static.com\/image\/orb-9162f416a0b7b2e64a1f5480db8d4394\/2259228234-475x500-1.jpg"},"thumbnail":{"src":"https:\/\/products-images.di-static.com\/image\/orb-9162f416a0b7b2e64a1f5480db8d4394\/2259228234-120x160-1.jpg"}}}}]}',ZLIB_ENCODING_GZIP)])
->getMock();
Orb::setDefaultHttpClient($mock_browser);
}
/** @test */
public function whenCalledWithOrbIdentifiersAndDataNotInCacheShouldReturnExpectedJSON() {
$return_data =
[ 'media' => [
['original'=> 'https://products-images.di-static.com/image/orb-9162f416a0b7b2e64a1f5480db8d4394/2259228234-475x500-1.jpg',
'fullsize' =>'https://products-images.di-static.com/image/orb-9162f416a0b7b2e64a1f5480db8d4394/2259228234-475x500-1.jpg',
'thumbnail' => 'https://products-images.di-static.com/image/orb-9162f416a0b7b2e64a1f5480db8d4394/2259228234-120x160-1.jpg',
'type' => 'cover']
]
];
$this->assertJSON(json_encode($return_data),
$this->_response
);
}
}
class MediaWithOrbOnIsbnTest extends MediaWithOrbTestCase {
public function setUp() {
parent::setUp();
$this->_response = $this->httpGet('/1.0/media/isbn/2259228234',
['Authorization' => ['Pellicule ' . base64_encode(json_encode(['provider'=> 'orb',
'username'=>'leeloo',
'secret'=>'multipass']))]]);
}
/** @test */
public function databaseShouldContainsRecordForIsbn2259228234() {
$this->assertNotNull(Record::findFirstBy(['isbn' => '2259228234']));
}
}
......@@ -5,10 +5,10 @@ namespace Pellicule\Tests;
use Pellicule\AppFactory;
use Pellicule\Models\Record;
use Pellicule\Models\Media;
use Pellicule\Providers\Decitre;
use Pellicule\Providers\Orb;
class DecitreGetTest extends TestCase {
class OrbGetTest extends TestCase {
public function setUp(){
parent::setUp();
......@@ -21,14 +21,14 @@ class DecitreGetTest extends TestCase {
->getMock()
->beStrict();
Decitre::setDefaultHttpClient($mock_browser);
Orb::setDefaultHttpClient($mock_browser);
$ident_request = ['username'=>'test',
'secret'=>'s3cr3t'
];
$search_request = ['ean'=>'9782212144383'];
((new Decitre($ident_request))->fetchRecord($search_request))->getRecord()->save();
((new Orb($ident_request))->fetchRecord($search_request))->getRecord()->save();
}
......
......@@ -7,7 +7,7 @@ use \Slim\Psr7\Request;
use \Slim\Psr7\Headers;
use \Storm\Test\THelpers;
class TestCase extends \PHPUnit\Framework\TestCase {
abstract class TestCase extends \PHPUnit\Framework\TestCase {
public function setUp() {
parent::setUp();
\Storm\Model\ModelAbstract::unsetLoaders();
......
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