Skip to content

Commit 6be8846

Browse files
Merge pull request #20 from pimlie/master
Added tests, removed html output when in console, added env variables for configuration and refactored library code
2 parents 1da89ba + ccf3747 commit 6be8846

15 files changed

+709
-317
lines changed

composer.json

Lines changed: 14 additions & 2 deletions
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": [
@@ -22,6 +27,13 @@
2227
"Prestashop": "Protechstudio\\PrestashopWebService\\PrestashopWebServiceFacade"
2328
}
2429
}
25-
}
26-
30+
},
31+
"require-dev": {
32+
"squizlabs/php_codesniffer": "^3.2",
33+
"Orchestra/Testbench": "^3.5"
34+
},
35+
"scripts": {
36+
"test": "vendor/bin/phpunit",
37+
"cs": "vendor/bin/phpcs src/*"
38+
}
2739
}

phpcs.xml.dist

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="LaravelPrestashopWebservice">
3+
4+
<description>A basic coding standard.</description>
5+
6+
<exclude-pattern>vendor/*</exclude-pattern>
7+
8+
<rule ref="PSR1" />
9+
<rule ref="PSR2" />
10+
11+
</ruleset>

phpunit.xml renamed to phpunit.xml.dist

Lines changed: 4 additions & 1 deletion
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"/>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Protechstudio\PrestashopWebService\Exceptions;
4+
5+
use Protechstudio\PrestashopWebService\PrestaShopWebserviceException as PSWException;
6+
7+
class PrestashopWebServiceException extends PSWException
8+
{
9+
10+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Protechstudio\PrestashopWebService\Exceptions;
4+
5+
class PrestashopWebServiceRequestException extends PrestashopWebServiceException
6+
{
7+
static protected $label = 'This call to PrestaShop Web Services failed and returned an HTTP status of %d. That means: %s.';
8+
9+
protected $response;
10+
11+
public function __construct($message = null, $code = null, $response = null)
12+
{
13+
parent::__construct(sprintf(static::$label, $code, $message), $code);
14+
15+
$this->response = $response;
16+
}
17+
18+
public function hasResponse()
19+
{
20+
return isset($this->response) && !empty($this->response);
21+
}
22+
23+
public function getResponse()
24+
{
25+
return $this->response;
26+
}
27+
}

src/PrestaShopWebserviceException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
class PrestaShopWebserviceException extends \Exception
66
{
77

8-
}
8+
}

src/PrestashopWebService.php

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,36 @@ class PrestashopWebService extends PrestashopWebServiceLibrary
1515
* @return SimpleXMLElement
1616
* @throws PrestaShopWebserviceException
1717
*/
18-
public function getSchema($resource , $schema = 'blank')
18+
public function getSchema($resource, $schema = 'blank')
1919
{
2020
return $this->get(['resource' => $resource . "?schema=$schema"]);
2121
}
2222

2323
/**
24-
* Fill the provided schema with an associative array data, also remove the useless XML nodes if the corresponding flag is true
24+
* Fill the provided schema with an associative array data, also remove the useless XML nodes if
25+
* the corresponding flag is true
2526
*
2627
* @param SimpleXMLElement $xmlSchema
2728
* @param array $data
2829
* @param bool $removeUselessNodes set true if you want to remove nodes that are not present in the data array
29-
* @param array $removeSpecificNodes If $removeUselessNodes is false you may add here the first level nodes that you want to remove
30+
* @param array $removeSpecificNodes If $removeUselessNodes is false you may add here the first level nodes that
31+
* you want to remove
3032
* @return SimpleXMLElement
3133
*/
32-
public function fillSchema(SimpleXMLElement $xmlSchema, $data, $removeUselessNodes = true, $removeSpecificNodes=array())
33-
{
34+
public function fillSchema(
35+
SimpleXMLElement $xmlSchema,
36+
$data,
37+
$removeUselessNodes = true,
38+
$removeSpecificNodes = array()
39+
) {
3440
$resource = $xmlSchema->children()->children();
3541
foreach ($data as $key => $value) {
3642
$this->processNode($resource, $key, $value);
3743
}
3844
if ($removeUselessNodes) {
3945
$this->checkForUselessNodes($resource, $data);
40-
}
41-
else{
42-
$this->removeSpecificNodes($resource,$removeSpecificNodes);
46+
} else {
47+
$this->removeSpecificNodes($resource, $removeSpecificNodes);
4348
}
4449
return $xmlSchema;
4550
}
@@ -80,13 +85,12 @@ private function fillLanguageNode($node, $data)
8085
*/
8186
private function processNode(SimpleXMLElement $node, $dataKey, $dataValue)
8287
{
83-
if(is_int($dataKey)){
84-
if($dataKey===0){
88+
if (is_int($dataKey)) {
89+
if ($dataKey===0) {
8590
$this->emptyNode($node);
8691
}
87-
$this->createNode($node,$dataValue);
88-
}
89-
elseif (property_exists($node->$dataKey, 'language')) {
92+
$this->createNode($node, $dataValue);
93+
} elseif (property_exists($node->$dataKey, 'language')) {
9094
$this->fillLanguageNode($node->$dataKey, $dataValue);
9195
} elseif (is_array($dataValue)) {
9296
foreach ($dataValue as $key => $value) {
@@ -134,17 +138,15 @@ private function removeSpecificNodes($resource, $removeSpecificNodes)
134138
private function createNode(SimpleXMLElement $node, $dataValue)
135139
{
136140
foreach ($dataValue as $key => $value) {
137-
if(is_array($value)){
138-
if(is_int($key)){
139-
$this->createNode($node,$value);
140-
}
141-
else{
141+
if (is_array($value)) {
142+
if (is_int($key)) {
143+
$this->createNode($node, $value);
144+
} else {
142145
$childNode=$node->addChild($key);
143-
$this->createNode($childNode,$value);
146+
$this->createNode($childNode, $value);
144147
}
145-
}
146-
else{
147-
$node->addChild($key,$value);
148+
} else {
149+
$node->addChild($key, $value);
148150
}
149151
}
150152
}
@@ -162,4 +164,4 @@ private function emptyNode(SimpleXMLElement $node)
162164
unset($node->$nodeName);
163165
}
164166
}
165-
}
167+
}

src/PrestashopWebServiceFacade.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Protechstudio\PrestashopWebService;
44

5-
65
use Illuminate\Support\Facades\Facade;
76

87
class PrestashopWebServiceFacade extends Facade
@@ -12,5 +11,4 @@ protected static function getFacadeAccessor()
1211
{
1312
return PrestashopWebService::class;
1413
}
15-
16-
}
14+
}

0 commit comments

Comments
 (0)