diff --git a/Resources/config/extractor.yml b/Resources/config/extractor.yml index f1c0ed2..3b84e00 100644 --- a/Resources/config/extractor.yml +++ b/Resources/config/extractor.yml @@ -1,19 +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 ] ] 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 new file mode 100644 index 0000000..08b50be --- /dev/null +++ b/Sync/Extractor/AbstractExtractor.php @@ -0,0 +1,50 @@ + + * + * 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. + */ +abstract class AbstractExtractor +{ + /** + * @var ContainerInterface + */ + private $container; + + /** + * @param ContainerInterface $container + */ + public function setContainer(ContainerInterface $container = null) + { + $this->container = $container; + } + + /** + * Gets ids of shops from the configuration. + * + * @return array + */ + protected function getShopIds() + { + try { + $shops = $this->container->getParameter('shop_ids'); + } catch (InvalidArgumentException $e) { + $shops = []; + } + + return $shops; + } +} 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/Extractor/PassthroughExtractor.php b/Sync/Extractor/PassthroughExtractor.php index 341717b..de97ec6 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 @@ -34,34 +34,18 @@ class PassthroughExtractor implements ExtractorInterface */ 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->saveResult($item, ActionTypes::CREATE); } if ($item instanceof UpdateDiffItem) { - $this->storage->save( - ActionTypes::UPDATE, - $item->getCategory(), - $itemId, - $item->getTimestamp() - ); + $this->saveResult($item, ActionTypes::UPDATE); } if ($item instanceof DeleteDiffItem) { - $this->storage->save( - ActionTypes::DELETE, - $item->getCategory(), - $itemId, - $item->getTimestamp() - ); + $this->saveResult($item, ActionTypes::DELETE); } } @@ -80,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/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..4dc7372 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 = $this->getServiceContainer()->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/Functional/Sync/Extractor/PassthroughExtractorTest.php b/Tests/Functional/Sync/Extractor/PassthroughExtractorTest.php index 97915a4..02ac18a 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,24 +82,27 @@ 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)); + } } /** @@ -100,8 +114,6 @@ public function testExtractForUpdateItem() $id = 123; $timestamp = new DateTime('-1 hour 20 minutes'); - $this->storageManager->createStorage(); - $updateDiffItem = new UpdateDiffItem(); $updateDiffItem->setCategory($category); $updateDiffItem->setItemId($id); @@ -109,24 +121,27 @@ 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)); + } } /** @@ -138,8 +153,6 @@ public function testExtractForDeleteItem() $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); } 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