9
9
10
10
use Magento \Cron \Model \ResourceModel \Schedule as SchoduleResourceModel ;
11
11
use Magento \Cron \Model \Schedule ;
12
+ use Magento \Cron \Model \DeadlockRetrierInterface ;
12
13
use Magento \Framework \Exception \CronException ;
13
14
use Magento \Framework \Intl \DateTimeFactory ;
14
15
use Magento \Framework \Stdlib \DateTime \TimezoneInterface ;
15
- use Magento \Framework \TestFramework \ Unit \ Helper \ ObjectManager ;
16
+ use Magento \Framework \DB \ Adapter \ AdapterInterface ;
16
17
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager as ObjectManagerHelper ;
17
18
use PHPUnit \Framework \MockObject \MockObject ;
18
19
use PHPUnit \Framework \TestCase ;
@@ -42,16 +43,30 @@ class ScheduleTest extends TestCase
42
43
*/
43
44
private $ dateTimeFactoryMock ;
44
45
46
+ /**
47
+ * @var DeadlockRetrierInterface|MockObject
48
+ */
49
+ private $ retrierMock ;
50
+
45
51
/**
46
52
* @inheritdoc
47
53
*/
48
54
protected function setUp (): void
49
55
{
50
- $ this ->objectManagerHelper = new ObjectManager ($ this );
56
+ $ this ->objectManagerHelper = new ObjectManagerHelper ($ this );
51
57
52
58
$ this ->resourceJobMock = $ this ->getMockBuilder (SchoduleResourceModel::class)
53
59
->disableOriginalConstructor ()
54
- ->setMethods (['trySetJobUniqueStatusAtomic ' , '__wakeup ' , 'getIdFieldName ' ])
60
+ ->setMethods (
61
+ [
62
+ 'trySetJobStatusAtomic ' ,
63
+ '__wakeup ' ,
64
+ 'getIdFieldName ' ,
65
+ 'trySetJobStatuses ' ,
66
+ 'getConnection ' ,
67
+ 'getTable '
68
+ ]
69
+ )
55
70
->getMockForAbstractClass ();
56
71
57
72
$ this ->resourceJobMock ->expects ($ this ->any ())
@@ -65,6 +80,8 @@ protected function setUp(): void
65
80
$ this ->dateTimeFactoryMock = $ this ->getMockBuilder (DateTimeFactory::class)
66
81
->setMethods (['create ' ])
67
82
->getMock ();
83
+
84
+ $ this ->retrierMock = $ this ->createMock (DeadlockRetrierInterface::class);
68
85
}
69
86
70
87
/**
@@ -457,20 +474,49 @@ public function getNumericDataProvider(): array
457
474
public function testTryLockJobSuccess (): void
458
475
{
459
476
$ scheduleId = 1 ;
477
+ $ jobCode = 'test_job ' ;
478
+ $ tableName = 'cron_schedule ' ;
479
+
480
+ $ connectionMock = $ this ->createMock (AdapterInterface::class);
481
+ $ connectionMock ->expects ($ this ->once ())
482
+ ->method ('update ' )
483
+ ->with (
484
+ $ tableName ,
485
+ ['status ' => Schedule::STATUS_ERROR ],
486
+ ['job_code = ? ' => $ jobCode , 'status = ? ' => Schedule::STATUS_RUNNING ]
487
+ )
488
+ ->willReturn (1 );
460
489
461
490
$ this ->resourceJobMock ->expects ($ this ->once ())
462
- ->method ('trySetJobUniqueStatusAtomic ' )
491
+ ->method ('trySetJobStatusAtomic ' )
463
492
->with ($ scheduleId , Schedule::STATUS_RUNNING , Schedule::STATUS_PENDING )
464
493
->willReturn (true );
494
+ $ this ->resourceJobMock ->expects ($ this ->once ())
495
+ ->method ('getTable ' )
496
+ ->with ($ tableName )
497
+ ->willReturn ($ tableName );
498
+ $ this ->resourceJobMock ->expects ($ this ->exactly (3 ))
499
+ ->method ('getConnection ' )
500
+ ->willReturn ($ connectionMock );
501
+
502
+ $ this ->retrierMock ->expects ($ this ->exactly (2 ))
503
+ ->method ('execute ' )
504
+ ->willReturnCallback (
505
+ function ($ callback ) {
506
+ return $ callback ();
507
+ }
508
+ );
465
509
466
510
/** @var Schedule $model */
467
511
$ model = $ this ->objectManagerHelper ->getObject (
468
512
Schedule::class,
469
513
[
470
- 'resource ' => $ this ->resourceJobMock
514
+ 'resource ' => $ this ->resourceJobMock ,
515
+ 'retrier ' => $ this ->retrierMock ,
471
516
]
472
517
);
473
518
$ model ->setId ($ scheduleId );
519
+ $ model ->setJobCode ($ jobCode );
474
520
$ this ->assertEquals (0 , $ model ->getStatus ());
475
521
476
522
$ model ->tryLockJob ();
@@ -486,20 +532,49 @@ public function testTryLockJobSuccess(): void
486
532
public function testTryLockJobFailure (): void
487
533
{
488
534
$ scheduleId = 1 ;
535
+ $ jobCode = 'test_job ' ;
536
+ $ tableName = 'cron_schedule ' ;
537
+
538
+ $ connectionMock = $ this ->createMock (AdapterInterface::class);
539
+ $ connectionMock ->expects ($ this ->once ())
540
+ ->method ('update ' )
541
+ ->with (
542
+ $ tableName ,
543
+ ['status ' => Schedule::STATUS_ERROR ],
544
+ ['job_code = ? ' => $ jobCode , 'status = ? ' => Schedule::STATUS_RUNNING ]
545
+ )
546
+ ->willReturn (1 );
489
547
490
548
$ this ->resourceJobMock ->expects ($ this ->once ())
491
- ->method ('trySetJobUniqueStatusAtomic ' )
549
+ ->method ('trySetJobStatusAtomic ' )
492
550
->with ($ scheduleId , Schedule::STATUS_RUNNING , Schedule::STATUS_PENDING )
493
551
->willReturn (false );
552
+ $ this ->resourceJobMock ->expects ($ this ->once ())
553
+ ->method ('getTable ' )
554
+ ->with ($ tableName )
555
+ ->willReturn ($ tableName );
556
+ $ this ->resourceJobMock ->expects ($ this ->exactly (3 ))
557
+ ->method ('getConnection ' )
558
+ ->willReturn ($ connectionMock );
559
+
560
+ $ this ->retrierMock ->expects ($ this ->exactly (2 ))
561
+ ->method ('execute ' )
562
+ ->willReturnCallback (
563
+ function ($ callback ) {
564
+ return $ callback ();
565
+ }
566
+ );
494
567
495
568
/** @var Schedule $model */
496
569
$ model = $ this ->objectManagerHelper ->getObject (
497
570
Schedule::class,
498
571
[
499
- 'resource ' => $ this ->resourceJobMock
572
+ 'resource ' => $ this ->resourceJobMock ,
573
+ 'retrier ' => $ this ->retrierMock ,
500
574
]
501
575
);
502
576
$ model ->setId ($ scheduleId );
577
+ $ model ->setJobCode ($ jobCode );
503
578
$ this ->assertEquals (0 , $ model ->getStatus ());
504
579
505
580
$ model ->tryLockJob ();
0 commit comments