Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TemporaryStorageTest.php
Go to the documentation of this file.
1 <?php
7 
11 
12 class TemporaryStorageTest extends \PHPUnit\Framework\TestCase
13 {
17  private $adapter;
18 
22  private $tableName;
23 
27  private $model;
28 
32  private $config;
33 
34  protected function setUp()
35  {
36  $this->tableName = 'some_table_name';
37 
38  $this->adapter = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)
39  ->disableOriginalConstructor()
40  ->getMockForAbstractClass();
41 
42  $resource = $this->getMockBuilder(\Magento\Framework\App\ResourceConnection::class)
43  ->disableOriginalConstructor()
44  ->getMock();
45  $resource->expects($this->any())
46  ->method('getConnection')
47  ->willReturn($this->adapter);
48  $resource->expects($this->any())
49  ->method('getTableName')
50  ->willReturn($this->tableName);
51 
52  $this->config = $this->getMockBuilder(\Magento\Framework\App\DeploymentConfig::class)
53  ->disableOriginalConstructor()
54  ->getMock();
55 
56  $this->model = (new ObjectManager($this))->getObject(
57  \Magento\Framework\Search\Adapter\Mysql\TemporaryStorage::class,
58  ['resource' => $resource, 'config' => $this->config]
59  );
60  }
61 
62  public function testStoreDocumentsFromSelect()
63  {
64  $sql = 'some SQL query';
65 
67  $select = $this->getMockBuilder(\Magento\Framework\DB\Select::class)
68  ->disableOriginalConstructor()
69  ->getMock();
70 
71  $table = $this->createTemporaryTable();
72 
73  $table->expects($this->any())
74  ->method('getName')
75  ->willReturn($this->tableName);
76 
77  $this->adapter->expects($this->once())
78  ->method('insertFromSelect')
79  ->with($select, $this->tableName)
80  ->willReturn($sql);
81  $this->adapter->expects($this->once())
82  ->method('query')
83  ->with($sql);
84 
85  $result = $this->model->storeDocumentsFromSelect($select);
86 
87  $this->assertEquals($table, $result);
88  }
89 
90  public function testStoreDocuments()
91  {
92  $documentId = 312432;
93  $documentValue = 1.235123;
94 
95  $attributeValue = $this->getMockBuilder(\Magento\Framework\Api\AttributeValue::class)
96  ->disableOriginalConstructor()
97  ->getMock();
98  $attributeValue->expects($this->once())
99  ->method('getValue')
100  ->willReturn($documentValue);
101 
102  $document = $this->getMockBuilder(\Magento\Framework\Api\Search\Document::class)
103  ->disableOriginalConstructor()
104  ->getMock();
105  $document->expects($this->once())
106  ->method('getId')
107  ->willReturn($documentId);
108  $document->expects($this->once())
109  ->method('getCustomAttribute')
110  ->with('score')
111  ->willReturn($attributeValue);
112 
113  $table = $this->createTemporaryTable();
114 
115  $result = $this->model->storeDocuments([$document]);
116 
117  $this->assertEquals($result, $table);
118  }
119 
120  public function testStoreApiDocuments()
121  {
122  $documentId = 312432;
123  $documentValue = 1.235123;
124 
125  $attributeValue = $this->getMockBuilder(\Magento\Framework\Api\AttributeValue::class)
126  ->disableOriginalConstructor()
127  ->getMock();
128  $attributeValue->expects($this->once())
129  ->method('getValue')
130  ->willReturn($documentValue);
131 
132  $document = $this->getMockBuilder(\Magento\Framework\Api\Search\Document::class)
133  ->disableOriginalConstructor()
134  ->getMock();
135  $document->expects($this->once())
136  ->method('getId')
137  ->willReturn($documentId);
138  $document->expects($this->once())
139  ->method('getCustomAttribute')
140  ->with('score')
141  ->willReturn($attributeValue);
142 
143  $table = $this->createTemporaryTable();
144 
145  $result = $this->model->storeApiDocuments([$document]);
146 
147  $this->assertEquals($result, $table);
148  }
149 
150  public function testNoDropIfNotPersistent()
151  {
152  $this->createTemporaryTable(false);
153 
154  $this->adapter->expects($this->never())
155  ->method('dropTemporaryTable');
156 
157  // model->createTemporaryTable() is a private method; this will call it
158  $this->model->storeApiDocuments([]);
159  }
160 
164  private function createTemporaryTable($persistentConnection = true)
165  {
166  $this->config->expects($this->any())
167  ->method('get')
168  ->with('db/connection/indexer/persistent')
169  ->willReturn($persistentConnection);
170 
171  $table = $this->getMockBuilder(\Magento\Framework\DB\Ddl\Table::class)
172  ->disableOriginalConstructor()
173  ->getMock();
174 
175  $tableInteractionCount = 0;
176  if ($persistentConnection) {
177  $this->adapter->expects($this->once())
178  ->method('dropTemporaryTable');
179  $tableInteractionCount += 1;
180  }
181  $table->expects($this->at($tableInteractionCount))
182  ->method('addColumn')
183  ->with(
186  10,
187  ['unsigned' => true, 'nullable' => false, 'primary' => true],
188  'Entity ID'
189  );
190  $tableInteractionCount += 1;
191  $table->expects($this->at($tableInteractionCount))
192  ->method('addColumn')
193  ->with(
194  'score',
196  [32, 16],
197  ['unsigned' => true, 'nullable' => false],
198  'Score'
199  );
200  $table->expects($this->once())
201  ->method('setOption')
202  ->with('type', 'memory');
203 
204  $this->adapter->expects($this->once())
205  ->method('newTable')
206  ->with($this->tableName)
207  ->willReturn($table);
208  $this->adapter->expects($this->once())
209  ->method('createTemporaryTable')
210  ->with($table);
211 
212  return $table;
213  }
214 }
$resource
Definition: bulk.php:12
$table
Definition: trigger.php:14