BulkCleanupTest.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\AsynchronousOperations\Test\Unit\Controller\Cron;
  7. class BulkCleanupTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \PHPUnit_Framework_MockObject_MockObject
  11. */
  12. private $metadataPoolMock;
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject
  15. */
  16. private $resourceConnectionMock;
  17. /**
  18. * @var \PHPUnit_Framework_MockObject_MockObject
  19. */
  20. private $dateTimeMock;
  21. /**
  22. * @var \PHPUnit_Framework_MockObject_MockObject
  23. */
  24. private $scopeConfigMock;
  25. /**
  26. * @var \Magento\AsynchronousOperations\Cron\BulkCleanup
  27. */
  28. private $model;
  29. /**
  30. * @var \PHPUnit_Framework_MockObject_MockObject
  31. */
  32. private $timeMock;
  33. protected function setUp()
  34. {
  35. $this->dateTimeMock = $this->createMock(\Magento\Framework\Stdlib\DateTime::class);
  36. $this->scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
  37. $this->resourceConnectionMock = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
  38. $this->metadataPoolMock = $this->createMock(\Magento\Framework\EntityManager\MetadataPool::class);
  39. $this->timeMock = $this->createMock(\Magento\Framework\Stdlib\DateTime\DateTime::class);
  40. $this->model = new \Magento\AsynchronousOperations\Cron\BulkCleanup(
  41. $this->metadataPoolMock,
  42. $this->resourceConnectionMock,
  43. $this->dateTimeMock,
  44. $this->scopeConfigMock,
  45. $this->timeMock
  46. );
  47. }
  48. public function testExecute()
  49. {
  50. $entityType = 'BulkSummaryInterface';
  51. $connectionName = 'Connection';
  52. $bulkLifetimeMultiplier = 10;
  53. $bulkLifetime = 3600 * 24 * $bulkLifetimeMultiplier;
  54. $adapterMock = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
  55. $entityMetadataMock = $this->createMock(\Magento\Framework\EntityManager\EntityMetadataInterface::class);
  56. $this->metadataPoolMock->expects($this->once())->method('getMetadata')->with($this->stringContains($entityType))
  57. ->willReturn($entityMetadataMock);
  58. $entityMetadataMock->expects($this->once())->method('getEntityConnectionName')->willReturn($connectionName);
  59. $this->resourceConnectionMock->expects($this->once())->method('getConnectionByName')->with($connectionName)
  60. ->willReturn($adapterMock);
  61. $this->scopeConfigMock->expects($this->once())->method('getValue')->with($this->stringContains('bulk/lifetime'))
  62. ->willReturn($bulkLifetimeMultiplier);
  63. $this->timeMock->expects($this->once())->method('gmtTimestamp')->willReturn($bulkLifetime*10);
  64. $this->dateTimeMock->expects($this->once())->method('formatDate')->with($bulkLifetime*9);
  65. $adapterMock->expects($this->once())->method('delete');
  66. $this->model->execute();
  67. }
  68. }