-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
1,081 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
composer.lock | ||
vendor | ||
.idea | ||
.phpunit.result.cache |
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,18 @@ | ||
<phpunit | ||
xmlns:xsi="http://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> |
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,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); | ||
} | ||
} |
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,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); | ||
} | ||
} |
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,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([]); | ||
} | ||
} |
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,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() | ||
); | ||
} | ||
} |
Oops, something went wrong.