Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ServiceVersionV1Test.php
Go to the documentation of this file.
1 <?php
10 namespace Magento\Webapi\Routing;
11 
16 use Magento\TestModule1\Service\V1\Entity\ItemFactory;
17 
19 {
23  protected $_version;
24 
28  protected $_restResourcePath;
29 
33  protected $_soapService = 'testModule1AllSoapAndRest';
34 
36  protected $valueFactory;
37 
39  protected $itemFactory;
40 
41  protected function setUp()
42  {
43  $this->_version = 'V1';
44  $this->_soapService = 'testModule1AllSoapAndRestV1';
45  $this->_restResourcePath = "/{$this->_version}/testmodule1/";
46 
47  $this->valueFactory = Bootstrap::getObjectManager()->create(
48  \Magento\Framework\Api\AttributeValueFactory::class
49  );
50 
51  $this->itemFactory = Bootstrap::getObjectManager()->create(
52  \Magento\TestModule1\Service\V1\Entity\ItemFactory::class
53  );
54  }
55 
59  public function testItem()
60  {
61  $itemId = 1;
62  $serviceInfo = [
63  'rest' => [
64  'resourcePath' => $this->_restResourcePath . $itemId,
66  ],
67  'soap' => ['service' => $this->_soapService, 'operation' => $this->_soapService . 'Item'],
68  ];
69  $requestData = ['itemId' => $itemId];
70  $item = $this->_webApiCall($serviceInfo, $requestData);
71  $this->assertEquals('testProduct1', $item['name'], 'Item was retrieved unsuccessfully');
72  }
73 
78  public function testItemAnyType()
79  {
80  $this->_markTestAsRestOnly('Test will fail for SOAP because attribute values get converted to strings.');
81  $customerAttributes = [
84  AttributeValue::VALUE => '12345',
85  ],
88  AttributeValue::VALUE => 12345,
89  ],
92  AttributeValue::VALUE => true,
93  ],
94  ];
95 
96  $attributeValue1 = $this->valueFactory->create()
97  ->setAttributeCode(Item::CUSTOM_ATTRIBUTE_1)
98  ->setValue('12345');
99  $attributeValue2 = $this->valueFactory->create()
100  ->setAttributeCode(Item::CUSTOM_ATTRIBUTE_2)
101  ->setValue(12345);
102  $attributeValue3 = $this->valueFactory->create()
103  ->setAttributeCode(Item::CUSTOM_ATTRIBUTE_3)
104  ->setValue(true);
105 
106  $item = $this->itemFactory->create()
107  ->setItemId(1)
108  ->setName('testProductAnyType')
109  ->setCustomAttributes([$attributeValue1, $attributeValue2, $attributeValue3]);
110 
111  $serviceInfo = [
112  'rest' => [
113  'resourcePath' => $this->_restResourcePath . 'itemAnyType',
115  ],
116  'soap' => ['service' => $this->_soapService, 'operation' => $this->_soapService . 'ItemAnyType'],
117  ];
118  $requestData = $item->__toArray();
119  $item = $this->_webApiCall($serviceInfo, ['entityItem' => $requestData]);
120 
121  $this->assertSame(
122  $attributeValue1->getValue(),
123  $item['custom_attributes'][0]['value'],
124  'Serialized attribute value type does\'t match pre-defined type.'
125  ); // string '12345' is expected
126 
127  $this->assertSame(
128  $attributeValue2->getValue(),
129  $item['custom_attributes'][1]['value'],
130  'Serialized attribute value type does\'t match pre-defined type.'
131  ); // integer 12345 is expected
132 
133  $this->assertSame(
134  $attributeValue3->getValue(),
135  $item['custom_attributes'][2]['value'],
136  'Serialized attribute value type does\'t match pre-defined type.'
137  ); // boolean true is expected
138  }
139 
143  public function testItems()
144  {
145  $itemArr = [['item_id' => 1, 'name' => 'testProduct1'], ['item_id' => 2, 'name' => 'testProduct2']];
146  $serviceInfo = [
147  'rest' => [
148  'resourcePath' => $this->_restResourcePath,
150  ],
151  'soap' => ['service' => $this->_soapService, 'operation' => $this->_soapService . 'Items'],
152  ];
153  $item = $this->_webApiCall($serviceInfo);
154  $this->assertEquals($itemArr, $item, 'Items were not retrieved');
155  }
156 
160  public function testCreate()
161  {
162  $createdItemName = 'createdItemName';
163  $serviceInfo = [
164  'rest' => [
165  'resourcePath' => $this->_restResourcePath,
167  ],
168  'soap' => ['service' => $this->_soapService, 'operation' => $this->_soapService . 'Create'],
169  ];
170  $requestData = ['name' => $createdItemName];
171  $item = $this->_webApiCall($serviceInfo, $requestData);
172  $this->assertEquals($createdItemName, $item['name'], 'Item creation failed');
173  }
174 
178  public function testCreateWithoutResources()
179  {
180  $createdItemName = 'createdItemName';
181  $serviceInfo = [
182  'rest' => [
183  'resourcePath' => $this->_restResourcePath,
185  ],
186  'soap' => ['service' => $this->_soapService, 'operation' => $this->_soapService . 'Create'],
187  ];
188  $requestData = ['name' => $createdItemName];
189 
190  // getting new credentials that do not match the api resources
192  OauthHelper::getApiAccessCredentials([]);
193  try {
194  $this->assertUnauthorizedException($serviceInfo, $requestData);
195  } catch (\Exception $e) {
197  throw $e;
198  }
199  // to allow good credentials to be restored (this is statically stored on OauthHelper)
201  }
202 
206  public function testUpdate()
207  {
208  $itemId = 1;
209  $serviceInfo = [
210  'rest' => [
211  'resourcePath' => $this->_restResourcePath . $itemId,
213  ],
214  'soap' => ['service' => $this->_soapService, 'operation' => $this->_soapService . 'Update'],
215  ];
216  $requestData = ['entityItem' => ['itemId' => $itemId, 'name' => 'testName']];
217  $item = $this->_webApiCall($serviceInfo, $requestData);
218  $this->assertEquals('Updated' . $requestData['entityItem']['name'], $item['name'], 'Item update failed');
219  }
220 
224  public function testDelete()
225  {
226  $itemId = 1;
227  $serviceInfo = [
228  'rest' => [
229  'resourcePath' => $this->_restResourcePath . $itemId,
231  ],
232  'soap' => ['service' => $this->_soapService, 'operation' => $this->_soapService . 'Delete'],
233  ];
234  $requestData = ['itemId' => $itemId, 'name' => 'testName'];
235  $this->_assertNoRouteOrOperationException($serviceInfo, $requestData);
236  }
237 
238  public function testOverwritten()
239  {
240  $this->_markTestAsRestOnly();
241  $serviceInfo = [
242  'rest' => [
243  'resourcePath' => $this->_restResourcePath . 'overwritten',
245  ],
246  ];
247  $item = $this->_webApiCall($serviceInfo, []);
248  $this->assertEquals(['item_id' => -55, 'name' => 'testProduct1'], $item);
249  }
250 
251  public function testDefaulted()
252  {
253  $this->_markTestAsRestOnly();
254  $serviceInfo = [
255  'rest' => [
256  'resourcePath' => $this->_restResourcePath . 'testOptionalParam',
258  ],
259  ];
260  $item = $this->_webApiCall($serviceInfo, []);
261  $this->assertEquals(['item_id' => 3, 'name' => 'Default Name'], $item);
262  }
263 
264  public function testDefaultedWithValue()
265  {
266  $this->_markTestAsRestOnly();
267  $serviceInfo = [
268  'rest' => [
269  'resourcePath' => $this->_restResourcePath . 'testOptionalParam',
271  ],
272  ];
273  $item = $this->_webApiCall($serviceInfo, ['name' => 'Ms. LaGrange']);
274  $this->assertEquals(['item_id' => 3, 'name' => 'Ms. LaGrange'], $item);
275  }
276 }
_assertNoRouteOrOperationException($serviceInfo, $requestData=null)
Definition: BaseService.php:65
_webApiCall( $serviceInfo, $arguments=[], $webApiAdapterCode=null, $storeCode=null, $integration=null)
assertUnauthorizedException($serviceInfo, $requestData=null)
Definition: BaseService.php:22