Skip to content

Commit

Permalink
Merge pull request #2 from esign/feature/categories
Browse files Browse the repository at this point in the history
Added (sub)category/relationship Requests + tests
  • Loading branch information
jordyvanderhaegen authored Jun 24, 2024
2 parents d5b40a4 + ea9f124 commit e9afb8f
Show file tree
Hide file tree
Showing 27 changed files with 830 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/DataTransferObjects/ProductAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Esign\Plytix\DataTransferObjects;

use Spatie\LaravelData\Attributes\MapInputName;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\Mappers\SnakeCaseMapper;

#[MapInputName(SnakeCaseMapper::class)]
class ProductAttribute extends Data
{
public function __construct(
public readonly string $id,
public readonly ?array $groups,
public readonly ?string $label,
public readonly ?string $name,
public readonly ?string $typeClass
) {}
}
41 changes: 41 additions & 0 deletions src/Requests/AssignProductsToRelationshipRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Esign\Plytix\Requests;

use Esign\Plytix\DataTransferObjects\ProductCategory;
use Esign\Plytix\DataTransferObjects\RelationshipInformation;
use Saloon\Contracts\Body\HasBody;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\Traits\Body\HasJsonBody;

class AssignProductsToRelationshipRequest extends Request implements HasBody
{
use HasJsonBody;

protected Method $method = Method::POST;

public function __construct(
protected string $productId,
protected string $relationshipId,
protected array $payload
) { }

public function resolveEndpoint(): string
{
return '/api/v1/products/' . $this->productId . "/relationships/" . $this->relationshipId;
}

public function defaultBody(): array
{
return $this->payload;
}

public function createDtoFromResponse(Response $response): mixed
{
return array_map(function (array $relationshipInfo) {
return RelationshipInformation::from($relationshipInfo);
}, $response->json('data'));
}
}
38 changes: 38 additions & 0 deletions src/Requests/CreateAssetRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Esign\Plytix\Requests;

use Esign\Plytix\DataTransferObjects\Asset;
use Saloon\Contracts\Body\HasBody;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\Traits\Body\HasJsonBody;

class CreateAssetRequest extends Request implements HasBody
{
use HasJsonBody;

protected Method $method = Method::POST;

public function __construct(protected array $payload)
{
}

public function resolveEndpoint(): string
{
return '/api/v1/assets';
}

public function defaultBody(): array
{
return $this->payload;
}

public function createDtoFromResponse(Response $response): mixed
{
return array_map(function (array $product) {
return Asset::from($product);
}, $response->json('data'));
}
}
38 changes: 38 additions & 0 deletions src/Requests/CreateProductAttributeRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Esign\Plytix\Requests;

use Esign\Plytix\DataTransferObjects\ProductAttribute;
use Saloon\Contracts\Body\HasBody;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\Traits\Body\HasJsonBody;

class CreateProductAttributeRequest extends Request implements HasBody
{
use HasJsonBody;

protected Method $method = Method::POST;

public function __construct(protected array $payload)
{
}

public function resolveEndpoint(): string
{
return '/api/v1/attributes/product';
}

public function defaultBody(): array
{
return $this->payload;
}

public function createDtoFromResponse(Response $response): mixed
{
return array_map(function (array $attribute) {
return ProductAttribute::from($attribute);
}, $response->json('data'));
}
}
38 changes: 38 additions & 0 deletions src/Requests/CreateProductCategoryRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Esign\Plytix\Requests;

use Esign\Plytix\DataTransferObjects\ProductCategory;
use Saloon\Contracts\Body\HasBody;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\Traits\Body\HasJsonBody;

class CreateProductCategoryRequest extends Request implements HasBody
{
use HasJsonBody;

protected Method $method = Method::POST;

public function __construct(protected array $payload)
{
}

public function resolveEndpoint(): string
{
return '/api/v1/categories/product';
}

public function defaultBody(): array
{
return $this->payload;
}

public function createDtoFromResponse(Response $response): mixed
{
return array_map(function (array $productCategory) {
return ProductCategory::from($productCategory);
}, $response->json('data'));
}
}
9 changes: 9 additions & 0 deletions src/Requests/CreateProductRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Esign\Plytix\Requests;

use Esign\Plytix\DataTransferObjects\Product;
use Saloon\Contracts\Body\HasBody;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\Traits\Body\HasJsonBody;

class CreateProductRequest extends Request implements HasBody
Expand All @@ -26,4 +28,11 @@ public function defaultBody(): array
{
return $this->payload;
}

public function createDtoFromResponse(Response $response): mixed
{
return array_map(function (array $product) {
return Product::from($product);
}, $response->json('data'));
}
}
39 changes: 39 additions & 0 deletions src/Requests/CreateProductSubcategoryRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Esign\Plytix\Requests;

