|
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,119 @@ 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 | + $config = $this->createMock(\Magento\Cron\Model\ConfigInterface::class); |
| 63 | + $config->expects($this->any()) |
| 64 | + ->method('getJobs') |
| 65 | + ->willReturn($this->getFilterTestCronGroups()); |
| 66 | + |
| 67 | + $request = Bootstrap::getObjectManager()->get(\Magento\Framework\App\Console\Request::class); |
| 68 | + $lockManager = $this->createMock(\Magento\Framework\Lock\LockManagerInterface::class); |
| 69 | + |
| 70 | + // The jobs are locked when they are run, assert on them to see which groups would run |
| 71 | + $expectedLockData = []; |
| 72 | + foreach ($expectedGroupsToRun as $expectedGroupToRun) { |
| 73 | + $expectedLockData[] = [ |
| 74 | + ProcessCronQueueObserver::LOCK_PREFIX . $expectedGroupToRun, |
| 75 | + ProcessCronQueueObserver::LOCK_TIMEOUT |
| 76 | + ]; |
| 77 | + } |
| 78 | + |
| 79 | + // No expected lock data, means we should never call it |
| 80 | + if (empty($expectedLockData)) { |
| 81 | + $lockManager->expects($this->never()) |
| 82 | + ->method('lock'); |
| 83 | + } |
| 84 | + |
| 85 | + $lockManager->expects($this->exactly(count($expectedLockData))) |
| 86 | + ->method('lock') |
| 87 | + ->withConsecutive(...$expectedLockData); |
| 88 | + |
| 89 | + $request->setParams( |
| 90 | + [ |
| 91 | + 'group' => $group, |
| 92 | + 'exclude-group' => $excludeGroup, |
| 93 | + 'standaloneProcessStarted' => '1' |
| 94 | + ] |
| 95 | + ); |
| 96 | + $this->_model = Bootstrap::getObjectManager() |
| 97 | + ->create(\Magento\Cron\Observer\ProcessCronQueueObserver::class, [ |
| 98 | + 'request' => $request, |
| 99 | + 'lockManager' => $lockManager, |
| 100 | + 'config' => $config |
| 101 | + ]); |
| 102 | + $this->_model->execute(new \Magento\Framework\Event\Observer()); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @return array|array[] |
| 107 | + */ |
| 108 | + public function groupFiltersDataProvider(): array |
| 109 | + { |
| 110 | + |
| 111 | + return [ |
| 112 | + 'no flags runs all groups' => [ |
| 113 | + ['index', 'consumers', 'default'] // 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 | + ['consumers', 'default'], // 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 | + ['default', 'consumers', 'index'] // groups to exclude, all of them |
| 143 | + ], |
| 144 | + 'exclude all groups but consumers, consumers runs' => [ |
| 145 | + ['consumers'], |
| 146 | + null, |
| 147 | + ['index', 'default'] |
| 148 | + ], |
| 149 | + ]; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Only run the filter group tests with a limited set of cron groups, keeps tests consistent between EE and CE |
| 154 | + * |
| 155 | + * @return array |
| 156 | + */ |
| 157 | + private function getFilterTestCronGroups() |
| 158 | + { |
| 159 | + $listOfGroups = []; |
| 160 | + $config = Bootstrap::getObjectManager()->get(\Magento\Cron\Model\ConfigInterface::class); |
| 161 | + foreach ($config->getJobs() as $groupId => $data) { |
| 162 | + if (in_array($groupId, ['default', 'consumers', 'index'])) { |
| 163 | + $listOfGroups[$groupId] = $data; |
| 164 | + } |
| 165 | + } |
| 166 | + return $listOfGroups; |
| 167 | + } |
52 | 168 | }
|
0 commit comments