From 710f67a3189a9108d176761033396e1872c3b490 Mon Sep 17 00:00:00 2001 From: Josh Bruce Date: Mon, 1 May 2023 21:01:27 -0400 Subject: [PATCH 1/2] refactor: Tests --- tests/ClientTest.php | 16 ++--------- tests/EndpointsTest.php | 9 ++----- tests/PrintifyTest.php | 4 +-- tests/Shops/Products/Images/ImageTest.php | 6 ++--- tests/Shops/Products/ProductTest.php | 30 +++------------------ tests/Shops/Products/ProductsTest.php | 17 +++--------- tests/Shops/ShopsTest.php | 14 +++------- tests/TestCase.php | 33 +++++++++++++++++++++++ 8 files changed, 52 insertions(+), 77 deletions(-) create mode 100644 tests/TestCase.php diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 7e7f3c1..8381a7f 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -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) diff --git a/tests/EndpointsTest.php b/tests/EndpointsTest.php index 7e7b79a..d6c1e7f 100644 --- a/tests/EndpointsTest.php +++ b/tests/EndpointsTest.php @@ -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 @@ -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); diff --git a/tests/PrintifyTest.php b/tests/PrintifyTest.php index cf8792a..0c15f31 100644 --- a/tests/PrintifyTest.php +++ b/tests/PrintifyTest.php @@ -3,7 +3,7 @@ namespace Eightfold\Printify\Tests; -use PHPUnit\Framework\TestCase; +use Eightfold\Printify\Tests\TestCase; use Eightfold\Printify\Printify; @@ -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'; diff --git a/tests/Shops/Products/Images/ImageTest.php b/tests/Shops/Products/Images/ImageTest.php index 075f107..6a87ec4 100644 --- a/tests/Shops/Products/Images/ImageTest.php +++ b/tests/Shops/Products/Images/ImageTest.php @@ -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 @@ -14,7 +14,7 @@ public function can_get_filename(): void { $expected = 'mug-11oz.jpg'; - $product = $this->fullProduct(); + $product = parent::fullProduct(); $variants = $product->variants(); diff --git a/tests/Shops/Products/ProductTest.php b/tests/Shops/Products/ProductTest.php index dbe2382..3b46f0a 100644 --- a/tests/Shops/Products/ProductTest.php +++ b/tests/Shops/Products/ProductTest.php @@ -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; @@ -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'; @@ -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(); diff --git a/tests/Shops/Products/ProductsTest.php b/tests/Shops/Products/ProductsTest.php index 75a4c73..33276c2 100644 --- a/tests/Shops/Products/ProductsTest.php +++ b/tests/Shops/Products/ProductsTest.php @@ -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 */ @@ -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; @@ -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; diff --git a/tests/Shops/ShopsTest.php b/tests/Shops/ShopsTest.php index eefa4f6..0ea6e9d 100644 --- a/tests/Shops/ShopsTest.php +++ b/tests/Shops/ShopsTest.php @@ -1,27 +1,19 @@ printifyAccount() + ); + } + + protected function fullProduct(): Product + { + $json = file_get_contents(__DIR__ . '/api-responses/get-product.json'); + + return Product::fromJson($this->nonApiClient(), $json); + } +} From 237b8c581fe71ade34f82bdbfa9a45d9449c06e2 Mon Sep 17 00:00:00 2001 From: Josh Bruce Date: Mon, 1 May 2023 21:01:37 -0400 Subject: [PATCH 2/2] add: Event objects --- src/EventHandling/Event.php | 103 ++++++++++++++++++ src/EventHandling/EventError.php | 10 ++ src/EventHandling/EventType.php | 10 ++ tests/EventHandling/EventTest.php | 50 +++++++++ .../event-product-publish-started.json | 23 ++++ 5 files changed, 196 insertions(+) create mode 100644 src/EventHandling/Event.php create mode 100644 src/EventHandling/EventError.php create mode 100644 src/EventHandling/EventType.php create mode 100644 tests/EventHandling/EventTest.php create mode 100644 tests/api-responses/event-product-publish-started.json diff --git a/src/EventHandling/Event.php b/src/EventHandling/Event.php new file mode 100644 index 0000000..b4412c4 --- /dev/null +++ b/src/EventHandling/Event.php @@ -0,0 +1,103 @@ +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 + }; + } +} diff --git a/src/EventHandling/EventError.php b/src/EventHandling/EventError.php new file mode 100644 index 0000000..eb5ab61 --- /dev/null +++ b/src/EventHandling/EventError.php @@ -0,0 +1,10 @@ +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 + ); + } +} diff --git a/tests/api-responses/event-product-publish-started.json b/tests/api-responses/event-product-publish-started.json new file mode 100644 index 0000000..181a014 --- /dev/null +++ b/tests/api-responses/event-product-publish-started.json @@ -0,0 +1,23 @@ +{ + "id": "653b6be8-2ff7-4ab5-a7a6-6889a8b3bbf5", + "type": "product:publish:started", + "created_at": "2022-05-17 15:00:00+00:00", + "resource": { + "id": "5cb87a8cd490a2ccb256cec4", + "type": "product", + "data": { + "shop_id": 815256, + "publish_details": { + "title": true, + "variants": false, + "description": true, + "tags": true, + "images": false, + "key_features": false, + "shipping_template": true + }, + "action": "create", + "out_of_stock_publishing": 0 + } + } +}