4
4
* See COPYING.txt for license details.
5
5
*/
6
6
namespace Magento \Cron \Test \Unit \Model ;
7
-
7
+ use Magento \ Cron \ Model \ Schedule ;
8
8
/**
9
9
* Class \Magento\Cron\Test\Unit\Model\ObserverTest
10
10
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -576,6 +576,9 @@ public function testDispatchCleanup()
576
576
$ this ->_cache ->expects ($ this ->at (0 ))->method ('load ' )->will ($ this ->returnValue (time () + 10000000 ));
577
577
$ this ->_cache ->expects ($ this ->at (1 ))->method ('load ' )->will ($ this ->returnValue (time () - 10000000 ));
578
578
579
+ //XML_PATH_HISTORY_CLEANUP_EVERY
580
+ $ this ->_scopeConfig ->expects ($ this ->any ())->method ('getValue ' )->will ($ this ->returnValue (0 ));
581
+ //XML_PATH_SCHEDULE_LIFETIME
579
582
$ this ->_scopeConfig ->expects ($ this ->any ())->method ('getValue ' )->will ($ this ->returnValue (0 ));
580
583
581
584
$ scheduleMock = $ this ->getMockBuilder ('Magento\Cron\Model\Schedule ' )->disableOriginalConstructor ()->getMock ();
@@ -600,28 +603,67 @@ public function testDispatchCleanup()
600
603
601
604
public function testMissedJobsCleanedInTime ()
602
605
{
606
+ /* 1. Initialize dependencies of _generate() method which is called first */
603
607
$ jobConfig = [
604
608
'test_group ' => ['test_job1 ' => ['instance ' => 'CronJob ' , 'method ' => 'execute ' ]],
605
609
];
606
610
607
- $ schedule = $ this ->getMockBuilder (
611
+ // This item was scheduled 2 days ago
612
+ $ schedule1 = $ this ->getMockBuilder (
608
613
'Magento\Cron\Model\Schedule '
609
614
)->disableOriginalConstructor ()->setMethods (
610
615
['getExecutedAt ' , 'getScheduledAt ' , 'getStatus ' , 'delete ' , '__wakeup ' ]
611
616
)->getMock ();
612
- $ schedule ->expects ($ this ->any ())->method ('getExecutedAt ' )->will ($ this ->returnValue (null ));
613
- $ schedule ->expects ($ this ->any ())->method ('getScheduledAt ' )->will ($ this ->returnValue ('-1 day ' ));
614
- $ schedule ->expects ($ this ->any ())->method ('getStatus ' )->will ($ this ->returnValue (Schedule::STATUS_MISSED ));
615
-
616
- $ this ->_collection ->addItem ($ schedule );
617
-
617
+ $ schedule1 ->expects ($ this ->any ())->method ('getExecutedAt ' )->will ($ this ->returnValue (null ));
618
+ $ schedule1 ->expects ($ this ->any ())->method ('getScheduledAt ' )->will ($ this ->returnValue ('-2 day -1 hour ' ));
619
+ $ schedule1 ->expects ($ this ->any ())->method ('getStatus ' )->will (
620
+ $ this ->returnValue (Schedule::STATUS_MISSED ));
621
+ //we expect this job be deleted from the list
622
+ $ schedule1 ->expects ($ this ->once ())->method ('delete ' )->will (
623
+ $ this ->returnValue (true ));
624
+
625
+ // This item was scheduled 1 day ago
626
+ $ schedule2 = $ this ->getMockBuilder (
627
+ 'Magento\Cron\Model\Schedule '
628
+ )->disableOriginalConstructor ()->setMethods (
629
+ ['getExecutedAt ' , 'getScheduledAt ' , 'getStatus ' , 'delete ' , '__wakeup ' ]
630
+ )->getMock ();
631
+ $ schedule2 ->expects ($ this ->any ())->method ('getExecutedAt ' )->will ($ this ->returnValue (null ));
632
+ $ schedule2 ->expects ($ this ->any ())->method ('getScheduledAt ' )->will ($ this ->returnValue ('-1 day ' ));
633
+ $ schedule2 ->expects ($ this ->any ())->method ('getStatus ' )->will (
634
+ $ this ->returnValue (Schedule::STATUS_MISSED ));
635
+ //we don't expect this job be deleted from the list
636
+ $ schedule2 ->expects ($ this ->never ())->method ('delete ' )->will (
637
+ $ this ->returnValue (true ));
638
+
639
+ $ this ->_collection ->addItem ($ schedule1 );
618
640
$ this ->_config ->expects ($ this ->once ())->method ('getJobs ' )->will ($ this ->returnValue ($ jobConfig ));
619
641
642
+ //get configuration value CACHE_KEY_LAST_HISTORY_CLEANUP_AT in the "_generate()"
620
643
$ this ->_cache ->expects ($ this ->at (0 ))->method ('load ' )->will ($ this ->returnValue (time () + 10000000 ));
644
+ //get configuration value CACHE_KEY_LAST_HISTORY_CLEANUP_AT in the "_cleanup()"
621
645
$ this ->_cache ->expects ($ this ->at (1 ))->method ('load ' )->will ($ this ->returnValue (time () - 10000000 ));
622
646
623
- $ this ->_scopeConfig ->expects ($ this ->any ())->method ('getValue ' )->will ($ this ->returnValue (0 ));
624
-
647
+ $ this ->_scopeConfig ->expects ($ this ->at (0 ))->method ('getValue ' )
648
+ ->with ($ this ->equalTo ('system/cron/test_group/use_separate_process ' ))
649
+ ->will ($ this ->returnValue (0 ));
650
+ $ this ->_scopeConfig ->expects ($ this ->at (1 ))->method ('getValue ' )
651
+ ->with ($ this ->equalTo ('system/cron/test_group/schedule_generate_every ' ))
652
+ ->will ($ this ->returnValue (0 ));
653
+ $ this ->_scopeConfig ->expects ($ this ->at (2 ))->method ('getValue ' )
654
+ ->with ($ this ->equalTo ('system/cron/test_group/history_cleanup_every ' ))
655
+ ->will ($ this ->returnValue (0 ));
656
+ $ this ->_scopeConfig ->expects ($ this ->at (3 ))->method ('getValue ' )
657
+ ->with ($ this ->equalTo ('system/cron/test_group/schedule_lifetime ' ))
658
+ ->will ($ this ->returnValue (2 *24 *60 ));
659
+ $ this ->_scopeConfig ->expects ($ this ->at (4 ))->method ('getValue ' )
660
+ ->with ($ this ->equalTo ('system/cron/test_group/history_success_lifetime ' ))
661
+ ->will ($ this ->returnValue (0 ));
662
+ $ this ->_scopeConfig ->expects ($ this ->at (5 ))->method ('getValue ' )
663
+ ->with ($ this ->equalTo ('system/cron/test_group/history_failure_lifetime ' ))
664
+ ->will ($ this ->returnValue (0 ));
665
+
666
+ /* 2. Initialize dependencies of _cleanup() method which is called second */
625
667
$ scheduleMock = $ this ->getMockBuilder ('Magento\Cron\Model\Schedule ' )->disableOriginalConstructor ()->getMock ();
626
668
$ scheduleMock ->expects ($ this ->any ())->method ('getCollection ' )->will ($ this ->returnValue ($ this ->_collection ));
627
669
$ this ->_scheduleFactory ->expects ($ this ->at (0 ))->method ('create ' )->will ($ this ->returnValue ($ scheduleMock ));
@@ -633,7 +675,8 @@ public function testMissedJobsCleanedInTime()
633
675
)->disableOriginalConstructor ()->getMock ();
634
676
$ collection ->expects ($ this ->any ())->method ('addFieldToFilter ' )->will ($ this ->returnSelf ());
635
677
$ collection ->expects ($ this ->any ())->method ('load ' )->will ($ this ->returnSelf ());
636
- $ collection ->addItem ($ schedule );
678
+ $ collection ->addItem ($ schedule1 );
679
+ $ collection ->addItem ($ schedule2 );
637
680
638
681
$ scheduleMock = $ this ->getMockBuilder ('Magento\Cron\Model\Schedule ' )->disableOriginalConstructor ()->getMock ();
639
682
$ scheduleMock ->expects ($ this ->any ())->method ('getCollection ' )->will ($ this ->returnValue ($ collection ));
0 commit comments