PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/internal/Magento/Framework/Webapi/Test/Unit/Rest/Response/FieldsFilterTest.php

https://gitlab.com/crazybutterfly815/magento2
PHP | 233 lines | 181 code | 24 blank | 28 comment | 0 complexity | 1366790a212cb135d7997572ba7d1151 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Webapi\Test\Unit\Rest\Response;
  7. use \Magento\Framework\Webapi\Rest\Response\FieldsFilter;
  8. /**
  9. * Unit test for FieldsFilter
  10. */
  11. class FieldsFilterTest extends \PHPUnit_Framework_TestCase
  12. {
  13. /**
  14. * @var FieldsFilter SUT
  15. */
  16. protected $fieldsFilter;
  17. /**
  18. * @var string
  19. */
  20. protected $sampleResponseValue;
  21. /** @var \Magento\Framework\Webapi\Rest\Request|\PHPUnit_Framework_MockObject_MockObject */
  22. protected $requestMock;
  23. /**
  24. * Setup SUT
  25. */
  26. protected function setUp()
  27. {
  28. $this->requestMock = $this->getMock(\Magento\Framework\Webapi\Rest\Request::class, [], [], '', false);
  29. $this->processor = new FieldsFilter($this->requestMock);
  30. $this->sampleResponseValue = [
  31. 'customer' => [
  32. 'id' => '1',
  33. 'website_id' => '0',
  34. 'created_in' => 'Default Store View',
  35. 'store_id' => '1',
  36. 'group_id' => '1',
  37. 'custom_attributes' => [
  38. 0 => [
  39. 'attribute_code' => 'disable_auto_group_change',
  40. 'value' => '0',
  41. ],
  42. ],
  43. 'firstname' => 'Jane',
  44. 'lastname' => 'Doe',
  45. 'email' => 'jdoe@example.com',
  46. 'default_billing' => '1',
  47. 'default_shipping' => '1',
  48. 'created_at' => '2014-05-27 18:59:43',
  49. 'dob' => '1983-05-26 00:00:00',
  50. 'taxvat' => '1212121212',
  51. 'gender' => '1',
  52. ],
  53. 'addresses' => [
  54. 0 => [
  55. 'firstname' => 'Jane',
  56. 'lastname' => 'Doe',
  57. 'street' => [
  58. 0 => '7700 Parmer ln',
  59. ],
  60. 'city' => 'Austin',
  61. 'country_id' => 'US',
  62. 'region' => [
  63. 'region' => 'Texas',
  64. 'region_id' => 57,
  65. 'region_code' => 'TX',
  66. ],
  67. 'postcode' => '78728',
  68. 'telephone' => '1111111111',
  69. 'default_billing' => true,
  70. 'default_shipping' => true,
  71. 'id' => '1',
  72. 'customer_id' => '1',
  73. ],
  74. 1 => [
  75. 'firstname' => 'Jane',
  76. 'lastname' => 'Doe',
  77. 'street' => [
  78. 0 => '2211 N First St ',
  79. ],
  80. 'city' => 'San Jose',
  81. 'country_id' => 'US',
  82. 'region' => [
  83. 'region' => 'California',
  84. 'region_id' => 23,
  85. 'region_code' => 'CA',
  86. ],
  87. 'postcode' => '98454',
  88. 'telephone' => '2222222222',
  89. 'default_billing' => true,
  90. 'default_shipping' => true,
  91. 'id' => '2',
  92. 'customer_id' => '1',
  93. ],
  94. ],
  95. ];
  96. }
  97. public function testFilterNoNesting()
  98. {
  99. $expected = ['customer' => $this->sampleResponseValue['customer']];
  100. $simpleFilter = 'customer';
  101. $this->requestMock->expects($this->any())->method('getParam')->will($this->returnValue($simpleFilter));
  102. $filteredResponse = $this->processor->filter($this->sampleResponseValue);
  103. $this->assertEquals($expected, $filteredResponse);
  104. }
  105. public function testFilterSimpleNesting()
  106. {
  107. $expected = [
  108. 'customer' => [
  109. 'email' => $this->sampleResponseValue['customer']['email'],
  110. 'id' => $this->sampleResponseValue['customer']['id'],
  111. ],
  112. ];
  113. $simpleFilter = "customer[email,id]";
  114. $this->requestMock->expects($this->any())->method('getParam')->will($this->returnValue($simpleFilter));
  115. $filteredResponse = $this->processor->filter($this->sampleResponseValue);
  116. $this->assertEquals($expected, $filteredResponse);
  117. }
  118. public function testFilterMultilevelNesting()
  119. {
  120. $expected = [
  121. 'customer' => [
  122. 'id' => '1',
  123. 'email' => 'jdoe@example.com',
  124. ],
  125. 'addresses' => [
  126. 0 => [
  127. 'city' => 'Austin',
  128. 'region' => [
  129. 'region' => 'Texas',
  130. 'region_code' => 'TX',
  131. ],
  132. 'postcode' => '78728',
  133. ],
  134. 1 => [
  135. 'city' => 'San Jose',
  136. 'region' => [
  137. 'region' => 'California',
  138. 'region_code' => 'CA',
  139. ],
  140. 'postcode' => '98454',
  141. ],
  142. ],
  143. ];
  144. $nestedFilter = 'customer[id,email],addresses[city,postcode,region[region_code,region]]';
  145. $this->requestMock->expects($this->any())->method('getParam')->will($this->returnValue($nestedFilter));
  146. $filteredResponse = $this->processor->filter($this->sampleResponseValue);
  147. $this->assertEquals($expected, $filteredResponse);
  148. }
  149. public function testNonExistentFieldFilter()
  150. {
  151. //TODO : Make sure if this behavior is acceptable
  152. $expected = [
  153. 'customer' => [
  154. 'id' => '1',
  155. 'email' => 'jdoe@example.com',
  156. ],
  157. 'addresses' => [
  158. 0 => [
  159. //'city' => 'Austin', //City has been substituted with 'invalid' field
  160. 'region' => [
  161. 'region' => 'Texas',
  162. 'region_code' => 'TX',
  163. ],
  164. 'postcode' => '78728',
  165. ],
  166. 1 => [
  167. //'city' => 'San Jose',
  168. 'region' => [
  169. 'region' => 'California',
  170. 'region_code' => 'CA',
  171. ],
  172. 'postcode' => '98454',
  173. ],
  174. ],
  175. ];
  176. $nonExistentFieldFilter = 'customer[id,email],addresses[invalid,postcode,region[region_code,region]]';
  177. $this->requestMock
  178. ->expects($this->any())
  179. ->method('getParam')
  180. ->will($this->returnValue($nonExistentFieldFilter));
  181. $filteredResponse = $this->processor->filter($this->sampleResponseValue);
  182. $this->assertEquals($expected, $filteredResponse);
  183. }
  184. /**
  185. * @dataProvider invalidFilterDataProvider
  186. */
  187. public function testInvalidFilters($invalidFilter)
  188. {
  189. $this->requestMock->expects($this->any())->method('getParam')->will($this->returnValue($invalidFilter));
  190. $filteredResponse = $this->processor->filter($this->sampleResponseValue);
  191. $this->assertEmpty($filteredResponse);
  192. }
  193. /**
  194. * Data provider for invalid Filters
  195. *
  196. * @return array
  197. */
  198. public function invalidFilterDataProvider()
  199. {
  200. return [
  201. [' '],
  202. [null],
  203. ['customer(email)'],
  204. [' customer[email]'],
  205. ['-'],
  206. ['customer[id,email],addresses[city,postcode,region[region_code,region]'] //Missing last parentheses
  207. ];
  208. }
  209. }