Skip to content

Commit

Permalink
stop lists api
Browse files Browse the repository at this point in the history
  • Loading branch information
KoTRpa committed Mar 20, 2023
1 parent 8b4e047 commit 9dae41c
Show file tree
Hide file tree
Showing 12 changed files with 356 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/Endpoints/General/Menu/OutOfStockRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace KMA\IikoTransport\Endpoints\General\Menu;

use JetBrains\PhpStorm\ArrayShape;
use KMA\IikoTransport\Contracts\IRequestBody;
use KMA\IikoTransport\Entities\Entity;
use KMA\IikoTransport\Exceptions\MissingRequiredFieldException;

class OutOfStockRequest extends Entity implements IRequestBody
{
/**
* @required
* @var string[] <uuid> Organizations for which out-of-stock lists will be requested
* Can be obtained by /api/1/organizations operation
*/
public array $organizationIds;

const ATTRIBUTES = [
'organizationIds' => 'string[]',
];

/**
* @param array|null $data
* @throws \KMA\IikoTransport\Exceptions\MissingRequiredFieldException
*/
public function __construct(
#[ArrayShape(self::ATTRIBUTES)]
?array $data = null
)
{
if (null !== $data) {
if (!isset($data['organizationIds'])) {
throw new MissingRequiredFieldException('organisationId');
}

$this->organizationIds = $data['organizationIds'];
}
}
}
34 changes: 34 additions & 0 deletions src/Endpoints/General/Menu/OutOfStockResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace KMA\IikoTransport\Endpoints\General\Menu;

use Illuminate\Support\Collection;
use KMA\IikoTransport\Contracts\HasCorrelationId;
use KMA\IikoTransport\Entities\Entity;
use KMA\IikoTransport\Entities\StopLists\TerminalGroupStopListWrap;

class OutOfStockResponse extends Entity
{
use HasCorrelationId;

/**
* Stock list group
* @required
* @var \Illuminate\Support\Collection<int, \KMA\IikoTransport\Entities\StopLists\TerminalGroupStopListWrap>
*/
public Collection $terminalGroupStopLists;

/**
* @param array|null $data
*/
public function __construct(?array $data = null)
{
if (null !== $data) {
$this->correlationId = $data['correlationId'];

$this->terminalGroupStopLists =
collect($data['terminalGroupStopLists'])
->map(fn (array $item): TerminalGroupStopListWrap => TerminalGroupStopListWrap::fromArray($item));
}
}
}
24 changes: 24 additions & 0 deletions src/Endpoints/Menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use KMA\IikoTransport\Endpoints\General\Menu\NomenclatureRequest;
use KMA\IikoTransport\Endpoints\General\Menu\NomenclatureResponse;
use KMA\IikoTransport\Endpoints\General\Menu\OutOfStockRequest;
use KMA\IikoTransport\Endpoints\General\Menu\OutOfStockResponse;

/**
* Menu APIs
Expand Down Expand Up @@ -34,4 +36,26 @@ public function nomenclature(NomenclatureRequest $req): NomenclatureResponse

return NomenclatureResponse::fromJson($res->getBody());
}

/**
* @param \KMA\IikoTransport\Endpoints\General\Menu\OutOfStockRequest $req
*
* @return \KMA\IikoTransport\Endpoints\General\Menu\OutOfStockResponse
*
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \JsonException
* @throws \KMA\IikoTransport\Exceptions\MissingRequiredFieldException
* @throws \KMA\IikoTransport\Exceptions\MissingTokenException
* @throws \KMA\IikoTransport\Exceptions\ResponseException
* @throws \Psr\Http\Client\ClientExceptionInterface
*/
public function outOfStock(OutOfStockRequest $req): OutOfStockResponse
{
$res = $this->post(
'stop_lists',
$req
);

return OutOfStockResponse::fromJson($res->getBody());
}
}
39 changes: 39 additions & 0 deletions src/Entities/StopLists/StopListItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace KMA\IikoTransport\Entities\StopLists;

