diff --git a/modules.local/ppcp-api-client/tests/PHPUnit/Entity/AddressTest.php b/modules.local/ppcp-api-client/tests/PHPUnit/Entity/AddressTest.php new file mode 100644 index 000000000..5c00e2954 --- /dev/null +++ b/modules.local/ppcp-api-client/tests/PHPUnit/Entity/AddressTest.php @@ -0,0 +1,52 @@ +assertEquals('countryCode', $testee->countryCode()); + $this->assertEquals('addressLine1', $testee->addressLine1()); + $this->assertEquals('addressLine2', $testee->addressLine2()); + $this->assertEquals('adminArea1', $testee->adminArea1()); + $this->assertEquals('adminArea2', $testee->adminArea2()); + $this->assertEquals('postalCode', $testee->postalCode()); + } + public function testToArray() + { + $testee = new Address( + 'countryCode', + 'addressLine1', + 'addressLine2', + 'adminArea1', + 'adminArea2', + 'postalCode' + ); + + $expected = [ + 'country_code' => 'countryCode', + 'address_line_1' => 'addressLine1', + 'address_line_2' => 'addressLine2', + 'admin_area_1' => 'adminArea1', + 'admin_area_2' => 'adminArea2', + 'postal_code' => 'postalCode', + ]; + + $actual = $testee->toArray(); + $this->assertEquals($expected, $actual); + } +} \ No newline at end of file diff --git a/modules.local/ppcp-api-client/tests/PHPUnit/Entity/AmountBreakdownTest.php b/modules.local/ppcp-api-client/tests/PHPUnit/Entity/AmountBreakdownTest.php new file mode 100644 index 000000000..bc18959ba --- /dev/null +++ b/modules.local/ppcp-api-client/tests/PHPUnit/Entity/AmountBreakdownTest.php @@ -0,0 +1,122 @@ +expects('toArray')->andReturn(['itemTotal']); + $shipping = \Mockery::mock(Money::class); + $shipping + ->expects('toArray')->andReturn(['shipping']); + $taxTotal = \Mockery::mock(Money::class); + $taxTotal + ->expects('toArray')->andReturn(['taxTotal']); + $handling = \Mockery::mock(Money::class); + $handling + ->expects('toArray')->andReturn(['handling']); + $insurance = \Mockery::mock(Money::class); + $insurance + ->expects('toArray')->andReturn(['insurance']); + $shippingDiscount = \Mockery::mock(Money::class); + $shippingDiscount + ->expects('toArray')->andReturn(['shippingDiscount']); + $discount = \Mockery::mock(Money::class); + $discount + ->expects('toArray')->andReturn(['discount']); + $testee = new AmountBreakdown( + $itemTotal, + $shipping, + $taxTotal, + $handling, + $insurance, + $shippingDiscount, + $discount + ); + + $this->assertEquals($itemTotal, $testee->itemTotal()); + $this->assertEquals($shipping, $testee->shipping()); + $this->assertEquals($taxTotal, $testee->taxTotal()); + $this->assertEquals($handling, $testee->handling()); + $this->assertEquals($insurance, $testee->insurance()); + $this->assertEquals($shippingDiscount, $testee->shippingDiscount()); + $this->assertEquals($discount, $testee->discount()); + + $expected = [ + 'item_total' => ['itemTotal'], + 'shipping' => ['shipping'], + 'tax_total' => ['taxTotal'], + 'handling' => ['handling'], + 'insurance' => ['insurance'], + 'shipping_discount' => ['shippingDiscount'], + 'discount' => ['discount'], + ]; + + $this->assertEquals($expected, $testee->toArray()); + } + + /** + * @dataProvider dataDropArrayKeyIfNoValueGiven + */ + public function testDropArrayKeyIfNoValueGiven($keyMissing, $methodName) { + + $itemTotal = \Mockery::mock(Money::class); + $itemTotal + ->expects('toArray')->andReturn(['itemTotal']); + $shipping = \Mockery::mock(Money::class); + $shipping + ->expects('toArray')->andReturn(['shipping']); + $taxTotal = \Mockery::mock(Money::class); + $taxTotal + ->expects('toArray')->andReturn(['taxTotal']); + $handling = \Mockery::mock(Money::class); + $handling + ->expects('toArray')->andReturn(['handling']); + $insurance = \Mockery::mock(Money::class); + $insurance + ->expects('toArray')->andReturn(['insurance']); + $shippingDiscount = \Mockery::mock(Money::class); + $shippingDiscount + ->expects('toArray')->andReturn(['shippingDiscount']); + $discount = \Mockery::mock(Money::class); + $discount + ->expects('toArray')->andReturn(['discount']); + + + $items = [ + 'item_total' => $itemTotal, + 'shipping' => $shipping, + 'tax_total' => $taxTotal, + 'handling' => $handling, + 'insurance' => $insurance, + 'shipping_discount' => $shippingDiscount, + 'discount' => $discount, + ]; + $items[$keyMissing] = null; + + $testee = new AmountBreakdown(...array_values($items)); + $array = $testee->toArray(); + $result = ! array_key_exists($keyMissing, $array); + $this->assertTrue($result); + $this->assertNull($testee->{$methodName}(), "$methodName should return null"); + } + + public function dataDropArrayKeyIfNoValueGiven() : array { + return [ + ['item_total', 'itemTotal'], + ['shipping', 'shipping'], + ['tax_total', 'taxTotal'], + ['handling', 'handling'], + ['insurance', 'insurance'], + ['shipping_discount', 'shippingDiscount'], + ['discount', 'discount'], + ]; + } +} \ No newline at end of file diff --git a/modules.local/ppcp-api-client/tests/PHPUnit/Entity/AmountTest.php b/modules.local/ppcp-api-client/tests/PHPUnit/Entity/AmountTest.php new file mode 100644 index 000000000..306e3336e --- /dev/null +++ b/modules.local/ppcp-api-client/tests/PHPUnit/Entity/AmountTest.php @@ -0,0 +1,52 @@ +expects('currencyCode')->andReturn('currencyCode'); + $money->expects('value')->andReturn(1.10); + $testee = new Amount($money); + + $this->assertEquals('currencyCode', $testee->currencyCode()); + $this->assertEquals(1.10, $testee->value()); + } + public function testBreakdownIsNull() { + $money = \Mockery::mock(Money::class); + $money->expects('currencyCode')->andReturn('currencyCode'); + $money->expects('value')->andReturn(1.10); + $testee = new Amount($money); + + $this->assertNull($testee->breakdown()); + + $expectedArray = [ + 'currency_code' => 'currencyCode', + 'value' => 1.10, + ]; + $this->assertEquals($expectedArray, $testee->toArray()); + } + public function testBreakdown() { + $money = \Mockery::mock(Money::class); + $money->expects('currencyCode')->andReturn('currencyCode'); + $money->expects('value')->andReturn(1.10); + $breakdown = \Mockery::mock(AmountBreakdown::class); + $breakdown->expects('toArray')->andReturn([1]); + $testee = new Amount($money, $breakdown); + + $this->assertEquals($breakdown, $testee->breakdown()); + + $expectedArray = [ + 'currency_code' => 'currencyCode', + 'value' => 1.10, + 'breakdown' => [1] + ]; + $this->assertEquals($expectedArray, $testee->toArray()); + } +} \ No newline at end of file diff --git a/modules.local/ppcp-api-client/tests/PHPUnit/Entity/ItemTest.php b/modules.local/ppcp-api-client/tests/PHPUnit/Entity/ItemTest.php new file mode 100644 index 000000000..a8e6574c4 --- /dev/null +++ b/modules.local/ppcp-api-client/tests/PHPUnit/Entity/ItemTest.php @@ -0,0 +1,82 @@ +assertEquals('name', $testee->name()); + $this->assertEquals($unitAmount, $testee->unitAmount()); + $this->assertEquals(1, $testee->quantity()); + $this->assertEquals('description', $testee->description()); + $this->assertEquals($tax, $testee->tax()); + $this->assertEquals('sku', $testee->sku()); + $this->assertEquals('PHYSICAL_GOODS', $testee->category()); + } + + public function testDigitalGoodsCategory() { + $unitAmount = \Mockery::mock(Money::class); + $tax = \Mockery::mock(Money::class); + $testee = new Item( + 'name', + $unitAmount, + 1, + 'description', + $tax, + 'sku', + 'DIGITAL_GOODS' + ); + + $this->assertEquals('DIGITAL_GOODS', $testee->category()); + } + + public function testToArray() { + + $unitAmount = \Mockery::mock(Money::class); + $unitAmount + ->expects('toArray') + ->andReturn([1]); + $tax = \Mockery::mock(Money::class); + $tax + ->expects('toArray') + ->andReturn([2]); + $testee = new Item( + 'name', + $unitAmount, + 1, + 'description', + $tax, + 'sku', + 'PHYSICAL_GOODS' + ); + + $expected = [ + 'name' => 'name', + 'unit_amount' => [1], + 'quantity' => 1, + 'description' => 'description', + 'sku' => 'sku', + 'category' => 'PHYSICAL_GOODS', + 'tax' => [2], + ]; + + $this->assertEquals($expected, $testee->toArray()); + } +} \ No newline at end of file diff --git a/modules.local/ppcp-api-client/tests/PHPUnit/Entity/MoneyTest.php b/modules.local/ppcp-api-client/tests/PHPUnit/Entity/MoneyTest.php new file mode 100644 index 000000000..347210200 --- /dev/null +++ b/modules.local/ppcp-api-client/tests/PHPUnit/Entity/MoneyTest.php @@ -0,0 +1,24 @@ +assertEquals(1.10, $testee->value()); + $this->assertEquals('currencyCode', $testee->currencyCode()); + + $expected = [ + 'currency_code' => 'currencyCode', + 'value' => 1.10, + ]; + $this->assertEquals($expected, $testee->toArray()); + } +} \ No newline at end of file diff --git a/modules.local/ppcp-api-client/tests/PHPUnit/Entity/PurchaseUnitTest.php b/modules.local/ppcp-api-client/tests/PHPUnit/Entity/PurchaseUnitTest.php new file mode 100644 index 000000000..42b55e9b2 --- /dev/null +++ b/modules.local/ppcp-api-client/tests/PHPUnit/Entity/PurchaseUnitTest.php @@ -0,0 +1,191 @@ +expects('breakdown')->andReturnNull(); + $amount->expects('toArray')->andReturn(['amount']); + $item1 = \Mockery::mock(Item::class); + $item1->expects('toArray')->andReturn(['item1']); + $item2 = \Mockery::mock(Item::class); + $item2->expects('toArray')->andReturn(['item2']); + $shipping = \Mockery::mock(Shipping::class); + $shipping->expects('toArray')->andReturn(['shipping']); + $testee = new PurchaseUnit( + $amount, + [], + $shipping, + 'referenceId', + 'description', + null, + 'customId', + 'invoiceId', + 'softDescriptor' + ); + + $this->assertEquals($amount, $testee->amount()); + $this->assertEquals('referenceId', $testee->referenceId()); + $this->assertEquals('description', $testee->description()); + $this->assertNull($testee->payee()); + $this->assertEquals('customId', $testee->customId()); + $this->assertEquals('invoiceId', $testee->invoiceId()); + $this->assertEquals('softDescriptor', $testee->softDescriptor()); + $this->assertEquals($shipping, $testee->shipping()); + $this->assertEquals([], $testee->items()); + + $expected = [ + 'reference_id' => 'referenceId', + 'amount' => ['amount'], + 'description' => 'description', + 'items' => [], + 'shipping' => ['shipping'], + 'custom_id' => 'customId', + 'invoice_id' => 'invoiceId', + 'soft_descriptor' => 'softDescriptor', + ]; + + $this->assertEquals($expected, $testee->toArray()); + } + + public function testDontDitchBecauseOfBreakdown() { + + $breakdown = \Mockery::mock(AmountBreakdown::class); + $breakdown->expects('shipping')->andReturnNull(); + $breakdown->expects('itemTotal')->andReturnNull(); + $breakdown->expects('discount')->andReturnNull(); + $breakdown->expects('taxTotal')->andReturnNull(); + $breakdown->expects('shippingDiscount')->andReturnNull(); + $breakdown->expects('handling')->andReturnNull(); + $breakdown->expects('insurance')->andReturnNull(); + $amount = \Mockery::mock(Amount::class); + $amount->expects('breakdown')->andReturn($breakdown); + $amount->expects('value')->andReturn(0.0); + $amount->expects('toArray')->andReturn(['amount']); + $item1 = \Mockery::mock(Item::class); + $item1->expects('toArray')->andReturn(['item1']); + $item2 = \Mockery::mock(Item::class); + $item2->expects('toArray')->andReturn(['item2']); + $shipping = \Mockery::mock(Shipping::class); + $shipping->expects('toArray')->andReturn(['shipping']); + $testee = new PurchaseUnit( + $amount, + [], + $shipping, + 'referenceId', + 'description', + null, + 'customId', + 'invoiceId', + 'softDescriptor' + ); + + $expected = [ + 'reference_id' => 'referenceId', + 'amount' => ['amount'], + 'description' => 'description', + 'shipping' => ['shipping'], + 'custom_id' => 'customId', + 'items' => [], + 'invoice_id' => 'invoiceId', + 'soft_descriptor' => 'softDescriptor', + ]; + + $this->assertEquals($expected, $testee->toArray()); + } + + public function testDitchBecauseOfBreakdown() { + + $breakdown = \Mockery::mock(AmountBreakdown::class); + $breakdown->expects('shipping')->andReturnNull(); + $breakdown->expects('itemTotal')->andReturnNull(); + $breakdown->expects('discount')->andReturnNull(); + $breakdown->expects('taxTotal')->andReturnNull(); + $breakdown->expects('shippingDiscount')->andReturnNull(); + $breakdown->expects('handling')->andReturnNull(); + $breakdown->expects('insurance')->andReturnNull(); + $amount = \Mockery::mock(Amount::class); + $amount->expects('breakdown')->andReturn($breakdown); + $amount->expects('value')->andReturn(1.00); + $amount->expects('toArray')->andReturn(['amount']); + $item1 = \Mockery::mock(Item::class); + $item1->expects('toArray')->andReturn(['item1']); + $item2 = \Mockery::mock(Item::class); + $item2->expects('toArray')->andReturn(['item2']); + $shipping = \Mockery::mock(Shipping::class); + $shipping->expects('toArray')->andReturn(['shipping']); + $testee = new PurchaseUnit( + $amount, + [], + $shipping, + 'referenceId', + 'description', + null, + 'customId', + 'invoiceId', + 'softDescriptor' + ); + + $expected = [ + 'reference_id' => 'referenceId', + 'amount' => ['amount'], + 'description' => 'description', + 'shipping' => ['shipping'], + 'custom_id' => 'customId', + 'invoice_id' => 'invoiceId', + 'soft_descriptor' => 'softDescriptor', + ]; + + $this->assertEquals($expected, $testee->toArray()); + } + + public function testPayee() { + + $amount = \Mockery::mock(Amount::class); + $amount->expects('breakdown')->andReturnNull(); + $amount->expects('toArray')->andReturn(['amount']); + $item1 = \Mockery::mock(Item::class); + $item1->expects('toArray')->andReturn(['item1']); + $item2 = \Mockery::mock(Item::class); + $item2->expects('toArray')->andReturn(['item2']); + $shipping = \Mockery::mock(Shipping::class); + $shipping->expects('toArray')->andReturn(['shipping']); + $payee = \Mockery::mock(Payee::class); + $payee->expects('toArray')->andReturn(['payee']); + $testee = new PurchaseUnit( + $amount, + [], + $shipping, + 'referenceId', + 'description', + $payee, + 'customId', + 'invoiceId', + 'softDescriptor' + ); + + $this->assertEquals($payee, $testee->payee()); + + $expected = [ + 'reference_id' => 'referenceId', + 'amount' => ['amount'], + 'description' => 'description', + 'items' => [], + 'shipping' => ['shipping'], + 'custom_id' => 'customId', + 'invoice_id' => 'invoiceId', + 'soft_descriptor' => 'softDescriptor', + 'payee' => ['payee'], + ]; + + $this->assertEquals($expected, $testee->toArray()); + } +} \ No newline at end of file