Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SynchronizeTest.php
Go to the documentation of this file.
1 <?php
7 
14 
15 class SynchronizeTest extends \PHPUnit\Framework\TestCase
16 {
20  private $synchronize;
21 
25  private $contextMock;
26 
30  private $synchronizerMock;
31 
35  private $requestMock;
36 
40  private $jsonFactoryMock;
41 
42  protected function setUp()
43  {
44  $this->contextMock = $this->getMockBuilder(Context::class)
45  ->disableOriginalConstructor()
46  ->getMock();
47  $this->synchronizerMock = $this->getMockBuilder(Synchronizer::class)
48  ->disableOriginalConstructor()
49  ->getMock();
50  $this->jsonFactoryMock = $this->getMockBuilder(JsonFactory::class)
51  ->disableOriginalConstructor()
52  ->getMock();
53 
54  $this->requestMock = $this->getMockBuilder(RequestInterface::class)
55  ->disableOriginalConstructor()
56  ->getMockForAbstractClass();
57 
58  $this->contextMock->expects($this->any())
59  ->method('getRequest')
60  ->willReturn($this->requestMock);
61 
62  $this->synchronize = new Synchronize(
63  $this->contextMock,
64  $this->synchronizerMock,
65  $this->jsonFactoryMock
66  );
67  }
68 
69  public function testExecuteAction()
70  {
71  $data = [
72  'type_id' => null,
73  'ids' => []
74  ];
75 
76  $jsonObject = $this->getMockBuilder(Json::class)
77  ->disableOriginalConstructor()
78  ->getMock();
79 
80  $this->jsonFactoryMock->expects($this->once())
81  ->method('create')
82  ->willReturn($jsonObject);
83 
84  $this->requestMock->expects($this->at(0))
85  ->method('getParam')
86  ->with('ids', [])
87  ->willReturn($data['ids']);
88 
89  $this->requestMock->expects($this->at(1))
90  ->method('getParam')
91  ->with('type_id', null)
92  ->willReturn($data['type_id']);
93 
94  $this->synchronizerMock->expects($this->once())
95  ->method('syncActions')
96  ->with([], null);
97 
98  $jsonObject->expects($this->once())
99  ->method('setData')
100  ->with([]);
101 
102  $this->synchronize->execute();
103  }
104 
105  public function testExecuteActionException()
106  {
107  $data = [
108  'type_id' => null,
109  'ids' => []
110  ];
111  $jsonObject = $this->getMockBuilder(Json::class)
112  ->disableOriginalConstructor()
113  ->getMock();
114 
115  $this->jsonFactoryMock->expects($this->once())
116  ->method('create')
117  ->willReturn($jsonObject);
118 
119  $this->requestMock->expects($this->at(0))
120  ->method('getParam')
121  ->with('ids', [])
122  ->willReturn($data['ids']);
123 
124  $this->requestMock->expects($this->at(1))
125  ->method('getParam')
126  ->with('type_id', null)
127  ->willReturn($data['type_id']);
128 
129  $this->synchronizerMock->expects($this->once())
130  ->method('syncActions')
131  ->willThrowException(new \Exception);
132 
133  $jsonObject->expects($this->once())
134  ->method('setStatusHeader')
135  ->with(
136  \Zend\Http\Response::STATUS_CODE_400,
137  \Zend\Http\AbstractMessage::VERSION_11,
138  'Bad Request'
139  );
140  $jsonObject->expects($this->once())
141  ->method('setData')
142  ->with([]);
143 
144  $this->synchronize->execute();
145  }
146 }