use KMA\IikoTransport\Entities\Entity;
use KMA\IikoTransport\Exceptions\MissingRequiredFieldException;

class StopListItem extends Entity
{
/**
* @required
* @var float Product balance
*/
public float $balance;

/**
* @required
* @var string <uuid> Out-of-stock list product ID
*/
public string $productId;

/**
* @throws \KMA\IikoTransport\Exceptions\MissingRequiredFieldException
*/
public function __construct(?array $data = null)
{
if (null !== $data) {
if (!isset($data['balance'])) {
throw new MissingRequiredFieldException('balance');
}
if (!isset($data['productId'])) {
throw new MissingRequiredFieldException('productId');
}
$this->balance = $data['balance'];
$this->productId = $data['productId'];
}
}

}
40 changes: 40 additions & 0 deletions src/Entities/StopLists/TerminalGroupStopList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace KMA\IikoTransport\Entities\StopLists;

use Illuminate\Support\Collection;
use KMA\IikoTransport\Entities\Entity;
use KMA\IikoTransport\Exceptions\MissingRequiredFieldException;

class TerminalGroupStopList extends Entity
{
/**
* @required
* @var string <uuid>
*/
public string $terminalGroupId;

/**
* @required
* @var \Illuminate\Support\Collection<int, \KMA\IikoTransport\Entities\StopLists\StopListItem>
*/
public Collection $items;

/**
* @throws \KMA\IikoTransport\Exceptions\MissingRequiredFieldException
*/
public function __construct(?array $data = null)
{
if (null !== $data) {
if (!isset($data['terminalGroupId'])) {
throw new MissingRequiredFieldException('terminalGroupId');
}
if (!isset($data['items'])) {
throw new MissingRequiredFieldException('items');
}
$this->terminalGroupId = $data['terminalGroupId'];
$this->items = collect($data['items'])
->map(fn (array $item): StopListItem => StopListItem::fromArray($item));
}
}
}
33 changes: 33 additions & 0 deletions src/Entities/StopLists/TerminalGroupStopListWrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace KMA\IikoTransport\Entities\StopLists;

use Illuminate\Support\Collection;
use KMA\IikoTransport\Entities\Entity;

class TerminalGroupStopListWrap extends Entity
{
/**
* @required
* @var string <uuid> Organization ID
* Can be obtained by /api/1/organizations operation
*/
public string $organizationId;

/**
* Items for organization
* @required
* @var \Illuminate\Support\Collection<int, \KMA\IikoTransport\Entities\StopLists\TerminalGroupStopList>
*/
public Collection $items;

public function __construct(?array $data = null)
{
if (null !== $data) {
$this->organizationId = $data['organizationId'];
$this->items =
collect($data['items'])
->map(fn (array $item): TerminalGroupStopList => TerminalGroupStopList::fromArray($item));
}
}
}
15 changes: 15 additions & 0 deletions tests/Endpoints/MenuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace KMA\IikoTransport\Tests\Endpoints;

use KMA\IikoTransport\Endpoints\General\Menu\OutOfStockRequest;
use KMA\IikoTransport\Endpoints\General\Menu\OutOfStockResponse;
use KMA\IikoTransport\Tests\EndpointTestCase;
use KMA\IikoTransport\Endpoints\General\Menu\NomenclatureRequest;
use KMA\IikoTransport\Endpoints\General\Menu\NomenclatureResponse;
Expand All @@ -21,4 +23,17 @@ public function testNomenclature()
'/api/1/nomenclature'
);
}
/**
* @covers \KMA\IikoTransport\IikoTransport::outOfStock
*/
public function testOutOfStock()
{
$this->runTests(
'Menu/OutOfStockResponse',
'outOfStock',
OutOfStockRequest::class,
OutOfStockResponse::class,
'/api/1/stop_lists'
);
}
}
33 changes: 33 additions & 0 deletions tests/Entities/StopLists/StopListItemTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace KMA\IikoTransport\Tests\Entities\StopLists;

