Skip to content

Commit 96be025

Browse files
committed
Add tests
1 parent 9d5b402 commit 96be025

File tree

6 files changed

+206
-9
lines changed

6 files changed

+206
-9
lines changed

composer.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
"Protechstudio\\PrestashopWebService\\": "src/"
1414
}
1515
},
16+
"autoload-dev": {
17+
"psr-4": {
18+
"Protechstudio\\PrestashopWebService\\Tests\\": "tests/"
19+
}
20+
},
1621
"extra": {
1722
"laravel": {
1823
"providers": [
@@ -25,7 +30,7 @@
2530
},
2631
"require-dev": {
2732
"squizlabs/php_codesniffer": "^3.2",
28-
"phpunit/phpunit": "^6.5"
33+
"Orchestra/Testbench": "^3.5"
2934
},
3035
"scripts": {
3136
"test": "vendor/bin/phpunit",

phpunit.xml.dist

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit backupGlobals="false"
33
backupStaticAttributes="false"
4-
bootstrap="../../../bootstrap/autoload.php"
4+
bootstrap="vendor/autoload.php"
55
colors="true"
66
convertErrorsToExceptions="true"
77
convertNoticesToExceptions="true"
@@ -18,6 +18,9 @@
1818
<whitelist>
1919
<directory suffix=".php">app/</directory>
2020
</whitelist>
21+
<blacklist>
22+
<directory suffix=".php">./vendor</directory>
23+
</blacklist>
2124
</filter>
2225
<php>
2326
<env name="APP_ENV" value="testing"/>

tests/PrestashopWebServiceTest.php

+98-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,109 @@
11
<?php
22

3-
use Illuminate\Foundation\Testing\WithoutMiddleware;
4-
use Illuminate\Foundation\Testing\DatabaseMigrations;
5-
use Illuminate\Foundation\Testing\DatabaseTransactions;
3+
namespace Protechstudio\PrestashopWebService\Tests;
4+
65
use Protechstudio\PrestashopWebService\PrestashopWebService;
6+
use Protechstudio\PrestashopWebService\PrestashopWebServiceException;
7+
use Protechstudio\PrestashopWebService\PrestashopWebServiceLibrary;
8+
use Prestashop;
79

810
class PrestashopWebServiceTest extends TestCase
911
{
10-
11-
/**
12-
* @test
13-
*/
12+
/** @test */
1413
public function it_is_correctly_installed()
1514
{
1615
$this->assertInstanceOf(PrestashopWebService::class, Prestashop::getFacadeRoot());
1716
}
17+
18+
/** @test */
19+
public function it_can_perform_a_get_request()
20+
{
21+
$requestResponseStub = require('requests/category-schema.php');
22+
$ps = $this->getMockedLibrary('executeCurl', $requestResponseStub);
23+
24+
$xml = $ps->get(['resource' => 'categories']);
25+
26+
$this->assertEquals('prestashop', $xml->getName());
27+
$this->assertEquals('category', $xml->children()[0]->getName());
28+
}
29+
30+
/** @test */
31+
public function it_throws_exception_on_404()
32+
{
33+
$ps = $this->getMockedLibrary('executeRequest', [
34+
'status_code' => 404,
35+
'response' => '',
36+
'header' => ''
37+
]);
38+
39+
$this->expectException(PrestashopWebServiceException::class);
40+
$xml = $ps->get(['resource' => 'categories']);
41+
}
42+
43+
/** @test */
44+
public function it_throws_exception_on_empty_response()
45+
{
46+
$ps = $this->getMockedLibrary('executeRequest', [
47+
'status_code' => 200,
48+
'response' => '',
49+
'header' => ''
50+
]);
51+
52+
$this->expectExceptionMessage('HTTP response is empty', PrestashopWebServiceException::class);
53+
$xml = $ps->get(['resource' => 'categories']);
54+
}
55+
56+
/** @test */
57+
public function it_throws_exception_on_malformed_xml()
58+
{
59+
$ps = $this->getMockedLibrary('executeRequest', [
60+
'status_code' => 200,
61+
'response' => '<malformed>',
62+
'header' => ''
63+
]);
64+
65+
$this->expectExceptionMessage('HTTP XML response is not parsable', PrestashopWebServiceException::class);
66+
$xml = $ps->get(['resource' => 'categories']);
67+
}
68+
69+
/** @test */
70+
public function it_throws_exception_on_unsupported_version()
71+
{
72+
$this->expectExceptionMessage('This library is not compatible with this version of PrestaShop', PrestashopWebServiceException::class);
73+
Prestashop::isPrestashopVersionSupported('0.0.0.0');
74+
Prestashop::isPrestashopVersionSupported('99.99.99.9999');
75+
}
76+
77+
/** @test */
78+
public function it_throws_exception_on_unsupported_version_from_request()
79+
{
80+
$requestResponseStub = require('requests/category-schema.php');
81+
$requestResponseStub[0] = preg_replace('/^PSWS-Version:(.+?)$/im', 'PSWS-Version: 99.99.99.9999', $requestResponseStub[0]);
82+
$ps = $this->getMockedLibrary('executeCurl', $requestResponseStub);
83+
84+
$this->expectExceptionMessage('This library is not compatible with this version of PrestaShop', PrestashopWebServiceException::class);
85+
$xml = $ps->get(['resource' => 'categories']);
86+
}
87+
88+
protected function getMockedLibrary($method = null, $returns = null)
89+
{
90+
$ps = $this->getMockBuilder(PrestashopWebServiceLibrary::class)
91+
->setConstructorArgs([
92+
env('prestashop-webservice.url'),
93+
env('prestashop-webservice.key'),
94+
env('prestashop-webservice.debug'),
95+
]);
96+
97+
if (!$method) {
98+
return $ps->getMock();
99+
} else {
100+
$ps = $ps->setMethods([$method])->getMock();
101+
102+
$ps->expects($this->once())
103+
->method($method)
104+
->willReturn($returns);
105+
return $ps;
106+
}
107+
}
18108
}
109+

tests/TestCase.php

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Protechstudio\PrestashopWebService\Tests;
4+
5+
abstract class TestCase extends \Orchestra\Testbench\TestCase
6+
{
7+
/**
8+
* Get package providers.
9+
*
10+
* @param \Illuminate\Foundation\Application $app
11+
* @return array
12+
*/
13+
protected function getPackageProviders($app)
14+
{
15+
return [
16+
\Protechstudio\PrestashopWebService\PrestashopWebServiceProvider::class,
17+
];
18+
}
19+
/**
20+
* Define environment setup.
21+
*
22+
* @param Illuminate\Foundation\Application $app
23+
* @return void
24+
*/
25+
protected function getEnvironmentSetUp($app)
26+
{
27+
$app['config']->set('app.debug', true);
28+
$app['config']->set('prestashop-webservice', require('config/prestashop-webservice.php'));
29+
}
30+
31+
protected function getPackageAliases($app)
32+
{
33+
return [
34+
'Prestashop' => \Protechstudio\PrestashopWebService\PrestashopWebServiceFacade::class,
35+
];
36+
}
37+
}
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
return [
4+
'url' => 'http://example.com',
5+
'token' => '1234',
6+
'debug' => true
7+
];

tests/requests/category-schema.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
return [
4+
'HTTP/1.1 200 OK
5+
Server: nginx
6+
Date: Fri, 26 Jan 2018 20:38:53 GMT
7+
Content-Type: text/xml;charset=utf-8
8+
Content-Length: 825
9+
Connection: keep-alive
10+
Vary: Accept-Encoding
11+
Access-Time: 1516999133
12+
X-Powered-By: PrestaShop Webservice
13+
PSWS-Version: 1.7.2.4
14+
Execution-Time: 0.006
15+
Content-Sha1: 1234
16+
Set-Cookie: PrestaShop-1234; expires=Thu, 15-Feb-2018 20:38:53 GMT; Max-Age=1728000; path=/; domain=example.com; HttpOnly
17+
Vary: Accept-Encoding
18+
19+
<?xml version="1.0" encoding="UTF-8"?>
20+
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
21+
<category>
22+
<id></id>
23+
<id_parent></id_parent>
24+
<active></active>
25+
<id_shop_default></id_shop_default>
26+
<is_root_category></is_root_category>
27+
<position></position>
28+
<date_add></date_add>
29+
<date_upd></date_upd>
30+
<name><language id="1"></language></name>
31+
<link_rewrite><language id="1"></language></link_rewrite>
32+
<description><language id="1"></language></description>
33+
<meta_title><language id="1"></language></meta_title>
34+
<meta_description><language id="1"></language></meta_description>
35+
<meta_keywords><language id="1"></language></meta_keywords>
36+
<associations>
37+
<categories>
38+
<category>
39+
<id></id>
40+
</category>
41+
</categories>
42+
<products>
43+
<product>
44+
<id></id>
45+
</product>
46+
</products>
47+
</associations>
48+
</category>
49+
</prestashop>',
50+
51+
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),
52+
53+
false
54+
];

0 commit comments

Comments
 (0)