Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
EngineProviderTest.php
Go to the documentation of this file.
1 <?php
7 
12 
13 class EngineProviderTest extends \PHPUnit\Framework\TestCase
14 {
16  private $model;
17 
19  private $objectManagerMock;
20 
22  private $engineResolverMock;
23 
24  protected function setUp()
25  {
26  $this->objectManagerMock = $this->getMockBuilder(ObjectManagerInterface::class)
27  ->getMockForAbstractClass();
28  $this->engineResolverMock = $this->getMockBuilder(EngineResolverInterface::class)
29  ->getMockForAbstractClass();
30  }
31 
32  public function testGet()
33  {
34  $currentEngine = 'current_engine';
35  $currentEngineClass = EngineInterface::class;
36  $engines = [
37  $currentEngine => $currentEngineClass,
38  ];
39 
40  $this->engineResolverMock->expects($this->once())
41  ->method('getCurrentSearchEngine')
42  ->willReturn($currentEngine);
43 
44  $engineMock = $this->getMockBuilder($currentEngineClass)
45  ->setMethods(['isAvailable'])
46  ->getMockForAbstractClass();
47 
48  $this->objectManagerMock->expects($this->once())
49  ->method('create')
50  ->with($currentEngineClass)
51  ->willReturn($engineMock);
52 
53  $engineMock->expects($this->once())
54  ->method('isAvailable')
55  ->willReturn(true);
56 
57  $this->model = new EngineProvider(
58  $this->objectManagerMock,
59  $engines,
60  $this->engineResolverMock
61  );
62 
63  $this->assertEquals($engineMock, $this->model->get());
64  $this->assertEquals($engineMock, $this->model->get());
65  }
66 
71  public function testGetWithoutEngines()
72  {
73  $currentEngine = 'current_engine';
74  $engines = [];
75 
76  $this->engineResolverMock->expects($this->once())
77  ->method('getCurrentSearchEngine')
78  ->willReturn($currentEngine);
79 
80  $this->objectManagerMock->expects($this->never())
81  ->method('create');
82 
83  $this->model = new EngineProvider(
84  $this->objectManagerMock,
85  $engines,
86  $this->engineResolverMock
87  );
88 
89  $this->model->get();
90  }
91 
96  public function testGetWithWrongEngine()
97  {
98  $currentEngine = 'current_engine';
99  $currentEngineClass = \stdClass::class;
100  $engines = [
101  $currentEngine => $currentEngineClass,
102  ];
103 
104  $this->engineResolverMock->expects($this->once())
105  ->method('getCurrentSearchEngine')
106  ->willReturn($currentEngine);
107 
108  $engineMock = $this->getMockBuilder($currentEngineClass)
109  ->getMockForAbstractClass();
110 
111  $this->objectManagerMock->expects($this->once())
112  ->method('create')
113  ->with($currentEngineClass)
114  ->willReturn($engineMock);
115 
116  $this->model = new EngineProvider(
117  $this->objectManagerMock,
118  $engines,
119  $this->engineResolverMock
120  );
121 
122  $this->model->get();
123  }
124 
130  {
131  $currentEngine = 'current_engine';
132  $currentEngineClass = EngineInterface::class;
133  $engines = [
134  $currentEngine => $currentEngineClass,
135  ];
136 
137  $this->engineResolverMock->expects($this->once())
138  ->method('getCurrentSearchEngine')
139  ->willReturn($currentEngine);
140 
141  $engineMock = $this->getMockBuilder($currentEngineClass)
142  ->setMethods(['isAvailable'])
143  ->getMockForAbstractClass();
144 
145  $this->objectManagerMock->expects($this->once())
146  ->method('create')
147  ->with($currentEngineClass)
148  ->willReturn($engineMock);
149 
150  $engineMock->expects($this->once())
151  ->method('isAvailable')
152  ->willReturn(false);
153 
154  $this->model = new EngineProvider(
155  $this->objectManagerMock,
156  $engines,
157  $this->engineResolverMock
158  );
159 
160  $this->model->get();
161  }
162 }