Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
FormTest.php
Go to the documentation of this file.
1 <?php
11 
12 class FormTest extends \PHPUnit\Framework\TestCase
13 {
17  protected $_model = null;
18 
22  protected $_attributes = null;
23 
27  protected $_systemAttribute = null;
28 
32  protected $_userAttribute = null;
33 
37  protected $_entity = null;
38 
42  protected function setUp()
43  {
44  $this->_model = $this->getMockBuilder(
45  \Magento\Eav\Model\Form::class
46  )->setMethods(
47  ['_getFilteredFormAttributeCollection', '_getValidator', 'getEntity']
48  )->disableOriginalConstructor()->getMock();
49 
50  $this->_userAttribute = new \Magento\Framework\DataObject(
51  ['is_user_defined' => true, 'attribute_code' => 'attribute_visible_user', 'is_visible' => true]
52  );
53  $this->_systemAttribute = new \Magento\Framework\DataObject(
54  ['is_user_defined' => false, 'attribute_code' => 'attribute_invisible_system', 'is_visible' => false]
55  );
56  $this->_attributes = [$this->_userAttribute, $this->_systemAttribute];
57  $this->_model->expects(
58  $this->any()
59  )->method(
60  '_getFilteredFormAttributeCollection'
61  )->will(
62  $this->returnValue($this->_attributes)
63  );
64 
65  $this->_entity = new \Magento\Framework\DataObject(['id' => 1, 'attribute_visible_user' => 'abc']);
66  $this->_model->expects($this->any())->method('getEntity')->will($this->returnValue($this->_entity));
67  }
68 
72  protected function tearDown()
73  {
74  unset($this->_model);
75  }
76 
80  public function testGetAttributes()
81  {
82  $expected = [
83  'attribute_visible_user' => $this->_userAttribute,
84  'attribute_invisible_system' => $this->_systemAttribute,
85  ];
86  $this->assertEquals($expected, $this->_model->getAttributes());
87  }
88 
92  public function testGetUserAttributes()
93  {
94  $expected = ['attribute_visible_user' => $this->_userAttribute];
95  $this->assertEquals($expected, $this->_model->getUserAttributes());
96  }
97 
101  public function testGetSystemAttributes()
102  {
103  $expected = ['attribute_invisible_system' => $this->_systemAttribute];
104  $this->assertEquals($expected, $this->_model->getSystemAttributes());
105  }
106 
110  public function testGetAllowedAttributes()
111  {
112  $expected = ['attribute_visible_user' => $this->_userAttribute];
113  $this->assertEquals($expected, $this->_model->getAllowedAttributes());
114  }
115 
125  public function testValidateDataPassed($isValid, $expected, $messages = null)
126  {
127  $validator = $this->getMockBuilder(
128  \Magento\Eav\Model\Validator\Attribute\Data::class
129  )->disableOriginalConstructor()->setMethods(
130  ['isValid', 'getMessages']
131  )->getMock();
132  $validator->expects($this->once())->method('isValid')->will($this->returnValue($isValid));
133  if ($messages) {
134  $validator->expects($this->once())->method('getMessages')->will($this->returnValue($messages));
135  } else {
136  $validator->expects($this->never())->method('getMessages');
137  }
138 
139  $this->_model->expects($this->once())->method('_getValidator')->will($this->returnValue($validator));
140 
141  $data = ['test' => true];
142  $this->assertEquals($expected, $this->_model->validateData($data));
143  }
144 
150  public function validateDataProvider()
151  {
152  return [
153  'is_valid' => [true, true, null],
154  'is_invalid' => [false, ['Invalid'], ['attribute_visible_user' => ['Invalid']]]
155  ];
156  }
157 }
testValidateDataPassed($isValid, $expected, $messages=null)
Definition: FormTest.php:125