Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractModelTest.php
Go to the documentation of this file.
1 <?php
8 
16 class AbstractModelTest extends \PHPUnit\Framework\TestCase
17 {
18 
22  private $model;
23 
27  private $contextMock;
28 
32  private $registryMock;
33 
37  private $formFactoryMock;
38 
42  private $localeDateMock;
43 
47  private $eventManagerMock;
48 
49  protected function setUp()
50  {
51  $this->localeDateMock = $this->getMockBuilder(\Magento\Framework\Stdlib\DateTime\TimezoneInterface::class)
52  ->disableOriginalConstructor()
53  ->getMock();
54 
55  $this->formFactoryMock = $this->getMockBuilder(\Magento\Framework\Data\FormFactory::class)
56  ->disableOriginalConstructor()
57  ->getMock();
58 
59  $this->registryMock = $this->getMockBuilder(\Magento\Framework\Registry::class)
60  ->disableOriginalConstructor()
61  ->getMock();
62 
63  $this->contextMock = $this->getMockBuilder(\Magento\Framework\Model\Context::class)
64  ->disableOriginalConstructor()
65  ->setMethods(['getEventDispatcher'])
66  ->getMock();
67 
68  $this->eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
69  ->disableOriginalConstructor()
70  ->setMethods(['dispatch'])
71  ->getMock();
72  $this->contextMock->expects($this->any())
73  ->method('getEventDispatcher')
74  ->will($this->returnValue($this->eventManagerMock));
75 
76  $resourceMock = $this->getMockBuilder(\Magento\Framework\Model\ResourceModel\AbstractResource::class)
77  ->disableOriginalConstructor()
78  ->getMock();
79  $resourceCollectionMock = $this->getMockBuilder(\Magento\Framework\Data\Collection\AbstractDb::class)
80  ->disableOriginalConstructor()
81  ->getMock();
82  $extensionFactory = $this->getMockBuilder(\Magento\Framework\Api\ExtensionAttributesFactory::class)
83  ->disableOriginalConstructor()
84  ->getMock();
85  $customAttributeFactory = $this->getMockBuilder(\Magento\Framework\Api\AttributeValueFactory::class)
86  ->disableOriginalConstructor()
87  ->getMock();
88 
89  $this->model = $this->getMockForAbstractClass(
90  \Magento\Rule\Model\AbstractModel::class,
91  [
92  'context' => $this->contextMock,
93  'registry' => $this->registryMock,
94  'formFactory' => $this->formFactoryMock,
95  'localeDate' => $this->localeDateMock,
96  'resource' => $resourceMock,
97  'resourceCollection' => $resourceCollectionMock,
98  'data' => [],
99  'extensionFactory' => $extensionFactory,
100  'customAttributeFactory' => $customAttributeFactory,
101  'serializer' => $this->getSerializerMock(),
102  ]
103  );
104  }
105 
111  private function getSerializerMock()
112  {
113  $serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class)
114  ->disableOriginalConstructor()
115  ->setMethods(['serialize', 'unserialize'])
116  ->getMock();
117 
118  $serializerMock->expects($this->any())
119  ->method('serialize')
120  ->will(
121  $this->returnCallback(
122  function ($value) {
123  return json_encode($value);
124  }
125  )
126  );
127 
128  $serializerMock->expects($this->any())
129  ->method('unserialize')
130  ->will(
131  $this->returnCallback(
132  function ($value) {
133  return json_decode($value, true);
134  }
135  )
136  );
137 
138  return $serializerMock;
139  }
140 
141  public function testGetConditions()
142  {
143  $conditionsArray = ['conditions' => 'serialized'];
144  $serializedConditions = json_encode($conditionsArray);
145  $conditions = $this->getMockBuilder(\Magento\Rule\Model\Condition\Combine::class)
146  ->setMethods(['setRule', 'setId', 'setPrefix', 'loadArray'])
147  ->disableOriginalConstructor()
148  ->getMock();
149 
150  $conditions->expects($this->once())->method('setRule')->will($this->returnSelf());
151  $conditions->expects($this->once())->method('setId')->will($this->returnSelf());
152  $conditions->expects($this->once())->method('setPrefix')->will($this->returnSelf());
153 
154  $this->model->expects($this->once())->method('getConditionsInstance')->will($this->returnValue($conditions));
155 
156  $this->model->setConditionsSerialized($serializedConditions);
157 
158  $conditions->expects($this->once())->method('loadArray')->with($conditionsArray);
159 
160  $this->assertEquals($conditions, $this->model->getConditions());
161  }
162 
163  public function testGetActions()
164  {
165  $actionsArray = ['actions' => 'some_actions'];
166  $actionsSerialized = json_encode($actionsArray);
167  $actions = $this->getMockBuilder(\Magento\Rule\Model\Action\Collection::class)
168  ->setMethods(['setRule', 'setId', 'setPrefix', 'loadArray'])
169  ->disableOriginalConstructor()
170  ->getMock();
171 
172  $actions->expects($this->once())->method('setRule')->will($this->returnSelf());
173  $actions->expects($this->once())->method('setId')->will($this->returnSelf());
174  $actions->expects($this->once())->method('setPrefix')->will($this->returnSelf());
175 
176  $this->model->expects($this->once())->method('getActionsInstance')->will($this->returnValue($actions));
177 
178  $this->model->setActionsSerialized($actionsSerialized);
179 
180  $actions->expects($this->once())->method('loadArray')->with($actionsArray);
181 
182  $this->assertEquals($actions, $this->model->getActions());
183  }
184 
185  public function testBeforeSave()
186  {
187  $conditions = $this->getMockBuilder(\Magento\Rule\Model\Condition\Combine::class)
188  ->setMethods(['asArray'])
189  ->disableOriginalConstructor()
190  ->getMock();
191 
192  $actions = $this->getMockBuilder(\Magento\Rule\Model\Action\Collection::class)
193  ->setMethods(['asArray'])
194  ->disableOriginalConstructor()
195  ->getMock();
196 
197  $this->model->setConditions($conditions);
198  $this->model->setActions($actions);
199 
200  $conditions->expects($this->any())->method('asArray')->will($this->returnValue(['conditions' => 'array']));
201  $actions->expects($this->any())->method('asArray')->will($this->returnValue(['actions' => 'array']));
202 
203  $this->eventManagerMock->expects($this->exactly(2))->method('dispatch');
204 
205  $this->assertEquals($this->model, $this->model->beforeSave());
206  $this->assertEquals(json_encode(['conditions' => 'array']), $this->model->getConditionsSerialized());
207  $this->assertEquals(json_encode(['actions' => 'array']), $this->model->getActionsSerialized());
208  }
209 }
$value
Definition: gender.phtml:16