|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +namespace Magento\Bundle\V1; |
| 7 | + |
| 8 | +/** |
| 9 | + * API test for creation of Invoice for order with bundle product. |
| 10 | + */ |
| 11 | +class OrderInvoiceCreateTest extends \Magento\TestFramework\TestCase\WebapiAbstract |
| 12 | +{ |
| 13 | + const SERVICE_READ_NAME = 'salesInvoiceOrderV1'; |
| 14 | + const SERVICE_VERSION = 'V1'; |
| 15 | + |
| 16 | + /** |
| 17 | + * @var \Magento\Framework\ObjectManagerInterface |
| 18 | + */ |
| 19 | + private $objectManager; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var \Magento\Sales\Api\InvoiceRepositoryInterface |
| 23 | + */ |
| 24 | + private $invoiceRepository; |
| 25 | + |
| 26 | + /** |
| 27 | + * Set up. |
| 28 | + * |
| 29 | + * @return void |
| 30 | + */ |
| 31 | + protected function setUp() |
| 32 | + { |
| 33 | + $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 34 | + $this->invoiceRepository = $this->objectManager->get( |
| 35 | + \Magento\Sales\Api\InvoiceRepositoryInterface::class |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Test create a partial invoice for order with bundle and Simple products. |
| 41 | + * |
| 42 | + * @return void |
| 43 | + * @magentoApiDataFixture Magento/Bundle/_files/order_items_simple_and_bundle.php |
| 44 | + */ |
| 45 | + public function testInvoiceWithSimpleAndBundleCreate() |
| 46 | + { |
| 47 | + /** @var \Magento\Sales\Api\Data\OrderInterface $existingOrder*/ |
| 48 | + $existingOrder = $this->objectManager->create(\Magento\Sales\Api\Data\OrderInterface::class) |
| 49 | + ->loadByIncrementId('100000001'); |
| 50 | + |
| 51 | + $serviceInfo = [ |
| 52 | + 'rest' => [ |
| 53 | + 'resourcePath' => '/V1/order/' . $existingOrder->getId() . '/invoice', |
| 54 | + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST, |
| 55 | + ], |
| 56 | + 'soap' => [ |
| 57 | + 'service' => self::SERVICE_READ_NAME, |
| 58 | + 'serviceVersion' => self::SERVICE_VERSION, |
| 59 | + 'operation' => self::SERVICE_READ_NAME . 'execute', |
| 60 | + ], |
| 61 | + ]; |
| 62 | + |
| 63 | + $requestData = [ |
| 64 | + 'orderId' => $existingOrder->getId(), |
| 65 | + 'items' => [], |
| 66 | + 'comment' => [ |
| 67 | + 'comment' => 'Test Comment', |
| 68 | + 'is_visible_on_front' => 1, |
| 69 | + ], |
| 70 | + ]; |
| 71 | + $grantTotal = 0; |
| 72 | + foreach ($existingOrder->getAllItems() as $item) { |
| 73 | + $requestData['items'] = []; |
| 74 | + $requestData['items'][] = [ |
| 75 | + 'order_item_id' => $item->getItemId(), |
| 76 | + 'qty' => $item->getQtyOrdered(), |
| 77 | + ]; |
| 78 | + $result = $this->_webApiCall($serviceInfo, $requestData); |
| 79 | + $this->assertNotEmpty($result); |
| 80 | + try { |
| 81 | + $invoice = $this->invoiceRepository->get($result); |
| 82 | + $grantTotal += $invoice->getGrandTotal(); |
| 83 | + } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { |
| 84 | + $this->fail('Failed asserting that Invoice was created'); |
| 85 | + } |
| 86 | + } |
| 87 | + $this->assertNotEquals( |
| 88 | + $existingOrder->getGrandTotal(), |
| 89 | + $grantTotal, |
| 90 | + 'Failed asserting that invoice is correct.' |
| 91 | + ); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Test create invoice with Bundle product. |
| 96 | + * |
| 97 | + * @return void |
| 98 | + * @magentoApiDataFixture Magento/Bundle/_files/order_item_with_bundle_and_options.php |
| 99 | + */ |
| 100 | + public function testInvoiceWithBundleCreate() |
| 101 | + { |
| 102 | + /** @var \Magento\Sales\Api\Data\OrderInterface $existingOrder*/ |
| 103 | + $existingOrder = $this->objectManager->create(\Magento\Sales\Api\Data\OrderInterface::class) |
| 104 | + ->loadByIncrementId('100000001'); |
| 105 | + |
| 106 | + $serviceInfo = [ |
| 107 | + 'rest' => [ |
| 108 | + 'resourcePath' => '/V1/order/' . $existingOrder->getId() . '/invoice', |
| 109 | + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST, |
| 110 | + ], |
| 111 | + 'soap' => [ |
| 112 | + 'service' => self::SERVICE_READ_NAME, |
| 113 | + 'serviceVersion' => self::SERVICE_VERSION, |
| 114 | + 'operation' => self::SERVICE_READ_NAME . 'execute', |
| 115 | + ], |
| 116 | + ]; |
| 117 | + |
| 118 | + $requestData = [ |
| 119 | + 'orderId' => $existingOrder->getId(), |
| 120 | + 'items' => [], |
| 121 | + 'comment' => [ |
| 122 | + 'comment' => 'Test Comment', |
| 123 | + 'is_visible_on_front' => 1, |
| 124 | + ], |
| 125 | + ]; |
| 126 | + |
| 127 | + /** @var \Magento\Sales\Api\Data\OrderItemInterface $item */ |
| 128 | + foreach ($existingOrder->getAllItems() as $item) { |
| 129 | + $requestData['items'][] = [ |
| 130 | + 'order_item_id' => $item->getItemId(), |
| 131 | + 'qty' => $item->getQtyOrdered(), |
| 132 | + ]; |
| 133 | + } |
| 134 | + $result = $this->_webApiCall($serviceInfo, $requestData); |
| 135 | + $this->assertNotEmpty($result); |
| 136 | + $invoice = $this->invoiceRepository->get($result); |
| 137 | + $this->assertNotEquals( |
| 138 | + $existingOrder->getGrandTotal(), |
| 139 | + $invoice->getGrandTotal(), |
| 140 | + 'Failed asserting that invoice is correct.' |
| 141 | + ); |
| 142 | + } |
| 143 | +} |
0 commit comments