Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions
CustomerMetadataTest Class Reference
Inheritance diagram for CustomerMetadataTest:

Public Member Functions

 testGetCustomAttributesMetadata ()
 
 testGetNestedOptionsCustomerAttributesMetadata ()
 
 testGetCustomAttributesMetadataWithCustomAttributes ()
 
 testGetAllAttributesMetadataWithCustomAttribute ()
 
 testGetCustomerAttributeMetadata ()
 
 testGetCustomerAttributeMetadataNoSuchEntity ()
 
 testGetAttributes ()
 

Protected Member Functions

 setUp ()
 
 tearDown ()
 

Detailed Description

Definition at line 14 of file CustomerMetadataTest.php.

Member Function Documentation

◆ setUp()

setUp ( )
protected

Definition at line 30 of file CustomerMetadataTest.php.

31  {
34  $objectManager->configure(
35  [\Magento\Framework\Api\ExtensionAttribute\Config\Reader::class => [
36  'arguments' => [
37  'fileResolver' => ['instance' => \Magento\Customer\Model\FileResolverStub::class],
38  ],
39  ],
40  ]
41  );
42  $this->customerRepository = $objectManager->create(
43  \Magento\Customer\Api\CustomerRepositoryInterface::class
44  );
45  $this->service = $objectManager->create(\Magento\Customer\Api\CustomerMetadataInterface::class);
46  $this->serviceTwo = $objectManager->create(\Magento\Customer\Api\CustomerMetadataInterface::class);
47  $this->extensibleDataObjectConverter = $objectManager->get(
48  \Magento\Framework\Api\ExtensibleDataObjectConverter::class
49  );
50  }
$objectManager
Definition: bootstrap.php:17

◆ tearDown()

tearDown ( )
protected

Definition at line 377 of file CustomerMetadataTest.php.

378  {
380 
381  /* @var \Magento\Framework\Config\CacheInterface $cache */
382  $cache = $objectManager->create(\Magento\Framework\Config\CacheInterface::class);
383  $cache->remove('extension_attributes_config');
384  }
$objectManager
Definition: bootstrap.php:17

◆ testGetAllAttributesMetadataWithCustomAttribute()

testGetAllAttributesMetadataWithCustomAttribute ( )

@magentoDataFixture Magento/Customer/_files/attribute_user_defined_custom_attribute.php

Definition at line 176 of file CustomerMetadataTest.php.

177  {
178  $allAttributesMetadata = $this->service->getAllAttributesMetadata();
179 
180  // Verify the consistency of getAllAttributesMetadata() function from the 2nd call of the same service
181  $allAttributesMetadata2 = $this->service->getAllAttributesMetadata();
182  $this->assertEquals(
183  $allAttributesMetadata,
184  $allAttributesMetadata2,
185  'Different attribute metadata returned from the 2nd call of the same service'
186  );
187 
188  // Verify the consistency of getAllAttributesMetadata() function from the 2nd service
189  $allAttributesMetadata3 = $this->serviceTwo->getAllAttributesMetadata();
190  $this->assertEquals(
191  $allAttributesMetadata,
192  $allAttributesMetadata3,
193  'Different attribute metadata returned from the 2nd service'
194  );
195  }

◆ testGetAttributes()

testGetAttributes ( )

Check some fields of one attribute metadata

Definition at line 327 of file CustomerMetadataTest.php.

