ClearExpiredCronJobObserverTest.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. *
  4. * Copyright © Magento, Inc. All rights reserved.
  5. * See COPYING.txt for license details.
  6. */
  7. namespace Magento\Persistent\Test\Unit\Observer;
  8. class ClearExpiredCronJobObserverTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Persistent\Observer\ClearExpiredCronJobObserver
  12. */
  13. protected $model;
  14. /**
  15. * @var \PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $collectionFactoryMock;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $sessionFactoryMock;
  22. /**
  23. * @var \PHPUnit_Framework_MockObject_MockObject
  24. */
  25. protected $scheduleMock;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $websiteCollectionMock;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. protected $sessionMock;
  34. protected function setUp()
  35. {
  36. $this->collectionFactoryMock =
  37. $this->createPartialMock(\Magento\Store\Model\ResourceModel\Website\CollectionFactory::class, ['create']);
  38. $this->sessionFactoryMock = $this->createPartialMock(
  39. \Magento\Persistent\Model\SessionFactory::class,
  40. ['create']
  41. );
  42. $this->scheduleMock = $this->createMock(\Magento\Cron\Model\Schedule::class);
  43. $this->sessionMock = $this->createMock(\Magento\Persistent\Model\Session::class);
  44. $this->websiteCollectionMock
  45. = $this->createMock(\Magento\Store\Model\ResourceModel\Website\Collection::class);
  46. $this->model = new \Magento\Persistent\Observer\ClearExpiredCronJobObserver(
  47. $this->collectionFactoryMock,
  48. $this->sessionFactoryMock
  49. );
  50. }
  51. public function testExecute()
  52. {
  53. $this->collectionFactoryMock
  54. ->expects($this->once())
  55. ->method('create')
  56. ->will($this->returnValue($this->websiteCollectionMock));
  57. $this->websiteCollectionMock->expects($this->once())->method('getAllIds')->will($this->returnValue([1]));
  58. $this->sessionFactoryMock
  59. ->expects($this->once())
  60. ->method('create')
  61. ->will($this->returnValue($this->sessionMock));
  62. $this->sessionMock->expects($this->once())->method('deleteExpired')->with(1);
  63. $this->model->execute($this->scheduleMock);
  64. }
  65. public function testExecuteForNotExistingWebsite()
  66. {
  67. $this->collectionFactoryMock
  68. ->expects($this->once())
  69. ->method('create')
  70. ->will($this->returnValue($this->websiteCollectionMock));
  71. $this->websiteCollectionMock->expects($this->once())->method('getAllIds');
  72. $this->sessionFactoryMock
  73. ->expects($this->never())
  74. ->method('create');
  75. $this->model->execute($this->scheduleMock);
  76. }
  77. }