use KMA\IikoTransport\Entities\StopLists\StopListItem;
use KMA\IikoTransport\Tests\EntityTestCase;

/**
* @covers \KMA\IikoTransport\Entities\StopLists\StopListItem
*/
class StopListItemTest extends EntityTestCase
{
protected array $fixture = [
'name' => 'Menu/OutOfStockResponse',
'path' => 'terminalGroupStopLists.items.items'
];
protected string $entityClass = StopListItem::class;
protected array $fields = [
'balance',
'productId',
];

public function testCreateEntity(): void
{
$this->runCreateTests();
}

protected function assertFieldValidity($entity): void
{
$this->assertIsFloat($entity->balance);
$this->assertIsUuid($entity->productId);
}
}
38 changes: 38 additions & 0 deletions tests/Entities/StopLists/TerminalGroupStopListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace KMA\IikoTransport\Tests\Entities\StopLists;

use Illuminate\Support\Collection;
use KMA\IikoTransport\Entities\StopLists\TerminalGroupStopList;
use KMA\IikoTransport\Entities\StopLists\StopListItem;
use KMA\IikoTransport\Tests\EntityTestCase;

/**
* @covers \KMA\IikoTransport\Entities\StopLists\TerminalGroupStopList
*/
class TerminalGroupStopListTest extends EntityTestCase
{
protected array $fixture = [
'name' => 'Menu/OutOfStockResponse',
'path' => 'terminalGroupStopLists.items'
];
protected string $entityClass = TerminalGroupStopList::class;
protected array $fields = [
'terminalGroupId',
'items',
];

public function testCreateEntity(): void
{
$this->runCreateTests();
}

protected function assertFieldValidity($entity): void
{
$this->assertIsUuid($entity->terminalGroupId);
$this->assertInstanceOf(Collection::class, $entity->items);
$entity->items->each(function($item) {
$this->assertInstanceOf(StopListItem::class, $item);
});
}
}
38 changes: 38 additions & 0 deletions tests/Entities/StopLists/TerminalGroupStopListWrapTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace KMA\IikoTransport\Tests\Entities\StopLists;

use Illuminate\Support\Collection;
use KMA\IikoTransport\Entities\StopLists\TerminalGroupStopList;
use KMA\IikoTransport\Entities\StopLists\TerminalGroupStopListWrap;
use KMA\IikoTransport\Tests\EntityTestCase;

/**
* @covers \KMA\IikoTransport\Entities\StopLists\TerminalGroupStopListWrap
*/
class TerminalGroupStopListWrapTest extends EntityTestCase
{
protected array $fixture = [
'name' => 'Menu/OutOfStockResponse',
'path' => 'terminalGroupStopLists'
];
protected string $entityClass = TerminalGroupStopListWrap::class;
protected array $fields = [
'organizationId',
'items',
];

public function testCreateEntity(): void
{
$this->runCreateTests();
}

protected function assertFieldValidity($entity): void
{
$this->assertIsUuid($entity->organizationId);
$this->assertInstanceOf(Collection::class, $entity->items);
$entity->items->each(function($item) {
$this->assertInstanceOf(TerminalGroupStopList::class, $item);
});
}
}
3 changes: 3 additions & 0 deletions tests/Fixtures/Menu/OutOfStockRequest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"organizationIds": ["7bc05553-4b68-44e8-b7bc-37be63c6d9e9"]
}
19 changes: 19 additions & 0 deletions tests/Fixtures/Menu/OutOfStockResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"correlationId": "48fb4cd3-2ef6-4479-bea1-7c92721b988c",
"terminalGroupStopLists": [
{
"organizationId": "7bc05553-4b68-44e8-b7bc-37be63c6d9e9",
"items": [
{
"terminalGroupId": "4fab19a5-203c-4bf5-94eb-f572aa8b117b",
"items": [
{
"balance": -1.0,
"productId": "dcd53ddb-8104-4e48-8cc0-5df1088c6113"
}
]
}
]
}
]
}

0 comments on commit 9dae41c

Please sign in to comment.