|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Sales\Model\Service; |
| 9 | + |
| 10 | +use Magento\Sales\Api\Data\OrderInterface; |
| 11 | +use Magento\Sales\Model\Order; |
| 12 | +use Magento\TestFramework\Helper\Bootstrap; |
| 13 | + |
| 14 | +/** |
| 15 | + * Tests \Magento\Sales\Model\Service\InvoiceService |
| 16 | + */ |
| 17 | +class InvoiceServiceTest extends \PHPUnit\Framework\TestCase |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var InvoiceService |
| 21 | + */ |
| 22 | + private $invoiceService; |
| 23 | + |
| 24 | + /** |
| 25 | + * @inheritdoc |
| 26 | + */ |
| 27 | + protected function setUp() |
| 28 | + { |
| 29 | + $this->invoiceService = Bootstrap::getObjectManager()->create(InvoiceService::class); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @param int $invoiceQty |
| 34 | + * @magentoDataFixture Magento/Sales/_files/order_configurable_product.php |
| 35 | + * @return void |
| 36 | + * @dataProvider prepareInvoiceConfigurableProductDataProvider |
| 37 | + */ |
| 38 | + public function testPrepareInvoiceConfigurableProduct(int $invoiceQty): void |
| 39 | + { |
| 40 | + /** @var OrderInterface $order */ |
| 41 | + $order = Bootstrap::getObjectManager()->create(Order::class)->load('100000001', 'increment_id'); |
| 42 | + $orderItems = $order->getItems(); |
| 43 | + foreach ($orderItems as $orderItem) { |
| 44 | + if ($orderItem->getParentItemId()) { |
| 45 | + $parentItemId = $orderItem->getParentItemId(); |
| 46 | + } |
| 47 | + } |
| 48 | + $invoice = $this->invoiceService->prepareInvoice($order, [$parentItemId => $invoiceQty]); |
| 49 | + $invoiceItems = $invoice->getItems(); |
| 50 | + foreach ($invoiceItems as $invoiceItem) { |
| 51 | + $this->assertEquals($invoiceQty, $invoiceItem->getQty()); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public function prepareInvoiceConfigurableProductDataProvider() |
| 56 | + { |
| 57 | + return [ |
| 58 | + 'full invoice' => [2], |
| 59 | + 'partial invoice' => [1] |
| 60 | + ]; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @param int $invoiceQty |
| 65 | + * @magentoDataFixture Magento/Sales/_files/order.php |
| 66 | + * @return void |
| 67 | + * @dataProvider prepareInvoiceSimpleProductDataProvider |
| 68 | + */ |
| 69 | + public function testPrepareInvoiceSimpleProduct(int $invoiceQty): void |
| 70 | + { |
| 71 | + /** @var OrderInterface $order */ |
| 72 | + $order = Bootstrap::getObjectManager()->create(Order::class)->load('100000001', 'increment_id'); |
| 73 | + $orderItems = $order->getItems(); |
| 74 | + $invoiceQtys = []; |
| 75 | + foreach ($orderItems as $orderItem) { |
| 76 | + $invoiceQtys[$orderItem->getItemId()] = $invoiceQty; |
| 77 | + } |
| 78 | + $invoice = $this->invoiceService->prepareInvoice($order, $invoiceQtys); |
| 79 | + $invoiceItems = $invoice->getItems(); |
| 80 | + foreach ($invoiceItems as $invoiceItem) { |
| 81 | + $this->assertEquals($invoiceQty, $invoiceItem->getQty()); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + public function prepareInvoiceSimpleProductDataProvider() |
| 86 | + { |
| 87 | + return [ |
| 88 | + 'full invoice' => [2], |
| 89 | + 'partial invoice' => [1] |
| 90 | + ]; |
| 91 | + } |
| 92 | +} |
0 commit comments