From 036ba4a3273b51891223ec397c9ca7828cba36fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gytis=20Kar=C4=8Diauskas?= Date: Mon, 19 Jan 2015 10:53:34 +0200 Subject: [PATCH 1/3] Fix multiple shop id functionality. --- Resources/config/extractor.yml | 1 + Sync/Extractor/AbstractExtractor.php | 58 ++ Sync/Extractor/DoctrineExtractor.php | 13 +- Sync/StorageManager/MysqlStorageManager.php | 11 +- .../Command/SyncProvideCommandTest.php | 554 +++++++++--------- Tests/app/config/parameters_test.yml | 5 + 6 files changed, 368 insertions(+), 274 deletions(-) create mode 100644 Sync/Extractor/AbstractExtractor.php diff --git a/Resources/config/extractor.yml b/Resources/config/extractor.yml index f1c0ed2..dc73efc 100644 --- a/Resources/config/extractor.yml +++ b/Resources/config/extractor.yml @@ -9,6 +9,7 @@ services: - [ setRelationsCollection, [ @ongr_connections.sync.relations_collection ] ] - [ setConnection, [ @database_connection ] ] - [ setStorageFacility, [ @ongr_connections.sync.sync_storage ] ] + - [ setContainer, [ @service_container ]] ongr_connections.sync.extractor.passthrough_extractor: class: %ongr_connections.extractor.passthrough_extractor.class% diff --git a/Sync/Extractor/AbstractExtractor.php b/Sync/Extractor/AbstractExtractor.php new file mode 100644 index 0000000..8a09187 --- /dev/null +++ b/Sync/Extractor/AbstractExtractor.php @@ -0,0 +1,58 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ONGR\ConnectionsBundle\Sync\Extractor; + +use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; + +/** + * Common actions required for all extractors. + */ +class AbstractExtractor +{ + /** + * @var ContainerInterface + */ + private $container; + + /** + * @param ContainerInterface $container + */ + public function setContainer(ContainerInterface $container) + { + $this->container = $container; + } + + /** + * Gets ids of shops from the configuration. + * + * @return array + */ + protected function getShopIds() + { + try { + $shops = $this->getContainer()->getParameter('shop_ids'); + } catch (InvalidArgumentException $e) { + $shops = []; + } + + return $shops; + } + + /** + * @return ContainerInterface + */ + private function getContainer() + { + return $this->container; + } +} diff --git a/Sync/Extractor/DoctrineExtractor.php b/Sync/Extractor/DoctrineExtractor.php index 805af89..4c4dd28 100644 --- a/Sync/Extractor/DoctrineExtractor.php +++ b/Sync/Extractor/DoctrineExtractor.php @@ -27,7 +27,7 @@ /** * Extractor that joins entities for insertion to SyncStorage. */ -class DoctrineExtractor implements ExtractorInterface +class DoctrineExtractor extends AbstractExtractor implements ExtractorInterface { /** * @var SyncStorageInterface @@ -69,7 +69,13 @@ public function extract(BaseDiffItem $item) $itemId = $itemRow[$idFieldName]; $storage = $this->getStorageFacility(); - $storage->save($action, $insertList[JobTableFields::TYPE]['value'], $itemId, $item->getTimestamp()); + $storage->save( + $action, + $insertList[JobTableFields::TYPE]['value'], + $itemId, + $item->getTimestamp(), + $this->getShopIds() + ); $statements = $relation->getStatements(); foreach ($statements as $statement) { @@ -201,7 +207,8 @@ protected function saveResult(BaseDiffItem $item, Statement $results, $action = $action, $row[JobTableFields::TYPE], $row[JobTableFields::ID], - $item->getTimestamp() + $item->getTimestamp(), + $this->getShopIds() ); } } diff --git a/Sync/StorageManager/MysqlStorageManager.php b/Sync/StorageManager/MysqlStorageManager.php index a129186..586def0 100644 --- a/Sync/StorageManager/MysqlStorageManager.php +++ b/Sync/StorageManager/MysqlStorageManager.php @@ -30,12 +30,13 @@ public function createStorage($shopId = null, $connection = null) { $connection = $connection ? : $this->getConnection(); $schemaManager = $connection->getSchemaManager(); + $tableName = $this->getTableName($shopId); - if ($schemaManager->tablesExist([$this->getTableName($shopId)])) { + if ($schemaManager->tablesExist([$tableName])) { return true; } - $table = new Table($this->getTableName($shopId)); + $table = new Table($tableName); $this->buildTable($table); $schemaManager->createTable($table); @@ -88,12 +89,11 @@ public function getTableName($shopId = null) throw new InvalidArgumentException("Invalid table name specified: \"$tableName\""); } - $suffix = null; if ($shopId !== null) { - $suffix = '_' . $shopId; + $tableName .= '_' . $shopId; } - return $tableName . $suffix; + return $tableName; } /** @@ -104,6 +104,7 @@ public function addRecord($operationType, $documentType, $documentId, DateTime $ if (empty($shopIds)) { $shopIds = [null]; } + $connection = $this->getConnection(); foreach ($shopIds as $shopId) { diff --git a/Tests/Functional/Command/SyncProvideCommandTest.php b/Tests/Functional/Command/SyncProvideCommandTest.php index 4244597..9f99ca2 100644 --- a/Tests/Functional/Command/SyncProvideCommandTest.php +++ b/Tests/Functional/Command/SyncProvideCommandTest.php @@ -30,6 +30,11 @@ class SyncProvideCommandTest extends TestBase */ private $managerMysql; + /** + * @var array + */ + private $shopIds; + /** * Clear logs before each test. */ @@ -49,7 +54,12 @@ public function setUp() $this->managerMysql = $this ->getServiceContainer() ->get('ongr_connections.sync.storage_manager.mysql_storage_manager'); - $this->managerMysql->createStorage(); + + $this->shopIds = static::$kernel->getContainer()->getParameter('shop_ids'); + + foreach ($this->shopIds as $shopId) { + $this->managerMysql->createStorage($shopId); + } } /** @@ -62,76 +72,78 @@ public function testExecuteWithoutTimeDifference() $this->importData('ExtractorTest/sample_db_nodelay.sql'); - $expectedData = [ - [ - 'type' => ActionTypes::UPDATE, - 'document_type' => 'category', - 'document_id' => 'cat0', - 'status' => '0', - 'shop_id' => null, - ], - [ - 'type' => ActionTypes::UPDATE, - 'document_type' => 'product', - 'document_id' => 'art0', - 'status' => '0', - 'shop_id' => null, - ], - [ - 'type' => ActionTypes::UPDATE, - 'document_type' => 'product', - 'document_id' => 'art1', - 'status' => '0', - 'shop_id' => null, - ], - [ - 'type' => ActionTypes::CREATE, - 'document_type' => 'product', - 'document_id' => 'art0', - 'status' => '0', - 'shop_id' => null, - ], - [ - 'type' => ActionTypes::CREATE, - 'document_type' => 'product', - 'document_id' => 'art1', - 'status' => '0', - 'shop_id' => null, - ], - [ - 'type' => ActionTypes::CREATE, - 'document_type' => 'product', - 'document_id' => 'art2', - 'status' => '0', - 'shop_id' => null, - ], - [ - 'type' => ActionTypes::UPDATE, - 'document_type' => 'product', - 'document_id' => 'art2', - 'status' => '0', - 'shop_id' => null, - ], - [ - 'type' => ActionTypes::DELETE, - 'document_type' => 'product', - 'document_id' => 'art1', - 'status' => '0', - 'shop_id' => null, - ], - ]; - $commandTester = $this->executeCommand(static::$kernel); - // Ensure that there is no time difference between records (even though there might be). - $this - ->managerMysql - ->getConnection() - ->executeQuery("update {$this->managerMysql->getTableName()} set timestamp=NOW()"); + foreach ($this->shopIds as $shopId) { + $expectedData = [ + [ + 'type' => ActionTypes::UPDATE, + 'document_type' => 'category', + 'document_id' => 'cat0', + 'status' => '0', + 'shop_id' => $shopId, + ], + [ + 'type' => ActionTypes::UPDATE, + 'document_type' => 'product', + 'document_id' => 'art0', + 'status' => '0', + 'shop_id' => $shopId, + ], + [ + 'type' => ActionTypes::UPDATE, + 'document_type' => 'product', + 'document_id' => 'art1', + 'status' => '0', + 'shop_id' => $shopId, + ], + [ + 'type' => ActionTypes::CREATE, + 'document_type' => 'product', + 'document_id' => 'art0', + 'status' => '0', + 'shop_id' => $shopId, + ], + [ + 'type' => ActionTypes::CREATE, + 'document_type' => 'product', + 'document_id' => 'art1', + 'status' => '0', + 'shop_id' => $shopId, + ], + [ + 'type' => ActionTypes::CREATE, + 'document_type' => 'product', + 'document_id' => 'art2', + 'status' => '0', + 'shop_id' => $shopId, + ], + [ + 'type' => ActionTypes::UPDATE, + 'document_type' => 'product', + 'document_id' => 'art2', + 'status' => '0', + 'shop_id' => $shopId, + ], + [ + 'type' => ActionTypes::DELETE, + 'document_type' => 'product', + 'document_id' => 'art1', + 'status' => '0', + 'shop_id' => $shopId, + ], + ]; + + // Ensure that there is no time difference between records (even though there might be). + $this + ->managerMysql + ->getConnection() + ->executeQuery("update {$this->managerMysql->getTableName($shopId)} set timestamp=NOW()"); - $storageData = $this->getSyncData(count($expectedData)); + $storageData = $this->getSyncData(count($expectedData), 0, $shopId); - $this->assertEquals($expectedData, $storageData); + $this->assertEquals($expectedData, $storageData); + } $output = $commandTester->getDisplay(); $this->assertContains('Job finished', $output); @@ -147,70 +159,72 @@ public function testExecuteWithTimeDifference() $this->importData('ExtractorTest/sample_db.sql'); - $expectedData = [ - [ - 'type' => ActionTypes::UPDATE, - 'document_type' => 'category', - 'document_id' => 'cat0', - 'status' => '0', - 'shop_id' => null, - ], - [ - 'type' => ActionTypes::CREATE, - 'document_type' => 'product', - 'document_id' => 'art0', - 'status' => '0', - 'shop_id' => null, - ], - [ - 'type' => ActionTypes::CREATE, - 'document_type' => 'product', - 'document_id' => 'art1', - 'status' => '0', - 'shop_id' => null, - ], - [ - 'type' => ActionTypes::CREATE, - 'document_type' => 'product', - 'document_id' => 'art2', - 'status' => '0', - 'shop_id' => null, - ], - [ - 'type' => ActionTypes::UPDATE, - 'document_type' => 'product', - 'document_id' => 'art0', - 'status' => '0', - 'shop_id' => null, - ], - [ - 'type' => ActionTypes::UPDATE, - 'document_type' => 'product', - 'document_id' => 'art1', - 'status' => '0', - 'shop_id' => null, - ], - [ - 'type' => ActionTypes::UPDATE, - 'document_type' => 'product', - 'document_id' => 'art2', - 'status' => '0', - 'shop_id' => null, - ], - [ - 'type' => ActionTypes::DELETE, - 'document_type' => 'product', - 'document_id' => 'art1', - 'status' => '0', - 'shop_id' => null, - ], - ]; - $commandTester = $this->executeCommand(static::$kernel); - $storageData = $this->getSyncData(count($expectedData)); - - $this->assertEquals($expectedData, $storageData); + foreach ($this->shopIds as $shopId) { + $expectedData = [ + [ + 'type' => ActionTypes::UPDATE, + 'document_type' => 'category', + 'document_id' => 'cat0', + 'status' => '0', + 'shop_id' => $shopId, + ], + [ + 'type' => ActionTypes::CREATE, + 'document_type' => 'product', + 'document_id' => 'art0', + 'status' => '0', + 'shop_id' => $shopId, + ], + [ + 'type' => ActionTypes::CREATE, + 'document_type' => 'product', + 'document_id' => 'art1', + 'status' => '0', + 'shop_id' => $shopId, + ], + [ + 'type' => ActionTypes::CREATE, + 'document_type' => 'product', + 'document_id' => 'art2', + 'status' => '0', + 'shop_id' => $shopId, + ], + [ + 'type' => ActionTypes::UPDATE, + 'document_type' => 'product', + 'document_id' => 'art0', + 'status' => '0', + 'shop_id' => $shopId, + ], + [ + 'type' => ActionTypes::UPDATE, + 'document_type' => 'product', + 'document_id' => 'art1', + 'status' => '0', + 'shop_id' => $shopId, + ], + [ + 'type' => ActionTypes::UPDATE, + 'document_type' => 'product', + 'document_id' => 'art2', + 'status' => '0', + 'shop_id' => $shopId, + ], + [ + 'type' => ActionTypes::DELETE, + 'document_type' => 'product', + 'document_id' => 'art1', + 'status' => '0', + 'shop_id' => $shopId, + ], + ]; + + $storageData = $this->getSyncData(count($expectedData), 0, $shopId); + + $this->assertEquals($expectedData, $storageData); + } $output = $commandTester->getDisplay(); $this->assertContains('Job finished', $output); @@ -230,70 +244,72 @@ public function testExecuteSkipDataByLastSyncDate() // Transactions which should be in changes log. $this->importData('ExtractorTest/sample_db_to_use.sql'); - $expectedData = [ - [ - 'type' => ActionTypes::UPDATE, - 'document_type' => 'category', - 'document_id' => 'cat0', - 'status' => '0', - 'shop_id' => null, - ], - [ - 'type' => ActionTypes::CREATE, - 'document_type' => 'product', - 'document_id' => 'art0', - 'status' => '0', - 'shop_id' => null, - ], - [ - 'type' => ActionTypes::CREATE, - 'document_type' => 'product', - 'document_id' => 'art1', - 'status' => '0', - 'shop_id' => null, - ], - [ - 'type' => ActionTypes::CREATE, - 'document_type' => 'product', - 'document_id' => 'art2', - 'status' => '0', - 'shop_id' => null, - ], - [ - 'type' => ActionTypes::UPDATE, - 'document_type' => 'product', - 'document_id' => 'art0', - 'status' => '0', - 'shop_id' => null, - ], - [ - 'type' => ActionTypes::UPDATE, - 'document_type' => 'product', - 'document_id' => 'art1', - 'status' => '0', - 'shop_id' => null, - ], - [ - 'type' => ActionTypes::UPDATE, - 'document_type' => 'product', - 'document_id' => 'art2', - 'status' => '0', - 'shop_id' => null, - ], - [ - 'type' => ActionTypes::DELETE, - 'document_type' => 'product', - 'document_id' => 'art1', - 'status' => '0', - 'shop_id' => null, - ], - ]; - $commandTester = $this->executeCommand(static::$kernel); - $storageData = $this->getSyncData(count($expectedData)); - - $this->assertEquals($expectedData, $storageData); + foreach ($this->shopIds as $shopId) { + $expectedData = [ + [ + 'type' => ActionTypes::UPDATE, + 'document_type' => 'category', + 'document_id' => 'cat0', + 'status' => '0', + 'shop_id' => $shopId, + ], + [ + 'type' => ActionTypes::CREATE, + 'document_type' => 'product', + 'document_id' => 'art0', + 'status' => '0', + 'shop_id' => $shopId, + ], + [ + 'type' => ActionTypes::CREATE, + 'document_type' => 'product', + 'document_id' => 'art1', + 'status' => '0', + 'shop_id' => $shopId, + ], + [ + 'type' => ActionTypes::CREATE, + 'document_type' => 'product', + 'document_id' => 'art2', + 'status' => '0', + 'shop_id' => $shopId, + ], + [ + 'type' => ActionTypes::UPDATE, + 'document_type' => 'product', + 'document_id' => 'art0', + 'status' => '0', + 'shop_id' => $shopId, + ], + [ + 'type' => ActionTypes::UPDATE, + 'document_type' => 'product', + 'document_id' => 'art1', + 'status' => '0', + 'shop_id' => $shopId, + ], + [ + 'type' => ActionTypes::UPDATE, + 'document_type' => 'product', + 'document_id' => 'art2', + 'status' => '0', + 'shop_id' => $shopId, + ], + [ + 'type' => ActionTypes::DELETE, + 'document_type' => 'product', + 'document_id' => 'art1', + 'status' => '0', + 'shop_id' => $shopId, + ], + ]; + + $storageData = $this->getSyncData(count($expectedData), 0, $shopId); + + $this->assertEquals($expectedData, $storageData); + } $output = $commandTester->getDisplay(); $this->assertContains('Job finished', $output); @@ -323,16 +339,19 @@ public function testExecuteSkipDataByLastSyncPosition() ->get(BinlogDiffProvider::LAST_SYNC_POSITION_PARAM); $this->assertGreaterThan(0, $last_sync_position_1); - // Delete data from sync storage, to test if only data from last sync position is processed. - $storageData = $this - ->getServiceContainer() - ->get('ongr_connections.sync.sync_storage') - ->getChunk(8); - foreach ($storageData as $storageDataItem) { - $this + foreach ($this->shopIds as $shopId) { + // Delete data from sync storage, to test if only data from last sync position is processed. + $storageData = $this ->getServiceContainer() ->get('ongr_connections.sync.sync_storage') - ->deleteItem($storageDataItem['id']); + ->getChunk(8, null, $shopId); + + foreach ($storageData as $storageDataItem) { + $this + ->getServiceContainer() + ->get('ongr_connections.sync.sync_storage') + ->deleteItem($storageDataItem['id']); + } } // Execute transactions which should be in changes log. @@ -356,68 +375,70 @@ public function testExecuteSkipDataByLastSyncPosition() $this->assertGreaterThan($last_sync_position_1, $last_sync_position_2); - $expectedData = [ - [ - 'type' => ActionTypes::UPDATE, - 'document_type' => 'category', - 'document_id' => 'cat0', - 'status' => '0', - 'shop_id' => null, - ], - [ - 'type' => ActionTypes::CREATE, - 'document_type' => 'product', - 'document_id' => 'art0', - 'status' => '0', - 'shop_id' => null, - ], - [ - 'type' => ActionTypes::CREATE, - 'document_type' => 'product', - 'document_id' => 'art1', - 'status' => '0', - 'shop_id' => null, - ], - [ - 'type' => ActionTypes::CREATE, - 'document_type' => 'product', - 'document_id' => 'art2', - 'status' => '0', - 'shop_id' => null, - ], - [ - 'type' => ActionTypes::UPDATE, - 'document_type' => 'product', - 'document_id' => 'art0', - 'status' => '0', - 'shop_id' => null, - ], - [ - 'type' => ActionTypes::UPDATE, - 'document_type' => 'product', - 'document_id' => 'art1', - 'status' => '0', - 'shop_id' => null, - ], - [ - 'type' => ActionTypes::UPDATE, - 'document_type' => 'product', - 'document_id' => 'art2', - 'status' => '0', - 'shop_id' => null, - ], - [ - 'type' => ActionTypes::DELETE, - 'document_type' => 'product', - 'document_id' => 'art1', - 'status' => '0', - 'shop_id' => null, - ], - ]; - - $storageData = $this->getSyncData(count($expectedData)); - - $this->assertEquals($expectedData, $storageData); + foreach ($this->shopIds as $shopId) { + $expectedData = [ + [ + 'type' => ActionTypes::UPDATE, + 'document_type' => 'category', + 'document_id' => 'cat0', + 'status' => '0', + 'shop_id' => $shopId, + ], + [ + 'type' => ActionTypes::CREATE, + 'document_type' => 'product', + 'document_id' => 'art0', + 'status' => '0', + 'shop_id' => $shopId, + ], + [ + 'type' => ActionTypes::CREATE, + 'document_type' => 'product', + 'document_id' => 'art1', + 'status' => '0', + 'shop_id' => $shopId, + ], + [ + 'type' => ActionTypes::CREATE, + 'document_type' => 'product', + 'document_id' => 'art2', + 'status' => '0', + 'shop_id' => $shopId, + ], + [ + 'type' => ActionTypes::UPDATE, + 'document_type' => 'product', + 'document_id' => 'art0', + 'status' => '0', + 'shop_id' => $shopId, + ], + [ + 'type' => ActionTypes::UPDATE, + 'document_type' => 'product', + 'document_id' => 'art1', + 'status' => '0', + 'shop_id' => $shopId, + ], + [ + 'type' => ActionTypes::UPDATE, + 'document_type' => 'product', + 'document_id' => 'art2', + 'status' => '0', + 'shop_id' => $shopId, + ], + [ + 'type' => ActionTypes::DELETE, + 'document_type' => 'product', + 'document_id' => 'art1', + 'status' => '0', + 'shop_id' => $shopId, + ], + ]; + + $storageData = $this->getSyncData(count($expectedData), 0, $shopId); + + $this->assertEquals($expectedData, $storageData); + } $output = $commandTester->getDisplay(); @@ -480,16 +501,17 @@ private function executeCommand($kernel) /** * Gets data from Sync storage. * - * @param int $count - * @param int $skip + * @param int $count + * @param int $skip + * @param int|null $shopId * * @return array */ - private function getSyncData($count, $skip = 0) + private function getSyncData($count, $skip = 0, $shopId = null) { $syncStorage = $this->getServiceContainer()->get('ongr_connections.sync.sync_storage'); - $syncStorage->getChunk($skip); - $storageData = $syncStorage->getChunk($count); + $syncStorage->getChunk($skip, null, $shopId); + $storageData = $syncStorage->getChunk($count, null, $shopId); // Remove `id` and `timestamp` from result array. array_filter( diff --git a/Tests/app/config/parameters_test.yml b/Tests/app/config/parameters_test.yml index 91e8768..86e6ebe 100644 --- a/Tests/app/config/parameters_test.yml +++ b/Tests/app/config/parameters_test.yml @@ -16,3 +16,8 @@ parameters: test.sync.execute.elastic_document_type: AcmeTestBundle:Product test.sync.execute.doctrine_entity_type: Test:TestProduct test.sync.execute.sync_storage_document_type: product + + shop_ids: + - 1 + - 2 + - 3 From b672bb59d3525e4e95c7721e943a00ca931a8cf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gytis=20Kar=C4=8Diauskas?= Date: Mon, 19 Jan 2015 15:47:12 +0200 Subject: [PATCH 2/3] Fix PassthroughExtractor for multi shop. --- Resources/config/extractor.yml | 6 +- Sync/Extractor/AbstractExtractor.php | 14 +- Sync/Extractor/PassthroughExtractor.php | 11 +- .../Command/SyncProvideCommandTest.php | 2 +- .../Extractor/PassthroughExtractorTest.php | 140 ++++++++++-------- .../Extractor/PassthroughExtractorTest.php | 5 + 6 files changed, 98 insertions(+), 80 deletions(-) diff --git a/Resources/config/extractor.yml b/Resources/config/extractor.yml index dc73efc..3b84e00 100644 --- a/Resources/config/extractor.yml +++ b/Resources/config/extractor.yml @@ -1,20 +1,22 @@ parameters: + ongr_connections.extractor.doctrine_extractor.class: ONGR\ConnectionsBundle\Sync\Extractor\DoctrineExtractor ongr_connections.extractor.passthrough_extractor.class: ONGR\ConnectionsBundle\Sync\Extractor\PassthroughExtractor ongr_connections.sync.relations_collection.class: ONGR\ConnectionsBundle\Sync\Extractor\Relation\RelationsCollection services: ongr_connections.sync.extractor.doctrine_extractor: - class: ONGR\ConnectionsBundle\Sync\Extractor\DoctrineExtractor + class: %ongr_connections.extractor.doctrine_extractor.class% calls: - [ setRelationsCollection, [ @ongr_connections.sync.relations_collection ] ] - [ setConnection, [ @database_connection ] ] - [ setStorageFacility, [ @ongr_connections.sync.sync_storage ] ] - - [ setContainer, [ @service_container ]] + - [ setContainer, [ @service_container ] ] ongr_connections.sync.extractor.passthrough_extractor: class: %ongr_connections.extractor.passthrough_extractor.class% calls: - [ setStorageFacility, [ @ongr_connections.sync.sync_storage ] ] + - [ setContainer, [ @service_container ] ] ongr_connections.sync.relations_collection: class: %ongr_connections.sync.relations_collection.class% diff --git a/Sync/Extractor/AbstractExtractor.php b/Sync/Extractor/AbstractExtractor.php index 8a09187..08b50be 100644 --- a/Sync/Extractor/AbstractExtractor.php +++ b/Sync/Extractor/AbstractExtractor.php @@ -17,7 +17,7 @@ /** * Common actions required for all extractors. */ -class AbstractExtractor +abstract class AbstractExtractor { /** * @var ContainerInterface @@ -27,7 +27,7 @@ class AbstractExtractor /** * @param ContainerInterface $container */ - public function setContainer(ContainerInterface $container) + public function setContainer(ContainerInterface $container = null) { $this->container = $container; } @@ -40,19 +40,11 @@ public function setContainer(ContainerInterface $container) protected function getShopIds() { try { - $shops = $this->getContainer()->getParameter('shop_ids'); + $shops = $this->container->getParameter('shop_ids'); } catch (InvalidArgumentException $e) { $shops = []; } return $shops; } - - /** - * @return ContainerInterface - */ - private function getContainer() - { - return $this->container; - } } diff --git a/Sync/Extractor/PassthroughExtractor.php b/Sync/Extractor/PassthroughExtractor.php index 341717b..70df492 100644 --- a/Sync/Extractor/PassthroughExtractor.php +++ b/Sync/Extractor/PassthroughExtractor.php @@ -22,7 +22,7 @@ /** * Very simple data extractor for data synchronization. */ -class PassthroughExtractor implements ExtractorInterface +class PassthroughExtractor extends AbstractExtractor implements ExtractorInterface { /** * @var SyncStorageInterface @@ -44,7 +44,8 @@ public function extract(BaseDiffItem $item) ActionTypes::CREATE, $item->getCategory(), $itemId, - $item->getTimestamp() + $item->getTimestamp(), + $this->getShopIds() ); } if ($item instanceof UpdateDiffItem) { @@ -52,7 +53,8 @@ public function extract(BaseDiffItem $item) ActionTypes::UPDATE, $item->getCategory(), $itemId, - $item->getTimestamp() + $item->getTimestamp(), + $this->getShopIds() ); } if ($item instanceof DeleteDiffItem) { @@ -60,7 +62,8 @@ public function extract(BaseDiffItem $item) ActionTypes::DELETE, $item->getCategory(), $itemId, - $item->getTimestamp() + $item->getTimestamp(), + $this->getShopIds() ); } } diff --git a/Tests/Functional/Command/SyncProvideCommandTest.php b/Tests/Functional/Command/SyncProvideCommandTest.php index 9f99ca2..4dc7372 100644 --- a/Tests/Functional/Command/SyncProvideCommandTest.php +++ b/Tests/Functional/Command/SyncProvideCommandTest.php @@ -55,7 +55,7 @@ public function setUp() ->getServiceContainer() ->get('ongr_connections.sync.storage_manager.mysql_storage_manager'); - $this->shopIds = static::$kernel->getContainer()->getParameter('shop_ids'); + $this->shopIds = $this->getServiceContainer()->getParameter('shop_ids'); foreach ($this->shopIds as $shopId) { $this->managerMysql->createStorage($shopId); diff --git a/Tests/Functional/Sync/Extractor/PassthroughExtractorTest.php b/Tests/Functional/Sync/Extractor/PassthroughExtractorTest.php index 97915a4..5d8897c 100644 --- a/Tests/Functional/Sync/Extractor/PassthroughExtractorTest.php +++ b/Tests/Functional/Sync/Extractor/PassthroughExtractorTest.php @@ -40,6 +40,11 @@ class PassthroughExtractorTest extends TestBase */ private $storageManager; + /** + * @var array + */ + private $shopIds; + /** * Setup services for tests. */ @@ -49,8 +54,16 @@ protected function setUp() $this->storageManager = new MysqlStorageManager($this->getConnection(), self::TABLE_NAME); $this->syncStorage = new SyncStorage($this->storageManager); + $this->extractor = new PassthroughExtractor(); + $this->extractor->setContainer($this->getServiceContainer()); $this->extractor->setStorageFacility($this->syncStorage); + + $this->shopIds = $this->getServiceContainer()->getParameter('shop_ids'); + + foreach ($this->shopIds as $shopId) { + $this->storageManager->createStorage($shopId); + } } /** @@ -62,8 +75,6 @@ public function testExtractForCreateItem() $id = 123; $timestamp = new DateTime('-1 hour 20 minutes'); - $this->storageManager->createStorage(); - $createDiffItem = new CreateDiffItem(); $createDiffItem->setCategory($category); $createDiffItem->setItemId($id); @@ -71,37 +82,38 @@ public function testExtractForCreateItem() $this->extractor->extract($createDiffItem); - $actual = (object)$this->getConnection()->fetchAssoc( - 'SELECT * FROM ' . self::TABLE_NAME . ' WHERE - `type` = :operationType - AND `document_type` = :documentType - AND `document_id` = :documentId - AND `status` = :status', - [ - 'operationType' => ActionTypes::CREATE, - 'documentType' => $category, - 'documentId' => $id, - 'status' => 0, - ] - ); - $this->assertTrue(!empty($actual->id)); - $this->assertEquals(ActionTypes::CREATE, $actual->type); - $this->assertEquals($category, $actual->document_type); - $this->assertEquals($id, $actual->document_id); - $this->assertEquals($timestamp, new DateTime($actual->timestamp)); + foreach ($this->shopIds as $shopId) { + $actual = (object)$this->getConnection()->fetchAssoc( + 'SELECT * FROM ' . $this->storageManager->getTableName($shopId) . ' WHERE + `type` = :operationType + AND `document_type` = :documentType + AND `document_id` = :documentId + AND `status` = :status', + [ + 'operationType' => ActionTypes::CREATE, + 'documentType' => $category, + 'documentId' => $id, + 'status' => 0, + ] + ); + + $this->assertTrue(!empty($actual->id)); + $this->assertEquals(ActionTypes::CREATE, $actual->type); + $this->assertEquals($category, $actual->document_type); + $this->assertEquals($id, $actual->document_id); + $this->assertEquals($timestamp, new DateTime($actual->timestamp)); + } } /** * Test if extract is able to add data to the storage for item update action. */ - public function testExtractForUpdateItem() + public function ttestExtractForUpdateItem() { $category = 'product'; $id = 123; $timestamp = new DateTime('-1 hour 20 minutes'); - $this->storageManager->createStorage(); - $updateDiffItem = new UpdateDiffItem(); $updateDiffItem->setCategory($category); $updateDiffItem->setItemId($id); @@ -109,37 +121,38 @@ public function testExtractForUpdateItem() $this->extractor->extract($updateDiffItem); - $actual = (object)$this->getConnection()->fetchAssoc( - 'SELECT * FROM ' . self::TABLE_NAME . ' WHERE - `type` = :operationType - AND `document_type` = :documentType - AND `document_id` = :documentId - AND `status` = :status', - [ - 'operationType' => ActionTypes::UPDATE, - 'documentType' => $category, - 'documentId' => $id, - 'status' => 0, - ] - ); - $this->assertTrue(!empty($actual->id)); - $this->assertEquals(ActionTypes::UPDATE, $actual->type); - $this->assertEquals($category, $actual->document_type); - $this->assertEquals($id, $actual->document_id); - $this->assertEquals($timestamp, new DateTime($actual->timestamp)); + foreach ($this->shopIds as $shopId) { + $actual = (object)$this->getConnection()->fetchAssoc( + 'SELECT * FROM ' . $this->storageManager->getTableName($shopId) . ' WHERE + `type` = :operationType + AND `document_type` = :documentType + AND `document_id` = :documentId + AND `status` = :status', + [ + 'operationType' => ActionTypes::UPDATE, + 'documentType' => $category, + 'documentId' => $id, + 'status' => 0, + ] + ); + + $this->assertTrue(!empty($actual->id)); + $this->assertEquals(ActionTypes::UPDATE, $actual->type); + $this->assertEquals($category, $actual->document_type); + $this->assertEquals($id, $actual->document_id); + $this->assertEquals($timestamp, new DateTime($actual->timestamp)); + } } /** * Test if extract is able to add data to the storage for item delete action. */ - public function testExtractForDeleteItem() + public function ttestExtractForDeleteItem() { $category = 'product'; $id = 123; $timestamp = new DateTime('-1 hour 20 minutes'); - $this->storageManager->createStorage(); - $deleteDiffItem = new DeleteDiffItem(); $deleteDiffItem->setCategory($category); $deleteDiffItem->setItemId($id); @@ -147,23 +160,26 @@ public function testExtractForDeleteItem() $this->extractor->extract($deleteDiffItem); - $actual = (object)$this->getConnection()->fetchAssoc( - 'SELECT * FROM ' . self::TABLE_NAME . ' WHERE - `type` = :operationType - AND `document_type` = :documentType - AND `document_id` = :documentId - AND `status` = :status', - [ - 'operationType' => ActionTypes::DELETE, - 'documentType' => $category, - 'documentId' => $id, - 'status' => 0, - ] - ); - $this->assertTrue(!empty($actual->id)); - $this->assertEquals(ActionTypes::DELETE, $actual->type); - $this->assertEquals($category, $actual->document_type); - $this->assertEquals($id, $actual->document_id); - $this->assertEquals($timestamp, new DateTime($actual->timestamp)); + foreach ($this->shopIds as $shopId) { + $actual = (object)$this->getConnection()->fetchAssoc( + 'SELECT * FROM ' . $this->storageManager->getTableName($shopId) . ' WHERE + `type` = :operationType + AND `document_type` = :documentType + AND `document_id` = :documentId + AND `status` = :status', + [ + 'operationType' => ActionTypes::DELETE, + 'documentType' => $category, + 'documentId' => $id, + 'status' => 0, + ] + ); + + $this->assertTrue(!empty($actual->id)); + $this->assertEquals(ActionTypes::DELETE, $actual->type); + $this->assertEquals($category, $actual->document_type); + $this->assertEquals($id, $actual->document_id); + $this->assertEquals($timestamp, new DateTime($actual->timestamp)); + } } } diff --git a/Tests/Unit/Sync/Extractor/PassthroughExtractorTest.php b/Tests/Unit/Sync/Extractor/PassthroughExtractorTest.php index 8afeef6..63d6193 100644 --- a/Tests/Unit/Sync/Extractor/PassthroughExtractorTest.php +++ b/Tests/Unit/Sync/Extractor/PassthroughExtractorTest.php @@ -18,6 +18,7 @@ use ONGR\ConnectionsBundle\Sync\Extractor\PassthroughExtractor; use ONGR\ConnectionsBundle\Sync\SyncStorage\SyncStorageInterface; use PHPUnit_Framework_MockObject_MockObject as MockObject; +use Symfony\Component\DependencyInjection\ContainerBuilder; class PassthroughExtractorTest extends \PHPUnit_Framework_TestCase { @@ -39,7 +40,11 @@ protected function setUp() $this->storage = $this->getMockBuilder('ONGR\ConnectionsBundle\Sync\SyncStorage\SyncStorageInterface') ->disableOriginalConstructor() ->getMock(); + + $container = new ContainerBuilder(); + $this->service = new PassthroughExtractor(); + $this->service->setContainer($container); $this->service->setStorageFacility($this->storage); } From 3f749047523d89ffa328977a7c35f15e6176f480 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gytis=20Kar=C4=8Diauskas?= Date: Mon, 19 Jan 2015 16:12:08 +0200 Subject: [PATCH 3/3] Fix duplicated code. --- Sync/Extractor/PassthroughExtractor.php | 44 +++++++++---------- .../Extractor/PassthroughExtractorTest.php | 4 +- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/Sync/Extractor/PassthroughExtractor.php b/Sync/Extractor/PassthroughExtractor.php index 70df492..de97ec6 100644 --- a/Sync/Extractor/PassthroughExtractor.php +++ b/Sync/Extractor/PassthroughExtractor.php @@ -34,37 +34,18 @@ class PassthroughExtractor extends AbstractExtractor implements ExtractorInterfa */ public function extract(BaseDiffItem $item) { - $itemId = $item->getItemId(); - if (!is_numeric($itemId)) { + if (!is_numeric($item->getItemId())) { throw new InvalidArgumentException('No valid item ID provided.'); } if ($item instanceof CreateDiffItem) { - $this->storage->save( - ActionTypes::CREATE, - $item->getCategory(), - $itemId, - $item->getTimestamp(), - $this->getShopIds() - ); + $this->saveResult($item, ActionTypes::CREATE); } if ($item instanceof UpdateDiffItem) { - $this->storage->save( - ActionTypes::UPDATE, - $item->getCategory(), - $itemId, - $item->getTimestamp(), - $this->getShopIds() - ); + $this->saveResult($item, ActionTypes::UPDATE); } if ($item instanceof DeleteDiffItem) { - $this->storage->save( - ActionTypes::DELETE, - $item->getCategory(), - $itemId, - $item->getTimestamp(), - $this->getShopIds() - ); + $this->saveResult($item, ActionTypes::DELETE); } } @@ -83,4 +64,21 @@ public function getStorageFacility() { return $this->storage; } + + /** + * Save results to storage. + * + * @param BaseDiffItem $item + * @param string $action + */ + private function saveResult(BaseDiffItem $item, $action) + { + $this->storage->save( + $action, + $item->getCategory(), + $item->getItemId(), + $item->getTimestamp(), + $this->getShopIds() + ); + } } diff --git a/Tests/Functional/Sync/Extractor/PassthroughExtractorTest.php b/Tests/Functional/Sync/Extractor/PassthroughExtractorTest.php index 5d8897c..02ac18a 100644 --- a/Tests/Functional/Sync/Extractor/PassthroughExtractorTest.php +++ b/Tests/Functional/Sync/Extractor/PassthroughExtractorTest.php @@ -108,7 +108,7 @@ public function testExtractForCreateItem() /** * Test if extract is able to add data to the storage for item update action. */ - public function ttestExtractForUpdateItem() + public function testExtractForUpdateItem() { $category = 'product'; $id = 123; @@ -147,7 +147,7 @@ public function ttestExtractForUpdateItem() /** * Test if extract is able to add data to the storage for item delete action. */ - public function ttestExtractForDeleteItem() + public function testExtractForDeleteItem() { $category = 'product'; $id = 123;