diff --git a/modules/order/src/Adjustment.php b/modules/order/src/Adjustment.php index a4d228a..329fc0c 100644 --- a/modules/order/src/Adjustment.php +++ b/modules/order/src/Adjustment.php @@ -52,16 +52,13 @@ final class Adjustment { throw new \InvalidArgumentException(sprintf('Missing required property %s.', $required_property)); } } - - $this->type = $definition['type']; - $this->label = $definition['label']; - if (!$definition['price'] instanceof Price) { throw new \InvalidArgumentException(sprintf('Property price should be an instance of %s.', Price::class)); } - else { - $this->price = $definition['price']; - } + + $this->type = $definition['type']; + $this->label = $definition['label']; + $this->price = $definition['price']; if (!empty($definition['source_id'])) { $this->sourceId = $definition['source_id']; diff --git a/modules/order/tests/src/Unit/AdjustmentTest.php b/modules/order/tests/src/Unit/AdjustmentTest.php index 5ff48c2..fb04f1a 100644 --- a/modules/order/tests/src/Unit/AdjustmentTest.php +++ b/modules/order/tests/src/Unit/AdjustmentTest.php @@ -16,8 +16,44 @@ class AdjustmentTest extends UnitTestCase { * Tests the constructor and definition checks. * * @covers ::__construct + * @dataProvider invalidDefinitionProvider */ - public function testAdjustmentConstruct() { + public function testInvalidAdjustmentConstruct($definition, $message) { + $this->setExpectedException(\InvalidArgumentException::class, $message); + new Adjustment($definition); + } + + /** + * @return array + */ + public function invalidDefinitionProvider() { + return [ + [[], 'Missing required property type'], + [['type' => 'discount'], 'Missing required property label'], + [ + [ + 'type' => 'discount', + 'label' => 'Test', + ], + 'Missing required property price', + ], + [ + [ + 'type' => 'discount', + 'label' => 'Test', + 'price' => '10 USD' + ], + sprintf('Property price should be an instance of %s.', Price::class), + ], + ]; + } + + /** + * Tests the constructor and definition checks. + * + * @covers ::__construct + */ + public function testValidAdjustmentConstruct() { $definition = [ 'type' => 'discount', 'label' => '10% off', @@ -26,11 +62,7 @@ class AdjustmentTest extends UnitTestCase { ]; $adjustment = new Adjustment($definition); - - unset($definition['label']); - - $this->setExpectedException(\InvalidArgumentException::class, 'Missing required property label'); - $adjustment = new Adjustment($definition); + $this->assertInstanceOf(Adjustment::class, $adjustment); } /**