GridTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Customer\Test\Unit\Model\ResourceModel\Customer;
  7. use Magento\Customer\Model\ResourceModel\Customer\Grid;
  8. class GridTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /** @var \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject */
  11. protected $resource;
  12. /** @var \Magento\Framework\Indexer\IndexerRegistry|\PHPUnit_Framework_MockObject_MockObject */
  13. protected $indexerRegistry;
  14. /** @var \Magento\Framework\Indexer\ScopeResolver\FlatScopeResolver|\PHPUnit_Framework_MockObject_MockObject */
  15. protected $flatScopeResolver;
  16. /** @var \Magento\Framework\Indexer\IndexerInterface|\PHPUnit_Framework_MockObject_MockObject */
  17. protected $indexer;
  18. /** @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject */
  19. protected $connection;
  20. /** @var \Magento\Framework\DB\Select|\PHPUnit_Framework_MockObject_MockObject */
  21. protected $select;
  22. /** @var Grid */
  23. protected $observer;
  24. /** @var \Zend_Db_Statement_Interface|\PHPUnit_Framework_MockObject_MockObject */
  25. protected $queryResult;
  26. protected function setUp()
  27. {
  28. $this->resource = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
  29. $this->indexerRegistry = $this->createMock(\Magento\Framework\Indexer\IndexerRegistry::class);
  30. $this->flatScopeResolver = $this->createMock(\Magento\Framework\Indexer\ScopeResolver\FlatScopeResolver::class);
  31. $this->indexer = $this->getMockForAbstractClass(
  32. \Magento\Framework\Indexer\IndexerInterface::class,
  33. [],
  34. '',
  35. false
  36. );
  37. $this->connection = $this->getMockForAbstractClass(
  38. \Magento\Framework\DB\Adapter\AdapterInterface::class,
  39. [],
  40. '',
  41. false
  42. );
  43. $this->select = $this->createMock(\Magento\Framework\DB\Select::class);
  44. $this->queryResult = $this->getMockForAbstractClass(
  45. \Zend_Db_Statement_Interface::class,
  46. [],
  47. '',
  48. false
  49. );
  50. $this->observer = new Grid(
  51. $this->resource,
  52. $this->indexerRegistry,
  53. $this->flatScopeResolver
  54. );
  55. }
  56. public function testSyncCustomerGrid()
  57. {
  58. $gridTable = 'customer_grid_flat';
  59. $customerLogTable = 'customer_log';
  60. $this->indexerRegistry->expects($this->once())
  61. ->method('get')
  62. ->with(\Magento\Customer\Model\Customer::CUSTOMER_GRID_INDEXER_ID)
  63. ->willReturn($this->indexer);
  64. $this->resource
  65. ->expects($this->once())
  66. ->method('getConnection')
  67. ->willReturn($this->connection);
  68. $this->flatScopeResolver
  69. ->expects($this->once())
  70. ->method('resolve')
  71. ->with(\Magento\Customer\Model\Customer::CUSTOMER_GRID_INDEXER_ID, [])
  72. ->willReturn($gridTable);
  73. $this->resource->expects($this->exactly(2))
  74. ->method('getTableName')
  75. ->willReturnMap([
  76. [$gridTable],
  77. [$customerLogTable],
  78. ]);
  79. $this->connection->expects($this->exactly(2))
  80. ->method('select')
  81. ->willReturn($this->select);
  82. $this->select->expects($this->exactly(2))
  83. ->method('from')
  84. ->willReturnSelf();
  85. $this->select->expects($this->once())
  86. ->method('order')
  87. ->with('last_visit_at DESC')
  88. ->willReturnSelf();
  89. $this->select->expects($this->once())
  90. ->method('limit')
  91. ->with(1)
  92. ->willReturnSelf();
  93. $this->connection->expects($this->atLeastOnce())
  94. ->method('query')
  95. ->with($this->select)
  96. ->willReturn($this->queryResult);
  97. $this->queryResult->expects($this->once())
  98. ->method('fetchColumn')
  99. ->willReturn('2015-08-13 10:36:44');
  100. $this->select->expects($this->once())
  101. ->method('where')
  102. ->with('last_login_at > ?', '2015-08-13 10:36:44')
  103. ->willReturnSelf();
  104. $this->queryResult->expects($this->once())
  105. ->method('fetchAll')
  106. ->willReturn([['customer_id' => 23], ['customer_id' => 65]]);
  107. $this->indexer->expects($this->once())
  108. ->method('reindexList')
  109. ->with(['23', '65']);
  110. $this->observer->syncCustomerGrid();
  111. }
  112. }