Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RestErrorHandlingTest.php
Go to the documentation of this file.
1 <?php
9 namespace Magento\Webapi\Routing;
10 
12 use Magento\Framework\Webapi\Exception as WebapiException;
13 
15 {
19  protected $mode;
20 
21  protected function setUp()
22  {
23  $this->_markTestAsRestOnly();
24  $this->mode = Bootstrap::getObjectManager()->get(\Magento\Framework\App\State::class)->getMode();
25  parent::setUp();
26  }
27 
28  public function testSuccess()
29  {
30  $serviceInfo = [
31  'rest' => [
32  'resourcePath' => '/V1/errortest/success',
34  ],
35  ];
36 
37  $item = $this->_webApiCall($serviceInfo);
38 
39  // TODO: check Http Status = 200, cannot do yet due to missing header info returned
40 
41  $this->assertEquals('a good id', $item['value'], 'Success case is correct');
42  }
43 
44  public function testNotFound()
45  {
46  $serviceInfo = [
47  'rest' => [
48  'resourcePath' => '/V1/errortest/notfound',
50  ],
51  ];
52 
53  // \Magento\Framework\Api\ResourceNotFoundException
54  $this->_errorTest(
55  $serviceInfo,
56  ['resourceY'],
57  WebapiException::HTTP_NOT_FOUND,
58  'Resource with ID "%1" not found.'
59  );
60  }
61 
62  public function testUnauthorized()
63  {
64  $serviceInfo = [
65  'rest' => [
66  'resourcePath' => '/V1/errortest/unauthorized',
68  ],
69  ];
70 
71  // \Magento\Framework\Api\AuthorizationException
72  $this->_errorTest(
73  $serviceInfo,
74  [],
75  WebapiException::HTTP_UNAUTHORIZED,
76  "The consumer isn't authorized to access %1.",
77  ['resourceN']
78  );
79  }
80 
81  public function testOtherException()
82  {
83  $serviceInfo = [
84  'rest' => [
85  'resourcePath' => '/V1/errortest/otherException',
87  ],
88  ];
89 
90  /* TODO : Fix as part MAGETWO-31330
91  $expectedMessage = $this->mode == \Magento\Framework\App\State::MODE_DEVELOPER
92  ? 'Non service exception'
93  : 'Internal Error. Details are available in Magento log file. Report ID: webapi-XXX';
94  */
95  $expectedMessage = 'Internal Error. Details are available in Magento log file. Report ID: webapi-XXX';
96  $this->_errorTest(
97  $serviceInfo,
98  [],
99  WebapiException::HTTP_INTERNAL_ERROR,
100  $expectedMessage,
101  null,
102  'Magento\TestModule3\Service\V1\Error->otherException()' // Check if trace contains proper error source
103  );
104  }
105 
116  protected function _errorTest(
117  $serviceInfo,
118  $data,
119  $httpStatus,
120  $errorMessage,
121  $parameters = [],
122  $traceString = null
123  ) {
124  try {
125  $this->_webApiCall($serviceInfo, $data);
126  } catch (\Exception $e) {
127  $this->assertEquals($httpStatus, $e->getCode(), 'Checking HTTP status code');
128 
129  $body = json_decode($e->getMessage(), true);
130 
131  $errorMessages = is_array($errorMessage) ? $errorMessage : [$errorMessage];
132  $actualMessage = $body['message'];
133  $matches = [];
134  //Report ID was created dynamically, so we need to replace it with some static value in order to test
135  if (preg_match('/.*Report\sID\:\s([a-zA-Z0-9\-]*)/', $actualMessage, $matches)) {
136  $actualMessage = str_replace($matches[1], 'webapi-XXX', $actualMessage);
137  }
138  //make sure that the match for a report with an id is found if Internal error was reported
139  //Refer : \Magento\Framework\Webapi\ErrorProcessor::INTERNAL_SERVER_ERROR_MSG
140  if (count($matches) > 1) {
141  $this->assertTrue(!empty($matches[1]), 'Report id missing for internal error.');
142  }
143  $this->assertContains(
144  $actualMessage,
145  $errorMessages,
146  "Message is invalid. Actual: '{$actualMessage}'. Expected one of: {'" . implode(
147  "', '",
148  $errorMessages
149  ) . "'}"
150  );
151 
152  if ($parameters) {
153  $this->assertEquals($parameters, $body['parameters'], 'Checking body parameters');
154  }
155 
156  if ($this->mode == \Magento\Framework\App\State::MODE_DEVELOPER && $traceString) {
157  // TODO : Fix as part MAGETWO-31330
158  //$this->assertContains($traceString, $body['trace'], 'Trace information is incorrect.');
159  }
160  }
161  }
162 }
_webApiCall( $serviceInfo, $arguments=[], $webApiAdapterCode=null, $storeCode=null, $integration=null)
_errorTest( $serviceInfo, $data, $httpStatus, $errorMessage, $parameters=[], $traceString=null)