Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CustomlayoutupdateTest.php
Go to the documentation of this file.
1 <?php
8 
11 
12 class CustomlayoutupdateTest extends \PHPUnit\Framework\TestCase
13 {
17  private $attributeName = 'private';
18 
22  private $model;
23 
27  public function testValidateException()
28  {
29  $object = new DataObject();
30  $object->setData($this->attributeName, 'exception');
31  $this->model->validate($object);
32  }
33 
38  public function testValidate($data)
39  {
40  $object = new DataObject();
41  $object->setData($this->attributeName, $data);
42 
43  $this->assertTrue($this->model->validate($object));
44  $this->assertTrue($this->model->validate($object));
45  }
46 
50  public function validateProvider()
51  {
52  return [[''], ['xml']];
53  }
54 
55  protected function setUp()
56  {
57  $helper = new ObjectManager($this);
58  $this->model = $helper->getObject(
59  \Magento\Catalog\Model\Attribute\Backend\Customlayoutupdate::class,
60  [
61  'layoutUpdateValidatorFactory' => $this->getMockedLayoutUpdateValidatorFactory()
62  ]
63  );
64  $this->model->setAttribute($this->getMockedAttribute());
65  }
66 
70  private function getMockedLayoutUpdateValidatorFactory()
71  {
72  $mockBuilder = $this->getMockBuilder(\Magento\Framework\View\Model\Layout\Update\ValidatorFactory::class);
73  $mockBuilder->disableOriginalConstructor();
74  $mockBuilder->setMethods(['create']);
75  $mock = $mockBuilder->getMock();
76 
77  $mock->expects($this->any())
78  ->method('create')
79  ->will($this->returnValue($this->getMockedValidator()));
80 
81  return $mock;
82  }
83 
87  private function getMockedValidator()
88  {
89  $mockBuilder = $this->getMockBuilder(\Magento\Framework\View\Model\Layout\Update\Validator::class);
90  $mockBuilder->disableOriginalConstructor();
91  $mock = $mockBuilder->getMock();
92 
93  $mock->expects($this->any())
94  ->method('isValid')
95  ->will(
100  $this->returnCallback(
101  function ($xml) {
102  if ($xml == 'exception') {
103  return false;
104  } else {
105  return true;
106  }
107  }
108  )
109  );
110 
111  $mock->expects($this->any())
112  ->method('getMessages')
113  ->will($this->returnValue(['error']));
114 
115  return $mock;
116  }
117 
121  private function getMockedAttribute()
122  {
123  $mockBuilder = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class);
124  $mockBuilder->disableOriginalConstructor();
125  $mock = $mockBuilder->getMock();
126 
127  $mock->expects($this->any())
128  ->method('getName')
129  ->will($this->returnValue($this->attributeName));
130 
131  $mock->expects($this->any())
132  ->method('getIsRequired')
133  ->will($this->returnValue(false));
134 
135  return $mock;
136  }
137 }
$helper
Definition: iframe.phtml:13