Skip to content

Commit

Permalink
Merge pull request #3 from 8fold/initial
Browse files Browse the repository at this point in the history
Initial
  • Loading branch information
joshbruce authored May 2, 2023
2 parents 885f7e8 + 237b8c5 commit 7924484
Show file tree
Hide file tree
Showing 13 changed files with 248 additions and 77 deletions.
103 changes: 103 additions & 0 deletions src/EventHandling/Event.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php
declare(strict_types=1);

namespace Eightfold\Printify\EventHandling;

use StdClass;
use DateTime;

use Psr\Http\Message\ServerRequestInterface;

use Eightfold\Printify\EventHandling\EventType;

use Eightfold\Printify\Client;

use Eightfold\Printify\EventHandling\EventError;

use Eightfold\Printify\Shops\Products\Product;

class Event
{
public const TIMESTAMP_FORMAT = 'Y-m-d H:i:sP';

public static function fromRequest(
Client $client,
ServerRequestInterface $request
): self|EventError {
$json = $request->getBody()->getContents();
return self::fromJson($client, $json);
}

public static function fromJson(Client $client, string $json): self|EventError
{
$object = json_decode($json);
if (
is_object($object) == false or
is_a($object, StdClass::class) === false
) {
return EventError::InvalidJson;
}

return self::fromObject($client, $object);
}

public static function fromObject(Client $client, StdClass $object): self
{
return new self($client, $object);
}

final private function __construct(
private readonly Client $client,
private readonly StdClass $object
) {
}

private function client(): Client
{
return $this->client;
}

private function object(): StdClass
{
return $this->object;
}

public function id(): string
{
return $this->object()->id;
}

public function type(): EventType
{
return match ($this->object()->type) {
'product:publish:started' => EventType::ProductPublishStarted,
default => EventType::Unsupported
};
}

public function createdAt(): DateTime|false
{
return DateTime::createFromFormat(
self::TIMESTAMP_FORMAT,
$this->object()->created_at
);
}

public function resourceRaw(): StdClass
{
return $this->object()->resource;
}

public function resource(): Product|EventError
{
$meta = $this->resourceRaw();
return match ($meta->type) {
'product' => Product::withId(
$this->client(),
$meta->data->shop_id,
$meta->id
),
default => EventError::UnsupportedResourceType
};
}
}
10 changes: 10 additions & 0 deletions src/EventHandling/EventError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);

namespace Eightfold\Printify\EventHandling;

enum EventError
{
case InvalidJson;
case UnsupportedResourceType;
}
10 changes: 10 additions & 0 deletions src/EventHandling/EventType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);

namespace Eightfold\Printify\EventHandling;

enum EventType
{
case Unsupported;
case ProductPublishStarted;
}
16 changes: 2 additions & 14 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,18 @@

namespace Eightfold\Printify\Tests;

use PHPUnit\Framework\TestCase;

use Eightfold\Printify\Client;

use Eightfold\Printify\Printify;
use Eightfold\Printify\Tests\TestCase;

use Eightfold\Printify\Shops\Shop;
use Eightfold\Printify\Shops\Products\Product;

