Skip to content

Commit dbda45a

Browse files
committed
Added Unit tests
1 parent 634f9e0 commit dbda45a

14 files changed

Lines changed: 1081 additions & 2 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
composer.lock
22
vendor
33
.idea
4+
.phpunit.result.cache

composer.json

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,32 @@
1111
],
1212
"type": "library",
1313
"license": "MIT",
14-
"version": "1.0.2",
14+
"version": "1.0.3",
1515
"require": {
1616
"php": ">=7.4",
1717
"guzzlehttp/guzzle": "^6|^7",
1818
"tightenco/collect": "^8.83",
1919
"ext-json": "*",
2020
"ext-ctype": "*"
2121
},
22+
"require-dev": {
23+
"phpunit/phpunit": "^9.0"
24+
},
2225
"autoload": {
2326
"psr-4": {
2427
"MerchOne\\PhpApiSdk\\": "src/"
2528
}
2629
},
30+
"autoload-dev": {
31+
"psr-4": {
32+
"MerchOne\\PhpApiSdk\\Tests\\": "tests/"
33+
}
34+
},
2735
"authors": [
2836
{
2937
"name": "The Customization Group"
3038
}
3139
],
32-
"minimum-stability": "dev"
40+
"minimum-stability": "dev",
41+
"prefer-stable": true
3342
}

phpunit.xml.dist

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<phpunit
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
executionOrder="random"
7+
failOnWarning="true"
8+
failOnRisky="true"
9+
failOnEmptyTestSuite="true"
10+
beStrictAboutOutputDuringTests="true"
11+
verbose="true"
12+
>
13+
<testsuites>
14+
<testsuite name="Unit">
15+
<directory>tests/Unit</directory>
16+
</testsuite>
17+
</testsuites>
18+
</phpunit>

readme.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
PHP SDK for MerchOne API integration
33
</h2>
44

5+
<p align="center">
6+
<a href="https://packagist.org/packages/merch-one/php-api-sdk"><img src="https://img.shields.io/packagist/v/merch-one/php-api-sdk?color=orange&style=flat-square" alt="Packagist Version"></a>
7+
<a href="https://packagist.org/packages/merch-one/php-api-sdk"><img src="https://img.shields.io/packagist/l/merch-one/php-api-sdk?color=brightgreen&style=flat-square" alt="License"></a>
8+
<a href="https://packagist.org/packages/merch-one/php-api-sdk"><img src="https://img.shields.io/packagist/dependency-v/merch-one/php-api-sdk/php?style=flat-square" alt="Minimum PHP version"></a>
9+
<a href="https://packagist.org/packages/merch-one/php-api-sdk"><img src="https://img.shields.io/github/last-commit/merch-one/php-api-sdk?color=blue&style=flat-square" alt="GitHub last commit"></a>
10+
</p>
11+
512
This package provide a set of tools that allow developers to easily integrate with MerchOne API.
613

714
## Installation
@@ -112,3 +119,12 @@ The package can throw the following exceptions:
112119
| *MerchOneApiServerException* | A server error occurred. |
113120
| *InvalidApiVersionException* | An invalid API version was provided to the Client. |
114121
| *InvalidCredentialsException* | Invalid API credentials was provided to the Client. |
122+
123+
### Tests
124+
125+
Package comes with a set of tests to ensure that everything works as expected.
126+
To run tests, execute the following command:
127+
128+
```shell
129+
./vendor/bin/phpunit
130+
```

tests/ApiClientTestCase.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace MerchOne\PhpApiSdk\Tests;
4+
5+
use GuzzleHttp\Handler\MockHandler;
6+
use GuzzleHttp\HandlerStack;
7+
use GuzzleHttp\Psr7\Response;
8+
use MerchOne\PhpApiSdk\Contracts\Http\HttpClient;
9+
use MerchOne\PhpApiSdk\Http\Client;
10+
11+
abstract class ApiClientTestCase extends TestCase
12+
{
13+
/**
14+
* @var HttpClient|Client
15+
*/
16+
protected HttpClient $client;
17+
18+
/**
19+
* @return void
20+
*/
21+
public function setUp(): void
22+
{
23+
$this->client = new Client();
24+
}
25+
26+
/**
27+
* @param mixed $body
28+
* @param array $headers
29+
* @param int $statusCode
30+
* @return void
31+
* @noinspection PhpUnhandledExceptionInspection
32+
* @noinspection PhpDocMissingThrowsInspection
33+
*/
34+
protected function mockGuzzleClient($body = '', $headers = [], $statusCode = 200): void
35+
{
36+
if (is_array($body)) {
37+
$body = json_encode($body);
38+
}
39+
40+
$mock = new MockHandler([
41+
new Response($statusCode, $headers, $body),
42+
]);
43+
$handler = HandlerStack::create($mock);
44+
45+
$options = $this->getObjectProperty($this->client, 'clientOptions');
46+
$options['handler'] = $handler;
47+
48+
$guzzleClient = new \GuzzleHttp\Client($options);
49+
50+
$this->setObjectProperty($this->client, 'httpClient', $guzzleClient);
51+
}
52+
}

