Skip to content

Commit

Permalink
100% coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
keithbrink committed Apr 18, 2024
1 parent 1b7e04b commit 4a7ec2f
Show file tree
Hide file tree
Showing 19 changed files with 358 additions and 116 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ jobs:
- name: Install dependencies
uses: php-actions/composer@v6
with:
php_version: "8.3"
php_version: "8.2"
- name: PHPUnit Tests
uses: php-actions/phpunit@v3
with:
php_version: "8.3"
php_version: "8.2"
php_extensions: pcov
bootstrap: vendor/autoload.php
configuration: phpunit.xml
Expand Down
6 changes: 4 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" colors="true" processIsolation="false" testdox="true" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" colors="true" processIsolation="false" testdox="true" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false" displayDetailsOnTestsThatTriggerWarnings="true" displayDetailsOnTestsThatTriggerDeprecations="true">
<coverage>
<report>
<clover outputFile="tests/resources/clover.xml"/>
Expand All @@ -22,7 +22,9 @@
<directory suffix=".php">./src</directory>
</include>
<exclude>
<directory suffix="ListSchema.php">./src/Data</directory>
<directory suffix=".php">./src/Data/Requests</directory>
<directory suffix=".php">./src/Data/Responses</directory>
<directory suffix=".php">./src/Data/Schemas</directory>
</exclude>
</source>
</phpunit>
14 changes: 7 additions & 7 deletions src/Data/Base/DataBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,19 @@ private function getParameterValue(
}

