FrontendActionsFlushTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Catalog\Test\Unit\Cron;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
  8. class FrontendActionsFlushTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /** @var \Magento\Catalog\Cron\FrontendActionsFlush */
  11. protected $model;
  12. /** @var ObjectManagerHelper */
  13. protected $objectManagerHelper;
  14. /** @var \Magento\Catalog\Model\ResourceModel\ProductFrontendAction|\PHPUnit_Framework_MockObject_MockObject */
  15. protected $productFrontendActionMock;
  16. /** @var \Magento\Catalog\Model\FrontendStorageConfigurationPool|\PHPUnit_Framework_MockObject_MockObject */
  17. protected $frontendStorageConfigurationPoolMock;
  18. protected function setUp()
  19. {
  20. $this->productFrontendActionMock = $this->getMockBuilder(
  21. \Magento\Catalog\Model\ResourceModel\ProductFrontendAction::class
  22. )
  23. ->disableOriginalConstructor()
  24. ->getMock();
  25. $this->frontendStorageConfigurationPoolMock = $this->getMockBuilder(
  26. \Magento\Catalog\Model\FrontendStorageConfigurationPool::class
  27. )
  28. ->disableOriginalConstructor()
  29. ->getMock();
  30. $this->objectManagerHelper = new ObjectManagerHelper($this);
  31. $this->model = $this->objectManagerHelper->getObject(
  32. \Magento\Catalog\Cron\FrontendActionsFlush::class,
  33. [
  34. 'productFrontendActionResource' => $this->productFrontendActionMock,
  35. 'frontendStorageConfigurationPool' => $this->frontendStorageConfigurationPoolMock
  36. ]
  37. );
  38. }
  39. public function testExecute()
  40. {
  41. $connectionMock = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
  42. $selectMock = $this->createMock(\Magento\Framework\DB\Select::class);
  43. $frontendConfiguration = $this->createMock(\Magento\Catalog\Model\FrontendStorageConfigurationInterface::class);
  44. $selectMock
  45. ->expects($this->once())
  46. ->method('from')
  47. ->with('catalog_product_frontend_action', ['action_id', 'type_id'])
  48. ->will($this->returnSelf());
  49. $selectMock
  50. ->expects($this->once())
  51. ->method('group')
  52. ->with('type_id')
  53. ->willReturnSelf();
  54. $frontendConfiguration->expects($this->once())
  55. ->method('get')
  56. ->willReturn([
  57. 'lifetime' => 1500
  58. ]);
  59. $this->frontendStorageConfigurationPoolMock->expects($this->once())
  60. ->method('get')
  61. ->with('recently_viewed_product')
  62. ->willReturn($frontendConfiguration);
  63. $this->productFrontendActionMock->expects($this->exactly(2))
  64. ->method('getMainTable')
  65. ->willReturn('catalog_product_frontend_action');
  66. $this->productFrontendActionMock->expects($this->exactly(2))
  67. ->method('getConnection')
  68. ->willReturn($connectionMock);
  69. $connectionMock->expects($this->once())
  70. ->method('select')
  71. ->willReturn($selectMock);
  72. $connectionMock->expects($this->once())
  73. ->method('fetchPairs')
  74. ->with($selectMock)
  75. ->willReturn([
  76. 'recently_viewed_product'
  77. ]);
  78. $connectionMock->expects($this->once())
  79. ->method('quoteInto')
  80. ->with('added_at < ?', time() - 1500)
  81. ->willReturn(['added_at < ?', time() - 1500]);
  82. $connectionMock->expects($this->once())
  83. ->method('delete')
  84. ->with('catalog_product_frontend_action', [['added_at < ?', time() - 1500]]);
  85. $this->model->execute();
  86. }
  87. }