tests/TestCase.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace MerchOne\PhpApiSdk\Tests;
4+
5+
use PHPUnit\Framework\TestCase as BaseTestCase;
6+
use ReflectionClass;
7+
use ReflectionException;
8+
9+
abstract class TestCase extends BaseTestCase
10+
{
11+
/**
12+
* @param object $object
13+
* @param string $property
14+
* @return string|int|float|bool|array|object|null
15+
*
16+
* @throws ReflectionException
17+
*/
18+
protected function getObjectProperty(object $object, string $property)
19+
{
20+
$reflection = new ReflectionClass($object);
21+
$property = $reflection->getProperty($property);
22+
$property->setAccessible(true);
23+
24+
return $property->getValue($object);
25+
}
26+
27+
/**
28+
* @param object $object
29+
* @param string $method
30+
* @param array $arguments
31+
* @return string|int|float|bool|array|object|null
32+
*
33+
* @throws ReflectionException
34+
*/
35+
protected function callObjectMethod(object $object, string $method, array $arguments = [])
36+
{
37+
$reflection = new ReflectionClass($object);
38+
$method = $reflection->getMethod($method);
39+
$method->setAccessible(true);
40+
41+
return $method->invoke($object, ...$arguments);
42+
}
43+
44+
/**
45+
* @param object $object
46+
* @param string $property
47+
* @param $value
48+
* @return void
49+
*
50+
* @throws ReflectionException
51+
*/
52+
protected function setObjectProperty(object $object, string $property, $value): void
53+
{
54+
$reflection = new ReflectionClass($object);
55+
$property = $reflection->getProperty($property);
56+
$property->setAccessible(true);
57+
$property->setValue($object, $value);
58+
}
59+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace MerchOne\PhpApiSdk\Tests\Unit\Clients;
4+
5+
use MerchOne\PhpApiSdk\Exceptions\InvalidCredentialsException;
6+
use MerchOne\PhpApiSdk\Exceptions\MerchOneApiClientException;
7+
use MerchOne\PhpApiSdk\Tests\ApiClientTestCase;
8+
9+
class BaseApiClientTest extends ApiClientTestCase
10+
{
11+
/**
12+
* @return void
13+
*/
14+
public function testRightExceptionThrownWhenUnauthorized(): void
15+
{
16+
$this->expectException(InvalidCredentialsException::class);
17+
18+
$this->mockGuzzleClient([], [], 401);
19+
20+
$this->client->orders()->all();
21+
}
22+
23+
/**
24+
* @return void
25+
*/
26+
public function testRightExceptionThrownWhenValidationError(): void
27+
{
28+
$this->mockGuzzleClient([
29+
'message' => 'Bad Request',
30+
'errors' => [
31+
'email' => [
32+
'The email field is required.',
33+
],
34+
'name' => [
35+
'The name field is required.',
36+
],
37+
],
38+
], [], 422);
39+
40+
$this->expectException(MerchOneApiClientException::class);
41+
$this->expectExceptionMessage('The email field is required.|The name field is required.');
42+
43+
$this->client->orders()->create([]);
44+
}
45+
46+
/**
47+
* @return void
48+
*/
49+
public function testRightExceptionThrownWhenBadRequest(): void
50+
{
51+
$this->mockGuzzleClient(['message' => 'Bad Request'], [], 400);
52+
53+
$this->expectException(MerchOneApiClientException::class);
54+
$this->expectExceptionMessage('Bad Request');
55+
56+
$this->client->orders()->create([]);
57+
}
58+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace MerchOne\PhpApiSdk\Tests\Unit\Clients;
4+
5+
use MerchOne\PhpApiSdk\Tests\ApiClientTestCase;
6+
use MerchOne\PhpApiSdk\Tests\Unit\Clients\Helpers\CatalogApiResponse;
7+
use MerchOne\PhpApiSdk\Util\Data;
8+
9+
class CatalogApiTest extends ApiClientTestCase
10+
{
11+
/**
12+
* @return void
13+
*/
14+
public function testGetProductsReturnsProducts(): void
15+
{
16+
$this->mockGuzzleClient(CatalogApiResponse::GET_PRODUCTS);
17+
18+
$response = $this->client->catalog()->getProducts();
19+
20+
$this->assertInstanceOf(Data::class, $response);
21+
$this->assertEquals(
22+
CatalogApiResponse::GET_PRODUCTS['data'],
23+
$response->toArray()
24+
);
25+
}
26+
27+
/**
28+
* @return void
29+
*/
30+
public function testGetProductVariantsReturnVariants(): void
31+
{
32+
$this->mockGuzzleClient(CatalogApiResponse::GET_PRODUCT_VARIANTS);
33+
34+
$response = $this->client->catalog()->getProductVariants(1);
35+
36+
$this->assertEquals(
37+
CatalogApiResponse::GET_PRODUCT_VARIANTS['data'],
38+
$response->toArray()
39+
);
40+
}
41+
42+
/**
43+
* @return void
44+
*/
45+
public function testGetVariantOptionsReturnsOptions(): void
46+
{
47+
$this->mockGuzzleClient(CatalogApiResponse::GET_VARIANT_OPTIONS);
48+
49+
$response = $this->client->catalog()->getVariantOptions(1);
50+
51+
$this->assertEquals(
52+
CatalogApiResponse::GET_VARIANT_OPTIONS['data'],
53+
$response->toArray()
54+
);
55+
}
56+
57+
/**
58+
* @return void
59+
*/
60+
public function testGetVariantCombinationsReturnsCombinations(): void
61+
{
62+
$this->mockGuzzleClient(CatalogApiResponse::GET_VARIANT_COMBINATIONS);
63+
64+
$response = $this->client->catalog()->getVariantCombinations(1);
65+
66+
$this->assertEquals(
67+
CatalogApiResponse::GET_VARIANT_COMBINATIONS['data'],
68+
$response->toArray()
69+
);
70+
}
71+
}

0 commit comments

Comments
 (0)