328  {
329  $formAttributesMetadata = $this->service->getAttributes('adminhtml_customer');
330  $this->assertCount(14, $formAttributesMetadata, "Invalid number of attributes for the specified form.");
331  // Verify the consistency of getAttributes() function from the 2nd call of the same service
332  $formAttributesMetadata1 = $this->service->getAttributes('adminhtml_customer');
333  $this->assertEquals(
334  $formAttributesMetadata,
335  $formAttributesMetadata1,
336  'Different form attribute metadata returned from the 2nd call of the same service'
337  );
338  // Verify the consistency of getAttributes() function from the 2nd service
339  $formAttributesMetadata2 = $this->serviceTwo->getAttributes('adminhtml_customer');
340  $this->assertEquals(
341  $formAttributesMetadata,
342  $formAttributesMetadata2,
343  'Different form attribute metadata returned from the 2nd service'
344  );
345 
347  $attributeMetadata = $formAttributesMetadata['firstname'];
348  $this->assertInstanceOf(\Magento\Customer\Model\Data\AttributeMetadata::class, $attributeMetadata);
349  $this->assertEquals('firstname', $attributeMetadata->getAttributeCode(), 'Attribute code is invalid');
350  $this->assertNotEmpty($attributeMetadata->getValidationRules(), 'Validation rules are not set');
351  $this->assertEquals('1', $attributeMetadata->isSystem(), '"Is system" field value is invalid');
352  $this->assertEquals('40', $attributeMetadata->getSortOrder(), 'Sort order is invalid');
353 
354  // Verify the consistency of form attribute metadata from two calls of the same service
355  // after some getters were called
356  $attributeMetadata1 = $formAttributesMetadata1['firstname'];
357  $attributeMetadata1->getAttributeCode();
358  $attributeMetadata1->getValidationRules();
359  $this->assertEquals(
360  $formAttributesMetadata,
361  $formAttributesMetadata1,
362  'Form attribute metadata from the same service became different after some getters were called'
363  );
364 
365  // Verify the consistency of form attribute metadata from two services
366  // after some getters were called
367  $attributeMetadata2 = $formAttributesMetadata2['firstname'];
368  $attributeMetadata2->getAttributeCode();
369  $attributeMetadata2->getValidationRules();
370  $this->assertEquals(
371  $formAttributesMetadata,
372  $formAttributesMetadata2,
373  'Form attribute metadata from two services are different after some getters were called'
374  );
375  }

◆ testGetCustomAttributesMetadata()

testGetCustomAttributesMetadata ( )

Definition at line 52 of file CustomerMetadataTest.php.

53  {
54  $customAttributesMetadata = $this->service->getCustomAttributesMetadata();
55  $this->assertCount(0, $customAttributesMetadata, "Invalid number of attributes returned.");
56 
57  // Verify the consistency of getCustomerAttributeMetadata() function from the 2nd call of the same service
58  $customAttributesMetadata1 = $this->service->getCustomAttributesMetadata();
59  $this->assertCount(0, $customAttributesMetadata1, "Invalid number of attributes returned.");
60 
61  // Verify the consistency of getCustomAttributesMetadata() function from the 2nd service
62  $customAttributesMetadata2 = $this->serviceTwo->getCustomAttributesMetadata();
63  $this->assertCount(0, $customAttributesMetadata2, "Invalid number of attributes returned.");
64  }

◆ testGetCustomAttributesMetadataWithCustomAttributes()

testGetCustomAttributesMetadataWithCustomAttributes ( )

@magentoDataFixture Magento/Customer/_files/attribute_user_defined_custom_attribute.php

Definition at line 121 of file CustomerMetadataTest.php.

122  {
123  $customAttributesMetadata = $this->service->getCustomAttributesMetadata();
124  // Verify the consistency of getCustomAttributesMetadata() function from the 2nd call of the same service
125  $customAttributesMetadata1 = $this->service->getCustomAttributesMetadata();
126  $this->assertEquals(
127  $customAttributesMetadata,
128  $customAttributesMetadata1,
129  'Different custom attribute metadata returned from the 2nd call of the same service'
130  );
131  // Verify the consistency of getCustomAttributesMetadata() function from the 2nd service
132  $customAttributesMetadata2 = $this->serviceTwo->getCustomAttributesMetadata();
133  $this->assertEquals(
134  $customAttributesMetadata,
135  $customAttributesMetadata2,
136  'Different custom attribute metadata returned from the 2nd service'
137  );
138 
139  $expectedCustomAttributeCodeArray = ['custom_attribute1', 'custom_attribute2', 'customer_image'];
140  $actual = [];
141  foreach ($customAttributesMetadata as $attribute) {
142  $actual[] = $attribute->getAttributeCode();
143  }
144  $this->assertEquals(
145  $expectedCustomAttributeCodeArray,
146  array_intersect($expectedCustomAttributeCodeArray, $actual),
147  "Expected attributes not returned from the service."
148  );
149 
150  // Verify the consistency of custom attribute metadata from two calls of the same service
151  // after getAttributeCode was called
152  foreach ($customAttributesMetadata1 as $attribute) {
153  $attribute->getAttributeCode();
154  }
155  $this->assertEquals(
156  $customAttributesMetadata,
157  $customAttributesMetadata1,
158  'Custom attribute metadata from the same service became different after getAttributeCode was called'
159  );
160 
161  // Verify the consistency of custom attribute metadata from two services
162  // after getAttributeCode was called
163  foreach ($customAttributesMetadata2 as $attribute) {
164  $attribute->getAttributeCode();
165  }
166  $this->assertEquals(
167  $customAttributesMetadata,
168  $customAttributesMetadata2,
169  'Custom attribute metadata from two services are different after getAttributeCode was called'
170  );
171  }

