Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ServiceVersionV2Test.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Webapi\Routing;
7 
9 {
10  protected function setUp()
11  {
12  $this->_version = 'V2';
13  $this->_soapService = 'testModule1AllSoapAndRestV2';
14  $this->_restResourcePath = "/{$this->_version}/testmodule1/";
15  }
16 
20  public function testItem()
21  {
22  $itemId = 1;
23  $serviceInfo = [
24  'rest' => [
25  'resourcePath' => $this->_restResourcePath . $itemId,
27  ],
28  'soap' => ['service' => $this->_soapService, 'operation' => $this->_soapService . 'Item'],
29  ];
30  $requestData = ['id' => $itemId];
31  $item = $this->_webApiCall($serviceInfo, $requestData);
32  // Asserting for additional attribute returned by the V2 api
33  $this->assertEquals(1, $item['price'], 'Item was retrieved unsuccessfully from V2');
34  }
35 
39  public function testItems()
40  {
41  $itemArr = [
42  ['id' => 1, 'name' => 'testProduct1', 'price' => '1'],
43  ['id' => 2, 'name' => 'testProduct2', 'price' => '2'],
44  ];
45  $serviceInfo = [
46  'rest' => [
47  'resourcePath' => $this->_restResourcePath,
49  ],
50  'soap' => ['service' => $this->_soapService, 'operation' => $this->_soapService . 'Items'],
51  ];
52  $item = $this->_webApiCall($serviceInfo);
53  $this->assertEquals($itemArr, $item, 'Items were not retrieved');
54  }
55 
63  public function testItemsWithFilters($filters, $expectedResult)
64  {
65  $restFilter = '';
66  foreach ($filters as $filterItemKey => $filterMetadata) {
67  foreach ($filterMetadata as $filterMetaKey => $filterMetaValue) {
68  $paramsDelimiter = empty($restFilter) ? '?' : '&';
69  $restFilter .= "{$paramsDelimiter}filters[{$filterItemKey}][{$filterMetaKey}]={$filterMetaValue}";
70  }
71  }
72  $serviceInfo = [
73  'rest' => [
74  'resourcePath' => $this->_restResourcePath . $restFilter,
76  ],
77  'soap' => [
78  'service' => $this->_soapService,
79  'operation' => $this->_soapService . 'Items',
80  ],
81  ];
82  $requestData = [];
83  if (!empty($filters)) {
84  $requestData['filters'] = $filters;
85  }
86  $item = $this->_webApiCall($serviceInfo, $requestData);
87  $this->assertEquals($expectedResult, $item, 'Filtration does not seem to work correctly.');
88  }
89 
90  public function itemsWithFiltersDataProvider()
91  {
92  $firstItem = ['id' => 1, 'name' => 'testProduct1', 'price' => 1];
93  $secondItem = ['id' => 2, 'name' => 'testProduct2', 'price' => 2];
94  return [
95  'Both items filter' => [
96  [
97  ['field' => 'id', 'conditionType' => 'eq','value' => 1],
98  ['field' => 'id', 'conditionType' => 'eq','value' => 2],
99  ],
100  [$firstItem, $secondItem],
101  ],
102  'First item filter' => [[['field' => 'id', 'conditionType' => 'eq','value' => 1]], [$firstItem]],
103  'Second item filter' => [[['field' => 'id', 'conditionType' => 'eq','value' => 2]], [$secondItem]],
104  'Empty filter' => [[], [$firstItem, $secondItem]],
105  ];
106  }
107 
111  public function testUpdate()
112  {
113  $itemId = 1;
114  $serviceInfo = [
115  'rest' => [
116  'resourcePath' => $this->_restResourcePath . $itemId,
118  ],
119  'soap' => ['service' => $this->_soapService, 'operation' => $this->_soapService . 'Update'],
120  ];
121  $requestData = ['entityItem' => ['id' => $itemId, 'name' => 'testName', 'price' => '4']];
122  $item = $this->_webApiCall($serviceInfo, $requestData);
123  $this->assertEquals('Updated' . $requestData['entityItem']['name'], $item['name'], 'Item update failed');
124  }
125 
129  public function testDelete()
130  {
131  $itemId = 1;
132  $serviceInfo = [
133  'rest' => [
134  'resourcePath' => $this->_restResourcePath . $itemId,
136  ],
137  'soap' => ['service' => $this->_soapService, 'operation' => $this->_soapService . 'Delete'],
138  ];
139  $requestData = ['id' => $itemId, 'name' => 'testName'];
140  $item = $this->_webApiCall($serviceInfo, $requestData);
141  $this->assertEquals($itemId, $item['id'], "Item delete failed");
142  }
143 }
_webApiCall( $serviceInfo, $arguments=[], $webApiAdapterCode=null, $storeCode=null, $integration=null)
$filters
Definition: uploader.phtml:11