IndexTableSwitcherTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\InventoryMultiDimensionalIndexerApi\Test\Unit;
  8. use Magento\Framework\App\ResourceConnection;
  9. use Magento\Framework\DB\Adapter\AdapterInterface;
  10. use Magento\InventoryMultiDimensionalIndexerApi\Model\IndexName;
  11. use Magento\InventoryMultiDimensionalIndexerApi\Model\IndexNameResolverInterface;
  12. use Magento\InventoryMultiDimensionalIndexerApi\Model\IndexTableSwitcher;
  13. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  14. use PHPUnit\Framework\TestCase;
  15. /**
  16. * Test for @see IndexTableSwitcher.
  17. */
  18. class IndexTableSwitcherTest extends TestCase
  19. {
  20. /**
  21. * @var IndexTableSwitcher|\PHPUnit_Framework_MockObject_MockObject
  22. */
  23. private $indexTableSwitcher;
  24. /**
  25. * @var IndexName|\PHPUnit_Framework_MockObject_MockObject
  26. */
  27. private $indexName;
  28. /**
  29. * @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  30. */
  31. private $resourceConnection;
  32. /**
  33. * @var IndexNameResolverInterface|\PHPUnit_Framework_MockObject_MockObject
  34. */
  35. private $indexNameResolver;
  36. /**
  37. * @var AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  38. */
  39. private $adapter;
  40. /**
  41. * @inheritdoc
  42. */
  43. protected function setUp()
  44. {
  45. parent::setUp();
  46. $objectManager = new ObjectManager($this);
  47. $this->indexName = $this->createMock(IndexName::class);
  48. $this->resourceConnection = $this->createMock(ResourceConnection::class);
  49. $this->indexNameResolver = $this->createMock(IndexNameResolverInterface::class);
  50. $this->adapter = $this->createMock(AdapterInterface::class);
  51. $this->indexTableSwitcher = $objectManager->getObject(
  52. IndexTableSwitcher::class,
  53. [
  54. 'resourceConnection' => $this->resourceConnection,
  55. 'indexNameResolver' => $this->indexNameResolver,
  56. ]
  57. );
  58. }
  59. public function testSwitch()
  60. {
  61. $connectionName = 'testConnection';
  62. $tableName = 'some_table_name';
  63. $toRename =
  64. [
  65. [
  66. 'oldName' => $tableName,
  67. 'newName' => $tableName . '_outdated',
  68. ],
  69. [
  70. 'oldName' => $tableName . '_replica',
  71. 'newName' => $tableName,
  72. ],
  73. [
  74. 'oldName' => $tableName . '_outdated',
  75. 'newName' => $tableName . '_replica',
  76. ],
  77. ];
  78. $this->resourceConnection->expects($this->once())->method('getConnection')
  79. ->with($connectionName)->willReturn($this->adapter);
  80. $this->indexNameResolver->expects($this->once())->method('resolveName')
  81. ->with($this->indexName)->willReturn($tableName);
  82. $this->adapter->expects($this->once())->method('renameTablesBatch')
  83. ->with($toRename);
  84. $this->indexTableSwitcher->switch($this->indexName, $connectionName);
  85. }
  86. }