|
5 | 5 | */
|
6 | 6 | namespace Magento\Cron\Observer;
|
7 | 7 |
|
| 8 | +use Magento\Cron\Observer\ProcessCronQueueObserver; |
8 | 9 | use \Magento\TestFramework\Helper\Bootstrap;
|
9 | 10 |
|
10 | 11 | class ProcessCronQueueObserverTest extends \PHPUnit\Framework\TestCase
|
@@ -49,4 +50,102 @@ public function testDispatchNoFailed()
|
49 | 50 | $this->fail($item->getMessages());
|
50 | 51 | }
|
51 | 52 | }
|
| 53 | + |
| 54 | + /** |
| 55 | + * @param array $expectedGroupsToRun |
| 56 | + * @param null $group |
| 57 | + * @param null $excludeGroup |
| 58 | + * @dataProvider groupFiltersDataProvider |
| 59 | + */ |
| 60 | + public function testGroupFilters(array $expectedGroupsToRun, $group = null, $excludeGroup = null) |
| 61 | + { |
| 62 | + $request = Bootstrap::getObjectManager()->get(\Magento\Framework\App\Console\Request::class); |
| 63 | + $lockManager = $this->createMock(\Magento\Framework\Lock\LockManagerInterface::class); |
| 64 | + |
| 65 | + // The jobs are locked when they are run, assert on them to see which groups would run |
| 66 | + $expectedLockData = []; |
| 67 | + foreach ($expectedGroupsToRun as $expectedGroupToRun) { |
| 68 | + $expectedLockData[] = [ |
| 69 | + ProcessCronQueueObserver::LOCK_PREFIX . $expectedGroupToRun, |
| 70 | + ProcessCronQueueObserver::LOCK_TIMEOUT |
| 71 | + ]; |
| 72 | + } |
| 73 | + |
| 74 | + // No expected lock data, means we should never call it |
| 75 | + if (empty($expectedLockData)){ |
| 76 | + $lockManager->expects($this->never()) |
| 77 | + ->method('lock'); |
| 78 | + } |
| 79 | + |
| 80 | + $lockManager->expects($this->exactly(count($expectedLockData))) |
| 81 | + ->method('lock') |
| 82 | + ->withConsecutive(...$expectedLockData); |
| 83 | + |
| 84 | + $request->setParams( |
| 85 | + [ |
| 86 | + 'group' => $group, |
| 87 | + 'exclude-group' => $excludeGroup, |
| 88 | + 'standaloneProcessStarted' => '1' |
| 89 | + ] |
| 90 | + ); |
| 91 | + $this->_model = Bootstrap::getObjectManager() |
| 92 | + ->create(\Magento\Cron\Observer\ProcessCronQueueObserver::class, [ |
| 93 | + 'request' => $request, |
| 94 | + 'lockManager' => $lockManager |
| 95 | + ]); |
| 96 | + $this->_model->execute(new \Magento\Framework\Event\Observer()); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @return array|array[] |
| 101 | + */ |
| 102 | + public function groupFiltersDataProvider(): array |
| 103 | + { |
| 104 | + $listOfGroups = []; |
| 105 | + $config = Bootstrap::getObjectManager()->get(\Magento\Cron\Model\ConfigInterface::class); |
| 106 | + foreach (array_keys($config->getJobs()) as $groupId) { |
| 107 | + $listOfGroups[$groupId] = $groupId; |
| 108 | + } |
| 109 | + $listOfGroups = array_reverse($listOfGroups, true); |
| 110 | + |
| 111 | + return [ |
| 112 | + 'no flags runs all groups' => [ |
| 113 | + $listOfGroups // groups to run |
| 114 | + ], |
| 115 | + '--group=default should run' => [ |
| 116 | + ['default'], // groups to run |
| 117 | + 'default', // --group default |
| 118 | + ], |
| 119 | + '--group=default with --exclude-group=default, nothing should run' => [ |
| 120 | + [], // groups to run |
| 121 | + 'default', // --group default |
| 122 | + ['default'], // --exclude-group default |
| 123 | + ], |
| 124 | + '--group=default with --exclude-group=index, default should run' => [ |
| 125 | + ['default'], // groups to run |
| 126 | + 'default', // --group default |
| 127 | + ['index'], // --exclude-group index |
| 128 | + ], |
| 129 | + '--group=index with --exclude-group=default, index should run' => [ |
| 130 | + ['index'], // groups to run |
| 131 | + 'index', // --group index |
| 132 | + ['default'], // --exclude-group default |
| 133 | + ], |
| 134 | + '--exclude-group=index, all other groups should run' => [ |
| 135 | + array_filter($listOfGroups, function($g) { return $g !== 'index'; }), // groups to run, all but index |
| 136 | + null, // |
| 137 | + ['index'] // --exclude-group index |
| 138 | + ], |
| 139 | + '--exclude-group for every group runs nothing' => [ |
| 140 | + [], // groups to run, none |
| 141 | + null, // |
| 142 | + $listOfGroups // groups to exclude, all of them |
| 143 | + ], |
| 144 | + 'exclude all groups but consumers, consumers runs' => [ |
| 145 | + array_filter($listOfGroups, function($g) { return $g === 'consumers'; }), |
| 146 | + null, |
| 147 | + array_filter($listOfGroups, function($g) { return $g !== 'consumers'; }) |
| 148 | + ], |
| 149 | + ]; |
| 150 | + } |
52 | 151 | }
|
0 commit comments