◆ testGetCustomerAttributeMetadata()

testGetCustomerAttributeMetadata ( )

@magentoDataFixture Magento/Customer/_files/customer.php

Definition at line 200 of file CustomerMetadataTest.php.

201  {
202  // Expect these attributes to exist but do not check the value
203  $expectAttrsWOutVals = ['created_at', 'updated_at'];
204 
205  // Expect these attributes to exist and check the value - values come from _files/customer.php
206  $expectAttrsWithVals = [
207  'id' => 1,
208  'website_id' => 1,
209  'store_id' => 1,
210  'group_id' => 1,
211  'prefix' => 'Mr.',
212  'firstname' => 'John',
213  'middlename' => 'A',
214  'lastname' => 'Smith',
215  'suffix' => 'Esq.',
216  'email' => '[email protected]',
217  'default_billing' => '1',
218  'default_shipping' => '1',
219  'disable_auto_group_change' => 0,
220  'taxvat' => '12',
221  'gender' => 0
222  ];
223 
224  $customer = $this->customerRepository->getById(1);
225  $this->assertNotNull($customer);
226 
227  $attributes = $this->extensibleDataObjectConverter->toFlatArray(
228  $customer,
229  [],
230  \Magento\Customer\Api\Data\CustomerInterface::class
231  );
232  $this->assertNotEmpty($attributes);
233 
234  // remove odd extension attributes
235  $allAtrributes = $expectAttrsWithVals;
236  $allAtrributes['created_at'] = $attributes['created_at'];
237  $allAtrributes['updated_at'] = $attributes['updated_at'];
238  $attributes = array_intersect_key($attributes, $allAtrributes);
239 
240  foreach ($attributes as $attributeCode => $attributeValue) {
241  $this->assertNotNull($attributeCode);
242  $this->assertNotNull($attributeValue);
243  $attributeMetadata = $this->service->getAttributeMetadata($attributeCode);
244  // Verify the consistency of getAttributeMetadata() function from the 2nd call of the same service
245  $attributeMetadata1 = $this->service->getAttributeMetadata($attributeCode);
246  $this->assertEquals(
247  $attributeMetadata,
248  $attributeMetadata1,
249  'Different attribute metadata returned from the 2nd call of the same service'
250  );
251  // Verify the consistency of getAttributeMetadata() function from the 2nd service
252  $attributeMetadata2 = $this->serviceTwo->getAttributeMetadata($attributeCode);
253  $this->assertEquals(
254  $attributeMetadata,
255  $attributeMetadata2,
256  'Different attribute metadata returned from the 2nd service'
257  );
258  $attrMetadataCode = $attributeMetadata->getAttributeCode();
259  // Verify the consistency of attribute metadata from two calls of the same service
260  // after getAttributeCode was called
261  $attributeMetadata1->getAttributeCode();
262  $this->assertEquals(
263  $attributeMetadata,
264  $attributeMetadata1,
265  'Attribute metadata from the same service became different after getAttributeCode was called'
266  );
267  // Verify the consistency of attribute metadata from two services
268  // after getAttributeCode was called
269  $attributeMetadata2->getAttributeCode();
270  $this->assertEquals(
271  $attributeMetadata,
272  $attributeMetadata2,
273  'Attribute metadata returned from the 2nd service became different after getAttributeCode was called'
274  );
275  $this->assertSame($attributeCode, $attrMetadataCode);
276  if (($key = array_search($attrMetadataCode, $expectAttrsWOutVals)) !== false) {
277  unset($expectAttrsWOutVals[$key]);
278  } else {
279  $this->assertArrayHasKey($attrMetadataCode, $expectAttrsWithVals);
280  $this->assertSame(
281  $expectAttrsWithVals[$attrMetadataCode],
282  $attributeValue,
283  "Failed for {$attrMetadataCode}"
284  );
285  unset($expectAttrsWithVals[$attrMetadataCode]);
286  }
287  }
288  $this->assertEmpty($expectAttrsWOutVals);
289  $this->assertEmpty($expectAttrsWithVals);
290  }
$customer
Definition: customers.php:11
$attributeCode
Definition: extend.phtml:12
$attributes
Definition: matrix.phtml:13

