Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IndexerTableSwapperTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
14 use PHPUnit_Framework_MockObject_MockObject;
15 
19 class IndexerTableSwapperTest extends \PHPUnit\Framework\TestCase
20 {
24  private $resourceConnectionMock;
25 
29  private $adapterInterfaceMock;
30 
34  private $statementInterfaceMock;
35 
39  private $tableMock;
40 
44  protected function setUp()
45  {
46  $this->resourceConnectionMock = $this->createMock(ResourceConnection::class);
47 
48  $this->adapterInterfaceMock = $this->getMockBuilder(AdapterInterface::class)->getMockForAbstractClass();
50  $this->statementInterfaceMock = $this->getMockBuilder(\Zend_Db_Statement_Interface::class)
51  ->getMockForAbstractClass();
53  $this->tableMock = $this->createMock(Table::class);
54  $this->resourceConnectionMock->expects($this->any())
55  ->method('getConnection')
56  ->willReturn($this->adapterInterfaceMock);
57  }
58 
63  {
64  $model = new IndexerTableSwapper($this->resourceConnectionMock);
65  $originalTableName = 'catalogrule_product';
66  $temporaryTableNames = ['catalogrule_product' => 'catalogrule_product__temp9604'];
67  $this->setObjectProperty($model, 'temporaryTables', $temporaryTableNames);
68 
69  $this->resourceConnectionMock->expects($this->once())
70  ->method('getTableName')
71  ->with($originalTableName)
72  ->willReturn($originalTableName);
73 
74  $this->assertEquals(
75  $temporaryTableNames[$originalTableName],
76  $model->getWorkingTableName($originalTableName)
77  );
78  }
79 
84  {
85  $model = new IndexerTableSwapper($this->resourceConnectionMock);
86  $originalTableName = 'catalogrule_product';
87  $temporaryTableName = 'catalogrule_product__temp9604';
88  $this->setObjectProperty($model, 'temporaryTables', []);
89 
90  $this->resourceConnectionMock->expects($this->at(0))
91  ->method('getTableName')
92  ->with($originalTableName)
93  ->willReturn($originalTableName);
94  $this->resourceConnectionMock->expects($this->at(1))
95  ->method('getTableName')
96  ->with($this->stringStartsWith($originalTableName . '__temp'))
97  ->willReturn($temporaryTableName);
98 
99  $this->assertEquals(
100  $temporaryTableName,
101  $model->getWorkingTableName($originalTableName)
102  );
103  }
104 
114  private function setObjectProperty($object, string $propertyName, $value): void
115  {
116  $reflectionClass = new \ReflectionClass($object);
117  $reflectionProperty = $reflectionClass->getProperty($propertyName);
118  $reflectionProperty->setAccessible(true);
119  $reflectionProperty->setValue($object, $value);
120  }
121 
125  public function testSwapIndexTables(): void
126  {
127  $model = $this->getMockBuilder(IndexerTableSwapper::class)
128  ->setMethods(['getWorkingTableName'])
129  ->setConstructorArgs([$this->resourceConnectionMock])
130  ->getMock();
131  $originalTableName = 'catalogrule_product';
132  $temporaryOriginalTableName = 'catalogrule_product9604';
133  $temporaryTableName = 'catalogrule_product__temp9604';
134  $toRename = [
135  [
136  'oldName' => $originalTableName,
137  'newName' => $temporaryOriginalTableName,
138  ],
139  [
140  'oldName' => $temporaryTableName,
141  'newName' => $originalTableName,
142  ],
143  ];
144 
145  $this->resourceConnectionMock->expects($this->at(0))
146  ->method('getTableName')
147  ->with($originalTableName)
148  ->willReturn($originalTableName);
149  $this->resourceConnectionMock->expects($this->at(1))
150  ->method('getTableName')
151  ->with($this->stringStartsWith($originalTableName))
152  ->willReturn($temporaryOriginalTableName);
153  $model->expects($this->once())
154  ->method('getWorkingTableName')
155  ->with($originalTableName)
156  ->willReturn($temporaryTableName);
157  $this->adapterInterfaceMock->expects($this->once())
158  ->method('renameTablesBatch')
159  ->with($toRename)
160  ->willReturn(true);
161  $this->adapterInterfaceMock->expects($this->once())
162  ->method('dropTable')
163  ->with($temporaryOriginalTableName)
164  ->willReturn(true);
165 
166  $model->swapIndexTables([$originalTableName]);
167  }
168 }
$value
Definition: gender.phtml:16
$reflectionClass
Definition: categories.php:25