if ($parameter->getType() instanceof \ReflectionUnionType) {
foreach ($parameter->getType()->getTypes() as $type) {
if ($type->getName() === 'null') {
continue;
}

$types = array_filter(
$parameter->getType()->getTypes(),
fn ($type) => $type->getName() !== 'null',
);
foreach ($types as $type) {
try {
return self::getValueFromNamedType($type, $payload_value);
} catch (\InvalidArgumentException) {
} catch (\Throwable) {
continue;
}
}

throw new \InvalidArgumentException("Unsupported parameter union for: {$parameter->getName()}");
throw new \InvalidArgumentException("Unsupported parameter union for: {$parameter->getName()}"); // @codeCoverageIgnore
}

return self::getValueFromNamedType($parameter->getType()->getName(), $payload_value);
Expand Down
22 changes: 0 additions & 22 deletions src/Data/Base/DataValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,9 @@ public function validate(object $data): void
$reflection = new \ReflectionClass($data);

foreach ($reflection->getProperties() as $property) {
if (! $property->isInitialized($data)) {
continue;
}

$property->setAccessible(true);
$property_value = $property->getValue($data);

// If is iterable, validate all items that are data objects
if (is_iterable($property_value)) {
foreach ($property_value as $item) {
if ($item instanceof Data) {
$this->validate($item);
}
}

continue;
}

// If is data object, validate it recursively
if ($property_value instanceof Data) {
$this->validate($property_value);

continue;
}

foreach ($this->getValidators($property) as $validator) {
try {
$validator->validate($property_value);
Expand Down
14 changes: 0 additions & 14 deletions src/Data/Base/Validators/DataValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,4 @@

class DataValidationException extends \Exception
{
public static function create(
string $class,
string $property,
mixed $value,
?string $message = null,
): self {
$exception_message = json_encode($value)." is not a valid value for $class::$property.";

if ($message) {
$exception_message .= " $message";
}

return new self($exception_message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Carbon\CarbonImmutable;
use Jasara\AmznSPA\Constants\AmazonEnums;
use Jasara\AmznSPA\Data\Base\Casts\CarbonFromStringCaster;
use Jasara\AmznSPA\Data\Base\Casts\EmptyArrayToNullCaster;
use Jasara\AmznSPA\Data\Base\Validators\StringEnumValidator;
use Jasara\AmznSPA\Data\Schemas\AddressSchema;
use Jasara\AmznSPA\Data\Schemas\BaseSchema;
Expand All @@ -15,7 +14,6 @@ class InboundShipmentInfoSchema extends BaseSchema
public function __construct(
public ?string $shipment_id,
public ?string $shipment_name,
#[EmptyArrayToNullCaster]
public ?AddressSchema $ship_from_address,
public ?string $destination_fulfillment_center_id,
#[StringEnumValidator(AmazonEnums::SHIPMENT_STATUSES)]
Expand Down
14 changes: 0 additions & 14 deletions src/Resources/FulfillmentInboundResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Jasara\AmznSPA\Resources;

use Carbon\CarbonImmutable;
use Illuminate\Support\Arr;
use Jasara\AmznSPA\AmznSPAHttp;
use Jasara\AmznSPA\Constants\AmazonEnums;
use Jasara\AmznSPA\Constants\MarketplacesList;
Expand Down Expand Up @@ -239,19 +238,6 @@ public function getShipments(
return $response;
}

private function setEmptyShipFromAddressToNull(array $response): array
{
$response['payload']['shipment_data'] = array_map(function (array $shipment_data) {
if (Arr::get($shipment_data, 'ship_from_address') === []) {
$shipment_data['ship_from_address'] = null;
}

return $shipment_data;
}, $response['payload']['shipment_data']);

return $response;
}

public function getShipmentItemsByShipmentId(string $shipment_id, string $marketplace_id): GetShipmentItemsResponse
{
$this->validateStringEnum($marketplace_id, MarketplacesList::allIdentifiers());
Expand Down
5 changes: 0 additions & 5 deletions src/Resources/OrdersResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Jasara\AmznSPA\Resources;

use Carbon\CarbonImmutable;
use Illuminate\Support\Arr;
use Jasara\AmznSPA\AmznSPAHttp;
use Jasara\AmznSPA\Constants\MarketplacesList;
use Jasara\AmznSPA\Contracts\ResourceContract;
Expand Down Expand Up @@ -91,10 +90,6 @@ public function getOrder(string $order_id): GetOrderResponse
->responseClass(GetOrderResponse::class)
->get($this->endpoint . self::BASE_PATH . 'orders/' . $order_id);

if (Arr::get($response, 'payload') === []) {
$response['payload'] = null;
}

return $response;
}

Expand Down
1 change: 1 addition & 0 deletions tests/Setup/SetupAmznSPAConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function setupLiveConfig(): AmznSPAConfig
return $config;
}

/** @return array{0: AmznSPAConfig, 1: Factory} */
public function setupConfigWithFakeHttp(string | array $stubs, int $status_code = 200): array
{
if (is_string($stubs)) {
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/AmznSPAConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\HttpKernel\Log\Logger;

#[CoversClass(AmznSPAConfig::class)]
#[CoversClass(ApplicationKeys::class)]
class AmznSPAConfigTest extends UnitTestCase
{
public function testGetNewConfig()
Expand Down
18 changes: 18 additions & 0 deletions tests/Unit/AmznSPAHttpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -535,4 +535,22 @@ public function testResponseCallbackIsCalled()
$amzn = new AmznSPA($config);
$amzn->fulfillment_outbound->cancelFulfillmentOrder('some-order-id');
}

public function testCannotSetResponseThatIsNotBaseResponse()
{
$this->expectExceptionMessage('Response class must extend BaseResponse');

$http = new AmznSPAHttp($this->setupMinimalConfig());
$http->responseClass('stdClass');
}

public function testReturnsArrayWithNoResponseClassSet()
{
[$config] = $this->setupConfigWithFakeHttp('reports/get-report-document');

$http = new AmznSPAHttp($config);
$response = $http->get($config->getMarketplace()->getBaseUrl() . '/orders/v0/orders');

$this->assertIsArray($response);
}
}
47 changes: 0 additions & 47 deletions tests/Unit/Data/AuthTokensTest.php

This file was deleted.

72 changes: 72 additions & 0 deletions tests/Unit/Data/Base/DataBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
namespace Jasara\AmznSPA\Tests\Unit\Data\Base;

use Carbon\CarbonImmutable;
use Jasara\AmznSPA\Contracts\IsFlatResponse;
use Jasara\AmznSPA\Data\Base\Data;
use Jasara\AmznSPA\Data\Base\DataBuilder;
use Jasara\AmznSPA\Data\Responses\CatalogItems\v20201201\GetCatalogItemResponse;
use Jasara\AmznSPA\Data\Responses\FulfillmentOutbound\CreateFulfillmentOrderResponse;
use Jasara\AmznSPA\Data\Responses\Tokens\CreateRestrictedDataTokenResponse;
use Jasara\AmznSPA\Data\Responses\Uploads\CreateUploadDestinationResponse;
use Jasara\AmznSPA\Data\Schemas\AddressSchema;
use Jasara\AmznSPA\Data\Schemas\FulfillmentInbound\InboundShipmentInfoSchema;
use Jasara\AmznSPA\Data\Schemas\FulfillmentInbound\NonPartneredSmallParcelDataOutputSchema;
use Jasara\AmznSPA\Data\Schemas\FulfillmentInbound\NonPartneredSmallParcelPackageOutputListSchema;
Expand Down Expand Up @@ -78,6 +82,15 @@ public function testBuildDataWithCollectionLikeSchema(): void
$this->assertEquals('IN_TRANSIT', $data->package_list[1]->package_status);
}

public function testBuildDataFailsIfInvalidDataSentToCollectionParameter(): void
{
$this->expectExceptionMessage('Expected iterable for collection parameter');

NonPartneredSmallParcelDataOutputSchema::from([
'package_list' => 'INVALID',
]);
}

public function testBuildDataWithNestedData(): void
{
$data = CreateUploadDestinationResponse::from([
Expand Down Expand Up @@ -111,6 +124,17 @@ public function testBuildsDataWithUnionType(): void
$this->assertLessThanOrEqual(3600, $data->expires_in->diffInSeconds());
}

public function testBuildsDataWithInvalidData(): void
{
$this->expectExceptionMessage('Missing required parameter: name');

AddressSchema::from([
'address_line_1' => 'address_line_1',
'country_code' => 'CA',
'postal_code' => 'postal_code',
]);
}

public function testBuildsDataWithCarbonCaster(): void
{
$data = PartneredLtlDataInputSchema::from([
Expand Down Expand Up @@ -144,4 +168,52 @@ public function testBuildsDataWithEmptyArrayNotNullableDoesNotMapToNull(): void
$this->assertInstanceOf(FeesEstimateErrorSchema::class, $data);
$this->assertIsArray($data->detail);
}

public function testBuildDataWithNoParameters(): void
{
$data = CreateFulfillmentOrderResponse::from();

$this->assertInstanceOf(CreateFulfillmentOrderResponse::class, $data);
}

public function testBuildDataWithFlatResponse(): void
{
$data = GetCatalogItemResponse::from([
'asin' => 'asin',
]);

$this->assertInstanceOf(GetCatalogItemResponse::class, $data);
$this->assertEquals('asin', $data->item->asin);
}

public function testBuildDataWithEmptyFlatResponseIsIsError(): void
{
$data = GetCatalogItemResponse::from([
'errors' => [],
]);

$this->assertInstanceOf(GetCatalogItemResponse::class, $data);
$this->assertNull($data->item);
}

public function testBuildDataWithFlatResponseFailsIfParameterDoesntExist(): void
{
$this->expectExceptionMessage('Missing required parameter: invalid');

$class = new class('test') extends Data implements IsFlatResponse {
public function __construct(
public string $not_matching,
) {
}

public static function mapResponseToParameter(): string
{
return 'invalid';
}
};

$class::from([
'invalid' => 'invalid',
]);
}
}
Loading

0 comments on commit 4a7ec2f

Please sign in to comment.