Skip to content

Commit

Permalink
Merge pull request #7 from sendynl/add-new-endpoint-for-shipments
Browse files Browse the repository at this point in the history
Add a new endpoint for the Shipment resource
  • Loading branch information
wgriffioen authored Jan 16, 2025
2 parents 50b288b + 45cc55f commit 2ffedc4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
16 changes: 15 additions & 1 deletion src/Resources/Shipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Sendy\Api\ApiException;
use Sendy\Api\Meta;

class Shipment extends Resource
final class Shipment extends Resource
{
/**
* List all shipments
Expand Down Expand Up @@ -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<string, mixed|array<string,mixed>> $data
* @return array<string, mixed|array<string|mixed>>
* @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
*
Expand Down
36 changes: 26 additions & 10 deletions tests/Resources/ShipmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand All @@ -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());
}

Expand All @@ -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());
}
Expand All @@ -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());
}

Expand All @@ -82,18 +82,34 @@ 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());

$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([
Expand All @@ -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());
}

Expand All @@ -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());
}

Expand All @@ -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());
}
}

0 comments on commit 2ffedc4

Please sign in to comment.