OrderGetTest.php 6.76 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Magento\Sales\Service\V1;

use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Webapi\Rest\Request;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\Order;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\WebapiAbstract;

class OrderGetTest extends WebapiAbstract
{
    const RESOURCE_PATH = '/V1/orders';

    const SERVICE_READ_NAME = 'salesOrderRepositoryV1';

    const SERVICE_VERSION = 'V1';

    const ORDER_INCREMENT_ID = '100000001';

    /**
     * @var ObjectManagerInterface
     */
    private $objectManager;

    /**
     * @inheritdoc
     */
    protected function setUp(): void
    {
        $this->objectManager = Bootstrap::getObjectManager();
    }

    /**
     * Checks order attributes.
     *
     * @magentoApiDataFixture Magento/Sales/_files/order.php
     */
    public function testOrderGet(): void
    {
        $expectedOrderData = [
            'base_subtotal' => '100.0000',
            'subtotal' => '100.0000',
            'customer_is_guest' => '1',
            'increment_id' => self::ORDER_INCREMENT_ID,
        ];
        $expectedPayments = [
            'method' => 'checkmo',
            'additional_information' => [
                0 => '11122', // last transaction id
                // metadata
                1 => json_encode([
                    'type' => 'free',
                    'fraudulent' => false
                ])
            ]
        ];
        $expectedBillingAddressNotEmpty = [
            'city',
            'postcode',
            'lastname',
            'street',
            'region',
            'telephone',
            'country_id',
            'firstname',
        ];
        $expectedShippingAddress = [
            'address_type' => 'shipping',
            'city' => 'Los Angeles',
            'email' => 'customer@null.com',
            'postcode' => '11111',
            'region' => 'CA'
        ];

        $result = $this->makeServiceCall(self::ORDER_INCREMENT_ID);

        foreach ($expectedOrderData as $field => $value) {
            self::assertArrayHasKey($field, $result);
            self::assertEquals($value, $result[$field]);
        }

        self::assertArrayHasKey('payment', $result);
        foreach ($expectedPayments as $field => $value) {
            self::assertEquals($value, $result['payment'][$field]);
        }

        self::assertArrayHasKey('billing_address', $result);
        foreach ($expectedBillingAddressNotEmpty as $field) {
            self::assertArrayHasKey($field, $result['billing_address']);
        }

        self::assertArrayHasKey('extension_attributes', $result);
        self::assertArrayHasKey('shipping_assignments', $result['extension_attributes']);

        $shippingAssignments = $result['extension_attributes']['shipping_assignments'];
        self::assertCount(1, $shippingAssignments);
        $shippingAddress = $shippingAssignments[0]['shipping']['address'];
        foreach ($expectedShippingAddress as $key => $value) {
            self::assertArrayHasKey($key, $shippingAddress);
            self::assertEquals($value, $shippingAddress[$key]);
        }

        //check that nullable fields were marked as optional and were not sent
        foreach ($result as $value) {
            self::assertNotNull($value);
        }
    }

    /**
     * Checks order extension attributes.
     *
     * @magentoApiDataFixture Magento/Sales/_files/order_with_tax.php
     */
    public function testOrderGetExtensionAttributes(): void
    {
        $expectedTax = [
            'code' => 'US-NY-*-Rate 1',
            'type' => 'shipping'
        ];

        $result = $this->makeServiceCall(self::ORDER_INCREMENT_ID);

        $appliedTaxes = $result['extension_attributes']['applied_taxes'];
        self::assertEquals($expectedTax['code'], $appliedTaxes[0]['code']);
        $appliedTaxes = $result['extension_attributes']['item_applied_taxes'];
        self::assertEquals($expectedTax['type'], $appliedTaxes[0]['type']);
        self::assertNotEmpty($appliedTaxes[0]['applied_taxes']);
        self::assertEquals(true, $result['extension_attributes']['converting_from_quote']);
        self::assertArrayHasKey('payment_additional_info', $result['extension_attributes']);
        self::assertNotEmpty($result['extension_attributes']['payment_additional_info']);
    }

    /**
     * Checks if the order contains product option attributes.
     *
     * @magentoApiDataFixture Magento/Sales/_files/order_with_bundle.php
     */
    public function testGetOrderWithProductOption(): void
    {
        $expected = [
            'extension_attributes' => [
                'bundle_options' => [
                    [
                        'option_id' => 1,
                        'option_selections' => [1],
                        'option_qty' => 1
                    ]
                ]
            ]
        ];
        $result = $this->makeServiceCall(self::ORDER_INCREMENT_ID);

        $bundleProduct = $this->getBundleProduct($result['items']);
        self::assertNotEmpty($bundleProduct, '"Bundle Product" should not be empty.');
        self::assertNotEmpty($bundleProduct['product_option'], '"Product Option" should not be empty.');
        self::assertEquals($expected, $bundleProduct['product_option']);
    }

    /**
     * Gets order by increment ID.
     *
     * @param string $incrementId
     * @return OrderInterface
     */
    private function getOrder(string $incrementId): OrderInterface
    {
        /** @var Order $order */
        $order = $this->objectManager->create(Order::class);
        $order->loadByIncrementId($incrementId);

        return $order;
    }

    /**
     * Makes service call.
     *
     * @param string $incrementId
     * @return array
     */
    private function makeServiceCall(string $incrementId): array
    {
        $order = $this->getOrder($incrementId);
        $serviceInfo = [
            'rest' => [
                'resourcePath' => self::RESOURCE_PATH . '/' . $order->getId(),
                'httpMethod' => Request::HTTP_METHOD_GET,
            ],
            'soap' => [
                'service' => self::SERVICE_READ_NAME,
                'serviceVersion' => self::SERVICE_VERSION,
                'operation' => self::SERVICE_READ_NAME . 'get',
            ],
        ];
        return $this->_webApiCall($serviceInfo, ['id' => $order->getId()]);
    }

    /**
     * Gets a bundle product from the result.
     *
     * @param array $items
     * @return array
     */
    private function getBundleProduct(array $items): array
    {
        foreach ($items as $item) {
            if ($item['product_type'] == 'bundle') {
                return $item;
            }
        }

        return [];
    }
}