ServiceVersionV2Test.php 5.36 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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Webapi\Routing;

class ServiceVersionV2Test extends \Magento\Webapi\Routing\BaseService
{
    protected function setUp()
    {
        $this->_version = 'V2';
        $this->_soapService = 'testModule1AllSoapAndRestV2';
        $this->_restResourcePath = "/{$this->_version}/testmodule1/";
    }

    /**
     *  Test to assert overriding of the existing 'Item' api in V2 version of the same service
     */
    public function testItem()
    {
        $itemId = 1;
        $serviceInfo = [
            'rest' => [
                'resourcePath' => $this->_restResourcePath . $itemId,
                'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
            ],
            'soap' => ['service' => $this->_soapService, 'operation' => $this->_soapService . 'Item'],
        ];
        $requestData = ['id' => $itemId];
        $item = $this->_webApiCall($serviceInfo, $requestData);
        // Asserting for additional attribute returned by the V2 api
        $this->assertEquals(1, $item['price'], 'Item was retrieved unsuccessfully from V2');
    }

    /**
     * Test fetching all items
     */
    public function testItems()
    {
        $itemArr = [
            ['id' => 1, 'name' => 'testProduct1', 'price' => '1'],
            ['id' => 2, 'name' => 'testProduct2', 'price' => '2'],
        ];
        $serviceInfo = [
            'rest' => [
                'resourcePath' => $this->_restResourcePath,
                'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
            ],
            'soap' => ['service' => $this->_soapService, 'operation' => $this->_soapService . 'Items'],
        ];
        $item = $this->_webApiCall($serviceInfo);
        $this->assertEquals($itemArr, $item, 'Items were not retrieved');
    }

    /**
     * Test fetching items when filters are applied
     *
     * @param string[] $filters
     * @param array $expectedResult
     * @dataProvider itemsWithFiltersDataProvider
     */
    public function testItemsWithFilters($filters, $expectedResult)
    {
        $restFilter = '';
        foreach ($filters as $filterItemKey => $filterMetadata) {
            foreach ($filterMetadata as $filterMetaKey => $filterMetaValue) {
                $paramsDelimiter = empty($restFilter) ? '?' : '&';
                $restFilter .= "{$paramsDelimiter}filters[{$filterItemKey}][{$filterMetaKey}]={$filterMetaValue}";
            }
        }
        $serviceInfo = [
            'rest' => [
                'resourcePath' => $this->_restResourcePath . $restFilter,
                'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
            ],
            'soap' => [
                'service' => $this->_soapService,
                'operation' => $this->_soapService . 'Items',
            ],
        ];
        $requestData = [];
        if (!empty($filters)) {
            $requestData['filters'] = $filters;
        }
        $item = $this->_webApiCall($serviceInfo, $requestData);
        $this->assertEquals($expectedResult, $item, 'Filtration does not seem to work correctly.');
    }

    public function itemsWithFiltersDataProvider()
    {
        $firstItem = ['id' => 1, 'name' => 'testProduct1', 'price' => 1];
        $secondItem = ['id' => 2, 'name' => 'testProduct2', 'price' => 2];
        return [
            'Both items filter' => [
                [
                    ['field' => 'id', 'conditionType' => 'eq','value' => 1],
                    ['field' => 'id', 'conditionType' => 'eq','value' => 2],
                ],
                [$firstItem, $secondItem],
            ],
            'First item filter' => [[['field' => 'id', 'conditionType' => 'eq','value' => 1]], [$firstItem]],
            'Second item filter' => [[['field' => 'id', 'conditionType' => 'eq','value' => 2]], [$secondItem]],
            'Empty filter' => [[], [$firstItem, $secondItem]],
        ];
    }

    /**
     *  Test update item
     */
    public function testUpdate()
    {
        $itemId = 1;
        $serviceInfo = [
            'rest' => [
                'resourcePath' => $this->_restResourcePath . $itemId,
                'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT,
            ],
            'soap' => ['service' => $this->_soapService, 'operation' => $this->_soapService . 'Update'],
        ];
        $requestData = ['entityItem' => ['id' => $itemId, 'name' => 'testName', 'price' => '4']];
        $item = $this->_webApiCall($serviceInfo, $requestData);
        $this->assertEquals('Updated' . $requestData['entityItem']['name'], $item['name'], 'Item update failed');
    }

    /**
     *  Test to assert presence of new 'delete' api added in V2 version of the same service
     */
    public function testDelete()
    {
        $itemId = 1;
        $serviceInfo = [
            'rest' => [
                'resourcePath' => $this->_restResourcePath . $itemId,
                'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE,
            ],
            'soap' => ['service' => $this->_soapService, 'operation' => $this->_soapService . 'Delete'],
        ];
        $requestData = ['id' => $itemId, 'name' => 'testName'];
        $item = $this->_webApiCall($serviceInfo, $requestData);
        $this->assertEquals($itemId, $item['id'], "Item delete failed");
    }
}