Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DataTest.php
Go to the documentation of this file.
1 <?php
8 
10 
11 class DataTest extends \PHPUnit\Framework\TestCase
12 {
16  protected $helper;
17 
21  protected $eavConfig;
22 
26  protected $attributeConfig;
27 
31  protected $context;
32 
36  protected $scopeConfigMock;
37 
41  protected function setUp()
42  {
43  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
44 
45  $this->attributeConfig = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Config::class);
46  $this->eavConfig = $this->createMock(\Magento\Eav\Model\Config::class);
47  $this->context = $this->createPartialMock(\Magento\Framework\App\Helper\Context::class, ['getScopeConfig']);
48 
49  $this->scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
50  $this->context->expects($this->once())->method('getScopeConfig')->willReturn($this->scopeConfigMock);
51 
52  $this->helper = $objectManager->getObject(
53  \Magento\Eav\Helper\Data::class,
54  [
55  'attributeConfig' => $this->attributeConfig,
56  'eavConfig' => $this->eavConfig,
57  'context' => $this->context,
58  ]
59  );
60  }
61 
62  public function testGetAttributeMetadata()
63  {
64  $attribute = new \Magento\Framework\DataObject([
65  'entity_type_id' => '1',
66  'attribute_id' => '2',
67  'backend' => new \Magento\Framework\DataObject(['table' => 'customer_entity_varchar']),
68  'backend_type' => 'varchar',
69  ]);
70  $this->eavConfig->expects($this->once())
71  ->method('getAttribute')
72  ->will($this->returnValue($attribute));
73 
74  $result = $this->helper->getAttributeMetadata('customer', 'lastname');
75  $expected = [
76  'entity_type_id' => '1',
77  'attribute_id' => '2',
78  'attribute_table' => 'customer_entity_varchar',
79  'backend_type' => 'varchar',
80  ];
81 
82  foreach ($result as $key => $value) {
83  $this->assertArrayHasKey($key, $expected, 'Attribute metadata with key "' . $key . '" not found.');
84  $this->assertEquals(
85  $expected[$key],
86  $value,
87  'Attribute metadata with key "' . $key . '" has invalid value.'
88  );
89  }
90  }
91 
96  public function testGetFrontendClasses()
97  {
98  $result = $this->helper->getFrontendClasses('someNonExistedClass');
99  $this->assertTrue(count($result) > 1);
100  $this->assertContains(['value' => '', 'label' => 'None'], $result);
101  $this->assertContains(['value' => 'validate-number', 'label' => 'Decimal Number'], $result);
102  }
103 
108  {
109  $this->attributeConfig->expects($this->never())->method('getEntityAttributesLockedFields');
110  $this->assertEquals([], $this->helper->getAttributeLockedFields(''));
111  }
112 
117  {
118  $lockedFields = ['lockedField1', 'lockedField2'];
119 
120  $this->attributeConfig->expects($this->once())->method('getEntityAttributesLockedFields')
121  ->with('entityTypeCode')->will($this->returnValue($lockedFields));
122  $this->assertEquals($lockedFields, $this->helper->getAttributeLockedFields('entityTypeCode'));
123  }
124 
129  {
130  $lockedFields = ['lockedField1', 'lockedField2'];
131 
132  $this->attributeConfig->expects($this->once())->method('getEntityAttributesLockedFields')
133  ->with('entityTypeCode')->will($this->returnValue($lockedFields));
134 
135  $this->helper->getAttributeLockedFields('entityTypeCode');
136  $this->assertEquals($lockedFields, $this->helper->getAttributeLockedFields('entityTypeCode'));
137  }
138 
143  {
144  $this->attributeConfig->expects($this->once())->method('getEntityAttributesLockedFields')
145  ->with('entityTypeCode')->will($this->returnValue([]));
146 
147  $this->assertEquals([], $this->helper->getAttributeLockedFields('entityTypeCode'));
148  }
149 
151  {
152  $configValue = 'config_value';
153  $this->scopeConfigMock->expects($this->once())
154  ->method('getValue')
155  ->with(\Magento\Eav\Helper\Data::XML_PATH_VALIDATOR_DATA_INPUT_TYPES, ScopeInterface::SCOPE_STORE)
156  ->willReturn($configValue);
157 
158  $this->assertEquals($configValue, $this->helper->getInputTypesValidatorData());
159  }
160 }
$objectManager
Definition: bootstrap.php:17
$value
Definition: gender.phtml:16