Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow zero amount items #79

Merged
merged 1 commit into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/run-workflow.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ jobs:
run: composer lint

- name: Run phpstan
uses: php-actions/phpstan@v3.0.1
uses: php-actions/phpstan@v3
with:
php_version: ${{ matrix.php-version }}
version: 1.9.14
configuration: phpstan.neon
memory_limit: 256M

- name: Run tests
uses: php-actions/phpunit@v3.0.0
uses: php-actions/phpunit@v3
with:
php_version: ${{ matrix.php-version }}
version: 9
Expand Down
7 changes: 5 additions & 2 deletions src/Model/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -376,13 +376,16 @@ public function validate()
{
$props = get_object_vars($this);

if (empty($props['unitPrice'])) {
if ($props['unitPrice'] === null) {
throw new ValidationException('Item unitPrice is empty');
}
if ($props['unitPrice'] < 0) {
throw new ValidationException('Items unitPrice can\'t be a negative number');
}
if (empty($props['units'])) {
if ($props['unitPrice'] > 99999999) {
throw new ValidationException('Items unitPrice can\'t be over 99999999');
}
if ($props['units'] === null) {
throw new ValidationException('Item units is empty');
}
if ($props['units'] < 0) {
Expand Down
3 changes: 2 additions & 1 deletion tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,8 @@ public function testRequestSettlementsWithInvalidDateThrowsException()
public function testRequestSettlementsReturnsValidResponse()
{
$settlementRequest = (new SettlementRequest());
$this->client->requestSettlements($settlementRequest);
$settlementResponse = $this->client->requestSettlements($settlementRequest);
$this->assertIsArray($settlementResponse->getSettlements());
}

public function testGetSettlementsReturnsValidResponse()
Expand Down
46 changes: 33 additions & 13 deletions tests/Model/ItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function testIsItemValid()
->setDeliveryDate('2023-01-01')
->setDescription('description');

$this->assertEquals(true, $item->validate());
$this->assertTrue($item->validate());
}

public function testItemWithoutUnitPriceThrowsError()
Expand All @@ -34,18 +34,6 @@ public function testItemWithoutUnitPriceThrowsError()
->validate();
}

public function testItemWithNegativeUnitPriceThrowsError()
{
$this->expectException(ValidationException::class);
(new Item())->setUnitPrice(-1)
->setUnits(2)
->setStamp('thisIsStamp')
->setVatPercentage(0)
->setProductCode('productCode123')
->setDescription('description')
->validate();
}

public function testItemWithoutUnitsTrowsError()
{
$this->expectException(ValidationException::class);
Expand Down Expand Up @@ -91,4 +79,36 @@ public function testItemWithoutProductCodeThrowsError()
->setDescription('description')
->validate();
}

public static function providerForUnitPriceLimitValues()
{
return [
'Negative amount' => [-1, false],
'Zero amount' => [0, true],
'Maximum amount' => [99999999, true],
'Over maximum amount' => [100000000, false]
];
}

/**
* @dataProvider providerForUnitPriceLimitValues
*/
public function testUnitPriceLimitValues($unitPrice, $expectedResult)
{
$item = (new Item())->setUnitPrice($unitPrice)
->setUnits(2)
->setStamp('thisIsStamp')
->setVatPercentage(0)
->setProductCode('productCode123')
->setDeliveryDate('2023-01-01')
->setDescription('description');

try {
$validationResult = $item->validate();
} catch (ValidationException $exception) {
$validationResult = false;
}

$this->assertEquals($expectedResult, $validationResult);
}
}