@@ -53,6 +53,9 @@ class MagentoWebDriver extends WebDriver
53
53
{
54
54
use AttachmentSupport;
55
55
56
+ const MAGENTO_CRON_INTERVAL = 60 ;
57
+ const MAGENTO_CRON_COMMAND = 'cron:run ' ;
58
+
56
59
/**
57
60
* List of known magento loading masks by selector
58
61
*
@@ -121,6 +124,13 @@ class MagentoWebDriver extends WebDriver
121
124
*/
122
125
private $ jsErrors = [];
123
126
127
+ /**
128
+ * Contains last execution times for Cron
129
+ *
130
+ * @var int[]
131
+ */
132
+ private $ cronExecution = [];
133
+
124
134
/**
125
135
* Sanitizes config, then initializes using parent.
126
136
*
@@ -552,6 +562,75 @@ public function magentoCLI($command, $timeout = null, $arguments = null)
552
562
return $ response ;
553
563
}
554
564
565
+ /**
566
+ * Executes Magento Cron keeping the interval (> 60 seconds between each run)
567
+ *
568
+ * @param string|null $cronGroups
569
+ * @param integer|null $timeout
570
+ * @param string|null $arguments
571
+ * @return string
572
+ */
573
+ public function magentoCron ($ cronGroups = null , $ timeout = null , $ arguments = null )
574
+ {
575
+ $ cronGroups = explode (' ' , $ cronGroups );
576
+ return $ this ->executeCronjobs ($ cronGroups , $ timeout , $ arguments );
577
+ }
578
+
579
+ /**
580
+ * Updates last execution time for Cron
581
+ *
582
+ * @param array $cronGroups
583
+ * @return void
584
+ */
585
+ private function notifyCronFinished (array $ cronGroups = [])
586
+ {
587
+ if (empty ($ cronGroups )) {
588
+ $ this ->cronExecution ['* ' ] = time ();
589
+ }
590
+
591
+ foreach ($ cronGroups as $ group ) {
592
+ $ this ->cronExecution [$ group ] = time ();
593
+ }
594
+ }
595
+
596
+ /**
597
+ * Returns last Cron execution time for specific cron or all crons
598
+ *
599
+ * @param array $cronGroups
600
+ * @return integer
601
+ */
602
+ private function getLastCronExecution (array $ cronGroups = [])
603
+ {
604
+ if (empty ($ cronGroups )) {
605
+ return (int )max ($ this ->cronExecution );
606
+ }
607
+
608
+ $ cronGroups = array_merge ($ cronGroups , ['* ' ]);
609
+
610
+ return array_reduce ($ cronGroups , function ($ lastExecution , $ group ) {
611
+ if (isset ($ this ->cronExecution [$ group ]) && $ this ->cronExecution [$ group ] > $ lastExecution ) {
612
+ $ lastExecution = $ this ->cronExecution [$ group ];
613
+ }
614
+
615
+ return (int )$ lastExecution ;
616
+ }, 0 );
617
+ }
618
+
619
+ /**
620
+ * Returns time to wait for next run
621
+ *
622
+ * @param array $cronGroups
623
+ * @param integer $cronInterval
624
+ * @return integer
625
+ */
626
+ private function getCronWait (array $ cronGroups = [], int $ cronInterval = self ::MAGENTO_CRON_INTERVAL )
627
+ {
628
+ $ nextRun = $ this ->getLastCronExecution ($ cronGroups ) + $ cronInterval ;
629
+ $ toNextRun = $ nextRun - time ();
630
+
631
+ return max (0 , $ toNextRun );
632
+ }
633
+
555
634
/**
556
635
* Runs DELETE request to delete a Magento entity against the url given.
557
636
*
@@ -971,4 +1050,36 @@ public function getSecret($key)
971
1050
{
972
1051
return CredentialStore::getInstance ()->getSecret ($ key );
973
1052
}
1053
+
1054
+ /**
1055
+ * Waits proper amount of time to perform Cron execution
1056
+ *
1057
+ * @param string $cronGroups
1058
+ * @param integer $timeout
1059
+ * @param string $arguments
1060
+ * @return string
1061
+ * @throws TestFrameworkException
1062
+ */
1063
+ private function executeCronjobs ($ cronGroups , $ timeout , $ arguments ): string
1064
+ {
1065
+ $ cronGroups = array_filter ($ cronGroups );
1066
+
1067
+ $ waitFor = $ this ->getCronWait ($ cronGroups );
1068
+
1069
+ if ($ waitFor ) {
1070
+ $ this ->wait ($ waitFor );
1071
+ }
1072
+
1073
+ $ command = array_reduce ($ cronGroups , function ($ command , $ cronGroup ) {
1074
+ $ command .= ' --group= ' . $ cronGroup ;
1075
+ return $ command ;
1076
+ }, self ::MAGENTO_CRON_COMMAND );
1077
+ $ timeStart = microtime (true );
1078
+ $ cronResult = $ this ->magentoCLI ($ command , $ timeout , $ arguments );
1079
+ $ timeEnd = microtime (true );
1080
+
1081
+ $ this ->notifyCronFinished ($ cronGroups );
1082
+
1083
+ return sprintf ('%s (wait: %ss, execution: %ss) ' , $ cronResult , $ waitFor , round ($ timeEnd - $ timeStart , 2 ));
1084
+ }
974
1085
}
0 commit comments