From 891198c5117d4d2345b66745f86bfdfca1978525 Mon Sep 17 00:00:00 2001 From: nikunj Date: Wed, 20 Mar 2019 18:16:44 +0530 Subject: [PATCH 01/11] #21853: Allow mview indexers to use different entity columns. --- .../Framework/Mview/View/Subscription.php | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/internal/Magento/Framework/Mview/View/Subscription.php b/lib/internal/Magento/Framework/Mview/View/Subscription.php index 67dff1a2cc5db..6e02f1d169daa 100644 --- a/lib/internal/Magento/Framework/Mview/View/Subscription.php +++ b/lib/internal/Magento/Framework/Mview/View/Subscription.php @@ -117,8 +117,19 @@ public function create() // Add statements for linked views foreach ($this->getLinkedViews() as $view) { + // Store current column name for reverting back later. + $originalColumnName = $this->getColumnName(); + /** @var \Magento\Framework\Mview\ViewInterface $view */ + // Use the column name from specific subscription instead of + // use from the one which is currently updated. + $subscriptions = $view->getSubscriptions(); + $subscription = $subscriptions[$this->getTableName()]; + $this->columnName = $subscription['column']; $trigger->addStatement($this->buildStatement($event, $view->getChangelog())); + + // Revert back the column name. + $this->columnName = $originalColumnName; } $this->connection->dropTrigger($trigger->getName()); @@ -146,8 +157,19 @@ public function remove() // Add statements for linked views foreach ($this->getLinkedViews() as $view) { + // Store current column name for reverting back later. + $originalColumnName = $this->columnName; + /** @var \Magento\Framework\Mview\ViewInterface $view */ + // Use the column name from specific subscription instead of + // use from the one which is currently updated. + $subscriptions = $view->getSubscriptions(); + $subscription = $subscriptions[$this->getTableName()]; + $this->columnName = $subscription['column']; $trigger->addStatement($this->buildStatement($event, $view->getChangelog())); + + // Revert back the column name. + $this->columnName = $originalColumnName; } $this->connection->dropTrigger($trigger->getName()); From b59fc5a549e85d556979b58c42b3cd39058a5a3f Mon Sep 17 00:00:00 2001 From: nikunj Date: Sun, 12 Apr 2020 17:37:36 +0530 Subject: [PATCH 02/11] #21853: Simplify the code change. --- .../Framework/Mview/View/Subscription.php | 25 +++++-------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/lib/internal/Magento/Framework/Mview/View/Subscription.php b/lib/internal/Magento/Framework/Mview/View/Subscription.php index 32841bd7e0f9d..3204722a86140 100644 --- a/lib/internal/Magento/Framework/Mview/View/Subscription.php +++ b/lib/internal/Magento/Framework/Mview/View/Subscription.php @@ -113,23 +113,16 @@ public function create() ->setEvent($event) ->setTable($this->resource->getTableName($this->tableName)); - $trigger->addStatement($this->buildStatement($event, $this->getView()->getChangelog())); + $trigger->addStatement($this->buildStatement($event, $this->getView()->getChangelog(), $this->getColumnName())); // Add statements for linked views foreach ($this->getLinkedViews() as $view) { - // Store current column name for reverting back later. - $originalColumnName = $this->getColumnName(); - /** @var \Magento\Framework\Mview\ViewInterface $view */ // Use the column name from specific subscription instead of // use from the one which is currently updated. $subscriptions = $view->getSubscriptions(); $subscription = $subscriptions[$this->getTableName()]; - $this->columnName = $subscription['column']; - $trigger->addStatement($this->buildStatement($event, $view->getChangelog())); - - // Revert back the column name. - $this->columnName = $originalColumnName; + $trigger->addStatement($this->buildStatement($event, $view->getChangelog(), $subscription['column'])); } $this->connection->dropTrigger($trigger->getName()); @@ -157,19 +150,12 @@ public function remove() // Add statements for linked views foreach ($this->getLinkedViews() as $view) { - // Store current column name for reverting back later. - $originalColumnName = $this->columnName; - /** @var \Magento\Framework\Mview\ViewInterface $view */ // Use the column name from specific subscription instead of // use from the one which is currently updated. $subscriptions = $view->getSubscriptions(); $subscription = $subscriptions[$this->getTableName()]; - $this->columnName = $subscription['column']; - $trigger->addStatement($this->buildStatement($event, $view->getChangelog())); - - // Revert back the column name. - $this->columnName = $originalColumnName; + $trigger->addStatement($this->buildStatement($event, $view->getChangelog(), $subscription['column'])); } $this->connection->dropTrigger($trigger->getName()); @@ -216,9 +202,10 @@ protected function getLinkedViews() * * @param string $event * @param \Magento\Framework\Mview\View\ChangelogInterface $changelog + * @param string $subscriptionColumnName * @return string */ - protected function buildStatement($event, $changelog) + protected function buildStatement($event, $changelog, $subscriptionColumnName) { switch ($event) { case Trigger::EVENT_INSERT: @@ -258,7 +245,7 @@ protected function buildStatement($event, $changelog) $trigger, $this->connection->quoteIdentifier($this->resource->getTableName($changelog->getName())), $this->connection->quoteIdentifier($changelog->getColumnName()), - $this->connection->quoteIdentifier($this->getColumnName()) + $this->connection->quoteIdentifier($subscriptionColumnName) ); } From 4035ec08ada4cb5d5495bc7325a982c81c5c02b3 Mon Sep 17 00:00:00 2001 From: nikunj Date: Sun, 12 Apr 2020 17:40:13 +0530 Subject: [PATCH 03/11] #21853: Correct the comment. --- .../Magento/Framework/Mview/View/Subscription.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/internal/Magento/Framework/Mview/View/Subscription.php b/lib/internal/Magento/Framework/Mview/View/Subscription.php index 3204722a86140..642f5bc610653 100644 --- a/lib/internal/Magento/Framework/Mview/View/Subscription.php +++ b/lib/internal/Magento/Framework/Mview/View/Subscription.php @@ -118,8 +118,9 @@ public function create() // Add statements for linked views foreach ($this->getLinkedViews() as $view) { /** @var \Magento\Framework\Mview\ViewInterface $view */ - // Use the column name from specific subscription instead of - // use from the one which is currently updated. + // Use the column name from specific linked view instead of + // using from the one which is currently updated for all + // the views. $subscriptions = $view->getSubscriptions(); $subscription = $subscriptions[$this->getTableName()]; $trigger->addStatement($this->buildStatement($event, $view->getChangelog(), $subscription['column'])); @@ -151,8 +152,9 @@ public function remove() // Add statements for linked views foreach ($this->getLinkedViews() as $view) { /** @var \Magento\Framework\Mview\ViewInterface $view */ - // Use the column name from specific subscription instead of - // use from the one which is currently updated. + // Use the column name from specific linked view instead of + // using from the one which is currently updated for all + // the views. $subscriptions = $view->getSubscriptions(); $subscription = $subscriptions[$this->getTableName()]; $trigger->addStatement($this->buildStatement($event, $view->getChangelog(), $subscription['column'])); From 320ced74bdf9e7739d20397b5eff2e8ad98c95f1 Mon Sep 17 00:00:00 2001 From: nikunj Date: Tue, 14 Apr 2020 12:12:03 +0530 Subject: [PATCH 04/11] 21853: Send the view object and do all processing in buildStatement. --- .../Framework/Mview/View/Subscription.php | 30 ++++++++----------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/lib/internal/Magento/Framework/Mview/View/Subscription.php b/lib/internal/Magento/Framework/Mview/View/Subscription.php index 642f5bc610653..cd6288a231eb9 100644 --- a/lib/internal/Magento/Framework/Mview/View/Subscription.php +++ b/lib/internal/Magento/Framework/Mview/View/Subscription.php @@ -113,17 +113,12 @@ public function create() ->setEvent($event) ->setTable($this->resource->getTableName($this->tableName)); - $trigger->addStatement($this->buildStatement($event, $this->getView()->getChangelog(), $this->getColumnName())); + $trigger->addStatement($this->buildStatement($event, $this->getView())); // Add statements for linked views foreach ($this->getLinkedViews() as $view) { /** @var \Magento\Framework\Mview\ViewInterface $view */ - // Use the column name from specific linked view instead of - // using from the one which is currently updated for all - // the views. - $subscriptions = $view->getSubscriptions(); - $subscription = $subscriptions[$this->getTableName()]; - $trigger->addStatement($this->buildStatement($event, $view->getChangelog(), $subscription['column'])); + $trigger->addStatement($this->buildStatement($event, $view)); } $this->connection->dropTrigger($trigger->getName()); @@ -152,12 +147,7 @@ public function remove() // Add statements for linked views foreach ($this->getLinkedViews() as $view) { /** @var \Magento\Framework\Mview\ViewInterface $view */ - // Use the column name from specific linked view instead of - // using from the one which is currently updated for all - // the views. - $subscriptions = $view->getSubscriptions(); - $subscription = $subscriptions[$this->getTableName()]; - $trigger->addStatement($this->buildStatement($event, $view->getChangelog(), $subscription['column'])); + $trigger->addStatement($this->buildStatement($event, $view)); } $this->connection->dropTrigger($trigger->getName()); @@ -203,12 +193,18 @@ protected function getLinkedViews() * Build trigger statement for INSERT, UPDATE, DELETE events * * @param string $event - * @param \Magento\Framework\Mview\View\ChangelogInterface $changelog - * @param string $subscriptionColumnName + * @param \Magento\Framework\Mview\ViewInterface $view * @return string */ - protected function buildStatement($event, $changelog, $subscriptionColumnName) + protected function buildStatement($event, $view) { + // Get the subscription for the specific view and specific table. + // We will use column name from it. + $subscription = $view->getSubscriptions()[$this->getTableName()]; + + // Get the changelog from View to get changelog column name. + $changelog = $view->getChangelog(); + switch ($event) { case Trigger::EVENT_INSERT: $trigger = "INSERT IGNORE INTO %s (%s) VALUES (NEW.%s);"; @@ -247,7 +243,7 @@ protected function buildStatement($event, $changelog, $subscriptionColumnName) $trigger, $this->connection->quoteIdentifier($this->resource->getTableName($changelog->getName())), $this->connection->quoteIdentifier($changelog->getColumnName()), - $this->connection->quoteIdentifier($subscriptionColumnName) + $this->connection->quoteIdentifier($subscription['column']) ); } From 7c2d2a6774717fe6374df61edd22f39a5c02c733 Mon Sep 17 00:00:00 2001 From: nikunj Date: Sat, 29 Aug 2020 12:02:13 +0530 Subject: [PATCH 05/11] #21853: Fix tests. --- .../Framework/Mview/Test/Unit/View/SubscriptionTest.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/lib/internal/Magento/Framework/Mview/Test/Unit/View/SubscriptionTest.php b/lib/internal/Magento/Framework/Mview/Test/Unit/View/SubscriptionTest.php index b91c0b525390f..e0bdf570945a2 100644 --- a/lib/internal/Magento/Framework/Mview/Test/Unit/View/SubscriptionTest.php +++ b/lib/internal/Magento/Framework/Mview/Test/Unit/View/SubscriptionTest.php @@ -220,8 +220,6 @@ public function testCreate() $this->viewMock->expects($this->exactly(3)) ->method('getId') ->willReturn('this_id'); - $this->viewMock->expects($this->never()) - ->method('getSubscriptions'); $this->viewCollectionMock->expects($this->exactly(1)) ->method('getViewsByStateMode') @@ -290,9 +288,7 @@ public function testRemove() $otherViewMock->expects($this->exactly(1)) ->method('getId') ->willReturn('other_id'); - $otherViewMock->expects($this->exactly(1)) - ->method('getSubscriptions') - ->willReturn([['name' => $this->tableName], ['name' => 'otherTableName']]); + $otherViewMock->expects($this->exactly(3)) ->method('getChangelog') ->willReturn($otherChangelogMock); @@ -300,8 +296,6 @@ public function testRemove() $this->viewMock->expects($this->exactly(3)) ->method('getId') ->willReturn('this_id'); - $this->viewMock->expects($this->never()) - ->method('getSubscriptions'); $this->viewCollectionMock->expects($this->exactly(1)) ->method('getViewsByStateMode') From 8388a90ea53e67a3f0bde1e03e2f24b64f1925ee Mon Sep 17 00:00:00 2001 From: nikunj Date: Mon, 31 Aug 2020 11:44:45 +0530 Subject: [PATCH 06/11] #21853: Fix tests. --- .../Magento/Framework/Mview/View/Subscription.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/internal/Magento/Framework/Mview/View/Subscription.php b/lib/internal/Magento/Framework/Mview/View/Subscription.php index cd6288a231eb9..a65f6e98016f1 100644 --- a/lib/internal/Magento/Framework/Mview/View/Subscription.php +++ b/lib/internal/Magento/Framework/Mview/View/Subscription.php @@ -178,7 +178,7 @@ protected function getLinkedViews() continue; } // Search in view subscriptions - foreach ($view->getSubscriptions() as $subscription) { + foreach ($view->getSubscriptions() ?? [] as $subscription) { if ($subscription['name'] != $this->getTableName()) { continue; } @@ -200,7 +200,12 @@ protected function buildStatement($event, $view) { // Get the subscription for the specific view and specific table. // We will use column name from it. - $subscription = $view->getSubscriptions()[$this->getTableName()]; + $subscriptions = $view->getSubscriptions() ?? []; + if (empty($subscriptions[$this->getTableName()])) { + return ''; + } + + $subscription = $subscriptions[$this->getTableName()]; // Get the changelog from View to get changelog column name. $changelog = $view->getChangelog(); From ff56be5640ab6654ba677f5fba24f98efa5faacd Mon Sep 17 00:00:00 2001 From: Pavel Bystritsky Date: Mon, 28 Sep 2020 16:39:52 +0300 Subject: [PATCH 07/11] magento/magento2#21853: Allow mview indexers to use different entity columns - unit & static tests fix. --- .../Mview/Test/Unit/View/SubscriptionTest.php | 32 +++++++++++++++++-- .../Framework/Mview/View/Subscription.php | 6 ++-- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/lib/internal/Magento/Framework/Mview/Test/Unit/View/SubscriptionTest.php b/lib/internal/Magento/Framework/Mview/Test/Unit/View/SubscriptionTest.php index e0bdf570945a2..dc14cae93ca70 100644 --- a/lib/internal/Magento/Framework/Mview/Test/Unit/View/SubscriptionTest.php +++ b/lib/internal/Magento/Framework/Mview/Test/Unit/View/SubscriptionTest.php @@ -44,7 +44,7 @@ class SubscriptionTest extends TestCase protected $viewMock; /** @var string */ - private $tableName; + private $tableName = 'thisTableName'; protected function setUp(): void { @@ -210,9 +210,14 @@ public function testCreate() $otherViewMock->expects($this->exactly(1)) ->method('getId') ->willReturn('other_id'); - $otherViewMock->expects($this->exactly(1)) + $otherViewMock->expects($this->exactly(4)) ->method('getSubscriptions') - ->willReturn([['name' => $this->tableName], ['name' => 'otherTableName']]); + ->willReturn( + [ + $this->tableName => ['name' => $this->tableName, 'column' => 'columnName'], + 'otherTableName' => ['name' => 'otherTableName', 'column' => 'columnName'] + ] + ); $otherViewMock->expects($this->exactly(3)) ->method('getChangelog') ->willReturn($otherChangelogMock); @@ -234,6 +239,17 @@ public function testCreate() ->method('createTrigger') ->with($triggerMock); + $this->tableName = 'thisTableName'; + + $this->viewMock->expects($this->exactly(3)) + ->method('getSubscriptions') + ->willReturn( + [ + $this->tableName => ['name' => $this->tableName, 'column' => 'columnName'], + 'otherTableName' => ['name' => 'otherTableName', 'column' => 'columnName'] + ] + ); + $this->model->create(); } @@ -285,6 +301,7 @@ public function testRemove() true, [] ); + $otherViewMock->expects($this->exactly(1)) ->method('getId') ->willReturn('other_id'); @@ -293,6 +310,15 @@ public function testRemove() ->method('getChangelog') ->willReturn($otherChangelogMock); + $otherViewMock->expects($this->any()) + ->method('getSubscriptions') + ->willReturn( + [ + $this->tableName => ['name' => $this->tableName, 'column' => 'columnName'], + 'otherTableName' => ['name' => 'otherTableName', 'column' => 'columnName'] + ] + ); + $this->viewMock->expects($this->exactly(3)) ->method('getId') ->willReturn('this_id'); diff --git a/lib/internal/Magento/Framework/Mview/View/Subscription.php b/lib/internal/Magento/Framework/Mview/View/Subscription.php index a65f6e98016f1..cfab8b2479bc1 100644 --- a/lib/internal/Magento/Framework/Mview/View/Subscription.php +++ b/lib/internal/Magento/Framework/Mview/View/Subscription.php @@ -11,9 +11,7 @@ use Magento\Framework\Mview\View\StateInterface; /** - * Class Subscription - * - * @package Magento\Framework\Mview\View + * Mview subscription. */ class Subscription implements SubscriptionInterface { @@ -202,7 +200,7 @@ protected function buildStatement($event, $view) // We will use column name from it. $subscriptions = $view->getSubscriptions() ?? []; if (empty($subscriptions[$this->getTableName()])) { - return ''; + return ''; } $subscription = $subscriptions[$this->getTableName()]; From 4c1094ac6a1e2439b5525878f7f273b6dfab45b4 Mon Sep 17 00:00:00 2001 From: "vadim.malesh" Date: Wed, 25 Nov 2020 16:02:37 +0200 Subject: [PATCH 08/11] extracted getting column to separate method --- .../Framework/Mview/View/Subscription.php | 72 +++++++++++-------- 1 file changed, 41 insertions(+), 31 deletions(-) diff --git a/lib/internal/Magento/Framework/Mview/View/Subscription.php b/lib/internal/Magento/Framework/Mview/View/Subscription.php index cfab8b2479bc1..6cbfa8012e71b 100644 --- a/lib/internal/Magento/Framework/Mview/View/Subscription.php +++ b/lib/internal/Magento/Framework/Mview/View/Subscription.php @@ -7,8 +7,10 @@ namespace Magento\Framework\Mview\View; use Magento\Framework\App\ResourceConnection; +use Magento\Framework\DB\Adapter\AdapterInterface; use Magento\Framework\DB\Ddl\Trigger; -use Magento\Framework\Mview\View\StateInterface; +use Magento\Framework\DB\Ddl\TriggerFactory; +use Magento\Framework\Mview\ViewInterface; /** * Mview subscription. @@ -18,17 +20,17 @@ class Subscription implements SubscriptionInterface /** * Database connection * - * @var \Magento\Framework\DB\Adapter\AdapterInterface + * @var AdapterInterface */ protected $connection; /** - * @var \Magento\Framework\DB\Ddl\TriggerFactory + * @var TriggerFactory */ protected $triggerFactory; /** - * @var \Magento\Framework\Mview\View\CollectionInterface + * @var CollectionInterface */ protected $viewCollection; @@ -60,7 +62,7 @@ class Subscription implements SubscriptionInterface * * @var array */ - private $ignoredUpdateColumns = []; + private $ignoredUpdateColumns; /** * @var Resource @@ -69,18 +71,18 @@ class Subscription implements SubscriptionInterface /** * @param ResourceConnection $resource - * @param \Magento\Framework\DB\Ddl\TriggerFactory $triggerFactory - * @param \Magento\Framework\Mview\View\CollectionInterface $viewCollection - * @param \Magento\Framework\Mview\ViewInterface $view + * @param TriggerFactory $triggerFactory + * @param CollectionInterface $viewCollection + * @param ViewInterface $view * @param string $tableName * @param string $columnName * @param array $ignoredUpdateColumns */ public function __construct( ResourceConnection $resource, - \Magento\Framework\DB\Ddl\TriggerFactory $triggerFactory, - \Magento\Framework\Mview\View\CollectionInterface $viewCollection, - \Magento\Framework\Mview\ViewInterface $view, + TriggerFactory $triggerFactory, + CollectionInterface $viewCollection, + ViewInterface $view, $tableName, $columnName, $ignoredUpdateColumns = [] @@ -96,9 +98,9 @@ public function __construct( } /** - * Create subsciption + * Create subscription * - * @return \Magento\Framework\Mview\View\SubscriptionInterface + * @return SubscriptionInterface */ public function create() { @@ -115,7 +117,7 @@ public function create() // Add statements for linked views foreach ($this->getLinkedViews() as $view) { - /** @var \Magento\Framework\Mview\ViewInterface $view */ + /** @var ViewInterface $view */ $trigger->addStatement($this->buildStatement($event, $view)); } @@ -129,7 +131,7 @@ public function create() /** * Remove subscription * - * @return \Magento\Framework\Mview\View\SubscriptionInterface + * @return SubscriptionInterface */ public function remove() { @@ -144,7 +146,7 @@ public function remove() // Add statements for linked views foreach ($this->getLinkedViews() as $view) { - /** @var \Magento\Framework\Mview\ViewInterface $view */ + /** @var ViewInterface $view */ $trigger->addStatement($this->buildStatement($event, $view)); } @@ -170,13 +172,13 @@ protected function getLinkedViews() $viewList = $this->viewCollection->getViewsByStateMode(StateInterface::MODE_ENABLED); foreach ($viewList as $view) { - /** @var \Magento\Framework\Mview\ViewInterface $view */ + /** @var ViewInterface $view */ // Skip the current view if ($view->getId() == $this->getView()->getId()) { continue; } // Search in view subscriptions - foreach ($view->getSubscriptions() ?? [] as $subscription) { + foreach ($view->getSubscriptions() as $subscription) { if ($subscription['name'] != $this->getTableName()) { continue; } @@ -191,21 +193,12 @@ protected function getLinkedViews() * Build trigger statement for INSERT, UPDATE, DELETE events * * @param string $event - * @param \Magento\Framework\Mview\ViewInterface $view + * @param ViewInterface $view * @return string */ protected function buildStatement($event, $view) { - // Get the subscription for the specific view and specific table. - // We will use column name from it. - $subscriptions = $view->getSubscriptions() ?? []; - if (empty($subscriptions[$this->getTableName()])) { - return ''; - } - - $subscription = $subscriptions[$this->getTableName()]; - - // Get the changelog from View to get changelog column name. + $column = $this->getSubscriptionColumn($view); $changelog = $view->getChangelog(); switch ($event) { @@ -242,14 +235,31 @@ protected function buildStatement($event, $view) default: return ''; } + return sprintf( $trigger, $this->connection->quoteIdentifier($this->resource->getTableName($changelog->getName())), $this->connection->quoteIdentifier($changelog->getColumnName()), - $this->connection->quoteIdentifier($subscription['column']) + $this->connection->quoteIdentifier($column) ); } + /** + * Returns subscription column name by view + * + * @param ViewInterface $view + * @return string + */ + private function getSubscriptionColumn(ViewInterface $view): string + { + $subscriptions = $view->getSubscriptions(); + if (!isset($subscriptions[$this->getTableName()]['column'])) { + throw new \RuntimeException(sprintf('Column name for view with id "%s" doesn\'t exist', $view->getId())); + } + + return $subscriptions[$this->getTableName()]['column']; + } + /** * Build an "after" event for the given table and event * @@ -269,7 +279,7 @@ private function getAfterEventTriggerName($event) /** * Retrieve View related to subscription * - * @return \Magento\Framework\Mview\ViewInterface + * @return ViewInterface * @codeCoverageIgnore */ public function getView() From 4ad7aae8b66c9b18a6c67c53d202014f1086b4f5 Mon Sep 17 00:00:00 2001 From: "vadim.malesh" Date: Mon, 30 Nov 2020 10:37:48 +0200 Subject: [PATCH 09/11] fix unit test --- .../Mview/Test/Unit/View/SubscriptionTest.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/Mview/Test/Unit/View/SubscriptionTest.php b/lib/internal/Magento/Framework/Mview/Test/Unit/View/SubscriptionTest.php index c4df73a79a5d8..df7d246f538aa 100644 --- a/lib/internal/Magento/Framework/Mview/Test/Unit/View/SubscriptionTest.php +++ b/lib/internal/Magento/Framework/Mview/Test/Unit/View/SubscriptionTest.php @@ -383,6 +383,18 @@ public function testBuildStatementIgnoredColumnSubscriptionLevel(): void ->method('getViewId') ->willReturn($viewId); + $this->viewMock->expects($this->once()) + ->method('getSubscriptions') + ->willReturn( + [ + $this->tableName => ['name' => $this->tableName, 'column' => 'columnName'], + 'cataloginventory_stock_item' => ['name' => 'otherTableName', 'column' => 'columnName'] + ] + ); + $this->viewMock->expects($this->once()) + ->method('getChangeLog') + ->willReturn($otherChangelogMock); + $model = new Subscription( $this->resourceMock, $this->triggerFactoryMock, @@ -396,7 +408,7 @@ public function testBuildStatementIgnoredColumnSubscriptionLevel(): void $method = new \ReflectionMethod($model, 'buildStatement'); $method->setAccessible(true); - $statement = $method->invoke($model, Trigger::EVENT_UPDATE, $otherChangelogMock); + $statement = $method->invoke($model, Trigger::EVENT_UPDATE, $this->viewMock); $this->assertStringNotContainsString($ignoredColumnName, $statement); $this->assertStringContainsString($notIgnoredColumnName, $statement); From 8a9b7689bb75567ae92e7e9b938f9095475fae3e Mon Sep 17 00:00:00 2001 From: "vadim.malesh" Date: Mon, 30 Nov 2020 10:41:31 +0200 Subject: [PATCH 10/11] define arguments & return types --- lib/internal/Magento/Framework/Mview/View/Subscription.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/Mview/View/Subscription.php b/lib/internal/Magento/Framework/Mview/View/Subscription.php index 20ea0998afc2d..d8367bb832e82 100644 --- a/lib/internal/Magento/Framework/Mview/View/Subscription.php +++ b/lib/internal/Magento/Framework/Mview/View/Subscription.php @@ -205,7 +205,7 @@ protected function getLinkedViews() * @param ViewInterface $view * @return string */ - protected function buildStatement($event, $view) + protected function buildStatement(string $event, ViewInterface $view): string { $column = $this->getSubscriptionColumn($view); $changelog = $view->getChangelog(); From 910eb4101338d505fefc48b378082658fc6d7ddf Mon Sep 17 00:00:00 2001 From: "vadim.malesh" Date: Mon, 28 Dec 2020 13:50:49 +0200 Subject: [PATCH 11/11] fixing conflict merge; fixed unit test --- .../Mview/Test/Unit/View/SubscriptionTest.php | 32 +++++++------------ .../Framework/Mview/View/Subscription.php | 23 ++++++------- 2 files changed, 24 insertions(+), 31 deletions(-) diff --git a/lib/internal/Magento/Framework/Mview/Test/Unit/View/SubscriptionTest.php b/lib/internal/Magento/Framework/Mview/Test/Unit/View/SubscriptionTest.php index 256f8d0cb19c7..ef55c71b06281 100644 --- a/lib/internal/Magento/Framework/Mview/Test/Unit/View/SubscriptionTest.php +++ b/lib/internal/Magento/Framework/Mview/Test/Unit/View/SubscriptionTest.php @@ -207,7 +207,7 @@ public function testCreate() ->method('getColumnName') ->willReturn('entity_id'); - $this->viewMock->expects($this->exactly(3)) + $this->viewMock->expects($this->atLeastOnce()) ->method('getChangelog') ->willReturn($changelogMock); @@ -251,17 +251,15 @@ public function testCreate() 'otherTableName' => ['name' => 'otherTableName', 'column' => 'columnName'] ] ); - $otherViewMock->expects($this->exactly(3)) + $otherViewMock->expects($this->atLeastOnce()) ->method('getChangelog') ->willReturn($otherChangelogMock); $this->viewMock->expects($this->any()) ->method('getId') ->willReturn('this_id'); - $this->viewMock->expects($this->never()) - ->method('getSubscriptions'); - $this->viewCollectionMock->expects($this->exactly(1)) + $this->viewCollectionMock->expects($this->once()) ->method('getViewsByStateMode') ->with(StateInterface::MODE_ENABLED) ->willReturn([$this->viewMock, $otherViewMock]); @@ -274,8 +272,6 @@ public function testCreate() ->method('createTrigger') ->with($triggerMock); - $this->tableName = 'thisTableName'; - $this->viewMock->expects($this->exactly(3)) ->method('getSubscriptions') ->willReturn( @@ -293,7 +289,7 @@ public function testRemove() $triggerMock = $this->createMock(Trigger::class); $triggerMock->expects($this->exactly(3)) ->method('setName')->willReturnSelf(); - $triggerMock->expects($this->exactly(3)) + $triggerMock->expects($this->any()) ->method('getName') ->willReturn('triggerName'); $triggerMock->expects($this->exactly(3)) @@ -320,7 +316,7 @@ public function testRemove() true, [] ); - $otherChangelogMock->expects($this->exactly(3)) + $otherChangelogMock->expects($this->any()) ->method('getName') ->willReturn('other_test_view_cl'); $otherChangelogMock->expects($this->exactly(3)) @@ -336,17 +332,17 @@ public function testRemove() true, [] ); - - $otherViewMock->expects($this->exactly(1)) + $otherViewMock->expects($this->atLeastOnce()) ->method('getId') ->willReturn('other_id'); - - $otherViewMock->expects($this->exactly(3)) + $otherViewMock->expects($this->atLeastOnce()) ->method('getChangelog') ->willReturn($otherChangelogMock); - $this->viewMock->expects($this->any()) - $otherViewMock->expects($this->any()) + $this->viewMock->expects($this->atLeastOnce()) + ->method('getId') + ->willReturn('this_id'); + $otherViewMock->expects($this->atLeastOnce()) ->method('getSubscriptions') ->willReturn( [ @@ -355,10 +351,6 @@ public function testRemove() ] ); - $this->viewMock->expects($this->exactly(3)) - ->method('getId') - ->willReturn('this_id'); - $this->viewCollectionMock->expects($this->exactly(1)) ->method('getViewsByStateMode') ->with(StateInterface::MODE_ENABLED) @@ -443,7 +435,7 @@ public function testBuildStatementIgnoredColumnSubscriptionLevel(): void 'cataloginventory_stock_item' => ['name' => 'otherTableName', 'column' => 'columnName'] ] ); - $this->viewMock->expects($this->once()) + $this->viewMock->expects($this->atLeastOnce()) ->method('getChangeLog') ->willReturn($otherChangelogMock); diff --git a/lib/internal/Magento/Framework/Mview/View/Subscription.php b/lib/internal/Magento/Framework/Mview/View/Subscription.php index cad9bc6675c1e..03a3bf9615ced 100644 --- a/lib/internal/Magento/Framework/Mview/View/Subscription.php +++ b/lib/internal/Magento/Framework/Mview/View/Subscription.php @@ -10,9 +10,8 @@ use Magento\Framework\App\ResourceConnection; use Magento\Framework\DB\Adapter\AdapterInterface; use Magento\Framework\DB\Ddl\Trigger; -use Magento\Framework\Mview\Config; -use Magento\Framework\Mview\View\StateInterface; use Magento\Framework\DB\Ddl\TriggerFactory; +use Magento\Framework\Mview\Config; use Magento\Framework\Mview\ViewInterface; /** @@ -212,13 +211,14 @@ protected function getLinkedViews() /** * Prepare columns for trigger statement. Should be protected in order to serve new approach * - * @param ChangelogInterface $changelog + * @param ViewInterface $view * @param string $event * @return array * @throws \Exception */ - protected function prepareColumns(ChangelogInterface $changelog, string $event): array + protected function prepareColumns(ViewInterface $view, string $event): array { + $changelog = $view->getChangelog(); $prefix = $event === Trigger::EVENT_DELETE ? 'OLD.' : 'NEW.'; $subscriptionData = $this->mviewConfig->getView($changelog->getViewId())['subscriptions'][$this->getTableName()]; $columns = [ @@ -226,7 +226,7 @@ protected function prepareColumns(ChangelogInterface $changelog, string $event): 'entity_id' => $this->connection->quoteIdentifier($changelog->getColumnName()) ], 'column_values' => [ - 'entity_id' => $this->getEntityColumn($prefix) + 'entity_id' => $this->getEntityColumn($prefix, $view) ] ]; @@ -251,7 +251,6 @@ protected function prepareColumns(ChangelogInterface $changelog, string $event): protected function buildStatement(string $event, ViewInterface $view): string { $trigger = "%sINSERT IGNORE INTO %s (%s) VALUES (%s);"; - $column = $this->getSubscriptionColumn($view); $changelog = $view->getChangelog(); switch ($event) { @@ -286,13 +285,14 @@ protected function buildStatement(string $event, ViewInterface $view): string } break; } - $columns = $this->prepareColumns($changelog, $event); + $columns = $this->prepareColumns($view, $event); + return sprintf( $trigger, $this->getProcessor()->getPreStatements(), $this->connection->quoteIdentifier($this->resource->getTableName($changelog->getName())), - implode(", " , $columns['column_names']), - implode(", ", $columns['column_values']) + implode(', ', $columns['column_names']), + implode(', ', $columns['column_values']) ); } @@ -319,11 +319,12 @@ private function getProcessor(): AdditionalColumnProcessorInterface /** * @param string $prefix + * @param ViewInterface $view * @return string */ - public function getEntityColumn(string $prefix): string + public function getEntityColumn(string $prefix, ViewInterface $view): string { - return $prefix . $this->connection->quoteIdentifier($this->getColumnName()); + return $prefix . $this->connection->quoteIdentifier($this->getSubscriptionColumn($view)); } /**