|
5 | 5 | */ |
6 | 6 | declare(strict_types=1); |
7 | 7 |
|
8 | | - |
9 | 8 | namespace Magento\CatalogRule\Test\Unit\Cron; |
10 | 9 |
|
11 | 10 | use Magento\CatalogRule\Cron\DailyCatalogUpdate; |
12 | 11 | use Magento\CatalogRule\Model\Indexer\Rule\RuleProductProcessor; |
13 | | -use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; |
| 12 | +use Magento\CatalogRule\Model\ResourceModel\Rule\Collection as RuleCollection; |
| 13 | +use Magento\CatalogRule\Model\ResourceModel\Rule\CollectionFactory as RuleCollectionFactory; |
14 | 14 | use PHPUnit\Framework\MockObject\MockObject; |
15 | 15 | use PHPUnit\Framework\TestCase; |
16 | 16 |
|
17 | 17 | class DailyCatalogUpdateTest extends TestCase |
18 | 18 | { |
19 | 19 | /** |
20 | | - * Processor |
21 | | - * |
22 | 20 | * @var RuleProductProcessor|MockObject |
23 | 21 | */ |
24 | | - protected $ruleProductProcessor; |
| 22 | + private $ruleProductProcessor; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var RuleCollectionFactory|MockObject |
| 26 | + */ |
| 27 | + private $ruleCollectionFactory; |
25 | 28 |
|
26 | 29 | /** |
27 | | - * Cron object |
28 | | - * |
29 | 30 | * @var DailyCatalogUpdate |
30 | 31 | */ |
31 | | - protected $cron; |
| 32 | + private $cron; |
32 | 33 |
|
33 | 34 | protected function setUp(): void |
34 | 35 | { |
35 | | - $this->ruleProductProcessor = $this->createMock( |
36 | | - RuleProductProcessor::class |
37 | | - ); |
38 | | - |
39 | | - $this->cron = (new ObjectManager($this))->getObject( |
40 | | - DailyCatalogUpdate::class, |
41 | | - [ |
42 | | - 'ruleProductProcessor' => $this->ruleProductProcessor, |
43 | | - ] |
44 | | - ); |
| 36 | + $this->ruleProductProcessor = $this->createMock(RuleProductProcessor::class); |
| 37 | + $this->ruleCollectionFactory = $this->createMock(RuleCollectionFactory::class); |
| 38 | + |
| 39 | + $this->cron = new DailyCatalogUpdate($this->ruleProductProcessor, $this->ruleCollectionFactory); |
45 | 40 | } |
46 | 41 |
|
47 | | - public function testDailyCatalogUpdate() |
| 42 | + /** |
| 43 | + * @dataProvider executeDataProvider |
| 44 | + * @param int $activeRulesCount |
| 45 | + * @param bool $isInvalidationNeeded |
| 46 | + */ |
| 47 | + public function testExecute(int $activeRulesCount, bool $isInvalidationNeeded) |
48 | 48 | { |
49 | | - $this->ruleProductProcessor->expects($this->once())->method('markIndexerAsInvalid'); |
| 49 | + $ruleCollection = $this->createMock(RuleCollection::class); |
| 50 | + $this->ruleCollectionFactory->expects($this->once()) |
| 51 | + ->method('create') |
| 52 | + ->willReturn($ruleCollection); |
| 53 | + $ruleCollection->expects($this->once()) |
| 54 | + ->method('addIsActiveFilter') |
| 55 | + ->willReturn($ruleCollection); |
| 56 | + $ruleCollection->expects($this->once()) |
| 57 | + ->method('getSize') |
| 58 | + ->willReturn($activeRulesCount); |
| 59 | + $this->ruleProductProcessor->expects($isInvalidationNeeded ? $this->once() : $this->never()) |
| 60 | + ->method('markIndexerAsInvalid'); |
50 | 61 |
|
51 | 62 | $this->cron->execute(); |
52 | 63 | } |
| 64 | + |
| 65 | + /** |
| 66 | + * @return array |
| 67 | + */ |
| 68 | + public function executeDataProvider(): array |
| 69 | + { |
| 70 | + return [ |
| 71 | + [2, true], |
| 72 | + [0, false], |
| 73 | + ]; |
| 74 | + } |
53 | 75 | } |
0 commit comments