|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright © Magento, Inc. All rights reserved. |
| 5 | + * See COPYING.txt for license details. |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace Magento\Cron\Test\Unit\Cron; |
| 11 | + |
| 12 | +use Magento\Cron\Cron\CleanOldJobs; |
| 13 | +use Magento\Cron\Model\DeadlockRetrierInterface; |
| 14 | +use Magento\Cron\Model\ResourceModel\Schedule as ScheduleResourceModel; |
| 15 | +use Magento\Cron\Model\Schedule; |
| 16 | +use Magento\Cron\Model\ScheduleFactory; |
| 17 | +use Magento\Framework\App\Config; |
| 18 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 19 | +use Magento\Framework\Stdlib\DateTime\DateTime; |
| 20 | +use PHPUnit\Framework\TestCase; |
| 21 | + |
| 22 | +// phpcs:disable Magento2.Commenting.ClassPropertyPHPDocFormatting.Missing, because such comments would add no value. |
| 23 | + |
| 24 | +class CleanOldJobsTest extends TestCase |
| 25 | +{ |
| 26 | + private CleanOldJobs $cleanOldJobs; |
| 27 | + private Config $configMock; |
| 28 | + private ScheduleFactory $scheduleFactoryMock; |
| 29 | + private DateTime $dateTimeMock; |
| 30 | + private ScheduleResourceModel $scheduleResourceMock; |
| 31 | + private DeadlockRetrierInterface $retrierMock; |
| 32 | + private Schedule $scheduleMock; |
| 33 | + private int $time = 1501538400; |
| 34 | + |
| 35 | + /** |
| 36 | + * @inheritdoc |
| 37 | + */ |
| 38 | + protected function setUp(): void |
| 39 | + { |
| 40 | + $this->configMock = $this->getMockBuilder(Config::class) |
| 41 | + ->disableOriginalConstructor() |
| 42 | + ->getMock(); |
| 43 | + |
| 44 | + $this->dateTimeMock = $this->getMockBuilder(DateTime::class) |
| 45 | + ->disableOriginalConstructor() |
| 46 | + ->getMock(); |
| 47 | + $this->dateTimeMock->method('gmtTimestamp') |
| 48 | + ->willReturn($this->time); |
| 49 | + |
| 50 | + $this->retrierMock = $this->getMockForAbstractClass(DeadlockRetrierInterface::class); |
| 51 | + |
| 52 | + $this->scheduleFactoryMock = $this->getMockBuilder(ScheduleFactory::class) |
| 53 | + ->onlyMethods(['create']) |
| 54 | + ->disableOriginalConstructor() |
| 55 | + ->getMock(); |
| 56 | + |
| 57 | + $this->scheduleMock = $this->getMockBuilder(Schedule::class) |
| 58 | + ->disableOriginalConstructor() |
| 59 | + ->getMock(); |
| 60 | + |
| 61 | + $this->scheduleResourceMock = $this->getMockBuilder(ScheduleResourceModel::class) |
| 62 | + ->disableOriginalConstructor() |
| 63 | + ->getMock(); |
| 64 | + |
| 65 | + $this->scheduleMock |
| 66 | + ->method('getResource') |
| 67 | + ->willReturn($this->scheduleResourceMock); |
| 68 | + |
| 69 | + $this->scheduleFactoryMock |
| 70 | + ->method('create') |
| 71 | + ->willReturn($this->scheduleMock); |
| 72 | + |
| 73 | + $this->cleanOldJobs = new CleanOldJobs( |
| 74 | + $this->configMock, |
| 75 | + $this->dateTimeMock, |
| 76 | + $this->retrierMock, |
| 77 | + $this->scheduleFactoryMock |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + public function testSuccess(): void |
| 82 | + { |
| 83 | + $tableName = 'cron_schedule'; |
| 84 | + |
| 85 | + $this->configMock->expects($this->once()) |
| 86 | + ->method('get') |
| 87 | + ->with('system') |
| 88 | + ->willReturn([ |
| 89 | + 'history_success_lifetime' => 100, |
| 90 | + 'history_failure_lifetime' => 200, |
| 91 | + ]); |
| 92 | + |
| 93 | + $connectionMock = $this->getMockForAbstractClass(AdapterInterface::class); |
| 94 | + |
| 95 | + $connectionMock->expects($this->once()) |
| 96 | + ->method('delete') |
| 97 | + ->with($tableName, [ |
| 98 | + 'scheduled_at < ?' => '$this->time - (86400 + (200 * 60))', |
| 99 | + ]); |
| 100 | + |
| 101 | + $connectionMock->method('formatDate') |
| 102 | + ->willReturnMap([ |
| 103 | + [1501538400, true, '$this->time'], |
| 104 | + [1501538200, true, '$this->time - 200'], |
| 105 | + [1501526400, true, '$this->time - (200 * 60)'], |
| 106 | + [1501452000, true, '$this->time - 86400'], |
| 107 | + [1501451800, true, '$this->time - (86400 + 200)'], |
| 108 | + [1501440000, true, '$this->time - (86400 + (200 * 60))'], |
| 109 | + ]); |
| 110 | + |
| 111 | + $this->scheduleResourceMock->expects($this->once()) |
| 112 | + ->method('getTable') |
| 113 | + ->with($tableName) |
| 114 | + ->willReturn($tableName); |
| 115 | + $this->scheduleResourceMock->expects($this->exactly(3)) |
| 116 | + ->method('getConnection') |
| 117 | + ->willReturn($connectionMock); |
| 118 | + |
| 119 | + $this->retrierMock->expects($this->once()) |
| 120 | + ->method('execute') |
| 121 | + ->willReturnCallback( |
| 122 | + function ($callback) { |
| 123 | + return $callback(); |
| 124 | + } |
| 125 | + ); |
| 126 | + |
| 127 | + $this->cleanOldJobs->execute(); |
| 128 | + } |
| 129 | + |
| 130 | + public function testNoActionWhenEmptyConfig(): void |
| 131 | + { |
| 132 | + $this->configMock->expects($this->once()) |
| 133 | + ->method('get') |
| 134 | + ->with('system') |
| 135 | + ->willReturn([]); |
| 136 | + |
| 137 | + $this->scheduleFactoryMock->expects($this->never())->method('create'); |
| 138 | + |
| 139 | + $this->cleanOldJobs->execute(); |
| 140 | + } |
| 141 | +} |
0 commit comments