Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FieldsFilterTest.php
Go to the documentation of this file.
1 <?php
8 
10 
14 class FieldsFilterTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $fieldsFilter;
20 
25 
29  protected $requestMock;
30 
34  protected $processor;
35 
39  protected function setUp()
40  {
41  $this->requestMock = $this->createMock(\Magento\Framework\Webapi\Rest\Request::class);
42  $this->processor = new FieldsFilter($this->requestMock);
43  $this->sampleResponseValue = [
44  'customer' => [
45  'id' => '1',
46  'website_id' => '0',
47  'created_in' => 'Default Store View',
48  'store_id' => '1',
49  'group_id' => '1',
50  'custom_attributes' => [
51  0 => [
52  'attribute_code' => 'disable_auto_group_change',
53  'value' => '0',
54  ],
55  ],
56  'firstname' => 'Jane',
57  'lastname' => 'Doe',
58  'email' => '[email protected]',
59  'default_billing' => '1',
60  'default_shipping' => '1',
61  'created_at' => '2014-05-27 18:59:43',
62  'dob' => '1983-05-26 00:00:00',
63  'taxvat' => '1212121212',
64  'gender' => '1',
65  ],
66  'addresses' => [
67  0 => [
68  'firstname' => 'Jane',
69  'lastname' => 'Doe',
70  'street' => [
71  0 => '7700 Parmer ln',
72  ],
73  'city' => 'Austin',
74  'country_id' => 'US',
75  'region' => [
76  'region' => 'Texas',
77  'region_id' => 57,
78  'region_code' => 'TX',
79  ],
80  'postcode' => '78728',
81  'telephone' => '1111111111',
82  'default_billing' => true,
83  'default_shipping' => true,
84  'id' => '1',
85  'customer_id' => '1',
86  ],
87  1 => [
88  'firstname' => 'Jane',
89  'lastname' => 'Doe',
90  'street' => [
91  0 => '2211 N First St ',
92  ],
93  'city' => 'San Jose',
94  'country_id' => 'US',
95  'region' => [
96  'region' => 'California',
97  'region_id' => 23,
98  'region_code' => 'CA',
99  ],
100  'postcode' => '98454',
101  'telephone' => '2222222222',
102  'default_billing' => true,
103  'default_shipping' => true,
104  'id' => '2',
105  'customer_id' => '1',
106  ],
107  ],
108  ];
109  }
110 
111  public function testFilterNoNesting()
112  {
113  $expected = ['customer' => $this->sampleResponseValue['customer']];
114 
115  $simpleFilter = 'customer';
116  $this->requestMock->expects($this->any())->method('getParam')->will($this->returnValue($simpleFilter));
117  $filteredResponse = $this->processor->filter($this->sampleResponseValue);
118 
119  $this->assertEquals($expected, $filteredResponse);
120  }
121 
122  public function testFilterSimpleNesting()
123  {
124  $expected = [
125  'customer' => [
126  'email' => $this->sampleResponseValue['customer']['email'],
127  'id' => $this->sampleResponseValue['customer']['id'],
128  ],
129  ];
130 
131  $simpleFilter = "customer[email,id]";
132 
133  $this->requestMock->expects($this->any())->method('getParam')->will($this->returnValue($simpleFilter));
134  $filteredResponse = $this->processor->filter($this->sampleResponseValue);
135 
136  $this->assertEquals($expected, $filteredResponse);
137  }
138 
139  public function testFilterMultilevelNesting()
140  {
141  $expected = [
142  'customer' => [
143  'id' => '1',
144  'email' => '[email protected]',
145  ],
146  'addresses' => [
147  0 => [
148  'city' => 'Austin',
149  'region' => [
150  'region' => 'Texas',
151  'region_code' => 'TX',
152  ],
153  'postcode' => '78728',
154  ],
155  1 => [
156  'city' => 'San Jose',
157  'region' => [
158  'region' => 'California',
159  'region_code' => 'CA',
160  ],
161  'postcode' => '98454',
162  ],
163  ],
164  ];
165 
166  $nestedFilter = 'customer[id,email],addresses[city,postcode,region[region_code,region]]';
167 
168  $this->requestMock->expects($this->any())->method('getParam')->will($this->returnValue($nestedFilter));
169  $filteredResponse = $this->processor->filter($this->sampleResponseValue);
170 
171  $this->assertEquals($expected, $filteredResponse);
172  }
173 
174  public function testNonExistentFieldFilter()
175  {
176  //TODO : Make sure if this behavior is acceptable
177  $expected = [
178  'customer' => [
179  'id' => '1',
180  'email' => '[email protected]',
181  ],
182  'addresses' => [
183  0 => [
184  //'city' => 'Austin', //City has been substituted with 'invalid' field
185  'region' => [
186  'region' => 'Texas',
187  'region_code' => 'TX',
188  ],
189  'postcode' => '78728',
190  ],
191  1 => [
192  //'city' => 'San Jose',
193  'region' => [
194  'region' => 'California',
195  'region_code' => 'CA',
196  ],
197  'postcode' => '98454',
198  ],
199  ],
200  ];
201 
202  $nonExistentFieldFilter = 'customer[id,email],addresses[invalid,postcode,region[region_code,region]]';
203 
204  $this->requestMock
205  ->expects($this->any())
206  ->method('getParam')
207  ->will($this->returnValue($nonExistentFieldFilter));
208  $filteredResponse = $this->processor->filter($this->sampleResponseValue);
209 
210  $this->assertEquals($expected, $filteredResponse);
211  }
212 
216  public function testInvalidFilters($invalidFilter)
217  {
218  $this->requestMock->expects($this->any())->method('getParam')->will($this->returnValue($invalidFilter));
219  $filteredResponse = $this->processor->filter($this->sampleResponseValue);
220 
221  $this->assertEmpty($filteredResponse);
222  }
223 
229  public function invalidFilterDataProvider()
230  {
231  return [
232  [' '],
233  [null],
234  ['customer(email)'],
235  [' customer[email]'],
236  ['-'],
237  ['customer[id,email],addresses[city,postcode,region[region_code,region]'] //Missing last parentheses
238  ];
239  }
240 }