diff --git a/src/Resources/Shipment.php b/src/Resources/Shipment.php index 9fb4c6b..b38193b 100644 --- a/src/Resources/Shipment.php +++ b/src/Resources/Shipment.php @@ -6,7 +6,7 @@ use Sendy\Api\ApiException; use Sendy\Api\Meta; -class Shipment extends Resource +final class Shipment extends Resource { /** * List all shipments @@ -95,6 +95,20 @@ public function createFromPreference(array $data, bool $generateDirectly = true) return $this->connection->post("/shipments/preference", $data, ['generateDirectly' => $generateDirectly]); } + /** + * Create a shipment from a smart rule + * + * @param array> $data + * @return array> + * @throws ApiException + * @throws GuzzleException + * @see https://app.sendy.nl/api/docs#tag/Shipments/operation/shipments.smart-rule + */ + public function createWithSmartRules(array $data): array + { + return $this->connection->post('/shipments/smart-rule', $data); + } + /** * Generate a shipment * diff --git a/tests/Resources/ShipmentTest.php b/tests/Resources/ShipmentTest.php index 9258182..9a1d840 100644 --- a/tests/Resources/ShipmentTest.php +++ b/tests/Resources/ShipmentTest.php @@ -22,7 +22,7 @@ public function testList(): void $this->assertEquals([], $resource->list()); - $this->assertEquals('/api/shipments?page=1', (string) $handler->getLastRequest()->getUri()); + $this->assertEquals('/api/shipments?page=1', (string)$handler->getLastRequest()->getUri()); $this->assertEquals('GET', $handler->getLastRequest()->getMethod()); } @@ -36,7 +36,7 @@ public function testGet(): void $this->assertEquals([], $resource->get('1337')); - $this->assertEquals('/api/shipments/1337', (string) $handler->getLastRequest()->getUri()); + $this->assertEquals('/api/shipments/1337', (string)$handler->getLastRequest()->getUri()); $this->assertEquals('GET', $handler->getLastRequest()->getMethod()); } @@ -50,7 +50,7 @@ public function testUpdate(): void $this->assertEquals([], $resource->update('1337', ['foo' => 'bar'])); - $this->assertEquals('/api/shipments/1337', (string) $handler->getLastRequest()->getUri()); + $this->assertEquals('/api/shipments/1337', (string)$handler->getLastRequest()->getUri()); $this->assertEquals('PUT', $handler->getLastRequest()->getMethod()); $this->assertEquals('{"foo":"bar"}', $handler->getLastRequest()->getBody()->getContents()); } @@ -65,7 +65,7 @@ public function testDelete(): void $this->assertEquals([], $resource->delete('1337')); - $this->assertEquals('/api/shipments/1337', (string) $handler->getLastRequest()->getUri()); + $this->assertEquals('/api/shipments/1337', (string)$handler->getLastRequest()->getUri()); $this->assertEquals('DELETE', $handler->getLastRequest()->getMethod()); } @@ -82,7 +82,7 @@ public function testCreateFromPreference(): void $this->assertEquals( '/api/shipments/preference?generateDirectly=0', - (string) $handler->getLastRequest()->getUri() + (string)$handler->getLastRequest()->getUri() ); $this->assertEquals('POST', $handler->getLastRequest()->getMethod()); $this->assertEquals('{"foo":"bar"}', $handler->getLastRequest()->getBody()->getContents()); @@ -90,10 +90,26 @@ public function testCreateFromPreference(): void $this->assertEquals([], $resource->createFromPreference(['foo' => 'bar'])); $this->assertEquals( '/api/shipments/preference?generateDirectly=1', - (string) $handler->getLastRequest()->getUri() + (string)$handler->getLastRequest()->getUri() ); } + public function testCreateWithSmartRules(): void + { + $handler = new MockHandler([ + new Response(200, [], json_encode(['foo' => 'bar'])), + ]); + + $resource = new Shipment($this->buildConnectionWithMockHandler($handler)); + + $resource->createWithSmartRules(['foo' => 'bar']); + + $this->assertEquals('/api/shipments/smart-rule', (string)$handler->getLastRequest()->getUri()); + $this->assertEquals('POST', $handler->getLastRequest()->getMethod()); + $this->assertEquals('{"foo":"bar"}', $handler->getLastRequest()->getBody()->getContents()); + } + + public function testGenerate(): void { $handler = new MockHandler([ @@ -105,13 +121,13 @@ public function testGenerate(): void $this->assertEquals([], $resource->generate('1337')); - $this->assertEquals('/api/shipments/1337/generate', (string) $handler->getLastRequest()->getUri()); + $this->assertEquals('/api/shipments/1337/generate', (string)$handler->getLastRequest()->getUri()); $this->assertEquals('POST', $handler->getLastRequest()->getMethod()); $this->assertEquals('{"asynchronous":true}', $handler->getLastRequest()->getBody()->getContents()); $this->assertEquals([], $resource->generate('1337', false)); - $this->assertEquals('/api/shipments/1337/generate', (string) $handler->getLastRequest()->getUri()); + $this->assertEquals('/api/shipments/1337/generate', (string)$handler->getLastRequest()->getUri()); $this->assertEquals('{"asynchronous":false}', $handler->getLastRequest()->getBody()->getContents()); } @@ -125,7 +141,7 @@ public function testLabels(): void $this->assertEquals([], $resource->labels('1337')); - $this->assertEquals('/api/shipments/1337/labels', (string) $handler->getLastRequest()->getUri()); + $this->assertEquals('/api/shipments/1337/labels', (string)$handler->getLastRequest()->getUri()); $this->assertEquals('GET', $handler->getLastRequest()->getMethod()); } @@ -139,7 +155,7 @@ public function testDocuments(): void $this->assertEquals([], $resource->documents('1337')); - $this->assertEquals('/api/shipments/1337/documents', (string) $handler->getLastRequest()->getUri()); + $this->assertEquals('/api/shipments/1337/documents', (string)$handler->getLastRequest()->getUri()); $this->assertEquals('GET', $handler->getLastRequest()->getMethod()); } }