Skip to content

Commit

Permalink
Added Unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
allanvb committed Apr 5, 2023
1 parent 634f9e0 commit dbda45a
Show file tree
Hide file tree
Showing 14 changed files with 1,081 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
composer.lock
vendor
.idea
.phpunit.result.cache
13 changes: 11 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,32 @@
],
"type": "library",
"license": "MIT",
"version": "1.0.2",
"version": "1.0.3",
"require": {
"php": ">=7.4",
"guzzlehttp/guzzle": "^6|^7",
"tightenco/collect": "^8.83",
"ext-json": "*",
"ext-ctype": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.0"
},
"autoload": {
"psr-4": {
"MerchOne\\PhpApiSdk\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"MerchOne\\PhpApiSdk\\Tests\\": "tests/"
}
},
"authors": [
{
"name": "The Customization Group"
}
],
"minimum-stability": "dev"
"minimum-stability": "dev",
"prefer-stable": true
}
18 changes: 18 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<phpunit
xmlns:xsi="http:https://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
executionOrder="random"
failOnWarning="true"
failOnRisky="true"
failOnEmptyTestSuite="true"
beStrictAboutOutputDuringTests="true"
verbose="true"
>
<testsuites>
<testsuite name="Unit">
<directory>tests/Unit</directory>
</testsuite>
</testsuites>
</phpunit>
16 changes: 16 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
PHP SDK for MerchOne API integration
</h2>

<p align="center">
<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>
<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>
<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>
<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>
</p>

This package provide a set of tools that allow developers to easily integrate with MerchOne API.

## Installation
Expand Down Expand Up @@ -112,3 +119,12 @@ The package can throw the following exceptions:
| *MerchOneApiServerException* | A server error occurred. |
| *InvalidApiVersionException* | An invalid API version was provided to the Client. |
| *InvalidCredentialsException* | Invalid API credentials was provided to the Client. |

### Tests

Package comes with a set of tests to ensure that everything works as expected.
To run tests, execute the following command:

```shell
./vendor/bin/phpunit
```
52 changes: 52 additions & 0 deletions tests/ApiClientTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace MerchOne\PhpApiSdk\Tests;

use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use MerchOne\PhpApiSdk\Contracts\Http\HttpClient;
use MerchOne\PhpApiSdk\Http\Client;

abstract class ApiClientTestCase extends TestCase
{
/**
* @var HttpClient|Client
*/
protected HttpClient $client;

/**
* @return void
*/
public function setUp(): void
{
$this->client = new Client();
}

/**
* @param mixed $body
* @param array $headers
* @param int $statusCode
* @return void
* @noinspection PhpUnhandledExceptionInspection
* @noinspection PhpDocMissingThrowsInspection
*/
protected function mockGuzzleClient($body = '', $headers = [], $statusCode = 200): void
{
if (is_array($body)) {
$body = json_encode($body);
}

$mock = new MockHandler([
new Response($statusCode, $headers, $body),
]);
$handler = HandlerStack::create($mock);

$options = $this->getObjectProperty($this->client, 'clientOptions');
$options['handler'] = $handler;

$guzzleClient = new \GuzzleHttp\Client($options);

$this->setObjectProperty($this->client, 'httpClient', $guzzleClient);
}
}
59 changes: 59 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace MerchOne\PhpApiSdk\Tests;

use PHPUnit\Framework\TestCase as BaseTestCase;
use ReflectionClass;
use ReflectionException;

