Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DefaultFrontendTest.php
Go to the documentation of this file.
1 <?php
7 
9 use Magento\Eav\Model\Entity\Attribute\Source\BooleanFactory;
16 
17 class DefaultFrontendTest extends \PHPUnit\Framework\TestCase
18 {
22  protected $model;
23 
27  protected $booleanFactory;
28 
32  private $serializerMock;
33 
37  private $storeManagerMock;
38 
42  private $storeMock;
43 
47  private $cacheMock;
48 
52  private $attributeMock;
53 
57  private $cacheTags;
58 
62  private $sourceMock;
63 
64  protected function setUp()
65  {
66  $this->cacheTags = ['tag1', 'tag2'];
67 
68  $this->booleanFactory = $this->getMockBuilder(BooleanFactory::class)
69  ->disableOriginalConstructor()
70  ->getMock();
71  $this->serializerMock = $this->getMockBuilder(Serializer::class)
72  ->getMock();
73  $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class)
74  ->getMockForAbstractClass();
75  $this->storeMock = $this->getMockBuilder(StoreInterface::class)
76  ->getMockForAbstractClass();
77  $this->cacheMock = $this->getMockBuilder(CacheInterface::class)
78  ->getMockForAbstractClass();
79  $this->attributeMock = $this->getMockBuilder(AbstractAttribute::class)
80  ->disableOriginalConstructor()
81  ->setMethods(['getAttributeCode', 'getSource'])
82  ->getMockForAbstractClass();
83  $this->sourceMock = $this->getMockBuilder(AbstractSource::class)
84  ->disableOriginalConstructor()
85  ->setMethods(['getAllOptions'])
86  ->getMockForAbstractClass();
87 
88  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
89  $this->model = $objectManager->getObject(
90  DefaultFrontend::class,
91  [
92  '_attrBooleanFactory' => $this->booleanFactory,
93  'cache' => $this->cacheMock,
94  'storeManager' => $this->storeManagerMock,
95  'serializer' => $this->serializerMock,
96  '_attribute' => $this->attributeMock,
97  'cacheTags' => $this->cacheTags
98  ]
99  );
100  }
101 
102  public function testGetClassEmpty()
103  {
104  $attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
105  ->disableOriginalConstructor()
106  ->setMethods([
107  'getIsRequired',
108  'getFrontendClass',
109  'getValidateRules',
110  ])
111  ->getMock();
112  $attributeMock->expects($this->once())
113  ->method('getIsRequired')
114  ->willReturn(false);
115  $attributeMock->expects($this->once())
116  ->method('getFrontendClass')
117  ->willReturn('');
118  $attributeMock->expects($this->exactly(2))
119  ->method('getValidateRules')
120  ->willReturn('');
121 
122  $this->model->setAttribute($attributeMock);
123  $this->assertEmpty($this->model->getClass());
124  }
125 
126  public function testGetClass()
127  {
128  $attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
129  ->disableOriginalConstructor()
130  ->setMethods([
131  'getIsRequired',
132  'getFrontendClass',
133  'getValidateRules',
134  ])
135  ->getMock();
136  $attributeMock->expects($this->once())
137  ->method('getIsRequired')
138  ->willReturn(true);
139  $attributeMock->expects($this->once())
140  ->method('getFrontendClass')
141  ->willReturn('');
142  $attributeMock->expects($this->exactly(3))
143  ->method('getValidateRules')
144  ->willReturn([
145  'input_validation' => 'alphanumeric',
146  'min_text_length' => 1,
147  'max_text_length' => 2,
148  ]);
149 
150  $this->model->setAttribute($attributeMock);
151  $result = $this->model->getClass();
152 
153  $this->assertContains('validate-alphanum', $result);
154  $this->assertContains('minimum-length-1', $result);
155  $this->assertContains('maximum-length-2', $result);
156  $this->assertContains('validate-length', $result);
157  }
158 
159  public function testGetClassLength()
160  {
161  $attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class)
162  ->disableOriginalConstructor()
163  ->setMethods([
164  'getIsRequired',
165  'getFrontendClass',
166  'getValidateRules',
167  ])
168  ->getMock();
169  $attributeMock->expects($this->once())
170  ->method('getIsRequired')
171  ->willReturn(true);
172  $attributeMock->expects($this->once())
173  ->method('getFrontendClass')
174  ->willReturn('');
175  $attributeMock->expects($this->exactly(3))
176  ->method('getValidateRules')
177  ->willReturn([
178  'input_validation' => 'length',
179  'min_text_length' => 1,
180  'max_text_length' => 2,
181  ]);
182 
183  $this->model->setAttribute($attributeMock);
184  $result = $this->model->getClass();
185 
186  $this->assertContains('minimum-length-1', $result);
187  $this->assertContains('maximum-length-2', $result);
188  $this->assertContains('validate-length', $result);
189  }
190 
191  public function testGetSelectOptions()
192  {
193  $storeId = 1;
194  $attributeCode = 'attr1';
195  $cacheKey = 'attribute-navigation-option-' . $attributeCode . '-' . $storeId;
196  $options = ['option1', 'option2'];
197  $serializedOptions = "{['option1', 'option2']}";
198 
199  $this->storeManagerMock->expects($this->once())
200  ->method('getStore')
201  ->willReturn($this->storeMock);
202  $this->storeMock->expects($this->once())
203  ->method('getId')
204  ->willReturn($storeId);
205  $this->attributeMock->expects($this->once())
206  ->method('getAttributeCode')
207  ->willReturn($attributeCode);
208  $this->cacheMock->expects($this->once())
209  ->method('load')
210  ->with($cacheKey)
211  ->willReturn(false);
212  $this->attributeMock->expects($this->once())
213  ->method('getSource')
214  ->willReturn($this->sourceMock);
215  $this->sourceMock->expects($this->once())
216  ->method('getAllOptions')
217  ->willReturn($options);
218  $this->serializerMock->expects($this->once())
219  ->method('serialize')
220  ->with($options)
221  ->willReturn($serializedOptions);
222  $this->cacheMock->expects($this->once())
223  ->method('save')
224  ->with($serializedOptions, $cacheKey, $this->cacheTags);
225 
226  $this->assertSame($options, $this->model->getSelectOptions());
227  }
228 }
$objectManager
Definition: bootstrap.php:17
$attributeCode
Definition: extend.phtml:12