Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TableDataTest.php
Go to the documentation of this file.
1 <?php
7 
9 
10 class TableDataTest extends \PHPUnit\Framework\TestCase
11 {
15  protected $_connectionMock;
16 
21 
25  protected $_objectManager;
26 
30  protected $_resourceMock;
31 
32  protected function setUp()
33  {
34  $this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
35  $this->_connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
36  $this->_resourceMock = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
37  $this->_productIndexerHelper = $this->createMock(\Magento\Catalog\Helper\Product\Flat\Indexer::class);
38  }
39 
41  {
42  $flatTable = 'flat_table';
43  $flatDropName = 'flat_table_to_drop';
44  $temporaryFlatTableName = 'flat_tmp';
45 
46  $this->_connectionMock->expects($this->exactly(2))->method('dropTable')->with($flatDropName);
47  $this->_connectionMock->expects(
48  $this->once()
49  )->method(
50  'isTableExists'
51  )->with(
52  $flatTable
53  )->will(
54  $this->returnValue(false)
55  );
56 
57  $this->_connectionMock->expects(
58  $this->once()
59  )->method(
60  'renameTablesBatch'
61  )->with(
62  [['oldName' => 'flat_tmp', 'newName' => 'flat_table']]
63  );
64 
65  $this->_resourceMock->expects(
66  $this->once()
67  )->method(
68  'getConnection'
69  )->will(
70  $this->returnValue($this->_connectionMock)
71  );
72 
73  $model = $this->_objectManager->getObject(
74  \Magento\Catalog\Model\Indexer\Product\Flat\Action\Rows\TableData::class,
75  ['resource' => $this->_resourceMock, 'productIndexerHelper' => $this->_productIndexerHelper]
76  );
77 
78  $model->move($flatTable, $flatDropName, $temporaryFlatTableName);
79  }
80 
82  {
83  $flatTable = 'flat_table';
84  $flatDropName = 'flat_table_to_drop';
85  $temporaryFlatTableName = 'flat_tmp';
86 
87  $describedColumns = [
88  'column_11' => 'column_definition',
89  'column_2' => 'column_definition',
90  'column_3' => 'column_definition',
91  ];
92 
93  $flatColumns = [
94  'column_1' => 'column_definition',
95  'column_2' => 'column_definition',
96  'column_3' => 'column_definition',
97  ];
98 
99  $selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
100  $selectMock->expects(
101  $this->once()
102  )->method(
103  'from'
104  )->with(
105  ['tf' => sprintf('%s_tmp_indexer', $flatTable)],
106  ['column_2', 'column_3']
107  );
108  $sql = md5(time());
109  $selectMock->expects(
110  $this->once()
111  )->method(
112  'insertFromSelect'
113  )->with(
114  $flatTable,
115  ['column_2', 'column_3']
116  )->will(
117  $this->returnValue($sql)
118  );
119 
120  $this->_connectionMock->expects($this->once())->method('query')->with($sql);
121 
122  $this->_connectionMock->expects($this->once())->method('select')->will($this->returnValue($selectMock));
123 
124  $this->_connectionMock->expects(
125  $this->once()
126  )->method(
127  'isTableExists'
128  )->with(
129  $flatTable
130  )->will(
131  $this->returnValue(true)
132  );
133 
134  $this->_connectionMock->expects(
135  $this->once()
136  )->method(
137  'describeTable'
138  )->with(
139  $flatTable
140  )->will(
141  $this->returnValue($describedColumns)
142  );
143 
144  $this->_productIndexerHelper->expects(
145  $this->once()
146  )->method(
147  'getFlatColumns'
148  )->will(
149  $this->returnValue($flatColumns)
150  );
151 
152  $this->_connectionMock->expects(
153  $this->once()
154  )->method(
155  'dropTable'
156  )->with(
157  sprintf('%s_tmp_indexer', $flatTable)
158  );
159 
160  $this->_resourceMock->expects(
161  $this->any()
162  )->method(
163  'getConnection'
164  )->will(
165  $this->returnValue($this->_connectionMock)
166  );
167 
168  $model = $this->_objectManager->getObject(
169  \Magento\Catalog\Model\Indexer\Product\Flat\Action\Rows\TableData::class,
170  ['resource' => $this->_resourceMock, 'productIndexerHelper' => $this->_productIndexerHelper]
171  );
172 
173  $model->move($flatTable, $flatDropName, $temporaryFlatTableName);
174  }
175 }