class ClientTest extends TestCase
{
private function nonApiClient(): Client
{
return Client::connect(
Printify::account('token')
);
}

/**
* @test
*/
public function can_return_shop_without_api_call(): void
{
$shop = $this->nonApiClient()->shop(withId: 12345);
$shop = parent::nonApiClient()->shop(withId: 12345);

$this->assertTrue(
is_a($shop, Shop::class)
Expand Down
9 changes: 2 additions & 7 deletions tests/EndpointsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@

namespace Eightfold\Printify\Tests;

use PHPUnit\Framework\TestCase;
use Eightfold\Printify\Tests\TestCase;

use Eightfold\Printify\Endpoints;

use Eightfold\Printify\Client;
use Eightfold\Printify\Printify;

use Eightfold\Printify\Shops\Shop;

class EndpointsTest extends TestCase
Expand Down Expand Up @@ -46,9 +43,7 @@ public function get_products_is_expected_endpoint(): void
$result
);

$client = Client::connect(
Printify::account('token')
);
$client = parent::nonApiClient();

$shop = Shop::withId($client, 12345);

Expand Down
50 changes: 50 additions & 0 deletions tests/EventHandling/EventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
declare(strict_types=1);

namespace Eightfold\Printify\Tests\EventHandling;

use Eightfold\Printify\Tests\TestCase;

use Eightfold\Printify\EventHandling\Event;

use Eightfold\Printify\EventHandling\EventType;

use Eightfold\Printify\Shops\Products\Product;

class EventTest extends TestCase
{
/**
* @test
*/
public function can_get_resource(): void
{
$json = file_get_contents(
__DIR__ . '/../api-responses/event-product-publish-started.json'
);

$result = Event::fromJson(parent::nonApiClient(), $json)->resource();

$this->assertTrue(
is_a($result, Product::class)
);
}

/**
* @test
*/
public function has_expected_event_type(): void
{
$json = file_get_contents(
__DIR__ . '/../api-responses/event-product-publish-started.json'
);

$expected = EventType::ProductPublishStarted;

$result = Event::fromJson(parent::nonApiClient(), $json)->type();

$this->assertSame(
$expected,
$result
);
}
}
4 changes: 2 additions & 2 deletions tests/PrintifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Eightfold\Printify\Tests;

use PHPUnit\Framework\TestCase;
use Eightfold\Printify\Tests\TestCase;

use Eightfold\Printify\Printify;

Expand All @@ -14,7 +14,7 @@ class PrintifyTest extends TestCase
*/
public function can_get_configuration_values(): void
{
$account = Printify::account(accessToken: 'token');
$account = parent::printifyAccount();

$expected = 'https://api.printify.com/v1';

Expand Down
6 changes: 3 additions & 3 deletions tests/Shops/Products/Images/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace Eightfold\Printify\Tests\Shops\Products\Images;

use Eightfold\Printify\Tests\Shops\Products\ProductTest;
use Eightfold\Printify\Tests\TestCase;

class ImageTest extends ProductTest
class ImageTest extends TestCase
{
/**
* @test
Expand All @@ -14,7 +14,7 @@ public function can_get_filename(): void
{
$expected = 'mug-11oz.jpg';

$product = $this->fullProduct();
$product = parent::fullProduct();

$variants = $product->variants();

Expand Down
30 changes: 4 additions & 26 deletions tests/Shops/Products/ProductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,18 @@

namespace Eightfold\Printify\Tests\Shops\Products;

use PHPUnit\Framework\TestCase;

use Eightfold\Printify\Shops\Products\Products;
use Eightfold\Printify\Tests\TestCase;

use Eightfold\Printify\Shops\Products\Product;

use Eightfold\Printify\Client;

use Eightfold\Printify\Printify;

use DateTime;

class ProductTest extends TestCase
{
protected function nonApiClient(): Client
{
return Client::connect(
Printify::account('token')
);
}

protected function fullProduct(): Product
{
$json = file_get_contents(__DIR__ . '/../../api-responses/get-product.json');

return Product::fromJson($this->nonApiClient(), $json);
}

/**
* @test
*/
public function can_return_product_without_api_call_using_required_ids(): void
{
$sut = Product::withId($this->nonApiClient(), 1234, 'productid');
$sut = Product::withId(parent::nonApiClient(), 1234, 'productid');

$expected = 1234;

Expand All @@ -62,7 +40,7 @@ public function can_return_product_without_api_call_using_required_ids(): void
*/
public function can_get_dates(): void
{
$product = $this->fullProduct();
$product = parent::fullProduct();

$expected = '2019-07-25 13:40:41+00:00';

Expand Down Expand Up @@ -104,7 +82,7 @@ public function can_get_dates(): void
*/
public function can_get_images_for_variant(): void
{
$product = $this->fullProduct();
$product = parent::fullProduct();

$variants = $product->variants();

Expand Down
17 changes: 3 additions & 14 deletions tests/Shops/Products/ProductsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,14 @@

namespace Eightfold\Printify\Tests\Shops\Products;

use PHPUnit\Framework\TestCase;
use Eightfold\Printify\Tests\TestCase;

use Eightfold\Printify\Shops\Products\Products;

use Eightfold\Printify\Shops\Products\Product;

use Eightfold\Printify\Client;

use Eightfold\Printify\Printify;

class ProductsTest extends TestCase
{
private function nonApiClient(): Client
{
return Client::connect(
Printify::account('token')
);
}

/**
* @test
*/
Expand All @@ -31,7 +20,7 @@ public function can_return_products_without_api_call(): void
__DIR__ . '/../../api-responses/get-products-lite-required.json'
);

$sut = Products::fromJson($this->nonApiClient(), $json);
$sut = Products::fromJson(parent::nonApiClient(), $json);

$expected = 1;

Expand Down Expand Up @@ -76,7 +65,7 @@ public function can_initialize_without_api_call(): void
__DIR__ . '/../../api-responses/get-products-lite-all.json'
);

$sut = Products::fromJson($this->nonApiClient(), $json);
$sut = Products::fromJson(parent::nonApiClient(), $json);

$expected = 1;

Expand Down
Loading

0 comments on commit 7924484

Please sign in to comment.