Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RequestIdOverrideTest.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Webapi\Routing;
8 
10 
17 {
21  protected $_version;
22 
26  protected $_restResourcePath;
27 
31  protected $itemFactory;
32 
36  protected $_soapService = 'testModule5AllSoapAndRest';
37 
38  protected function setUp()
39  {
40  $this->_markTestAsRestOnly('Request Id overriding is a REST based feature.');
41  $this->_version = 'V1';
42  $this->_restResourcePath = "/{$this->_version}/TestModule5/";
43  $this->itemFactory = Bootstrap::getObjectManager()
44  ->create(\Magento\TestModule5\Service\V1\Entity\AllSoapAndRestFactory::class);
45  }
46 
47  public function testOverride()
48  {
49  $itemId = 1;
50  $incorrectItemId = 2;
51  $serviceInfo = [
52  'rest' => [
53  'resourcePath' => $this->_restResourcePath . $itemId,
55  ],
56  ];
57  $item = $this->itemFactory->create()
58  ->setEntityId($incorrectItemId)
59  ->setName('test');
60  $requestData = ['entityItem' => $item->__toArray()];
61  $item = $this->_webApiCall($serviceInfo, $requestData);
62  $this->assertEquals(
63  $itemId,
64  $item[\Magento\TestModule5\Service\V1\Entity\AllSoapAndRest::ID],
65  'Identifier overriding failed.'
66  );
67  }
68 
69  public function testOverrideNested()
70  {
71  $firstItemId = 1;
72  $secondItemId = 11;
73  $incorrectItemId = 2;
74  $serviceInfo = [
75  'rest' => [
76  'resourcePath' => $this->_restResourcePath . $firstItemId . '/nestedResource/' . $secondItemId,
78  ],
79  ];
80  $item = $this->itemFactory->create()
81  ->setEntityId($incorrectItemId)
82  ->setName('test');
83  $requestData = ['entityItem' => $item->__toArray()];
84  $item = $this->_webApiCall($serviceInfo, $requestData);
85  $this->assertEquals(
86  $secondItemId,
87  $item[\Magento\TestModule5\Service\V1\Entity\AllSoapAndRest::ID],
88  'Identifier overriding failed for nested resource request.'
89  );
90  }
91 
92  public function testOverrideAdd()
93  {
94  $itemId = 1;
95  $serviceInfo = [
96  'rest' => [
97  'resourcePath' => $this->_restResourcePath . $itemId,
99  ],
100  ];
101  $item = $this->itemFactory->create()
102  ->setName('test');
103  $requestData = ['entityItem' => $item->__toArray()];
104  $item = $this->_webApiCall($serviceInfo, $requestData);
105  $this->assertEquals(
106  $itemId,
107  $item[\Magento\TestModule5\Service\V1\Entity\AllSoapAndRest::ID],
108  'Identifier replacing failed.'
109  );
110  }
111 
117  public function testAddCaseMismatch()
118  {
119  $itemId = 1;
120  $incorrectItemId = 2;
121  $serviceInfo = [
122  'rest' => [
123  'resourcePath' => $this->_restResourcePath . $itemId,
125  ],
126  ];
127  $requestData = ['entityItem' => ['entityId' => $incorrectItemId, 'name' => 'test']];
128  $item = $this->_webApiCall($serviceInfo, $requestData);
129  $this->assertEquals(
130  $itemId,
131  $item[\Magento\TestModule5\Service\V1\Entity\AllSoapAndRest::ID],
132  'Identifier overriding failed.'
133  );
134  }
135 
137  {
138  $firstItemId = 1;
139  $secondItemId = 11;
140  $incorrectItemId = 2;
141  $serviceInfo = [
142  'rest' => [
143  'resourcePath' => "/{$this->_version}/TestModule5/OverrideService/" . $firstItemId
144  . '/nestedResource/' . $secondItemId,
146  ],
147  ];
148 
149  $requestData = ['entity_id' => $incorrectItemId, 'name' => 'test', 'orders' => true];
150  $item = $this->_webApiCall($serviceInfo, $requestData);
151  $this->assertEquals(
152  $secondItemId,
153  $item[\Magento\TestModule5\Service\V1\Entity\AllSoapAndRest::ID],
154  'Identifier overriding failed.'
155  );
156  }
157 }
_webApiCall( $serviceInfo, $arguments=[], $webApiAdapterCode=null, $storeCode=null, $integration=null)