Skip to content

Commit

Permalink
Create Interface IzettlePostable
Browse files Browse the repository at this point in the history
To make the Post functionality easier create an interface so we can implement this interface and post complete objects instead of a JSON string
  • Loading branch information
LauLaman committed Nov 16, 2017
1 parent 0c42ba9 commit ae0addd
Show file tree
Hide file tree
Showing 19 changed files with 67 additions and 41 deletions.
6 changes: 4 additions & 2 deletions src/API/Product/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
namespace LauLamanApps\IzettleApi\API\Product;

use DateTime;
use LauLamanApps\IzettleApi\API\Universal\IzettlePostable;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;

final class Category
final class Category implements IzettlePostable
{
private $uuid;
private $name;
Expand Down Expand Up @@ -73,7 +74,8 @@ public function getCreatedAt(): ?DateTime
return $this->createdAt;
}

public function getCreateData(): string

public function getPostBodyData(): string
{
$data = [
'uuid' => $this->uuid,
Expand Down
5 changes: 3 additions & 2 deletions src/API/Product/Discount.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

use DateTime;
use LauLamanApps\IzettleApi\API\ImageCollection;
use LauLamanApps\IzettleApi\API\Universal\IzettlePostable;
use Money\Money;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;

final class Discount
final class Discount implements IzettlePostable
{
private $uuid;
private $name;
Expand Down Expand Up @@ -153,7 +154,7 @@ private function __construct(
$this->createdAt = $createdAt;
}

public function getCreateData(): string
public function getPostBodyData(): string
{
$data = [
'uuid' => $this->uuid,
Expand Down
16 changes: 11 additions & 5 deletions src/API/Product/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@

use DateTime;
use LauLamanApps\IzettleApi\API\ImageCollection;
use LauLamanApps\IzettleApi\API\Universal\IzettlePostable;
use LauLamanApps\IzettleApi\Client\Exception\CantCreateProductException;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;

final class Product
final class Product implements IzettlePostable
{
private $uuid;
private $categories;
Expand Down Expand Up @@ -134,11 +135,9 @@ public function getVatPercentage(): float
return $this->vatPercentage;
}

public function getCreateData(): string
public function getPostBodyData(): string
{
if (count($this->variants->getAll()) == 0) {
throw new CantCreateProductException('A product should have at least one variant');
}
$this->validateMinimumVariants();

$data = [
'uuid' => $this->uuid,
Expand Down Expand Up @@ -180,4 +179,11 @@ private function __construct(
$this->createdAt = $createdAt;
$this->vatPercentage = $vatPercentage;
}

private function validateMinimumVariants(): void
{
if (count($this->variants->getAll()) == 0) {
throw new CantCreateProductException('A product should have at least one variant');
}
}
}
10 changes: 10 additions & 0 deletions src/API/Universal/IzettlePostable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace LauLamanApps\IzettleApi\API\Universal;

interface IzettlePostable
{
public function getPostBodyData(): string;
}
6 changes: 4 additions & 2 deletions src/Client/Image/ImageFileUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ public function __construct(string $filename)
$this->imageData = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode(file_get_contents($filename)));
}

public function getUploadRequest(): array
public function getPostBodyData(): string
{
return [
$data = [
'imageFormat' => $this->imageFormat,
'imageData' => $this->imageData,
];

return json_encode($data);
}

private function validateFile(string $file)
Expand Down
5 changes: 3 additions & 2 deletions src/Client/Image/ImageUploadRequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace LauLamanApps\IzettleApi\Client\Image;

interface ImageUploadRequestInterface
use LauLamanApps\IzettleApi\API\Universal\IzettlePostable;

interface ImageUploadRequestInterface extends IzettlePostable
{
public function getUploadRequest(): array;
}
6 changes: 4 additions & 2 deletions src/Client/Image/ImageUrlUpload.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ public function __construct(string $imageUrl)
$this->imageUrl = $imageUrl;
}

public function getUploadRequest(): array
public function getPostBodyData(): string
{
return [
$data = [
'imageUrl' => $this->imageUrl
];

return json_encode($data);
}
}
4 changes: 1 addition & 3 deletions src/Client/ImageClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ public function __construct(
public function postImage(ImageUploadRequestInterface $imageUploadRequest): Image
{
$url = sprintf(self::POST_IMAGE, $this->organizationUuid);
$data = json_encode($imageUploadRequest->getUploadRequest());

$response = $this->client->post($url, $data);
$response = $this->client->post($url, $imageUploadRequest);

return $this->imageBuilder->buildFromJson($this->client->getJson($response));
}
Expand Down
6 changes: 3 additions & 3 deletions src/Client/ProductClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function getCategories(): array
public function createCategory(Category $category): void
{
$url = sprintf(self::POST_CATEGORY, $this->organizationUuid);
$this->client->post($url, $category->getCreateData());
$this->client->post($url, $category);
}

/**
Expand All @@ -103,7 +103,7 @@ public function createDiscount(Discount $discount): void
{
$url = sprintf(self::POST_DISCOUNT, $this->organizationUuid);

$this->client->post($url, $discount->getCreateData());
$this->client->post($url, $discount);
}

public function deleteDiscount(Discount $discount): void
Expand Down Expand Up @@ -136,7 +136,7 @@ public function createProduct(Product $product): void
{
$url = sprintf(self::POST_PRODUCT, $this->organizationUuid);

$this->client->post($url, $product->getCreateData());
$this->client->post($url, $product);
}

public function deleteProduct(Product $product): void
Expand Down
5 changes: 3 additions & 2 deletions src/GuzzleIzettleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use DateTimeImmutable;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\RequestException;
use LauLamanApps\IzettleApi\API\Universal\IzettlePostable;
use LauLamanApps\IzettleApi\Client\AccessToken;
use LauLamanApps\IzettleApi\Client\ApiScope;
use LauLamanApps\IzettleApi\Client\Exception\AccessTokenExpiredException;
Expand Down Expand Up @@ -108,7 +109,7 @@ public function get(string $url, ?array $queryParameters = null): ResponseInterf
return $response;
}

public function post(string $url, string $jsonData): ResponseInterface
public function post(string $url, IzettlePostable $postable): ResponseInterface
{
$headers = array_merge(
$this->getAuthorizationHeader(),
Expand All @@ -118,7 +119,7 @@ public function post(string $url, string $jsonData): ResponseInterface
]
);

$options = array_merge(['headers' => $headers], ['body' => $jsonData]);
$options = array_merge(['headers' => $headers], ['body' => $postable->getPostBodyData()]);

return $this->guzzleClient->post($url, $options);
}
Expand Down
3 changes: 2 additions & 1 deletion src/IzettleClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace LauLamanApps\IzettleApi;

use LauLamanApps\IzettleApi\API\Universal\IzettlePostable;
use LauLamanApps\IzettleApi\Client\AccessToken;
use LauLamanApps\IzettleApi\Client\ApiScope;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -26,7 +27,7 @@ public function refreshAccessToken(?AccessToken $accessToken = null): AccessTok

public function get(string $url, ?array $queryParameters = null): ResponseInterface;

public function post(string $url, string $jsonData): ResponseInterface;
public function post(string $url, IzettlePostable $object): ResponseInterface;

public function put(string $url, string $jsonData): void;

Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Api/Product/CategoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function new(): void
$name = 'name';
$discount = Category::new($name);

$createData = json_decode($discount->getCreateData(), true);
$createData = json_decode($discount->getPostBodyData(), true);
self::assertTrue(Uuid::isValid($createData['uuid']));
self::assertSame($name, $createData['name']);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Api/Product/DiscountTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function new(
$externalReference
);

$createData = json_decode($discount->getCreateData(), true);
$createData = json_decode($discount->getPostBodyData(), true);
self::assertTrue(Uuid::isValid($createData['uuid']));
self::assertSame($name, $createData['name']);
self::assertSame($description, $createData['description']);
Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Api/Product/ProductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function new(
$externalReference
);

$createData = json_decode($product->getCreateData(), true);
$createData = json_decode($product->getPostBodyData(), true);
self::assertTrue(Uuid::isValid($createData['uuid']));
self::assertSame($name, $createData['name']);
self::assertSame($description, $createData['description']);
Expand All @@ -61,7 +61,7 @@ public function cantCreateProductWithoutVariant(): void
'externalReference3'
);

$product->getCreateData();
$product->getPostBodyData();
}

public function getDiscountData(): array
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Client/Image/ImageFileUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function productImageUpload_withImage(): void
self::assertInstanceOf(ImageFileUpload::class, $productImageUpload);
self::assertAttributeEquals($imageFormat, 'imageFormat', $productImageUpload);
self::assertAttributeEquals($imageData, 'imageData', $productImageUpload);
self::assertSame(['imageFormat' => $imageFormat, 'imageData' => $imageData], $productImageUpload->getUploadRequest());
self::assertSame(['imageFormat' => $imageFormat, 'imageData' => $imageData], json_decode($productImageUpload->getPostBodyData(), true));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Client/Image/ImageUrlUploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public function productImageUpload_withURL(): void

self::assertInstanceOf(ImageUrlUpload::class, $productImageUpload);
self::assertAttributeEquals($imageUrl, 'imageUrl', $productImageUpload);
self::assertSame(['imageUrl' => $imageUrl], $productImageUpload->getUploadRequest());
self::assertSame(['imageUrl' => $imageUrl], json_decode($productImageUpload->getPostBodyData(), true));
}
}
6 changes: 2 additions & 4 deletions tests/Unit/Client/ImageClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ public function postImage_WithImageUrlUpload(): void
$imageUploadRequest = new ImageUrlUpload('url');

$url = sprintf(ImageClient::POST_IMAGE, $organizationUuid->toString());
$postData = json_encode($imageUploadRequest->getUploadRequest());
$data = json_encode(['postImage']);

$izettleClientMock = $this->getIzettlePostMock($url, $postData);
$izettleClientMock = $this->getIzettlePostMock($url, $imageUploadRequest);
$izettleClientMock->shouldReceive('getJson')->once()->andReturn($data);

list($imageBuilderMock) = $this->getDependencyMocks();
Expand All @@ -50,10 +49,9 @@ public function postImage_WithImageFileUpload(): void
$imageUploadRequest = new ImageFileUpload($file);

$url = sprintf(ImageClient::POST_IMAGE, $organizationUuid->toString());
$postData = json_encode($imageUploadRequest->getUploadRequest());
$data = json_encode(['postImage']);

$izettleClientMock = $this->getIzettlePostMock($url, $postData);
$izettleClientMock = $this->getIzettlePostMock($url, $imageUploadRequest);
$izettleClientMock->shouldReceive('getJson')->once()->andReturn($data);

list($imageBuilderMock) = $this->getDependencyMocks();
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/Client/ProductClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function createCategory()
$category = Category::new('name');
$url = sprintf(ProductClient::POST_CATEGORY, (string) $organizationUuid);

$izettleClientMock = $this->getIzettlePostMock($url, $category->getCreateData());
$izettleClientMock = $this->getIzettlePostMock($url, $category);
list($categoryBuilderMock, $discountBuilderMock, $libraryBuilderMock, $productBuilderMock) = $this->getDependencyMocks();

$productClient = new ProductClient(
Expand Down Expand Up @@ -116,7 +116,7 @@ public function createDiscount()
$organizationUuid = Uuid::uuid1();
$url = sprintf(ProductClient::POST_DISCOUNT, (string) $organizationUuid);

$izettleClientMock = $this->getIzettlePostMock($url, $discount->getCreateData());
$izettleClientMock = $this->getIzettlePostMock($url, $discount);
list($categoryBuilderMock, $discountBuilderMock, $libraryBuilderMock, $productBuilderMock) = $this->getDependencyMocks();

$productClient = new ProductClient(
Expand Down Expand Up @@ -222,7 +222,7 @@ public function createProduct()
$organizationUuid = Uuid::uuid1();
$url = sprintf(ProductClient::POST_PRODUCT, (string) $organizationUuid);

$izettleClientMock = $this->getIzettlePostMock($url, $product->getCreateData());
$izettleClientMock = $this->getIzettlePostMock($url, $product);
list($categoryBuilderMock, $discountBuilderMock, $libraryBuilderMock, $productBuilderMock) = $this->getDependencyMocks();

$productClient = new ProductClient(
Expand Down
12 changes: 8 additions & 4 deletions tests/Unit/GuzzleIzettleClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use DateTimeImmutable;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\ClientInterface as GuzzleClientInterface;
use LauLamanApps\IzettleApi\API\Universal\IzettlePostable;
use LauLamanApps\IzettleApi\Client\AccessToken;
use LauLamanApps\IzettleApi\Client\ApiScope;
use LauLamanApps\IzettleApi\GuzzleIzettleClient;
Expand Down Expand Up @@ -167,15 +168,15 @@ public function getGetData(): array
* @test
* @dataProvider getPostData
*/
public function post($url, $data): void
public function post($url, IzettlePostable $data): void
{
$options = [
'headers' => [
'Authorization' => 'Bearer ' . self::ACCESS_TOKEN,
'content-type' => 'application/json',
'Accept' => 'application/json',
],
'body' => $data,
'body' => $data->getPostBodyData(),
];

$guzzleClientMock = Mockery::mock(GuzzleClientInterface::class);
Expand All @@ -188,9 +189,12 @@ public function post($url, $data): void

public function getPostData(): array
{
$postable = Mockery::mock(IzettlePostable::class);
$postable->shouldReceive('getPostBodyData')->once();

return [
['example.com/account', json_encode(['name' => 'John Doe'])],
['example.com/account/names', json_encode(['firstName' => 'john', 'lastName' => 'Doe'])],
['example.com/account', $postable],
['example.com/account/names', $postable],
];
}

Expand Down

0 comments on commit ae0addd

Please sign in to comment.