|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright © Magento, Inc. All rights reserved. |
| 5 | + * See COPYING.txt for license details. |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Magento\Framework\Mview\Test\Unit; |
| 10 | + |
| 11 | +use Magento\Framework\DB\Adapter\AdapterInterface; |
| 12 | +use Magento\Framework\DB\Select; |
| 13 | +use Magento\Framework\DB\Ddl\Trigger; |
| 14 | +use Magento\Framework\App\ResourceConnection; |
| 15 | +use Magento\Framework\Mview\View\CollectionFactory; |
| 16 | +use Magento\Framework\Mview\View\CollectionInterface; |
| 17 | +use Magento\Framework\Mview\View\Subscription; |
| 18 | +use Magento\Framework\Mview\View; |
| 19 | +use Magento\Framework\Mview\ViewFactory; |
| 20 | +use Magento\Framework\Mview\TriggerCleaner; |
| 21 | +use PHPUnit\Framework\MockObject\MockObject; |
| 22 | +use PHPUnit\Framework\TestCase; |
| 23 | + |
| 24 | +/** test Mview TriggerCleaner functionality |
| 25 | + */ |
| 26 | +class TriggerCleanerTest extends TestCase |
| 27 | +{ |
| 28 | + /** |
| 29 | + * @var View |
| 30 | + */ |
| 31 | + private $model; |
| 32 | + |
| 33 | + /** |
| 34 | + * @var MockObject|CollectionFactory |
| 35 | + */ |
| 36 | + private $viewCollectionFactory; |
| 37 | + |
| 38 | + /** |
| 39 | + * @var MockObject|ResourceConnection |
| 40 | + */ |
| 41 | + private $resource; |
| 42 | + |
| 43 | + /** |
| 44 | + * @var MockObject|ViewFactory |
| 45 | + */ |
| 46 | + private $viewFactory; |
| 47 | + |
| 48 | + /** |
| 49 | + * @inheritdoc |
| 50 | + */ |
| 51 | + protected function setUp(): void |
| 52 | + { |
| 53 | + $this->resource = $this->createMock(ResourceConnection::class); |
| 54 | + $this->viewCollectionFactory = $this->getMockBuilder(CollectionFactory::class) |
| 55 | + ->disableOriginalConstructor() |
| 56 | + ->onlyMethods(['create']) |
| 57 | + ->getMockForAbstractClass(); |
| 58 | + $this->viewFactory = $this->createMock(ViewFactory::class); |
| 59 | + $this->model = new TriggerCleaner( |
| 60 | + $this->viewCollectionFactory, |
| 61 | + $this->resource, |
| 62 | + $this->viewFactory |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Test triggers aren't recreated if their action statements are unchanged |
| 68 | + * |
| 69 | + * @return void |
| 70 | + */ |
| 71 | + public function testRemoveTriggersNoChanges() |
| 72 | + { |
| 73 | + $DBTriggers = [ |
| 74 | + 'trg_catalog_category_entity_int_after_insert' => [ |
| 75 | + 'TRIGGER_NAME' => 'trg_catalog_category_entity_int_after_insert', |
| 76 | + 'ACTION_STATEMENT' => 'BEGIN statement; END', |
| 77 | + 'EVENT_OBJECT_TABLE' => 'catalog_category_entity_int' |
| 78 | + ] |
| 79 | + ]; |
| 80 | + |
| 81 | + $connectionMock = $this->getConnectionMock(); |
| 82 | + $connectionMock->expects($this->once()) |
| 83 | + ->method('fetchAssoc') |
| 84 | + ->willReturn($DBTriggers); |
| 85 | + |
| 86 | + $this->resource->expects($this->once())->method('getConnection')->willReturn($connectionMock); |
| 87 | + |
| 88 | + $triggerMock = $this->getMockBuilder(Trigger::class) |
| 89 | + ->disableOriginalConstructor() |
| 90 | + ->onlyMethods(['getName', 'getStatements']) |
| 91 | + ->getMockForAbstractClass(); |
| 92 | + $triggerMock->expects($this->atLeastOnce()) |
| 93 | + ->method('getName') |
| 94 | + ->willReturn('trg_catalog_category_entity_int_after_insert'); |
| 95 | + $triggerMock->expects($this->once())->method('getStatements')->willReturn(['statement;']); |
| 96 | + |
| 97 | + $subscriptionMock = $this->createMock(Subscription::class); |
| 98 | + $subscriptionMock->expects($this->once())->method('getTriggers')->willReturn([$triggerMock]); |
| 99 | + $subscriptionMock->expects($this->once())->method('create')->willReturn($subscriptionMock); |
| 100 | + |
| 101 | + $viewMock = $this->createMock(View::class); |
| 102 | + $viewMock->expects($this->once()) |
| 103 | + ->method('getSubscriptions') |
| 104 | + ->willReturn(['subscriptionConfig' => []]); |
| 105 | + $viewMock->expects($this->once())->method('initSubscriptionInstance')->willReturn($subscriptionMock); |
| 106 | + |
| 107 | + $viewCollectionMock = $this->getMockForAbstractClass(CollectionInterface::class); |
| 108 | + $viewCollectionMock->expects($this->once())->method('getViewsByStateMode')->willReturn([$viewMock]); |
| 109 | + |
| 110 | + $this->viewCollectionFactory->expects($this->once())->method('create')->willReturn($viewCollectionMock); |
| 111 | + |
| 112 | + $subscriptionMock->expects($this->never())->method('saveTrigger'); |
| 113 | + $viewMock->expects($this->never())->method('unsubscribe'); |
| 114 | + $this->model->removeTriggers(); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Prepare connection mock |
| 119 | + * |
| 120 | + * @return AdapterInterface|MockObject |
| 121 | + */ |
| 122 | + private function getConnectionMock() |
| 123 | + { |
| 124 | + $selectMock = $this->createMock(Select::class); |
| 125 | + |
| 126 | + $selectMock->expects($this->once()) |
| 127 | + ->method('from') |
| 128 | + ->willReturn($selectMock); |
| 129 | + |
| 130 | + $selectMock->expects($this->once()) |
| 131 | + ->method('where'); |
| 132 | + |
| 133 | + $connectionMock = $this->createMock(AdapterInterface::class); |
| 134 | + |
| 135 | + $connectionMock->expects($this->once()) |
| 136 | + ->method('select') |
| 137 | + ->willReturn($selectMock); |
| 138 | + |
| 139 | + return $connectionMock; |
| 140 | + } |
| 141 | + |
| 142 | + private function getViewListMock() |
| 143 | + { |
| 144 | + $triggerMock = $this->getMockForAbstractClass(Trigger::class); |
| 145 | + $triggerMock->expects($this->once()) |
| 146 | + ->method('getName') |
| 147 | + ->willReturn('trg_catalog_category_entity_int_after_insert'); |
| 148 | + $triggerMock->expects($this->once())->method('getStatements')->willReturn(['statement;']); |
| 149 | + |
| 150 | + $subscriptionMock = $this->getMockForAbstractClass(SubscriptionInterface::class); |
| 151 | + $subscriptionMock->expects($this->once())->method('create')->willReturn($subscriptionMock); |
| 152 | + $subscriptionMock->expects($this->once())->method('getTriggers')->willReturn([$triggerMock]); |
| 153 | + |
| 154 | + $viewMock = $this->getMockForAbstractClass(ViewInterface::class); |
| 155 | + $viewMock->expects($this->once()) |
| 156 | + ->method('getSubscriptions') |
| 157 | + ->willReturn(['subscriptionConfig' => []]); |
| 158 | + $viewMock->expects($this->once())->method('initSubscriptionInstance')->willReturn($subscriptionMock); |
| 159 | + |
| 160 | + $viewCollectionMock = $this->getMockForAbstractClass(CollectionInterface::class); |
| 161 | + $viewCollectionMock->expects($this->once())->method('getViewsByStateMode')->willReturn([$viewMock]); |
| 162 | + |
| 163 | + $viewCollectionFactoryMock = $this->getMockForAbstractClass(CollectionFactory::class); |
| 164 | + $viewCollectionFactoryMock->expects($this->once())->method('create')->willReturn($viewCollectionMock); |
| 165 | + } |
| 166 | +} |
0 commit comments