Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
StopwordsTest.php
Go to the documentation of this file.
1 <?php
7 
10 use Magento\Framework\Locale\Resolver as LocaleResolver;
17 
21 class StopwordsTest extends \PHPUnit\Framework\TestCase
22 {
26  protected $model;
27 
31  protected $storeManager;
32 
36  protected $localeResolver;
37 
41  protected $readFactory;
42 
46  protected $configCache;
47 
51  protected $esConfig;
52 
56  private $serializerMock;
57 
63  protected function setUp()
64  {
65  $this->storeManager = $this->getMockBuilder(\Magento\Store\Model\StoreManagerInterface::class)
66  ->disableOriginalConstructor()
67  ->getMock();
68  $this->localeResolver = $this->getMockBuilder(\Magento\Framework\Locale\Resolver::class)
69  ->disableOriginalConstructor()
70  ->setMethods([
71  'emulate',
72  'getLocale',
73  ])
74  ->getMock();
75  $this->readFactory = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\ReadFactory::class)
76  ->disableOriginalConstructor()
77  ->setMethods(['create'])
78  ->getMock();
79  $this->configCache = $this->getMockBuilder(\Magento\Framework\App\Cache\Type\Config::class)
80  ->disableOriginalConstructor()
81  ->getMock();
82  $this->esConfig = $this->getMockBuilder(
83  \Magento\Elasticsearch\Model\Adapter\Index\Config\EsConfigInterface::class
84  )->disableOriginalConstructor()->getMock();
85 
86  $this->serializerMock = $this->createMock(SerializerInterface::class);
87 
88  $objectManager = new ObjectManagerHelper($this);
89  $this->model = $objectManager->getObject(
90  Stopwords::class,
91  [
92  'storeManager' => $this->storeManager,
93  'localeResolver' => $this->localeResolver,
94  'readFactory' => $this->readFactory,
95  'configCache' => $this->configCache,
96  'esConfig' => $this->esConfig,
97  'stopwordsModule' => '',
98  'stopwordsDirectory' => ''
99  ]
100  );
101  $objectManager->setBackwardCompatibleProperty(
102  $this->model,
103  'serializer',
104  $this->serializerMock
105  );
106  }
107 
111  public function testProcess()
112  {
113  $stopWordsFromFile = "a\nthe\nof";
114  $stopWords = ['a', 'the', 'of'];
115  $serializedStopWords = 'serialized stop words';
116  $this->esConfig->expects($this->once())
117  ->method('getStopwordsInfo')
118  ->willReturn([
119  'default' => 'default.csv',
120  ]);
121  $storeInterface = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
122  ->disableOriginalConstructor()
123  ->getMock();
124  $this->storeManager->expects($this->once())
125  ->method('getStore')
126  ->willReturn($storeInterface);
127  $storeInterface->expects($this->once())
128  ->method('getId')
129  ->willReturn(1);
130  $this->localeResolver->expects($this->once())
131  ->method('getLocale')
132  ->willReturn('en_US');
133 
134  $read = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\Read::class)
135  ->disableOriginalConstructor()
136  ->getMock();
137  $this->readFactory->expects($this->once())
138  ->method('create')
139  ->willReturn($read);
140  $read->expects($this->once())
141  ->method('stat')
142  ->willReturn([
143  'mtime' => 0,
144  ]);
145  $this->configCache->expects($this->once())
146  ->method('load')
147  ->willReturn(false);
148 
149  $read->expects($this->once())
150  ->method('readFile')
151  ->willReturn($stopWordsFromFile);
152  $this->serializerMock->expects($this->once())
153  ->method('serialize')
154  ->with($stopWords)
155  ->willReturn($serializedStopWords);
156  $this->configCache->expects($this->once())
157  ->method('save')
158  ->with(
159  $serializedStopWords,
161  );
162 
163  $this->assertEquals(
164  'test query',
165  $this->model->process('the test of a query')
166  );
167  }
168 
172  public function testProcessFromCache()
173  {
174  $serializedStopWords = 'serialized stop words';
175  $this->esConfig->expects($this->once())
176  ->method('getStopwordsInfo')
177  ->willReturn([
178  'default' => 'default.csv',
179  ]);
180  $storeInterface = $this->getMockBuilder(\Magento\Store\Api\Data\StoreInterface::class)
181  ->disableOriginalConstructor()
182  ->getMock();
183  $this->storeManager->expects($this->once())
184  ->method('getStore')
185  ->willReturn($storeInterface);
186  $storeInterface->expects($this->once())
187  ->method('getId')
188  ->willReturn(1);
189  $this->localeResolver->expects($this->once())
190  ->method('getLocale')
191  ->willReturn('en_US');
192 
193  $read = $this->getMockBuilder(\Magento\Framework\Filesystem\Directory\Read::class)
194  ->disableOriginalConstructor()
195  ->getMock();
196  $this->readFactory->expects($this->once())
197  ->method('create')
198  ->willReturn($read);
199  $read->expects($this->once())
200  ->method('stat')
201  ->willReturn([
202  'mtime' => 0,
203  ]);
204  $this->configCache->expects($this->once())
205  ->method('load')
206  ->willReturn($serializedStopWords);
207  $this->serializerMock->expects($this->once())
208  ->method('unserialize')
209  ->with($serializedStopWords)
210  ->willReturn(['a', 'the', 'of']);
211 
212  $this->assertEquals(
213  'test query',
214  $this->model->process('the test of a query')
215  );
216  }
217 }
$objectManager
Definition: bootstrap.php:17