PartialResponseTest.php 3.15 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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Webapi;

use Magento\Customer\Api\AccountManagementTest;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\Customer as CustomerHelper;

class PartialResponseTest extends \Magento\TestFramework\TestCase\WebapiAbstract
{
    /** @var CustomerHelper */
    protected $customerHelper;

    /** @var string */
    protected $customerData;

    protected function setUp()
    {
        $this->_markTestAsRestOnly('Partial response functionality available in REST mode only.');

        $this->customerHelper = Bootstrap::getObjectManager()
            ->get(\Magento\TestFramework\Helper\Customer::class);

        $this->customerData = $this->customerHelper->createSampleCustomer();
    }

    public function testCustomerWithEmailFilter()
    {
        $filter = 'email';
        $expected = ['email' => $this->customerData['email']];
        $result = $this->_getCustomerWithFilter($filter, $this->customerData['id']);
        $this->assertEquals($expected, $result);
    }

    public function testCustomerWithEmailAndAddressFilter()
    {
        $filter = 'email,addresses[city]';
        $expected = [
            'email' => $this->customerData['email'],
            'addresses' => [
                ['city' => CustomerHelper::ADDRESS_CITY1],
                ['city' => CustomerHelper::ADDRESS_CITY2],
            ],
        ];
        $result = $this->_getCustomerWithFilter($filter, $this->customerData['id']);
        $this->assertEquals($expected, $result);
    }

    public function testCustomerWithNestedAddressFilter()
    {
        $filter = 'addresses[region[region_code]]';
        $expected = [
            'addresses' => [
                ['region' => ['region_code' => CustomerHelper::ADDRESS_REGION_CODE1]],
                ['region' => ['region_code' => CustomerHelper::ADDRESS_REGION_CODE2]],
            ],
        ];
        $result = $this->_getCustomerWithFilter($filter, $this->customerData['id']);
        $this->assertEquals($expected, $result);
    }

    public function testCustomerInvalidFilter()
    {
        // Invalid filter should return an empty result
        $result = $this->_getCustomerWithFilter('invalid', $this->customerData['id']);
        $this->assertEmpty($result);
    }

    public function testFilterForCustomerApiWithSimpleResponse()
    {
        $result = $this->_getCustomerWithFilter('customers', $this->customerData['id'], '/permissions/readonly');
        // assert if filter is ignored and a normal response is returned
        $this->assertFalse($result);
    }

    protected function _getCustomerWithFilter($filter, $customerId, $path = '')
    {
        $resourcePath = sprintf(
            '%s/%d%s?fields=%s',
            AccountManagementTest::RESOURCE_PATH,
            $customerId,
            $path,
            $filter
        );

        $serviceInfo = [
            'rest' => [
                'resourcePath' => $resourcePath,
                'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
            ],
        ];

        return $this->_webApiCall($serviceInfo);
    }
}