Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TestConnectionTest.php
Go to the documentation of this file.
1 <?php
7 
11 
17 class TestConnectionTest extends \PHPUnit\Framework\TestCase
18 {
22  protected $requestMock;
23 
27  private $clientResolverMock;
28 
32  private $clientMock;
33 
37  private $resultJson;
38 
42  private $resultJsonFactory;
43 
47  private $tagFilterMock;
48 
52  private $controller;
53 
59  protected function setUp()
60  {
61  $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
62  $this->requestMock = $this->createPartialMock(\Magento\Framework\App\Request\Http::class, ['getParams']);
63  $responseMock = $this->createMock(\Magento\Framework\App\Response\Http::class);
64 
65  $context = $this->getMockBuilder(\Magento\Backend\App\Action\Context::class)
66  ->setMethods(['getRequest', 'getResponse', 'getMessageManager', 'getSession'])
67  ->setConstructorArgs(
68  $helper->getConstructArguments(
69  \Magento\Backend\App\Action\Context::class,
70  [
71  'request' => $this->requestMock
72  ]
73  )
74  )
75  ->getMock();
76  $context->expects($this->once())->method('getRequest')->will($this->returnValue($this->requestMock));
77  $context->expects($this->once())->method('getResponse')->will($this->returnValue($responseMock));
78 
79  $this->clientResolverMock = $this->getMockBuilder(\Magento\AdvancedSearch\Model\Client\ClientResolver::class)
80  ->disableOriginalConstructor()
81  ->setMethods(['create'])
82  ->getMock();
83 
84  $this->clientMock = $this->createMock(\Magento\AdvancedSearch\Model\Client\ClientInterface::class);
85 
86  $this->resultJson = $this->getMockBuilder(\Magento\Framework\Controller\Result\Json::class)
87  ->disableOriginalConstructor()
88  ->getMock();
89 
90  $this->resultJsonFactory = $this->getMockBuilder(\Magento\Framework\Controller\Result\JsonFactory::class)
91  ->disableOriginalConstructor()
92  ->setMethods(['create'])
93  ->getMock();
94 
95  $this->tagFilterMock = $this->getMockBuilder(\Magento\Framework\Filter\StripTags::class)
96  ->disableOriginalConstructor()
97  ->setMethods(['filter'])
98  ->getMock();
99 
100  $this->controller = new TestConnection(
101  $context,
102  $this->clientResolverMock,
103  $this->resultJsonFactory,
104  $this->tagFilterMock
105  );
106  }
107 
108  public function testExecuteEmptyEngine()
109  {
110  $this->requestMock->expects($this->once())->method('getParams')
111  ->will($this->returnValue(['engine' => '']));
112 
113  $this->resultJsonFactory->expects($this->once())->method('create')
114  ->will($this->returnValue($this->resultJson));
115 
116  $result = ['success' => false, 'errorMessage' => 'Missing search engine parameter.'];
117 
118  $this->resultJson->expects($this->once())->method('setData')
119  ->with($this->equalTo($result));
120 
121  $this->controller->execute();
122  }
123 
124  public function testExecute()
125  {
126  $this->requestMock->expects($this->once())->method('getParams')
127  ->will($this->returnValue(['engine' => 'engineName']));
128 
129  $this->clientResolverMock->expects($this->once())->method('create')
130  ->with($this->equalTo('engineName'))
131  ->will($this->returnValue($this->clientMock));
132 
133  $this->clientMock->expects($this->once())->method('testConnection')
134  ->will($this->returnValue(true));
135 
136  $this->resultJsonFactory->expects($this->once())->method('create')
137  ->will($this->returnValue($this->resultJson));
138 
139  $result = ['success' => true, 'errorMessage' => ''];
140 
141  $this->resultJson->expects($this->once())->method('setData')
142  ->with($this->equalTo($result));
143 
144  $this->controller->execute();
145  }
146 
147  public function testExecutePingFailed()
148  {
149  $this->requestMock->expects($this->once())->method('getParams')
150  ->will($this->returnValue(['engine' => 'engineName']));
151 
152  $this->clientResolverMock->expects($this->once())->method('create')
153  ->with($this->equalTo('engineName'))
154  ->will($this->returnValue($this->clientMock));
155 
156  $this->clientMock->expects($this->once())->method('testConnection')
157  ->will($this->returnValue(false));
158 
159  $this->resultJsonFactory->expects($this->once())->method('create')
160  ->will($this->returnValue($this->resultJson));
161 
162  $result = ['success' => false, 'errorMessage' => ''];
163 
164  $this->resultJson->expects($this->once())->method('setData')
165  ->with($this->equalTo($result));
166 
167  $this->controller->execute();
168  }
169 }
$helper
Definition: iframe.phtml:13