use Esign\Plytix\DataTransferObjects\ProductCategory;
use Saloon\Contracts\Body\HasBody;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\Traits\Body\HasJsonBody;

class CreateProductSubcategoryRequest extends Request implements HasBody
{
use HasJsonBody;

protected Method $method = Method::POST;

public function __construct(
protected string $categoryId,
protected array $payload
) { }

public function resolveEndpoint(): string
{
return '/api/v1/categories/product/' . $this->categoryId;
}

public function defaultBody(): array
{
return $this->payload;
}

public function createDtoFromResponse(Response $response): mixed
{
return array_map(function (array $productCategory) {
return ProductCategory::from($productCategory);
}, $response->json('data'));
}
}
38 changes: 38 additions & 0 deletions src/Requests/CreateRelationshipRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Esign\Plytix\Requests;

use Esign\Plytix\DataTransferObjects\Relationship;
use Saloon\Contracts\Body\HasBody;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\Traits\Body\HasJsonBody;

class CreateRelationshipRequest extends Request implements HasBody
{
use HasJsonBody;

protected Method $method = Method::POST;

public function __construct(protected array $payload)
{
}

public function resolveEndpoint(): string
{
return '/api/v1/relationships';
}

public function defaultBody(): array
{
return $this->payload;
}

public function createDtoFromResponse(Response $response): mixed
{
return array_map(function (array $relationship) {
return Relationship::from($relationship);
}, $response->json('data'));
}
}
39 changes: 39 additions & 0 deletions src/Requests/LinkAssetToProductRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Esign\Plytix\Requests;

use Esign\Plytix\DataTransferObjects\Asset;
use Saloon\Contracts\Body\HasBody;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\Traits\Body\HasJsonBody;

class LinkAssetToProductRequest extends Request implements HasBody
{
use HasJsonBody;

protected Method $method = Method::POST;

public function __construct(
protected string $productId,
protected array $payload
) { }

public function resolveEndpoint(): string
{
return '/api/v1/products/' . $this->productId . "/assets";
}

public function defaultBody(): array
{
return $this->payload;
}

public function createDtoFromResponse(Response $response): mixed
{
return array_map(function (array $asset) {
return Asset::from($asset);
}, $response->json('data'));
}
}
39 changes: 39 additions & 0 deletions src/Requests/ModifySelectedProductAttributesRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Esign\Plytix\Requests;

use Esign\Plytix\DataTransferObjects\Product;
use Saloon\Contracts\Body\HasBody;
use Saloon\Enums\Method;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\Traits\Body\HasJsonBody;

class ModifySelectedProductAttributesRequest extends Request implements HasBody
{
use HasJsonBody;

protected Method $method = Method::PATCH;

public function __construct(
protected string $productId,
protected array $payload
) { }

public function resolveEndpoint(): string
{
return '/api/v1/products/' . $this->productId;
}

public function defaultBody(): array
{
return $this->payload;
}

public function createDtoFromResponse(Response $response): mixed
{
return array_map(function (array $product) {
return Product::from($product);
}, $response->json('data'));
}
}
35 changes: 35 additions & 0 deletions tests/Feature/Requests/AssignProductsToRelationshipRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Esign\Plytix\Tests\Feature\Requests;

use Esign\Plytix\Plytix;
use Esign\Plytix\Requests\AssignProductsToRelationshipRequest;
use Esign\Plytix\Tests\Support\MockResponseFixture;
use Esign\Plytix\Tests\TestCase;
use Saloon\Http\Faking\MockClient;

class AssignProductsToRelationshipRequestTest extends TestCase
{
/** @test */
public function it_can_assign_products_to_relationship()
{
$plytix = new Plytix();
$mockClient = MockClient::global([
MockResponseFixture::make(fixtureName: 'token.json', status: 200),
MockResponseFixture::make(fixtureName: 'assign-products-to-relationship.json', status: 201),
]);

$response = $plytix->send(new AssignProductsToRelationshipRequest('productid', 'relationshipid',[
'name' => '12345'
]));

$relationshipInformation = $response->dto()[0];

$mockClient->assertSent(AssignProductsToRelationshipRequest::class);
$this->assertEquals('5d8a50b547397aea2a603079', $relationshipInformation->relationshipId);
$this->assertEquals('contains', $relationshipInformation->relationshipLabel);
$this->assertIsArray($relationshipInformation->relatedProducts);
$this->assertCount(4, $relationshipInformation->relatedProducts);
$this->assertEquals(9, $relationshipInformation->relatedProducts[1]->quantity);
}
}
Loading

0 comments on commit e9afb8f

Please sign in to comment.