Skip to content

Commit

Permalink
Update: improve method names; include separate methods for generators
Browse files Browse the repository at this point in the history
  • Loading branch information
jordyvanderhaegen committed Feb 12, 2022
1 parent 7367b50 commit c18472a
Show file tree
Hide file tree
Showing 5 changed files with 232 additions and 15 deletions.
30 changes: 22 additions & 8 deletions src/Picqer/Financials/Exact/Query/Findable.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,14 @@ public function findId($code, $key = 'Code')
}
}

public function filter($filter, $expand = '', $select = '', $system_query_options = null, array $headers = [])
public function filter($filter, $expand = '', $select = '', $system_query_options = null, array $headers = []): array
{
return iterator_to_array(
$this->filterAsGenerator($filter, $expand, $select, $system_query_options, $headers)
);
}

public function filterAsGenerator($filter, $expand = '', $select = '', $system_query_options = null, array $headers = []): Generator
{
$originalDivision = $this->connection()->getDivision();

Expand Down Expand Up @@ -115,7 +122,7 @@ public function filter($filter, $expand = '', $select = '', $system_query_option
$this->connection()->setDivision($originalDivision); // Restore division
}

return $this->collectionFromResult($result, $headers);
return $this->collectionFromResultAsGenerator($result, $headers);
}

