-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from pimlie/master
Added tests, removed html output when in console, added env variables for configuration and refactored library code
Showing
15 changed files
with
709 additions
and
317 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?xml version="1.0"?> | ||
<ruleset name="LaravelPrestashopWebservice"> | ||
|
||
<description>A basic coding standard.</description> | ||
|
||
<exclude-pattern>vendor/*</exclude-pattern> | ||
|
||
<rule ref="PSR1" /> | ||
<rule ref="PSR2" /> | ||
|
||
</ruleset> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Protechstudio\PrestashopWebService\Exceptions; | ||
|
||
use Protechstudio\PrestashopWebService\PrestaShopWebserviceException as PSWException; | ||
|
||
class PrestashopWebServiceException extends PSWException | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Protechstudio\PrestashopWebService\Exceptions; | ||
|
||
class PrestashopWebServiceRequestException extends PrestashopWebServiceException | ||
{ | ||
static protected $label = 'This call to PrestaShop Web Services failed and returned an HTTP status of %d. That means: %s.'; | ||
|
||
protected $response; | ||
|
||
public function __construct($message = null, $code = null, $response = null) | ||
{ | ||
parent::__construct(sprintf(static::$label, $code, $message), $code); | ||
|
||
$this->response = $response; | ||
} | ||
|
||
public function hasResponse() | ||
{ | ||
return isset($this->response) && !empty($this->response); | ||
} | ||
|
||
public function getResponse() | ||
{ | ||
return $this->response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,4 @@ | |
class PrestaShopWebserviceException extends \Exception | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
<?php | ||
|
||
return [ | ||
'url' => 'http://domain.com', | ||
'token' => '', | ||
'debug' => env('APP_DEBUG', false) | ||
]; | ||
'url' => env('PRESTASHOP_URL', 'http://domain.com'), | ||
'token' => env('PRESTASHOP_TOKEN', ''), | ||
'debug' => env('PRESTASHOP_DEBUG', env('APP_DEBUG', false)) | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,149 @@ | ||
<?php | ||
|
||
use Illuminate\Foundation\Testing\WithoutMiddleware; | ||
use Illuminate\Foundation\Testing\DatabaseMigrations; | ||
use Illuminate\Foundation\Testing\DatabaseTransactions; | ||
namespace Protechstudio\PrestashopWebService\Tests; | ||
|
||
use Protechstudio\PrestashopWebService\PrestashopWebService; | ||
use Protechstudio\PrestashopWebService\Exceptions\PrestashopWebServiceException; | ||
use Protechstudio\PrestashopWebService\Exceptions\PrestashopWebServiceRequestException; | ||
use Protechstudio\PrestashopWebService\PrestashopWebServiceLibrary; | ||
use Prestashop; | ||
|
||
class PrestashopWebServiceTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @test | ||
*/ | ||
/** @test */ | ||
public function it_is_correctly_installed() | ||
{ | ||
$this->assertInstanceOf(PrestashopWebService::class, Prestashop::getFacadeRoot()); | ||
} | ||
|
||
/** @test */ | ||
public function test_request_is_correct() | ||
{ | ||
$requestResponseStub = require(__DIR__.'/requests/category-schema.php'); | ||
|
||
list($header, $body) = explode("\n\n", $requestResponseStub[0], 2); | ||
$header_size = strlen($header) + 2; | ||
|
||
$this->assertEquals($header_size, $requestResponseStub[1]['header_size']); | ||
} | ||
|
||
/** @test */ | ||
public function it_can_perform_a_get_request() | ||
{ | ||
$requestResponseStub = require(__DIR__.'/requests/category-schema.php'); | ||
$ps = $this->getMockedLibrary('executeCurl', $requestResponseStub); | ||
|
||
$xml = $ps->get(['resource' => 'categories']); | ||
|
||
$this->assertEquals('prestashop', $xml->getName()); | ||
$this->assertEquals('category', $xml->children()[0]->getName()); | ||
} | ||
|
||
/** @test */ | ||
public function it_throws_exception_on_404() | ||
{ | ||
$ps = $this->getMockedLibrary('executeRequest', [ | ||
'status_code' => 404, | ||
'response' => '<?xml version="1.0" encoding="UTF-8"?> | ||
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink"> | ||
<errors> | ||
<error> | ||
<code><![CDATA[1]]></code> | ||
<message><![CDATA[Invalid ID]]></message> | ||
</error> | ||
</errors> | ||
</prestashop>', | ||
'header' => '' | ||
]); | ||
|
||
try { | ||
$xml = $ps->get(['resource' => 'categories']); | ||
} catch (PrestashopWebServiceRequestException $exception) { | ||
$this->assertEquals(404, $exception->getCode()); | ||
$this->assertTrue($exception->hasResponse()); | ||
$this->assertEquals('Invalid ID', (string)$exception->getResponse()->errors->error->message); | ||
} | ||
} | ||
|
||
/** @test */ | ||
public function it_throws_exception_on_unknown_http_status() | ||
{ | ||
$ps = $this->getMockedLibrary('executeRequest', [ | ||
'status_code' => 999, | ||
'response' => '<?xml version="1.0" encoding="UTF-8"?> | ||
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink"> | ||
</prestashop>', | ||
'header' => '' | ||
]); | ||
|
||
$this->expectExceptionMessage('unexpected HTTP status of: 999', PrestashopWebServiceException::class); | ||
$xml = $ps->get(['resource' => 'categories']); | ||
} | ||
|
||
/** @test */ | ||
public function it_throws_exception_on_empty_response() | ||
{ | ||
$ps = $this->getMockedLibrary('executeRequest', [ | ||
'status_code' => 200, | ||
'response' => '', | ||
'header' => '' | ||
]); | ||
|
||
$this->expectExceptionMessage('HTTP response is empty', PrestashopWebServiceException::class); | ||
$xml = $ps->get(['resource' => 'categories']); | ||
} | ||
|
||
/** @test */ | ||
public function it_throws_exception_on_malformed_xml() | ||
{ | ||
$ps = $this->getMockedLibrary('executeRequest', [ | ||
'status_code' => 200, | ||
'response' => '<malformed>', | ||
'header' => '' | ||
]); | ||
|
||
$this->expectExceptionMessage('HTTP XML response is not parsable', PrestashopWebServiceException::class); | ||
$xml = $ps->get(['resource' => 'categories']); | ||
} | ||
|
||
/** @test */ | ||
public function it_throws_exception_on_unsupported_version() | ||
{ | ||
$this->expectExceptionMessage('This library is not compatible with this version of PrestaShop', PrestashopWebServiceException::class); | ||
Prestashop::isPrestashopVersionSupported('0.0.0.0'); | ||
Prestashop::isPrestashopVersionSupported('99.99.99.9999'); | ||
} | ||
|
||
/** @test */ | ||
public function it_throws_exception_on_unsupported_version_from_request() | ||
{ | ||
$requestResponseStub = require(__DIR__.'/requests/category-schema.php'); | ||
$requestResponseStub[0] = preg_replace('/^PSWS-Version:(.+?)$/im', 'PSWS-Version: 99.99.99.9999', $requestResponseStub[0]); | ||
$ps = $this->getMockedLibrary('executeCurl', $requestResponseStub); | ||
|
||
$this->expectExceptionMessage('This library is not compatible with this version of PrestaShop', PrestashopWebServiceException::class); | ||
$xml = $ps->get(['resource' => 'categories']); | ||
} | ||
|
||
protected function getMockedLibrary($method = null, $returns = null) | ||
{ | ||
$ps = $this->getMockBuilder(PrestashopWebServiceLibrary::class) | ||
->setConstructorArgs([ | ||
env('prestashop-webservice.url'), | ||
env('prestashop-webservice.key'), | ||
env('prestashop-webservice.debug'), | ||
]); | ||
|
||
if (!$method) { | ||
return $ps->getMock(); | ||
} else { | ||
$ps = $ps->setMethods([$method])->getMock(); | ||
|
||
$ps->expects($this->once()) | ||
->method($method) | ||
->willReturn($returns); | ||
return $ps; | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Protechstudio\PrestashopWebService\Tests; | ||
|
||
abstract class TestCase extends \Orchestra\Testbench\TestCase | ||
{ | ||
/** | ||
* Get package providers. | ||
* | ||
* @param \Illuminate\Foundation\Application $app | ||
* @return array | ||
*/ | ||
protected function getPackageProviders($app) | ||
{ | ||
return [ | ||
\Protechstudio\PrestashopWebService\PrestashopWebServiceProvider::class, | ||
]; | ||
} | ||
/** | ||
* Define environment setup. | ||
* | ||
* @param Illuminate\Foundation\Application $app | ||
* @return void | ||
*/ | ||
protected function getEnvironmentSetUp($app) | ||
{ | ||
$app['config']->set('app.debug', true); | ||
$app['config']->set('prestashop-webservice', require('config/prestashop-webservice.php')); | ||
} | ||
|
||
protected function getPackageAliases($app) | ||
{ | ||
return [ | ||
'Prestashop' => \Protechstudio\PrestashopWebService\PrestashopWebServiceFacade::class, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
return [ | ||
'url' => 'http://example.com', | ||
'token' => '1234', | ||
'debug' => true | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
return [ | ||
'HTTP/1.1 200 OK | ||
Server: nginx | ||
Date: Fri, 26 Jan 2018 20:38:53 GMT | ||
Content-Type: text/xml;charset=utf-8 | ||
Content-Length: 825 | ||
Connection: keep-alive | ||
Vary: Accept-Encoding | ||
Access-Time: 1516999133 | ||
X-Powered-By: PrestaShop Webservice | ||
PSWS-Version: 1.7.2.4 | ||
Execution-Time: 0.006 | ||
Content-Sha1: 1234 | ||
Set-Cookie: PrestaShop-1234; expires=Thu, 15-Feb-2018 20:38:53 GMT; Max-Age=1728000; path=/; domain=example.com; HttpOnly | ||
Vary: Accept-Encoding | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink"> | ||
<category> | ||
<id></id> | ||
<id_parent></id_parent> | ||
<active></active> | ||
<id_shop_default></id_shop_default> | ||
<is_root_category></is_root_category> | ||
<position></position> | ||
<date_add></date_add> | ||
<date_upd></date_upd> | ||
<name><language id="1"></language></name> | ||
<link_rewrite><language id="1"></language></link_rewrite> | ||
<description><language id="1"></language></description> | ||
<meta_title><language id="1"></language></meta_title> | ||
<meta_description><language id="1"></language></meta_description> | ||
<meta_keywords><language id="1"></language></meta_keywords> | ||
<associations> | ||
<categories> | ||
<category> | ||
<id></id> | ||
</category> | ||
</categories> | ||
<products> | ||
<product> | ||
<id></id> | ||
</product> | ||
</products> | ||
</associations> | ||
</category> | ||
</prestashop>', | ||
|
||
json_decode('{"url":"http:\/\/example.com\/api\/categories?schema=blank","content_type":"text\/xml;charset=utf-8","http_code":200,"header_size":436,"request_size":155,"filetime":-1,"ssl_verify_result":0,"redirect_count":0,"total_time":0.149629,"namelookup_time":0.004162,"connect_time":0.025428,"pretransfer_time":0.025453,"size_upload":0,"size_download":825,"speed_download":5513,"speed_upload":0,"download_content_length":825,"upload_content_length":-1,"starttransfer_time":0.149337,"redirect_time":0,"redirect_url":"","primary_ip":"1.2.3.4","certinfo":[],"primary_port":80,"local_ip":"192.168.1.1","local_port":56568,"request_header":"GET \/api\/categories?schema=blank HTTP\/1.1\r\nHost: example.com\r\nAuthorization: Basic XXX\r\nAccept: *\/*\r\n\r\n"}', true), | ||
|
||
false | ||
]; |