From 5756ed3f4b209e68018c38c777ac39a8a326cae2 Mon Sep 17 00:00:00 2001 From: juhopekkavuorela Date: Mon, 24 Apr 2023 15:15:15 +0300 Subject: [PATCH] Allow zero amount items --- .github/workflows/run-workflow.yaml | 4 +-- src/Model/Item.php | 7 +++-- tests/ClientTest.php | 3 +- tests/Model/ItemTest.php | 46 +++++++++++++++++++++-------- 4 files changed, 42 insertions(+), 18 deletions(-) diff --git a/.github/workflows/run-workflow.yaml b/.github/workflows/run-workflow.yaml index 84dd810..94b2bf7 100644 --- a/.github/workflows/run-workflow.yaml +++ b/.github/workflows/run-workflow.yaml @@ -31,7 +31,7 @@ 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 @@ -39,7 +39,7 @@ jobs: 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 diff --git a/src/Model/Item.php b/src/Model/Item.php index 818e3d7..3847dca 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -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) { diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 2ca2a28..098d302 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -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() diff --git a/tests/Model/ItemTest.php b/tests/Model/ItemTest.php index 53a56ef..b544c34 100644 --- a/tests/Model/ItemTest.php +++ b/tests/Model/ItemTest.php @@ -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() @@ -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); @@ -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); + } }