Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
PartialResponseTest.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Webapi;
8 
11 use Magento\TestFramework\Helper\Customer as CustomerHelper;
12 
14 {
16  protected $customerHelper;
17 
19  protected $customerData;
20 
21  protected function setUp()
22  {
23  $this->_markTestAsRestOnly('Partial response functionality available in REST mode only.');
24 
25  $this->customerHelper = Bootstrap::getObjectManager()
26  ->get(\Magento\TestFramework\Helper\Customer::class);
27 
28  $this->customerData = $this->customerHelper->createSampleCustomer();
29  }
30 
31  public function testCustomerWithEmailFilter()
32  {
33  $filter = 'email';
34  $expected = ['email' => $this->customerData['email']];
35  $result = $this->_getCustomerWithFilter($filter, $this->customerData['id']);
36  $this->assertEquals($expected, $result);
37  }
38 
40  {
41  $filter = 'email,addresses[city]';
42  $expected = [
43  'email' => $this->customerData['email'],
44  'addresses' => [
45  ['city' => CustomerHelper::ADDRESS_CITY1],
46  ['city' => CustomerHelper::ADDRESS_CITY2],
47  ],
48  ];
49  $result = $this->_getCustomerWithFilter($filter, $this->customerData['id']);
50  $this->assertEquals($expected, $result);
51  }
52 
54  {
55  $filter = 'addresses[region[region_code]]';
56  $expected = [
57  'addresses' => [
58  ['region' => ['region_code' => CustomerHelper::ADDRESS_REGION_CODE1]],
59  ['region' => ['region_code' => CustomerHelper::ADDRESS_REGION_CODE2]],
60  ],
61  ];
62  $result = $this->_getCustomerWithFilter($filter, $this->customerData['id']);
63  $this->assertEquals($expected, $result);
64  }
65 
66  public function testCustomerInvalidFilter()
67  {
68  // Invalid filter should return an empty result
69  $result = $this->_getCustomerWithFilter('invalid', $this->customerData['id']);
70  $this->assertEmpty($result);
71  }
72 
74  {
75  $result = $this->_getCustomerWithFilter('customers', $this->customerData['id'], '/permissions/readonly');
76  // assert if filter is ignored and a normal response is returned
77  $this->assertFalse($result);
78  }
79 
80  protected function _getCustomerWithFilter($filter, $customerId, $path = '')
81  {
82  $resourcePath = sprintf(
83  '%s/%d%s?fields=%s',
86  $path,
87  $filter
88  );
89 
90  $serviceInfo = [
91  'rest' => [
92  'resourcePath' => $resourcePath,
94  ],
95  ];
96 
97  return $this->_webApiCall($serviceInfo);
98  }
99 }
_webApiCall( $serviceInfo, $arguments=[], $webApiAdapterCode=null, $storeCode=null, $integration=null)
_getCustomerWithFilter($filter, $customerId, $path='')