/**
Expand Down Expand Up @@ -147,19 +154,26 @@ public function getResultSet(array $params = [])
return new Resultset($this->connection(), $this->url(), get_class($this), $params);
}

public function get(array $params = [])
public function get(array $params = []): array
{
return iterator_to_array($this->getGenerator($params));
return iterator_to_array($this->getAsGenerator($params));
}

public function getGenerator(array $params = []): Generator
public function getAsGenerator(array $params = []): Generator
{
$result = $this->connection()->get($this->url(), $params);

return $this->collectionFromResult($result);
return $this->collectionFromResultAsGenerator($result);
}

public function collectionFromResult($result, array $headers = []): array
{
return iterator_to_array(
$this->collectionFromResultAsGenerator($result, $headers)
);
}

public function collectionFromResult($result, array $headers = []): Generator
public function collectionFromResultAsGenerator($result, array $headers = []): Generator
{
// If we have one result which is not an assoc array, make it the first element of an array for the
// collectionFromResult function so we always return a collection from filter
Expand All @@ -168,7 +182,7 @@ public function collectionFromResult($result, array $headers = []): Generator
}

foreach ($result as $row) {
yield new self($this->connection(), $row);
yield new static($this->connection(), $row);
}

while ($this->connection()->nextUrl !== null) {
Expand Down
15 changes: 11 additions & 4 deletions src/Picqer/Financials/Exact/Query/Resultset.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,16 @@ public function __construct(Connection $connection, $url, $class, array $params)

public function next(): array
{
return iterator_to_array($this->nextGenerator());
return iterator_to_array($this->nextAsGenerator());
}

public function nextGenerator(): Generator
public function nextAsGenerator(): Generator
{
$result = $this->connection->get($this->url, $this->params);
$this->url = $this->connection->nextUrl;
$this->params = [];

return $this->collectionFromResult($result);
return $this->collectionFromResultAsGenerator($result);
}

/**
Expand All @@ -68,7 +68,14 @@ public function hasMore()
return $this->url !== null;
}

protected function collectionFromResult(array $result): Generator
protected function collectionFromResult(array $result): array
{
return iterator_to_array(
$this->collectionFromResultAsGenerator($result)
);
}

protected function collectionFromResultAsGenerator(array $result): Generator
{
// If we have one result which is not an assoc array, make it the first element of an array for the
// collectionFromResult function so we always return a collection from filter
Expand Down
89 changes: 87 additions & 2 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,38 @@

namespace Picqer\Tests;

use Generator;
use PHPUnit\Framework\TestCase;
use Picqer\Financials\Exact\Item;
use Picqer\Financials\Exact\Query\Resultset;
use Picqer\Tests\Support\MocksExactConnection;

class ModelTest extends TestCase
{
use MocksExactConnection;

public function testCanFindModel()
{
$handler = $this->createMockHandlerUsingFixture('item.json');
$connection = $this->createMockConnection($handler);

$response = (new Item($connection))->find('00000000-0000-0000-0000-000000000000');

$this->assertInstanceOf(Item::class, $response);
$this->assertEquals('00000000-0000-0000-0000-000000000000', $response->primaryKeyContent());
}

public function testCanGetFirstModel()
{
$handler = $this->createMockHandlerUsingFixture('item.json');
$connection = $this->createMockConnection($handler);

$response = (new Item($connection))->first();

$this->assertInstanceOf(Item::class, $response);
$this->assertEquals('00000000-0000-0000-0000-000000000000', $response->primaryKeyContent());
}

public function testCanGetModels()
{
$handler = $this->createMockHandlerUsingFixture('items.json');
Expand All @@ -27,9 +51,70 @@ public function testCanGetModelsAsGenerator()
$handler = $this->createMockHandlerUsingFixture('items.json');
$connection = $this->createMockConnection($handler);

$response = (new Item($connection))->getGenerator();
$response = (new Item($connection))->getAsGenerator();

$this->assertInstanceOf(Generator::class, $response);
$this->assertCount(2, $response);
}

public function testCanFilterModels()
{
$handler = $this->createMockHandlerUsingFixture('items.json');
$connection = $this->createMockConnection($handler);

$response = (new Item($connection))->filter('IsWebshopItem eq 0');

$this->assertIsArray($response);
$this->assertInstanceOf(Item::class, $response[0]);
$this->assertCount(2, $response);
}

public function testCanFilterModelsAsGenerator()
{
$handler = $this->createMockHandlerUsingFixture('items.json');
$connection = $this->createMockConnection($handler);

$response = (new Item($connection))->filterAsGenerator('IsWebshopItem eq 0');

$this->assertIsIterable($response);
$this->assertInstanceOf(Generator::class, $response);
$this->assertCount(2, $response);
}

public function testCanGetCollectionFromResult()
{
$handler = $this->createMockHandlerUsingFixture('items.json');
$connection = $this->createMockConnection($handler);
$item = new Item($connection);

$result = $connection->get($item->url(), []);
$collection = $item->collectionFromResult($result);

$this->assertIsArray($collection);
$this->assertInstanceOf(Item::class, $collection[0]);
$this->assertCount(2, $collection);
}

public function testCanGetCollectionFromResultAsGenerator()
{
$handler = $this->createMockHandlerUsingFixture('items.json');
$connection = $this->createMockConnection($handler);
$item = new Item($connection);

$result = $connection->get($item->url(), []);
$collection = $item->collectionFromResultAsGenerator($result);

$this->assertInstanceOf(Generator::class, $collection);
$this->assertCount(2, $collection);
}

public function testCanGetResultSet()
{
$handler = $this->createMockHandler();
$connection = $this->createMockConnection($handler);
$item = new Item($connection);

$resultset = (new Item($connection))->getResultSet();

$this->assertInstanceOf(Resultset::class, $resultset);
}
}
2 changes: 1 addition & 1 deletion tests/ResultsetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function testCanGetNextAsGenerator()
'logistics/Items',
Item::class,
[]
))->nextGenerator();
))->nextAsGenerator();

$this->assertIsIterable($response);
$this->assertCount(2, $response);
Expand Down
111 changes: 111 additions & 0 deletions tests/fixtures/item.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
{
"d": [
{
"__metadata": {
"uri": "https://start.exactonline.be/api/v1/123456/logistics/Items(guid'00000000-0000-0000-0000-000000000000')",
"type": "Exact.Web.Api.Models.Item"
},
"ID": "00000000-0000-0000-0000-000000000000",
"IsSerialNumberItem": null,
"IsBatchNumberItem": 0,
"Class_01": null,
"Class_02": null,
"Class_03": null,
"Class_04": null,
"Class_05": null,
"Class_06": null,
"Class_07": null,
"Class_08": null,
"Class_09": null,
"Class_10": null,
"Code": "12345678",
"CopyRemarks": 0,
"CostPriceCurrency": "EUR",
"CostPriceNew": null,
"CostPriceStandard": 6,
"AverageCost": null,
"Created": "/Date(1641034800000)/",
"CreatorFullName": null,
"Creator": "00000000-0000-0000-0000-000000000000",
"Description": "Grenen Balk 10 x 10 x 200",
"Division": 123456,
"EndDate": "/Date(1641034800000)/",
"ExtraDescription": "Pijl rechts",
"Barcode": null,
"FreeBoolField_01": false,
"FreeBoolField_02": false,
"FreeBoolField_03": false,
"FreeBoolField_04": false,
"FreeBoolField_05": false,
"FreeDateField_01": null,
"FreeDateField_02": null,
"FreeDateField_03": null,
"FreeDateField_04": null,
"FreeDateField_05": null,
"FreeNumberField_01": null,
"FreeNumberField_02": null,
"FreeNumberField_03": null,
"FreeNumberField_04": null,
"FreeNumberField_05": null,
"FreeNumberField_06": null,
"FreeNumberField_07": null,
"FreeNumberField_08": null,
"FreeTextField_01": null,
"FreeTextField_02": null,
"FreeTextField_03": null,
"FreeTextField_04": null,
"FreeTextField_05": null,
"FreeTextField_06": null,
"FreeTextField_07": null,
"FreeTextField_08": null,
"FreeTextField_09": null,
"FreeTextField_10": null,
"GLCostsCode": null,
"GLCostsDescription": null,
"GLCosts": null,
"GLRevenueCode": null,
"GLRevenueDescription": null,
"GLRevenue": null,
"GLStockCode": null,
"GLStockDescription": null,
"GLStock": null,
"IsBatchItem": 0,
"IsFractionAllowedItem": true,
"IsMakeItem": 0,
"IsNewContract": 0,
"IsOnDemandItem": 1,
"IsPackageItem": false,
"IsPurchaseItem": true,
"IsRegistrationCodeItem": 0,
"IsSalesItem": false,
"IsSerialItem": false,
"IsStockItem": true,
"IsSubcontractedItem": false,
"IsTaxableItem": null,
"IsTime": 0,
"IsWebshopItem": 0,
"ItemGroupCode": "GROND",
"ItemGroupDescription": "Grondstoffen",
"ItemGroup": "00000000-0000-0000-0000-000000000000",
"Modified": "/Date(1641034800000)/",
"ModifierFullName": null,
"Modifier": "00000000-0000-0000-0000-000000000000",
"GrossWeight": null,
"NetWeight": null,
"NetWeightUnit": null,
"Notes": null,
"PictureName": "250x250 Grenen.jpg",
"PictureUrl": "https://start.exactonline.be/docs/SysImage.aspx?Table=Items&ID=00000000-0000-0000-0000-000000000000&ThumbSize=500&NoCache=1&OptimizeForWeb=1&_Division_=123456",
"PictureThumbnailUrl": "https://start.exactonline.be/docs/SysImage.aspx?Table=Items&ID=00000000-0000-0000-0000-000000000000&ThumbSize=100&NoCache=1&OptimizeForWeb=1&_Division_=123456",
"SalesVatCodeDescription": "Binnenland exclusief 21%",
"SalesVatCode": "5 ",
"SearchCode": null,
"SecurityLevel": 10,
"StartDate": "/Date(1641034800000)/",
"Stock": 0,
"Unit": "stuk ",
"UnitDescription": "pi\u00e8ce",
"UnitType": "O"
}
]
}

0 comments on commit c18472a

Please sign in to comment.