ChangelogTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Mview\View;
  7. use Magento\Framework\App\ResourceConnection;
  8. /**
  9. * Test Class for \Magento\Framework\Mview\View\Changelog
  10. */
  11. class ChangelogTest extends \PHPUnit\Framework\TestCase
  12. {
  13. /**
  14. * @var \Magento\Framework\ObjectManagerInterface
  15. */
  16. protected $objectManager;
  17. /**
  18. * @var \Magento\Framework\App\ResourceConnection
  19. */
  20. protected $resource;
  21. /**
  22. * Write connection adapter
  23. *
  24. * @var \Magento\Framework\DB\Adapter\AdapterInterface
  25. */
  26. protected $connection;
  27. /**
  28. * @var \Magento\Framework\Mview\View\Changelog
  29. */
  30. protected $model;
  31. /**
  32. * @return void
  33. */
  34. public function setUp()
  35. {
  36. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  37. $this->resource = $this->objectManager->get(\Magento\Framework\App\ResourceConnection::class);
  38. $this->connection = $this->resource->getConnection();
  39. $this->model = $this->objectManager->create(
  40. \Magento\Framework\Mview\View\Changelog::class,
  41. ['resource' => $this->resource]
  42. );
  43. $this->model->setViewId('test_view_id_1');
  44. $this->model->create();
  45. }
  46. /**
  47. * @return void
  48. */
  49. public function tearDown()
  50. {
  51. $this->model->drop();
  52. }
  53. /**
  54. * Test for create() and drop() methods
  55. *
  56. * @return void
  57. */
  58. public function testCreateAndDrop()
  59. {
  60. /** @var \Magento\Framework\Mview\View\Changelog $model */
  61. $model = $this->objectManager->create(
  62. \Magento\Framework\Mview\View\Changelog::class,
  63. ['resource' => $this->resource]
  64. );
  65. $model->setViewId('test_view_id_2');
  66. $changelogName = $this->resource->getTableName($model->getName());
  67. $this->assertFalse($this->connection->isTableExists($changelogName));
  68. $model->create();
  69. $this->assertTrue($this->connection->isTableExists($changelogName));
  70. $model->drop();
  71. $this->assertFalse($this->connection->isTableExists($changelogName));
  72. }
  73. /**
  74. * Test for getVersion() method
  75. *
  76. * @return void
  77. */
  78. public function testGetVersion()
  79. {
  80. $model = $this->objectManager->create(
  81. \Magento\Framework\Mview\View\Changelog::class,
  82. ['resource' => $this->resource]
  83. );
  84. $model->setViewId('test_view_id_2');
  85. $model->create();
  86. $this->assertEquals(0, $model->getVersion());
  87. $changelogName = $this->resource->getTableName($model->getName());
  88. $this->connection->insert($changelogName, [$model->getColumnName() => mt_rand(1, 200)]);
  89. $this->assertEquals($this->connection->lastInsertId($changelogName, 'version_id'), $model->getVersion());
  90. $model->drop();
  91. }
  92. /**
  93. * Test for clear() method
  94. *
  95. * @return void
  96. */
  97. public function testClear()
  98. {
  99. $this->assertEquals(0, $this->model->getVersion());
  100. //the same that a table is empty
  101. $changelogName = $this->resource->getTableName($this->model->getName());
  102. $this->connection->insert($changelogName, ['version_id' => 1, 'entity_id' => 1]);
  103. $this->assertEquals(1, $this->model->getVersion());
  104. $this->model->clear(1);
  105. $this->assertEquals(1, $this->model->getVersion()); //the same that a table is empty
  106. }
  107. /**
  108. * Test for getList() method
  109. *
  110. * @return void
  111. */
  112. public function testGetList()
  113. {
  114. $this->assertEquals(0, $this->model->getVersion());
  115. //the same that a table is empty
  116. $changelogName = $this->resource->getTableName($this->model->getName());
  117. $testChangelogData = [
  118. ['version_id' => 1, 'entity_id' => 1],
  119. ['version_id' => 2, 'entity_id' => 1],
  120. ['version_id' => 3, 'entity_id' => 2],
  121. ['version_id' => 4, 'entity_id' => 3],
  122. ['version_id' => 5, 'entity_id' => 1],
  123. ];
  124. foreach ($testChangelogData as $data) {
  125. $this->connection->insert($changelogName, $data);
  126. }
  127. $this->assertEquals(5, $this->model->getVersion());
  128. $this->assertEquals(3, count($this->model->getList(0, 5)));//distinct entity_ids
  129. $this->assertEquals(3, count($this->model->getList(2, 5)));//distinct entity_ids
  130. $this->assertEquals(2, count($this->model->getList(0, 3)));//distinct entity_ids
  131. $this->assertEquals(1, count($this->model->getList(0, 2)));//distinct entity_ids
  132. $this->assertEquals(1, count($this->model->getList(2, 3)));//distinct entity_ids
  133. $this->assertEquals(0, count($this->model->getList(4, 3)));//because fromVersionId > toVersionId
  134. }
  135. }