Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ClearExpiredCronJobObserverTest.php
Go to the documentation of this file.
1 <?php
9 
10 class ClearExpiredCronJobObserverTest extends \PHPUnit\Framework\TestCase
11 {
15  protected $model;
16 
21 
26 
30  protected $scheduleMock;
31 
36 
40  protected $sessionMock;
41 
42  protected function setUp()
43  {
44  $this->collectionFactoryMock =
45  $this->createPartialMock(\Magento\Store\Model\ResourceModel\Website\CollectionFactory::class, ['create']);
46  $this->sessionFactoryMock = $this->createPartialMock(
47  \Magento\Persistent\Model\SessionFactory::class,
48  ['create']
49  );
50  $this->scheduleMock = $this->createMock(\Magento\Cron\Model\Schedule::class);
51  $this->sessionMock = $this->createMock(\Magento\Persistent\Model\Session::class);
52  $this->websiteCollectionMock
53  = $this->createMock(\Magento\Store\Model\ResourceModel\Website\Collection::class);
54 
55  $this->model = new \Magento\Persistent\Observer\ClearExpiredCronJobObserver(
56  $this->collectionFactoryMock,
57  $this->sessionFactoryMock
58  );
59  }
60 
61  public function testExecute()
62  {
63  $this->collectionFactoryMock
64  ->expects($this->once())
65  ->method('create')
66  ->will($this->returnValue($this->websiteCollectionMock));
67  $this->websiteCollectionMock->expects($this->once())->method('getAllIds')->will($this->returnValue([1]));
68  $this->sessionFactoryMock
69  ->expects($this->once())
70  ->method('create')
71  ->will($this->returnValue($this->sessionMock));
72  $this->sessionMock->expects($this->once())->method('deleteExpired')->with(1);
73  $this->model->execute($this->scheduleMock);
74  }
75 
77  {
78  $this->collectionFactoryMock
79  ->expects($this->once())
80  ->method('create')
81  ->will($this->returnValue($this->websiteCollectionMock));
82  $this->websiteCollectionMock->expects($this->once())->method('getAllIds');
83  $this->sessionFactoryMock
84  ->expects($this->never())
85  ->method('create');
86  $this->model->execute($this->scheduleMock);
87  }
88 }