abstract class TestCase extends BaseTestCase
{
/**
* @param object $object
* @param string $property
* @return string|int|float|bool|array|object|null
*
* @throws ReflectionException
*/
protected function getObjectProperty(object $object, string $property)
{
$reflection = new ReflectionClass($object);
$property = $reflection->getProperty($property);
$property->setAccessible(true);

return $property->getValue($object);
}

/**
* @param object $object
* @param string $method
* @param array $arguments
* @return string|int|float|bool|array|object|null
*
* @throws ReflectionException
*/
protected function callObjectMethod(object $object, string $method, array $arguments = [])
{
$reflection = new ReflectionClass($object);
$method = $reflection->getMethod($method);
$method->setAccessible(true);

return $method->invoke($object, ...$arguments);
}

/**
* @param object $object
* @param string $property
* @param $value
* @return void
*
* @throws ReflectionException
*/
protected function setObjectProperty(object $object, string $property, $value): void
{
$reflection = new ReflectionClass($object);
$property = $reflection->getProperty($property);
$property->setAccessible(true);
$property->setValue($object, $value);
}
}
58 changes: 58 additions & 0 deletions tests/Unit/Clients/BaseApiClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace MerchOne\PhpApiSdk\Tests\Unit\Clients;

use MerchOne\PhpApiSdk\Exceptions\InvalidCredentialsException;
use MerchOne\PhpApiSdk\Exceptions\MerchOneApiClientException;
use MerchOne\PhpApiSdk\Tests\ApiClientTestCase;

class BaseApiClientTest extends ApiClientTestCase
{
/**
* @return void
*/
public function testRightExceptionThrownWhenUnauthorized(): void
{
$this->expectException(InvalidCredentialsException::class);

$this->mockGuzzleClient([], [], 401);

$this->client->orders()->all();
}

/**
* @return void
*/
public function testRightExceptionThrownWhenValidationError(): void
{
$this->mockGuzzleClient([
'message' => 'Bad Request',
'errors' => [
'email' => [
'The email field is required.',
],
'name' => [
'The name field is required.',
],
],
], [], 422);

$this->expectException(MerchOneApiClientException::class);
$this->expectExceptionMessage('The email field is required.|The name field is required.');

$this->client->orders()->create([]);
}

/**
* @return void
*/
public function testRightExceptionThrownWhenBadRequest(): void
{
$this->mockGuzzleClient(['message' => 'Bad Request'], [], 400);

$this->expectException(MerchOneApiClientException::class);
$this->expectExceptionMessage('Bad Request');

$this->client->orders()->create([]);
}
}
71 changes: 71 additions & 0 deletions tests/Unit/Clients/CatalogApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace MerchOne\PhpApiSdk\Tests\Unit\Clients;

use MerchOne\PhpApiSdk\Tests\ApiClientTestCase;
use MerchOne\PhpApiSdk\Tests\Unit\Clients\Helpers\CatalogApiResponse;
use MerchOne\PhpApiSdk\Util\Data;

class CatalogApiTest extends ApiClientTestCase
{
/**
* @return void
*/
public function testGetProductsReturnsProducts(): void
{
$this->mockGuzzleClient(CatalogApiResponse::GET_PRODUCTS);

$response = $this->client->catalog()->getProducts();

$this->assertInstanceOf(Data::class, $response);
$this->assertEquals(
CatalogApiResponse::GET_PRODUCTS['data'],
$response->toArray()
);
}

/**
* @return void
*/
public function testGetProductVariantsReturnVariants(): void
{
$this->mockGuzzleClient(CatalogApiResponse::GET_PRODUCT_VARIANTS);

$response = $this->client->catalog()->getProductVariants(1);

$this->assertEquals(
CatalogApiResponse::GET_PRODUCT_VARIANTS['data'],
$response->toArray()
);
}

/**
* @return void
*/
public function testGetVariantOptionsReturnsOptions(): void
{
$this->mockGuzzleClient(CatalogApiResponse::GET_VARIANT_OPTIONS);

$response = $this->client->catalog()->getVariantOptions(1);

$this->assertEquals(
CatalogApiResponse::GET_VARIANT_OPTIONS['data'],
$response->toArray()
);
}

/**
* @return void
*/
public function testGetVariantCombinationsReturnsCombinations(): void
{
$this->mockGuzzleClient(CatalogApiResponse::GET_VARIANT_COMBINATIONS);

$response = $this->client->catalog()->getVariantCombinations(1);

$this->assertEquals(
CatalogApiResponse::GET_VARIANT_COMBINATIONS['data'],
$response->toArray()
);
}
}
Loading

0 comments on commit dbda45a

Please sign in to comment.