◆ testGetCustomerAttributeMetadataNoSuchEntity()

testGetCustomerAttributeMetadataNoSuchEntity ( )

Definition at line 292 of file CustomerMetadataTest.php.

293  {
294  try {
295  $this->service->getAttributeMetadata('wrong_attribute_code');
296  $this->fail('Expected exception not thrown.');
297  } catch (NoSuchEntityException $e) {
298  $this->assertEquals(
299  'No such entity with entityType = customer, attributeCode = wrong_attribute_code',
300  $e->getMessage()
301  );
302  }
303 
304  // Verify the consistency of getAttributeMetadata() function from the 2nd call of the same service
305  try {
306  $this->service->getAttributeMetadata('wrong_attribute_code');
307  $this->fail('Expected exception not thrown when called the 2nd time.');
308  } catch (NoSuchEntityException $e) {
309  $this->assertEquals(
310  'No such entity with entityType = customer, attributeCode = wrong_attribute_code',
311  $e->getMessage()
312  );
313  }
314 
315  // Verify the consistency of getAttributeMetadata() function from the 2nd service
316  try {
317  $this->serviceTwo->getAttributeMetadata('wrong_attribute_code');
318  $this->fail('Expected exception not thrown when called with the 2nd service.');
319  } catch (NoSuchEntityException $e) {
320  $this->assertEquals(
321  'No such entity with entityType = customer, attributeCode = wrong_attribute_code',
322  $e->getMessage()
323  );
324  }
325  }

◆ testGetNestedOptionsCustomerAttributesMetadata()

testGetNestedOptionsCustomerAttributesMetadata ( )

Definition at line 66 of file CustomerMetadataTest.php.

67  {
68  $nestedOptionsAttribute = 'store_id';
69  $customAttributesMetadata = $this->service->getAttributeMetadata($nestedOptionsAttribute);
70  // Verify the consistency of getAttributeMetadata() function from the 2nd call of the same service
71  $customAttributesMetadata1 = $this->service->getAttributeMetadata($nestedOptionsAttribute);
72  $this->assertEquals(
73  $customAttributesMetadata,
74  $customAttributesMetadata1,
75  'Different attribute metadata returned from the 2nd call of the same service'
76  );
77  // Verify the consistency of getAttributeMetadata() function from the 2nd service
78  $customAttributesMetadata2 = $this->serviceTwo->getAttributeMetadata($nestedOptionsAttribute);
79  $this->assertEquals(
80  $customAttributesMetadata,
81  $customAttributesMetadata2,
82  'Different attribute metadata returned from the 2nd service'
83  );
84 
85  $options = $customAttributesMetadata->getOptions();
86  $nestedOptionExists = false;
87  foreach ($options as $option) {
88  if (strpos($option->getLabel(), 'Main Website Store') !== false) {
89  $this->assertNotEmpty($option->getOptions());
90  //Check nested option
91  $this->assertTrue(strpos($option->getOptions()[0]->getLabel(), 'Default Store View') !== false);
92  $nestedOptionExists = true;
93  }
94  }
95  if (!$nestedOptionExists) {
96  $this->fail('Nested attribute options were expected.');
97  }
98 
99  // Verify the consistency of attribute metadata from two calls of the same service
100  // after getOptions was called
101  $customAttributesMetadata1->getOptions();
102  $this->assertEquals(
103  $customAttributesMetadata,
104  $customAttributesMetadata1,
105  'Attribute metadata from the same service became different after getOptions was called'
106  );
107 
108  // Verify the consistency of attribute metadata from two services
109  // after getOptions was called
110  $customAttributesMetadata2->getOptions();
111  $this->assertEquals(
112  $customAttributesMetadata,
113  $customAttributesMetadata2,
114  'Attribute metadata from two services are different after getOptions was called'
115  );
116  }

The documentation for this class was generated from the following file: