├── .github └── FUNDING.yml ├── .gitignore ├── LICENSE ├── README.md ├── bin ├── csv-test.php ├── export.php └── import.php ├── composer.json ├── composer.lock ├── configuration.json ├── phpunit.xml.dist ├── src ├── ArrayConditionGenerator.php ├── CategoryExport.php ├── CategoryImport.php ├── CategoryInfo.php ├── CategoryInfoFactory.php ├── CmsBlockInfoFactory.php ├── CombinedRowMapper.php ├── ConnectionPool.php ├── CopyApplication.php ├── CsvFactory.php ├── CsvReader.php ├── CsvWriter.php ├── CustomMappers │ ├── BaseCostMapper.php │ ├── CustomerBalanceMapper.php │ ├── DescriptionMapper.php │ └── PriceMapper.php ├── CustomerExport.php ├── CustomerFeed.php ├── CustomerFeedFactory.php ├── CustomerImport.php ├── DbFactory.php ├── EavMetadataExport.php ├── EavMetadataImport.php ├── ExportApplication.php ├── Feed.php ├── FeedFactory.php ├── Import.php ├── ImportApplication.php ├── ImportFactory.php ├── MagentoEavInfo.php ├── MagentoExport.php ├── MagentoExportFactory.php ├── MergedRowMapper.php ├── ProductExport.php ├── ProductImport.php ├── ProductInfo.php ├── ProductInfoFactory.php ├── RowMapper.php ├── SelectCondition.php ├── SelectConditionFactory.php ├── SelectConditionGenerator.php ├── SelectInCondition.php ├── SelectRangeCondition.php ├── Sql │ ├── CombinedTableResolver.php │ ├── IdResolver.php │ ├── Identifier.php │ ├── IdentifierNotResolved.php │ ├── InsertOnDuplicate.php │ ├── ResolvedIdentifier.php │ ├── SingleTableResolver.php │ ├── TableResolverFactory.php │ └── UnresolvedIdentifier.php ├── TableRangeConditionGenerator.php └── TableRangeConditionGeneratorFactory.php └── tests ├── ArrayConditionGeneratorTest.php ├── CategoryImportTest.php ├── CombinedRowMapperTest.php ├── CsvFactoryTest.php ├── CsvWriterTest.php ├── CustomMappers ├── BaseCostMapperTest.php ├── CustomerBalanceMapperTest.php ├── DescriptionMapperTest.php └── PriceMapperTest.php ├── DbFactoryTest.php ├── EavMetadataImportTest.php ├── ExportFactoryTest.php ├── MagentoTwoEavInfoTest.php ├── MergedRowMapperTest.php ├── ProductImportTest.php ├── SelectConditionTest.php ├── Sql └── TableResolverTest.php ├── TableRangeConditionGeneratorTest.php ├── TestDb.php ├── TestDirectory.php ├── fixture ├── balance-file.csv ├── file1.csv └── file2.csv └── test.sql.gz /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: [EcomDev] -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | /.idea/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Open Software License v. 3.0 (OSL-3.0) 2 | This Open Software License (the "License") applies to any original work of authorship (the "Original Work") whose owner (the "Licensor") has placed the following licensing notice adjacent to the copyright notice for the Original Work: 3 | 4 | Licensed under the Open Software License version 3.0 5 | 6 | 1) Grant of Copyright License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, for the duration of the copyright, to do the following: 7 | 8 | a) to reproduce the Original Work in copies, either alone or as part of a collective work; 9 | 10 | b) to translate, adapt, alter, transform, modify, or arrange the Original Work, thereby creating derivative works ("Derivative Works") based upon the Original Work; 11 | 12 | c) to distribute or communicate copies of the Original Work and Derivative Works to the public, with the proviso that copies of Original Work or Derivative Works that You distribute or communicate shall be licensed under this Open Software License; 13 | 14 | d) to perform the Original Work publicly; and 15 | 16 | e) to display the Original Work publicly. 17 | 18 | 2) Grant of Patent License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, under patent claims owned or controlled by the Licensor that are embodied in the Original Work as furnished by the Licensor, for the duration of the patents, to make, use, sell, offer for sale, have made, and import the Original Work and Derivative Works. 19 | 20 | 3) Grant of Source Code License. The term "Source Code" means the preferred form of the Original Work for making modifications to it and all available documentation describing how to modify the Original Work. Licensor agrees to provide a machine-readable copy of the Source Code of the Original Work along with each copy of the Original Work that Licensor distributes. Licensor reserves the right to satisfy this obligation by placing a machine-readable copy of the Source Code in an information repository reasonably calculated to permit inexpensive and convenient access by You for as long as Licensor continues to distribute the Original Work. 21 | 22 | 4) Exclusions From License Grant. Neither the names of Licensor, nor the names of any contributors to the Original Work, nor any of their trademarks or service marks, may be used to endorse or promote products derived from this Original Work without express prior permission of the Licensor. Except as expressly stated herein, nothing in this License grants any license to Licensor's trademarks, copyrights, patents, trade secrets or any other intellectual property. No patent license is granted to make, use, sell, offer for sale, have made, or import embodiments of any patent claims other than the licensed claims defined in Section 2. No license is granted to the trademarks of Licensor even if such marks are included in the Original Work. Nothing in this License shall be interpreted to prohibit Licensor from licensing under terms different from this License any Original Work that Licensor otherwise would have a right to license. 23 | 24 | 5) External Deployment. The term "External Deployment" means the use, distribution, or communication of the Original Work or Derivative Works in any way such that the Original Work or Derivative Works may be used by anyone other than You, whether those works are distributed or communicated to those persons or made available as an application intended for use over a network. As an express condition for the grants of license hereunder, You must treat any External Deployment by You of the Original Work or a Derivative Work as a distribution under section 1(c). 25 | 26 | 6) Attribution Rights. You must retain, in the Source Code of any Derivative Works that You create, all copyright, patent, or trademark notices from the Source Code of the Original Work, as well as any notices of licensing and any descriptive text identified therein as an "Attribution Notice." You must cause the Source Code for any Derivative Works that You create to carry a prominent Attribution Notice reasonably calculated to inform recipients that You have modified the Original Work. 27 | 28 | 7) Warranty of Provenance and Disclaimer of Warranty. Licensor warrants that the copyright in and to the Original Work and the patent rights granted herein by Licensor are owned by the Licensor or are sublicensed to You under the terms of this License with the permission of the contributor(s) of those copyrights and patent rights. Except as expressly stated in the immediately preceding sentence, the Original Work is provided under this License on an "AS IS" BASIS and WITHOUT WARRANTY, either express or implied, including, without limitation, the warranties of non-infringement, merchantability or fitness for a particular purpose. THE ENTIRE RISK AS TO THE QUALITY OF THE ORIGINAL WORK IS WITH YOU. This DISCLAIMER OF WARRANTY constitutes an essential part of this License. No license to the Original Work is granted by this License except under this disclaimer. 29 | 30 | 8) Limitation of Liability. Under no circumstances and under no legal theory, whether in tort (including negligence), contract, or otherwise, shall the Licensor be liable to anyone for any indirect, special, incidental, or consequential damages of any character arising as a result of this License or the use of the Original Work including, without limitation, damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses. This limitation of liability shall not apply to the extent applicable law prohibits such limitation. 31 | 32 | 9) Acceptance and Termination. If, at any time, You expressly assented to this License, that assent indicates your clear and irrevocable acceptance of this License and all of its terms and conditions. If You distribute or communicate copies of the Original Work or a Derivative Work, You must make a reasonable effort under the circumstances to obtain the express assent of recipients to the terms of this License. This License conditions your rights to undertake the activities listed in Section 1, including your right to create Derivative Works based upon the Original Work, and doing so without honoring these terms and conditions is prohibited by copyright law and international treaty. Nothing in this License is intended to affect copyright exceptions and limitations (including "fair use" or "fair dealing"). This License shall terminate immediately and You may no longer exercise any of the rights granted to You by this License upon your failure to honor the conditions in Section 1(c). 33 | 34 | 10) Termination for Patent Action. This License shall terminate automatically and You may no longer exercise any of the rights granted to You by this License as of the date You commence an action, including a cross-claim or counterclaim, against Licensor or any licensee alleging that the Original Work infringes a patent. This termination provision shall not apply for an action alleging patent infringement by combinations of the Original Work with other software or hardware. 35 | 36 | 11) Jurisdiction, Venue and Governing Law. Any action or suit relating to this License may be brought only in the courts of a jurisdiction wherein the Licensor resides or in which Licensor conducts its primary business, and under the laws of that jurisdiction excluding its conflict-of-law provisions. The application of the United Nations Convention on Contracts for the International Sale of Goods is expressly excluded. Any use of the Original Work outside the scope of this License or after its termination shall be subject to the requirements and penalties of copyright or patent law in the appropriate jurisdiction. This section shall survive the termination of this License. 37 | 38 | 12) Attorneys' Fees. In any action to enforce the terms of this License or seeking damages relating thereto, the prevailing party shall be entitled to recover its costs and expenses, including, without limitation, reasonable attorneys' fees and costs incurred in connection with such action, including any appeal of such action. This section shall survive the termination of this License. 39 | 40 | 13) Miscellaneous. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable. 41 | 42 | 14) Definition of "You" in This License. "You" throughout this License, whether in upper or lower case, means an individual or a legal entity exercising rights under, and complying with all of the terms of, this License. For legal entities, "You" includes any entity that controls, is controlled by, or is under common control with you. For purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. 43 | 44 | 15) Right to Use. You may use the Original Work in all ways not otherwise restricted or conditioned by this License or by law, and Licensor promises not to interfere with or be responsible for such uses by You. 45 | 46 | 16) Modification of This License. This License is Copyright © 2005 Lawrence Rosen. Permission is granted to copy, distribute, or communicate this License without modification. Nothing in this License permits You to modify this License as applied to the Original Work or to Derivative Works. However, You may modify the text of this License and copy, distribute or communicate your modified version (the "Modified License") and apply it to other original works of authorship subject to the following conditions: (i) You may not indicate in any way that your Modified License is the "Open Software License" or "OSL" and you may not use those names in the name of your Modified License; (ii) You must replace the notice specified in the first paragraph above with the notice "Licensed under " or with a notice of your own that is not confusingly similar to the notice in this License; and (iii) You may not claim that your original works are open source software unless your Modified License has been approved by Open Source Initiative (OSI) and You comply with its license review and certification process. 47 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Simple Import / Export tool 2 | 3 | A tool that allows to quickly export data from Magento 1 and Magento 2 store and import it back into Magento 2. 4 | Table data gets exported without the knowledge of the entity identifiers and delta imports get processed by related unique keys: 5 | - Categories: id attribute (gets stored in map table) 6 | - Products: SKU 7 | - Customers: email + website 8 | 9 | When you export data from your existing stores you can configure mapping and skipped row conditions by using configuration.json: 10 | 11 | Here is an example of configuration.json that maps all manufacturer attribute code in product data into a brand 12 | and skips. 13 | ```json 14 | { 15 | "product_data.csv": { 16 | "map": { 17 | "attribute": { 18 | "manufacturer": "brand" 19 | } 20 | }, 21 | "skip": [ 22 | { 23 | "store": ["pl", "sk"] 24 | } 25 | ] 26 | } 27 | } 28 | ``` 29 | 30 | Also, you can add own mappers for exported files. Here is an example on mapping output of product attributes to create website level price instead of global during migration: 31 | 32 | ```json 33 | { 34 | "product": { 35 | "mappers": { 36 | "product_attributes": [ 37 | { 38 | "class": "EcomDev\\MagentoMigration\\CustomMappers\\PriceMapper", 39 | "setup": [ 40 | ["withStore", "us_en", 1.0], 41 | ["withStore", "uk_en", 0.76], 42 | ["withStore", "eu_en", 0.89] 43 | ] 44 | } 45 | ] 46 | } 47 | } 48 | } 49 | ``` 50 | 51 | 52 | ## Tests 53 | 54 | Right now the automation suite might not run, as tool is released by stripping of all customer specific data from tests and codebase. 55 | PRs to re-introduce test cases that has been removed are welcome. -------------------------------------------------------------------------------- /bin/csv-test.php: -------------------------------------------------------------------------------- 1 | createWriter('product_data_another.csv', ['sku', 'attribute', 'store', 'value']); 14 | foreach ($factory->createReader('product_data.csv') as $row) { 15 | $writer->write($row); 16 | } 17 | -------------------------------------------------------------------------------- /bin/export.php: -------------------------------------------------------------------------------- 1 | run(new CLImate()); 13 | -------------------------------------------------------------------------------- /bin/import.php: -------------------------------------------------------------------------------- 1 | run(new CLImate()); 15 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ecomdev/sync-magento-2-migration", 3 | "description": "Migration Application for M1 -> M2", 4 | "type": "project", 5 | "require": { 6 | "php": "8.2", 7 | "laminas/laminas-db": "^2.15", 8 | "nanawel/zend-db-sql-insertmultiple": "2.1", 9 | "league/climate": "^3.5", 10 | "league/csv": "^9.2", 11 | "laminas/laminas-dependency-plugin": "^2.1" 12 | }, 13 | "require-dev": { 14 | "phpunit/phpunit": "^8.5" 15 | }, 16 | "license": "MIT", 17 | "authors": [ 18 | { 19 | "name": "Ivan Chepurnyi", 20 | "email": "ivan.chepurnyi@ecomdev.org" 21 | } 22 | ], 23 | "autoload": { 24 | "psr-4": { 25 | "EcomDev\\MagentoMigration\\": "src/" 26 | } 27 | }, 28 | "autoload-dev": { 29 | "psr-4": { 30 | "EcomDev\\MagentoMigration\\": "tests/" 31 | } 32 | }, 33 | "minimum-stability": "dev", 34 | "prefer-stable": true, 35 | "config": { 36 | "allow-plugins": { 37 | "laminas/laminas-dependency-plugin": true 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /configuration.json: -------------------------------------------------------------------------------- 1 | { 2 | "attribute.csv": { 3 | "map": { 4 | 5 | } 6 | }, 7 | "attribute_set.csv": { 8 | "map": { 9 | 10 | } 11 | }, 12 | "attribute_option.csv": { 13 | "map": { 14 | "attribute": { 15 | "manufacturer": "brand" 16 | } 17 | }, 18 | "skip": [ 19 | { 20 | "attribute": [] 21 | } 22 | ] 23 | }, 24 | "category": { 25 | "active_filter": [ 26 | "en" 27 | ], 28 | "stores": { 29 | "default":"default","en":"en","cz":"cz","ru":"ru","de":"de","ro":"ro","hu":"hu","lt":"lt","ua":"ua","bg":"bg","gr":"gr","sk2":"sk2","se":"se","es":"es","it":"it","fr":"fr" 30 | } 31 | }, 32 | "product_data.csv": { 33 | "map": { 34 | "attribute": { 35 | } 36 | }, 37 | "skip": [ 38 | { 39 | "store": ["pl", "sk"] 40 | } 41 | ] 42 | }, 43 | "product": { 44 | "ignored_attributes": [ 45 | "tier_price", 46 | "url_path" 47 | ] 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /src/ArrayConditionGenerator.php: -------------------------------------------------------------------------------- 1 | conditions = $conditions; 22 | } 23 | 24 | public static function create(SelectCondition ...$conditions): self 25 | { 26 | return new self($conditions); 27 | } 28 | 29 | public function conditions(): \Generator 30 | { 31 | foreach ($this->conditions as $condition) { 32 | yield $condition; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/CategoryExport.php: -------------------------------------------------------------------------------- 1 | categoryInfo = $categoryInfo; 27 | $this->csvFactory = $csvFactory; 28 | } 29 | 30 | public function exportCategoryList(string $fileName) 31 | { 32 | $writer = $this->csvFactory->createWriter($fileName, ['name', 'id', 'parent_path']); 33 | 34 | foreach ($this->categoryInfo->fetchMainCategoryRows() as $row) { 35 | $writer->write($row); 36 | } 37 | } 38 | 39 | public function exportCategoryData(string $fileName) 40 | { 41 | $writer = $this->csvFactory->createWriter($fileName, ['id', 'attribute', 'store', 'value']); 42 | 43 | foreach ($this->categoryInfo->fetchCategoryData( 44 | ['name', 'description', 'meta_title', 'meta_keywords', 'meta_description', 'url_key', 'include_in_menu', 'display_mode', 'is_anchor'] 45 | ) as $row) { 46 | $writer->write($row); 47 | } 48 | } 49 | } 50 | 51 | -------------------------------------------------------------------------------- /src/CategoryInfo.php: -------------------------------------------------------------------------------- 1 | eavInfo = $eavInfo; 53 | $this->sql = $sql; 54 | $this->activeFilterStores = $activeFilterStores; 55 | $this->storeMap = $storeMap; 56 | $this->metadata = $metadata; 57 | } 58 | 59 | public function fetchCategoryTree() 60 | { 61 | $categoryMapTable = $this->createCategoryMapTableIfNotExists(); 62 | $categoryAttributeIds = $this->eavInfo->fetchAttributeIds('catalog_category', ['name', 'is_active']); 63 | 64 | $select = $this->sql->select(['category' => $categoryMapTable]) 65 | ->columns(['category_id', 'path_ids']) 66 | ->join( 67 | ['is_active' => 'catalog_category_entity_int'], 68 | (new Predicate()) 69 | ->equalTo('is_active.attribute_id', $categoryAttributeIds['is_active']) 70 | ->equalTo('is_active.store_id', 0) 71 | ->literal('is_active.entity_id = category.category_id') 72 | ->equalTo('is_active.value', 1), 73 | [] 74 | ) 75 | ->join( 76 | ['name' => 'catalog_category_entity_varchar'], 77 | (new Predicate()) 78 | ->equalTo('name.attribute_id', $categoryAttributeIds['name']) 79 | ->equalTo('name.store_id', 0) 80 | ->literal('name.entity_id = category.category_id'), 81 | ['name' => 'value'] 82 | ) 83 | ->order('category.path ASC') 84 | ; 85 | 86 | $categories = []; 87 | 88 | foreach ($this->sql->prepareStatementForSqlObject($select)->execute() as $row) { 89 | $categories[$row['category_id']] = $row; 90 | } 91 | 92 | $result = []; 93 | foreach ($categories as $row) { 94 | $names = []; 95 | foreach (explode(',', $row['path_ids']) as $parentId) { 96 | if (!isset($categories[$parentId])) { 97 | continue; 98 | } 99 | $names[] = $categories[$parentId]['name']; 100 | } 101 | 102 | $result[implode(' / ', $names)] = $row['category_id']; 103 | } 104 | 105 | return $result; 106 | } 107 | 108 | private function createCategoryMapTableIfNotExists(): string 109 | { 110 | if (!$this->mapTable) { 111 | $this->mapTable = uniqid('category_map'); 112 | $table = new CreateTable($this->mapTable); 113 | $table->addColumn(new Integer('category_id')) 114 | ->addColumn(new Varchar('path_ids', 255)) 115 | ->addColumn(new Varchar('path', 255)) 116 | ->addColumn(new Integer('level')) 117 | ->addConstraint(new PrimaryKey('category_id')) 118 | ->addConstraint(new Index('path_ids')) 119 | ->addConstraint(new Index('path')) 120 | ->addConstraint(new Index('level')) 121 | ; 122 | 123 | $this->sql->getAdapter()->getDriver()->getConnection()->execute($this->sql->buildSqlString($table)); 124 | 125 | $this->sql->prepareStatementForSqlObject($this->sql->insert($this->mapTable) 126 | ->values($this->sql->select('catalog_category_entity') 127 | ->columns([ 128 | 'category_id' => 'entity_id', 129 | 'path_ids' => new Expression('REPLACE(path, ?, ?)', ['/', ',']), 130 | 'path', 131 | 'level' 132 | ]) 133 | ->where(function (Where $where) { 134 | $where->greaterThan('level', 1); 135 | }))) 136 | ->execute(); 137 | } 138 | 139 | 140 | return $this->mapTable; 141 | } 142 | 143 | public function fetchMainCategoryRows(): iterable 144 | { 145 | if (!$this->activeFilterStores) { 146 | return; 147 | } 148 | 149 | foreach ($this->sql->prepareStatementForSqlObject( 150 | $this->createExportableCategorySelect() 151 | )->execute() as $row) { 152 | yield [ 153 | 'name' => $row['name'], 154 | 'id' => $this->exportId($row['category_id']), 155 | 'parent_path' => $this->mapParent(explode(',', $row['path_ids'])) 156 | ]; 157 | } 158 | } 159 | 160 | public function __destruct() 161 | { 162 | if ($this->mapTable) { 163 | $this->sql->getAdapter()->getDriver()->getConnection()->execute( 164 | $this->sql->buildSqlString(new DropTable($this->mapTable)) 165 | ); 166 | } 167 | } 168 | 169 | private function mapParent(array $parentIds) 170 | { 171 | return implode('/', array_map( 172 | function ($value) { 173 | return $this->exportId($value); 174 | }, 175 | array_slice($parentIds, 2, -1) 176 | )); 177 | } 178 | 179 | private function exportId(string|int $id) { 180 | return 'category_' . $id; 181 | } 182 | 183 | private function createExportableCategorySelect(): Select 184 | { 185 | $storeIds = array_intersect_key( 186 | $this->eavInfo->fetchStoreMap(), 187 | array_combine($this->activeFilterStores, $this->activeFilterStores) 188 | ); 189 | 190 | $categoryMapTable = $this->createCategoryMapTableIfNotExists(); 191 | $categoryAttributeIds = $this->eavInfo->fetchAttributeIds('catalog_category', ['name', 'is_active']); 192 | 193 | $select = $this->sql->select(['parent_category' => $categoryMapTable]) 194 | ->join( 195 | ['child_category' => $categoryMapTable], 196 | (new Predicate()) 197 | ->literal('FIND_IN_SET(parent_category.category_id, child_category.path_ids)'), 198 | ['category_id', 'path_ids'] 199 | ) 200 | ->columns([]) 201 | ->join( 202 | ['is_active_default' => 'catalog_category_entity_int'], 203 | (new Predicate()) 204 | ->equalTo('is_active_default.attribute_id', $categoryAttributeIds['is_active']) 205 | ->equalTo('is_active_default.store_id', 0) 206 | ->literal('is_active_default.entity_id = parent_category.category_id'), 207 | [] 208 | ) 209 | ->join( 210 | ['name' => 'catalog_category_entity_varchar'], 211 | (new Predicate()) 212 | ->equalTo('name.attribute_id', $categoryAttributeIds['name']) 213 | ->equalTo('name.store_id', 0) 214 | ->literal('name.entity_id = child_category.category_id'), 215 | ['name' => 'value'] 216 | ) 217 | ->join( 218 | ['is_active_store' => 'catalog_category_entity_int'], 219 | (new Predicate()) 220 | ->equalTo('is_active_store.attribute_id', $categoryAttributeIds['is_active']) 221 | ->in('is_active_store.store_id', $storeIds) 222 | ->literal('is_active_store.entity_id = parent_category.category_id'), 223 | [], 224 | Select::JOIN_LEFT 225 | ) 226 | ->where( 227 | function (Where $where) { 228 | $where->equalTo('parent_category.level', 2); 229 | $where->expression('IFNULL(is_active_store.value, is_active_default.value) = ?', [1]); 230 | } 231 | ) 232 | ->group('child_category.category_id') 233 | ->group('name.value') 234 | ->order('child_category.path ASC') 235 | ; 236 | 237 | return $select; 238 | } 239 | 240 | public function fetchCategoryData(array $attributeCodes): iterable 241 | { 242 | $categoryIds = []; 243 | 244 | foreach ($this->sql->prepareStatementForSqlObject($this->createExportableCategorySelect())->execute() as $row) { 245 | $categoryIds[] = $row['category_id']; 246 | } 247 | 248 | $storeIds = array_intersect_key($this->eavInfo->fetchStoreMap(), $this->storeMap); 249 | $storeIds[''] = 0; 250 | $storeMap = array_flip($storeIds); 251 | $attributeIds = $this->eavInfo->fetchAttributeIds('catalog_category', $attributeCodes); 252 | $attributeCodes = array_flip($attributeIds); 253 | 254 | $tables = array_intersect([ 255 | 'catalog_category_entity_varchar', 256 | 'catalog_category_entity_url_key', 257 | 'catalog_category_entity_int', 258 | 'catalog_category_entity_text' 259 | ], $this->metadata->getTableNames()); 260 | 261 | $selects = []; 262 | 263 | foreach ($tables as $table) { 264 | $selects[] = $this->sql->select($table) 265 | ->columns(['entity_id', 'store_id', 'attribute_id', 'value']) 266 | ->where(function (Where $where) use ($categoryIds, $attributeIds, $storeIds) { 267 | $where->in('entity_id', $categoryIds); 268 | $where->in('attribute_id', $attributeIds); 269 | $where->in('store_id', $storeIds); 270 | }); 271 | } 272 | 273 | foreach ($this->sql->prepareStatementForSqlObject( 274 | (new Combine())->union($selects, Select::QUANTIFIER_ALL) 275 | )->execute() as $row) { 276 | yield [ 277 | 'attribute' => $attributeCodes[$row['attribute_id']], 278 | 'id' => $this->exportId($row['entity_id']), 279 | 'value' => $row['value'], 280 | 'store' => $this->storeMap[$storeMap[$row['store_id']]] ?? '' 281 | ]; 282 | } 283 | 284 | } 285 | } 286 | -------------------------------------------------------------------------------- /src/CategoryInfoFactory.php: -------------------------------------------------------------------------------- 1 | eavInfo = $eavInfo; 45 | $this->sql = $sql; 46 | $this->metadata = $metadata; 47 | } 48 | 49 | public static function createFromAdapter(Adapter $adapter): self 50 | { 51 | return new self(MagentoEavInfo::createFromAdapter($adapter), new Sql($adapter), Factory::createSourceFromAdapter($adapter)); 52 | } 53 | 54 | 55 | public function create(): CategoryInfo 56 | { 57 | return new CategoryInfo($this->eavInfo, $this->sql, $this->metadata, $this->storeFilter, $this->storeMap); 58 | } 59 | 60 | public function withStoreFilter(array $storeCodes): self 61 | { 62 | $factory = clone $this; 63 | $factory->storeFilter = $storeCodes; 64 | return $factory; 65 | } 66 | 67 | public function withStoreMap(array $storeCodes): self 68 | { 69 | $factory = clone $this; 70 | $factory->storeMap = $storeCodes; 71 | 72 | return $factory; 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /src/CmsBlockInfoFactory.php: -------------------------------------------------------------------------------- 1 | mappers) { 20 | return $rows; 21 | } 22 | 23 | foreach ($this->mappers as $mapper) { 24 | $rows = $mapper->apply($rows); 25 | } 26 | 27 | return $rows; 28 | 29 | } 30 | 31 | public function pipe(RowMapper $mapper): self 32 | { 33 | $piped = clone $this; 34 | $piped->mappers[] = $mapper; 35 | return $piped; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/ConnectionPool.php: -------------------------------------------------------------------------------- 1 | 'Pdo_Mysql', 23 | 'hostname' => $this->host, 24 | 'username' => $this->username, 25 | 'password' => $this->password, 26 | 'database' => $this->database, 27 | 'driver_options' => [ 28 | PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => 0 29 | ] 30 | ]); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/CopyApplication.php: -------------------------------------------------------------------------------- 1 | exportFactory = $exportFactory; 28 | $this->dbFactory = $factory; 29 | } 30 | 31 | public static function create() 32 | { 33 | return new self(new MagentoExportFactory(), new DbFactory()); 34 | } 35 | 36 | public function run(CLImate $cli) 37 | { 38 | $this->initializeArguments($cli); 39 | try { 40 | $cli->arguments->parse(); 41 | 42 | $source = $cli->arguments->get('source_path'); 43 | $path = $cli->arguments->get('target_path'); 44 | 45 | $fileList = [ 46 | 'product.csv' => ['sku'], 47 | 'product_category.csv' => ['sku'], 48 | 'product_data.csv' => ['sku'], 49 | 'product_image.csv' => ['sku', 'image'], 50 | 'product_image_label.csv' => ['sku', 'image'], 51 | 'product_website.csv' => ['sku'], 52 | 'stock.csv' => ['sku'] 53 | ]; 54 | 55 | $prefixes = array_map( 56 | function () { 57 | return uniqid('COPY_'); 58 | }, 59 | range(0, 10) 60 | ); 61 | 62 | $targetFactory = new CsvFactory($path); 63 | $sourceFactory = new CsvFactory($source); 64 | 65 | foreach ($fileList as $fileName => $uniqueColumns) { 66 | $headers = []; 67 | foreach ($sourceFactory->createReader($fileName) as $row) { 68 | $headers = array_keys($row); 69 | break; 70 | } 71 | 72 | $this->copyFile( 73 | $sourceFactory, 74 | $targetFactory->createWriter($fileName, $headers), 75 | $fileName, 76 | $uniqueColumns, 77 | $prefixes 78 | ); 79 | } 80 | } catch (InvalidArgumentException $e) { 81 | $cli->error($e->getMessage()); 82 | $cli->usage(); 83 | } 84 | 85 | } 86 | 87 | private function initializeArguments(CLImate $cli) 88 | { 89 | $cli->arguments->add('source_path', [ 90 | 'description' => 'Import Directory', 91 | 'required' => true 92 | ]); 93 | 94 | $cli->arguments->add('target_path', [ 95 | 'description' => 'MagentoExport Directory', 96 | 'required' => true 97 | ]); 98 | } 99 | 100 | private function copyFile(CsvFactory $factory, CsvWriter $target, $fileName, array $uniqueFields, array $prefixes) 101 | { 102 | foreach ($prefixes as $prefix) { 103 | $reader = $factory->createReader($fileName); 104 | foreach ($reader as $row) { 105 | foreach ($uniqueFields as $field) { 106 | $row[$field] = $prefix . $row[$field]; 107 | } 108 | $target->write($row); 109 | } 110 | } 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /src/CsvFactory.php: -------------------------------------------------------------------------------- 1 | currentDirectory = $currentDirectory; 43 | $this->encodedData = $encodedData; 44 | $this->csvReader = new CsvReader(); 45 | } 46 | 47 | /** 48 | * @param string $fileName 49 | * @return Writer 50 | */ 51 | public function createNativeWriter(string $fileName): Writer 52 | { 53 | return Writer::createFromPath($this->currentDirectory . DIRECTORY_SEPARATOR . $fileName, 'w'); 54 | } 55 | 56 | /** 57 | * @param string $fileName 58 | * @param array $headers 59 | * @return CsvWriter 60 | */ 61 | public function createWriter(string $fileName, array $headers): CsvWriter 62 | { 63 | $csvFile = new \SplFileObject($this->currentDirectory . DIRECTORY_SEPARATOR . $fileName, 'w'); 64 | $csvFile->setCsvControl(',', '"', "\0"); 65 | $csvFile->fputcsv($headers); 66 | 67 | return new CsvWriter( 68 | $csvFile, 69 | $headers, 70 | $this->skipFilters[$fileName] ?? [], 71 | $this->mappings[$fileName] ?? [], 72 | $this->encodedData 73 | ); 74 | } 75 | 76 | /** 77 | * @param string $fileName 78 | * @return iterable 79 | */ 80 | public function createReader(string $fileName): iterable 81 | { 82 | return $this->csvReader->readFile($this->currentDirectory . DIRECTORY_SEPARATOR . $fileName, $this->encodedData); 83 | } 84 | 85 | /** 86 | * @param string $fileName 87 | * @param array $condition 88 | * @return $this 89 | */ 90 | public function withSkip(string $fileName, array $condition): self 91 | { 92 | $factory = clone $this; 93 | $factory->skipFilters[$fileName][] = $condition; 94 | return $factory; 95 | } 96 | 97 | /** 98 | * @param string $fileName 99 | * @param string $field 100 | * @param array $values 101 | * @return $this 102 | */ 103 | public function withMap(string $fileName, string $field, array $values): self 104 | { 105 | $factory = clone $this; 106 | $factory->mappings[$fileName][$field] = $values; 107 | return $factory; 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /src/CsvReader.php: -------------------------------------------------------------------------------- 1 | fgetcsv(',', '"', "\0"); 23 | 24 | while ($row = $reader->fgetcsv(',', '"', "\0")) { 25 | if (count($row) < count($headers)) { 26 | continue; 27 | } 28 | if($decodeData) { 29 | $row = array_map('base64_decode', $row); 30 | } 31 | $item = array_combine($headers, $row); 32 | yield $item; 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/CsvWriter.php: -------------------------------------------------------------------------------- 1 | writer = $writer; 43 | $this->headers = $headers; 44 | $this->skipConditions = $skipConditions; 45 | $this->mapping = $mapping; 46 | $this->encodeData = $encodeData; 47 | } 48 | 49 | public function write(array $row): void 50 | { 51 | $csvRow = []; 52 | 53 | foreach ($this->skipConditions as $condition) { 54 | $isIgnored = true; 55 | foreach ($condition as $code => $ignoredValues) { 56 | $isReverse = strpos($code, '!') === 0; 57 | if ($isReverse) { 58 | $code = substr($code, 1); 59 | } 60 | $value = $row[$code] ?? ''; 61 | $isValueMatch = in_array($value, $ignoredValues); 62 | 63 | if ($isReverse) { 64 | $isValueMatch = !$isValueMatch; 65 | } 66 | 67 | $isIgnored = $isIgnored && $isValueMatch; 68 | } 69 | 70 | if ($isIgnored) { 71 | return; 72 | } 73 | } 74 | 75 | foreach ($this->headers as $name) { 76 | $value = $row[$name] ?? ''; 77 | 78 | if (isset($this->mapping[$name][$value])) { 79 | $value = $this->mapping[$name][$value]; 80 | } 81 | 82 | $csvRow[] = (string)$value; 83 | } 84 | if ($this->encodeData) { 85 | $csvRow = array_map('base64_encode', $csvRow); 86 | } 87 | $this->writer->fputcsv($csvRow); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/CustomMappers/BaseCostMapper.php: -------------------------------------------------------------------------------- 1 | currencyConversion[$cost['currency']] ?? 1); 32 | 33 | yield [ 34 | 'sku' => $sku, 35 | 'attribute' => 'cost', 36 | 'store' => '', 37 | 'value' => $costValue 38 | ]; 39 | 40 | foreach ($this->stores as $store => $rate) { 41 | yield [ 42 | 'sku' => $sku, 43 | 'attribute' => 'cost', 44 | 'store' => $store, 45 | 'value' => $costValue * $rate 46 | ]; 47 | } 48 | }; 49 | 50 | foreach ($rows as $row) { 51 | if ($row['attribute'] === 'base_cost') { 52 | $baseCost[$row['sku']]['value'] = (float)$row['value']; 53 | if ($isFullBaseCost($baseCost[$row['sku']])) { 54 | foreach ($generateBaseCost($row['sku'], $baseCost[$row['sku']]) as $costRow) { 55 | yield $costRow; 56 | } 57 | unset($baseCost[$row['sku']]); 58 | } 59 | continue; 60 | } elseif ($row['attribute'] === 'base_cost_currency') { 61 | $baseCost[$row['sku']]['currency'] = $row['value']; 62 | if ($isFullBaseCost($baseCost[$row['sku']])) { 63 | foreach ($generateBaseCost($row['sku'], $baseCost[$row['sku']]) as $costRow) { 64 | yield $costRow; 65 | } 66 | unset($baseCost[$row['sku']]); 67 | } 68 | continue; 69 | } 70 | 71 | yield $row; 72 | } 73 | } 74 | 75 | public function withDefaultStore(string $storeCode): self 76 | { 77 | return $this->withStore($storeCode, 1); 78 | } 79 | 80 | public function withStore(string $store, $rate): self 81 | { 82 | $mapper = clone $this; 83 | $mapper->stores[$store] = (float)$rate; 84 | 85 | return $mapper; 86 | } 87 | 88 | public function withCurrencyConversion(string $currency, $rate): self 89 | { 90 | $mapper = clone $this; 91 | $mapper->currencyConversion[$currency] = (float)$rate; 92 | return $mapper; 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /src/CustomMappers/CustomerBalanceMapper.php: -------------------------------------------------------------------------------- 1 | mergeSource) { 31 | $mergeInfo[$row['email']] = $mergeInfo[$row['email']] ?? 0; 32 | $mergeInfo[$row['email']] += $row['value']; 33 | continue; 34 | } 35 | 36 | if ($row['website'] === $this->mergeTarget && isset($mergeInfo[$row['email']])) { 37 | $row['value'] += $mergeInfo[$row['email']]; 38 | unset($mergeInfo[$row['email']]); 39 | } 40 | 41 | $rate = $this->rates[$row['website']] ?? 0; 42 | 43 | if ($rate) { 44 | $row['value'] *= $rate; 45 | } 46 | 47 | yield $row; 48 | } 49 | } 50 | 51 | public function withMerge(string $source, string $target): self 52 | { 53 | $mapper = clone $this; 54 | $mapper->mergeSource = $source; 55 | $mapper->mergeTarget = $target; 56 | return $mapper; 57 | } 58 | 59 | public function withRate(string $websiteCode, float $rate): self 60 | { 61 | $mapper = clone $this; 62 | $mapper->rates[$websiteCode] = $rate; 63 | return $mapper; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/CustomMappers/DescriptionMapper.php: -------------------------------------------------------------------------------- 1 | ', '', '

', '

', '

', '

']; 23 | $h4new = ['

', '

', '

', '

', '

', '

']; 24 | yield ['value' => str_replace($h4, $h4new, $row['value'])] + $row; 25 | continue; 26 | } 27 | 28 | yield $row; 29 | 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/CustomMappers/PriceMapper.php: -------------------------------------------------------------------------------- 1 | rates as $code => $rate) { 26 | yield ['store' => $code, 'value' => $row['value']*$rate] + $row; 27 | } 28 | } 29 | } 30 | } 31 | 32 | public function withStore(string $store, float $rate): self 33 | { 34 | $mapper = clone $this; 35 | $mapper->rates[$store] = $rate; 36 | return $mapper; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/CustomerExport.php: -------------------------------------------------------------------------------- 1 | csvFactory = $csvFactory; 34 | $this->customerFeed = $customerFeed; 35 | $this->conditionGenerator = $conditionGenerator; 36 | } 37 | 38 | public function exportCustomers(string $fileName) 39 | { 40 | $writer = $this->csvFactory->createWriter($fileName, $this->customerFeed::CUSTOMER_FIELDS); 41 | 42 | foreach ($this->customerFeed->fetchCustomers($this->conditionGenerator) as $row) { 43 | $writer->write($row); 44 | } 45 | } 46 | 47 | public function exportCustomerAddresses(string $fileName) 48 | { 49 | $writer = $this->csvFactory->createWriter($fileName, $this->customerFeed::CUSTOMER_ADDRESS_FIELDS); 50 | 51 | foreach ($this->customerFeed->fetchCustomerAddresses($this->conditionGenerator) as $row) { 52 | $writer->write($row); 53 | } 54 | } 55 | 56 | public function exportCustomerBalance(string $fileName) 57 | { 58 | $writer = $this->csvFactory->createWriter($fileName, $this->customerFeed::CUSTOMER_BALANCE_FIELDS); 59 | 60 | foreach ($this->customerFeed->fetchCustomerBalance($this->conditionGenerator) as $row) { 61 | $writer->write($row); 62 | } 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/CustomerFeed.php: -------------------------------------------------------------------------------- 1 | sql = $sql; 52 | $this->eavInfo = $eavInfo; 53 | $this->rowMappers = $rowMappers; 54 | } 55 | 56 | public function fetchCustomers(SelectConditionGenerator $conditionGenerator): iterable 57 | { 58 | $customerAttributeIds = $this->eavInfo->fetchAttributeIds( 59 | 'customer', 60 | self::CUSTOMER_FIELDS 61 | ); 62 | 63 | $attributeIdToCode = array_flip($customerAttributeIds); 64 | 65 | $attributeOptions = $this->eavInfo->fetchAttributeOptions( 66 | 'customer', 67 | ['gender'] 68 | ); 69 | 70 | $storeMap = array_flip($this->eavInfo->fetchStoreMap()); 71 | $websiteMap = []; 72 | foreach ($this->eavInfo->fetchWebsiteMap($this->eavInfo->fetchStoreMap()) as $code => $websiteId) { 73 | $websiteMap[$websiteId] = $websiteMap[$websiteId] ?? $code; 74 | } 75 | 76 | $attributeTables = [ 77 | 'customer_entity_int', 78 | 'customer_entity_varchar', 79 | 'customer_entity_text', 80 | 'customer_entity_decimal', 81 | 'customer_entity_datetime' 82 | ]; 83 | 84 | foreach ($conditionGenerator->conditions() as $condition) { 85 | $attributeTableSelect = []; 86 | 87 | $mainSelect = $this->sql->select(['customer' => 'customer_entity']) 88 | ->join( 89 | ['group' => 'customer_group'], 90 | 'group.customer_group_id = customer.group_id', 91 | ['group' => 'customer_group_code'] 92 | ) 93 | ->where(function (Where $where) use ($condition) { 94 | $condition->apply('customer.entity_id', $where); 95 | }); 96 | 97 | foreach ($attributeTables as $table) { 98 | $attributeTableSelect[] = $this->sql->select(['attribute' => $table]) 99 | ->columns( 100 | ['entity_id', 'attribute_id', 'value'] 101 | ) 102 | ->where(function (Where $where) use ($condition, $customerAttributeIds) { 103 | $condition->apply('attribute.entity_id', $where); 104 | $where->in('attribute.attribute_id', $customerAttributeIds); 105 | }); 106 | ; 107 | } 108 | 109 | $customerAttributes = []; 110 | foreach ($this->sql->prepareStatementForSqlObject( 111 | new Combine($attributeTableSelect, Combine::COMBINE_UNION, Select::QUANTIFIER_ALL) 112 | ) 113 | ->execute() as $row) { 114 | $attributeCode = $attributeIdToCode[$row['attribute_id']]; 115 | 116 | $customerAttributes[$row['entity_id']][$attributeCode] = $row['value']; 117 | } 118 | 119 | $defaultData = array_combine( 120 | self::CUSTOMER_FIELDS, 121 | array_fill(0, count(self::CUSTOMER_FIELDS), '') 122 | ); 123 | 124 | foreach ($this->sql->prepareStatementForSqlObject($mainSelect)->execute() as $row) { 125 | $customer = array_replace($row, ($customerAttributes[$row['entity_id']] ?? [])) + $defaultData; 126 | $customer['store'] = $storeMap[$customer['store_id']] ?? ''; 127 | $customer['website'] = $websiteMap[$customer['website_id']] ?? ''; 128 | $customer['gender'] = $attributeOptions['gender'][$customer['gender']] ?? ''; 129 | $passwordHash = explode(':', $customer['password_hash'], 3); 130 | if (count($passwordHash) < 2) { 131 | $passwordHash[] = ''; 132 | $passwordHash[] = '0'; 133 | } elseif (strlen($passwordHash[0]) === 32) { 134 | $passwordHash[2] = '0'; 135 | } else { 136 | $passwordHash[2] = '1'; 137 | } 138 | 139 | $customer['password_hash'] = implode(':', $passwordHash); 140 | 141 | yield array_intersect_key($customer, $defaultData); 142 | } 143 | } 144 | } 145 | 146 | public function fetchCustomerAddresses(SelectConditionGenerator $conditionGenerator): iterable 147 | { 148 | $addressAttributeIds = $this->eavInfo->fetchAttributeIds( 149 | 'customer_address', 150 | self::CUSTOMER_ADDRESS_FIELDS 151 | ); 152 | 153 | $attributeIdToCode = array_flip($addressAttributeIds); 154 | 155 | $attributeTables = [ 156 | 'customer_address_entity_int', 157 | 'customer_address_entity_varchar', 158 | 'customer_address_entity_text', 159 | 'customer_address_entity_decimal', 160 | 'customer_address_entity_datetime' 161 | ]; 162 | 163 | foreach ($conditionGenerator->conditions() as $condition) { 164 | $attributeTableSelect = []; 165 | 166 | $mainSelect = $this->sql->select(['customer' => 'customer_entity']) 167 | ->columns( 168 | ['email'] 169 | ) 170 | ->join( 171 | ['address' => 'customer_address_entity'], 172 | 'address.parent_id = customer.entity_id' 173 | ) 174 | ->where( 175 | function (Where $where) use ($condition) { 176 | $condition->apply('customer.entity_id', $where); 177 | } 178 | ) 179 | ; 180 | 181 | foreach ($attributeTables as $table) { 182 | $attributeTableSelect[] = $this->sql->select(['attribute' => $table]) 183 | ->columns( 184 | ['entity_id', 'attribute_id', 'value'] 185 | ) 186 | ->join( 187 | ['address' => 'customer_address_entity'], 188 | 'address.entity_id = attribute.entity_id', 189 | [] 190 | ) 191 | ->join( 192 | ['customer' => 'customer_entity'], 193 | 'customer.entity_id = address.parent_id', 194 | [] 195 | ) 196 | ->where( 197 | function (Where $where) use ($condition, $addressAttributeIds) { 198 | $condition->apply('customer.entity_id', $where); 199 | $where->in('attribute.attribute_id', $addressAttributeIds); 200 | } 201 | ) 202 | ; 203 | } 204 | 205 | $addressAttributes = []; 206 | foreach ($this->sql->prepareStatementForSqlObject( 207 | new Combine($attributeTableSelect, Combine::COMBINE_UNION, Select::QUANTIFIER_ALL) 208 | )->execute() as $row) { 209 | $attributeCode = $attributeIdToCode[$row['attribute_id']]; 210 | 211 | $addressAttributes[$row['entity_id']][$attributeCode] = $row['value']; 212 | } 213 | 214 | $defaultData = array_combine( 215 | self::CUSTOMER_ADDRESS_FIELDS, 216 | array_fill(0, count(self::CUSTOMER_ADDRESS_FIELDS), '') 217 | ); 218 | 219 | foreach ($this->sql->prepareStatementForSqlObject($mainSelect)->execute() as $row) { 220 | $address = array_replace($row, ($addressAttributes[$row['entity_id']] ?? [])) + $defaultData; 221 | 222 | yield array_intersect_key($address, $defaultData); 223 | } 224 | } 225 | } 226 | 227 | public function fetchCustomerBalance(SelectConditionGenerator $conditionGenerator): iterable 228 | { 229 | $data = $this->fetchCustomerBalanceFromDatabase($conditionGenerator); 230 | return isset($this->rowMappers['balance']) ? $this->rowMappers['balance']->apply($data) : $data; 231 | } 232 | 233 | private function fetchCustomerBalanceFromDatabase(SelectConditionGenerator $conditionGenerator): \Generator 234 | { 235 | $websiteMap = []; 236 | foreach ($this->eavInfo->fetchWebsiteMap($this->eavInfo->fetchStoreMap()) as $code => $websiteId) { 237 | $websiteMap[$websiteId] = $websiteMap[$websiteId] ?? $code; 238 | } 239 | 240 | $defaultData = array_combine( 241 | self::CUSTOMER_BALANCE_FIELDS, 242 | array_fill(0, count(self::CUSTOMER_BALANCE_FIELDS), '') 243 | ); 244 | 245 | foreach ($conditionGenerator->conditions() as $condition) { 246 | $mainSelect = $this->sql->select(['customer' => 'customer_entity']) 247 | ->columns( 248 | ['email'] 249 | ) 250 | ->join( 251 | ['balance' => $this->eavInfo->isMagentoTwo() 252 | ? 'magento_customerbalance' 253 | : 'enterprise_customerbalance'], 254 | 'balance.customer_id = customer.entity_id', 255 | ['website_id', 'value' => 'amount', 'currency' => 'base_currency_code'] 256 | ) 257 | ->where( 258 | function (Where $where) use ($condition) { 259 | $condition->apply('customer.entity_id', $where); 260 | } 261 | ) 262 | ; 263 | 264 | foreach ($this->sql->prepareStatementForSqlObject($mainSelect)->execute() as $row) { 265 | $row['currency'] = $row['currency'] ?? ''; 266 | $row['website'] = $websiteMap[$row['website_id']]; 267 | yield array_intersect_key($row, $defaultData); 268 | } 269 | } 270 | } 271 | } 272 | -------------------------------------------------------------------------------- /src/CustomerFeedFactory.php: -------------------------------------------------------------------------------- 1 | sql = $sql; 35 | $this->eavInfo = $eavInfo; 36 | } 37 | 38 | public static function createFromAdapter(Adapter $adapter) 39 | { 40 | return new self( 41 | new Sql($adapter), 42 | MagentoEavInfo::createFromAdapter($adapter) 43 | ); 44 | } 45 | 46 | /** 47 | * @return CustomerFeed 48 | */ 49 | public function create(): Feed 50 | { 51 | return new CustomerFeed($this->sql, $this->eavInfo, $this->rowMappers); 52 | } 53 | 54 | /** 55 | * @return $this 56 | */ 57 | public function withRowMapper(string $feedCode, RowMapper $rowMapper): FeedFactory 58 | { 59 | $factory = clone $this; 60 | 61 | $piped = $factory->rowMappers[$feedCode] ?? new CombinedRowMapper(); 62 | $factory->rowMappers[$feedCode] = $piped->pipe($rowMapper); 63 | 64 | return $factory; 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/CustomerImport.php: -------------------------------------------------------------------------------- 1 | sql = $sql; 53 | $this->eavInfo = $eavInfo; 54 | $this->resolverFactory = $resolverFactory; 55 | } 56 | 57 | public static function createFromAdapter(Adapter $connection) 58 | { 59 | return new self( 60 | new Sql($connection), 61 | MagentoEavInfo::createFromAdapter($connection), 62 | TableResolverFactory::createFromAdapter($connection) 63 | ); 64 | } 65 | 66 | public function importCustomers(iterable $customers): void 67 | { 68 | $this->transactional(function () use ($customers) { 69 | $genderOptions = array_flip($this->eavInfo->fetchAttributeOptions('customer', ['gender'])['gender']); 70 | $select = $this->sql->select('customer_group') 71 | ->columns(['id' => 'customer_group_id', 'code' => 'customer_group_code']) 72 | ; 73 | 74 | $groupMap = []; 75 | foreach ($this->sql->prepareStatementForSqlObject($select)->execute() as $row) { 76 | $groupMap[$row['code']] = $row['id']; 77 | } 78 | 79 | $storeMap = $this->eavInfo->fetchStoreMap(); 80 | 81 | $websiteMap = $this->eavInfo->fetchWebsiteMap($storeMap); 82 | 83 | $insert = InsertOnDuplicate::create('customer_entity', self::CUSTOMER_FIELDS) 84 | ->onDuplicate(self::CUSTOMER_FIELDS); 85 | 86 | foreach ($customers as $customer) { 87 | $customer['website_id'] = $websiteMap[$customer['website']] ?? 0; 88 | $customer['store_id'] = $websiteMap[$customer['store']] ?? 0; 89 | $customer['group_id'] = $groupMap[$customer['group']] ?? 1; 90 | $customer['gender'] = $genderOptions[$customer['gender'] ?: self::NOT_SPECIFIED] ?? null; 91 | 92 | $insert->withAssocRow( 93 | $customer 94 | ); 95 | 96 | $insert = $insert->flushIfLimitReached($this->sql); 97 | } 98 | 99 | $insert->executeIfNotEmpty($this->sql); 100 | }); 101 | } 102 | 103 | private function transactional(callable $codeBlock) 104 | { 105 | $connection = $this->sql->getAdapter()->getDriver()->getConnection(); 106 | $connection->beginTransaction(); 107 | try { 108 | $codeBlock(); 109 | $connection->commit(); 110 | } catch (\Throwable $e) { 111 | $connection->rollback(); 112 | throw $e; 113 | } 114 | } 115 | 116 | public function importCustomerAddresses(iterable $addresses): void 117 | { 118 | $customerResolver = $this->resolverFactory->createSingleValueResolver( 119 | 'customer_entity', 120 | 'email', 121 | 'entity_id' 122 | ); 123 | 124 | $streetResolver = $this->resolverFactory->createCombinedValueResolver( 125 | 'customer_address_entity', 126 | 'entity_id', 127 | 'street', 128 | 'parent_id', 129 | $customerResolver 130 | ); 131 | 132 | $regionMap = []; 133 | 134 | $select = $this->sql->select('directory_country_region') 135 | ->columns(['country_id', 'default_name', 'region_id']); 136 | 137 | foreach ($this->sql->prepareStatementForSqlObject($select)->execute() as $row) { 138 | $regionMap[$row['country_id']][$row['default_name']] = $row['region_id']; 139 | } 140 | 141 | $insertAddress = InsertOnDuplicate::create('customer_address_entity', self::CUSTOMER_ADDRESS_FIELDS) 142 | ->withResolver($customerResolver) 143 | ->withResolver($streetResolver) 144 | ->onDuplicate(self::CUSTOMER_ADDRESS_FIELDS); 145 | 146 | $insertCustomer = InsertOnDuplicate::create('customer_entity', [ 147 | 'entity_id', 'default_billing', 'default_shipping' 148 | ]) 149 | ->onDuplicate(['default_billing', 'default_shipping']) 150 | ->withResolver($customerResolver) 151 | ->withResolver($streetResolver) 152 | ; 153 | 154 | foreach ($addresses as $address) { 155 | $addressId = $streetResolver->unresolved([$address['street'], $address['email']]); 156 | $parentId = $customerResolver->unresolved($address['email']); 157 | 158 | $address['entity_id'] = $addressId; 159 | $address['parent_id'] = $parentId; 160 | $address['region_id'] = $regionMap[$address['country_id']][$address['region']] ?? null; 161 | $address['region'] = $address['region_id'] ? $address['region'] : ''; 162 | 163 | $insertAddress->withAssocRow($address); 164 | $insertAddress = $insertAddress->flushIfLimitReached($this->sql); 165 | 166 | $insertCustomer->withRow($parentId, $addressId, $addressId) 167 | ->flushIfLimitReached($this->sql); 168 | } 169 | 170 | $insertAddress->executeIfNotEmpty($this->sql); 171 | $insertCustomer->executeIfNotEmpty($this->sql); 172 | } 173 | 174 | public function importCustomerBalance(iterable $customerBalance) 175 | { 176 | $customerResolver = $this->resolverFactory->createSingleValueResolver( 177 | 'customer_entity', 178 | 'email', 179 | 'entity_id' 180 | ); 181 | 182 | $storeMap = $this->eavInfo->fetchStoreMap(); 183 | 184 | $websiteMap = $this->eavInfo->fetchWebsiteMap($storeMap); 185 | 186 | $insertBalance = InsertOnDuplicate::create( 187 | 'magento_customerbalance', 188 | ['customer_id', 'website_id', 'amount', 'base_currency_code'] 189 | ) 190 | ->withResolver($customerResolver) 191 | ->onDuplicate(['website_id']); 192 | ; 193 | 194 | foreach ($customerBalance as $balance) { 195 | $insertBalance = $insertBalance->withRow( 196 | $customerResolver->unresolved($balance['email']), 197 | $websiteMap[$balance['website']], 198 | $balance['value'], 199 | $balance['currency'] 200 | )->flushIfLimitReached($this->sql); 201 | } 202 | 203 | $insertBalance->executeIfNotEmpty($this->sql); 204 | } 205 | } 206 | -------------------------------------------------------------------------------- /src/DbFactory.php: -------------------------------------------------------------------------------- 1 | crateConnectionPool($host, $user, $password, $dbName)->createConnection(); 18 | } 19 | 20 | public function crateConnectionPool(string $host, string $user, string $password, string $dbName): ConnectionPool 21 | { 22 | return new ConnectionPool($host, $user, $password, $dbName); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/EavMetadataExport.php: -------------------------------------------------------------------------------- 1 | eavInfo = $eavInfo; 30 | $this->csvFactory = $csvFactory; 31 | } 32 | 33 | public function exportAttributes(string $targetFile) 34 | { 35 | $writer = $this->csvFactory->createWriter($targetFile, [ 36 | 'name', 37 | 'code', 38 | 'type', 39 | 'input', 40 | 'scope', 41 | 'option', 42 | 'default', 43 | 'unique', 44 | 'required', 45 | 'validation', 46 | 'searchable', 47 | 'advanced_search', 48 | 'layered', 49 | 'layered_search', 50 | 'promotion', 51 | 'product_list', 52 | 'product_page', 53 | 'sortable', 54 | 'comparable', 55 | 'apply_to', 56 | 'html', 57 | 'position', 58 | ]); 59 | 60 | $attributes = $this->fetchAttributes(); 61 | $attributeCodes = array_keys($attributes); 62 | 63 | foreach ($this->eavInfo->fetchProductAttributeConfiguration($attributeCodes) as $code => $attribute) { 64 | $writer->write($attribute + $attributes[$code]); 65 | } 66 | } 67 | 68 | public function exportAttributeSets(string $targetFile) 69 | { 70 | $attributeCodes = array_keys($this->fetchAttributes()); 71 | $writer = $this->csvFactory->createWriter($targetFile, ['set', 'group', 'attribute']); 72 | 73 | foreach ($this->eavInfo->fetchAttributeSets($attributeCodes) as $row) { 74 | $writer->write($row); 75 | } 76 | } 77 | 78 | /** 79 | * 80 | * @return array 81 | */ 82 | private function fetchAttributes(): array 83 | { 84 | if (!$this->attributes) { 85 | $this->attributes = $this->eavInfo->fetchProductAttributes(['media_gallery']); 86 | } 87 | 88 | return $this->attributes; 89 | } 90 | 91 | public function exportAttributeOptions(string $targetFile) 92 | { 93 | $writer = $this->csvFactory->createWriter($targetFile, ['attribute', 'option', 'position']); 94 | 95 | $attributeCodes = array_keys($this->fetchAttributes()); 96 | 97 | foreach ($this->eavInfo 98 | ->fetchAttributeOptions('catalog_product', $attributeCodes) as $attributeCode => $options) { 99 | 100 | $position = 1; 101 | foreach ($options as $option) { 102 | $writer->write([ 103 | 'attribute' => $attributeCode, 104 | 'option' => $option, 105 | 'position' => $position ++ 106 | ]); 107 | } 108 | } 109 | 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /src/ExportApplication.php: -------------------------------------------------------------------------------- 1 | exportFactory = $exportFactory; 28 | $this->dbFactory = $factory; 29 | } 30 | 31 | public static function create() 32 | { 33 | return new self(new MagentoExportFactory(), new DbFactory()); 34 | } 35 | 36 | public function run(CLImate $cli) 37 | { 38 | $this->initializeArguments($cli); 39 | try { 40 | $cli->arguments->parse(); 41 | 42 | $adapter = $this->dbFactory->createConnection( 43 | $cli->arguments->get('mysql_host'), 44 | $cli->arguments->get('mysql_user'), 45 | $cli->arguments->get('mysql_password'), 46 | $cli->arguments->get('mysql_db') 47 | ); 48 | 49 | $path = $cli->arguments->get('target_path'); 50 | 51 | $configurationFile = $cli->arguments->get('config'); 52 | if ($configurationFile && file_exists($configurationFile)) { 53 | $configuration = json_decode(file_get_contents($configurationFile), true); 54 | $this->exportFactory = $this->exportFactory->withConfiguration((array)$configuration); 55 | } 56 | 57 | $export = $this->exportFactory->create($path, $adapter, (bool) $cli->arguments->get('encode_data')); 58 | 59 | if($cli->arguments->get('attributes_only')) { 60 | $export->exportAttributes(); 61 | } else if($cli->arguments->get('products_data_only')) { 62 | $export->exportProductsDataOnly(); 63 | } else { 64 | $export->exportAttributes(); 65 | $export->exportCategories(); 66 | $export->exportProducts(); 67 | $export->exportCustomers(); 68 | } 69 | } catch (InvalidArgumentException $e) { 70 | $cli->error($e->getMessage()); 71 | $cli->usage(); 72 | } 73 | 74 | } 75 | 76 | private function initializeArguments(CLImate $cli) 77 | { 78 | $cli->arguments->add('mysql_user', [ 79 | 'prefix' => 'u', 80 | 'longPrefix' => 'db-user', 81 | 'description' => 'Database User', 82 | 'defaultValue' => get_current_user() 83 | ]); 84 | 85 | $cli->arguments->add('mysql_host', [ 86 | 'prefix' => 'h', 87 | 'longPrefix' => 'db-host', 88 | 'description' => 'Database Host', 89 | 'defaultValue' => 'localhost' 90 | ]); 91 | 92 | $cli->arguments->add('mysql_password', [ 93 | 'prefix' => 'p', 94 | 'longPrefix' => 'db-password', 95 | 'description' => 'Database Password', 96 | 'defaultValue' => '' 97 | ]); 98 | 99 | $cli->arguments->add('config', [ 100 | 'prefix' => 'c', 101 | 'longPrefix' => 'configuration', 102 | 'description' => 'JSON Configuration file', 103 | 'defaultValue' => '' 104 | ]); 105 | 106 | $cli->arguments->add('mysql_db', [ 107 | 'description' => 'Database Name', 108 | 'required' => true 109 | ]); 110 | 111 | $cli->arguments->add('target_path', [ 112 | 'description' => 'MagentoExport Directory', 113 | 'required' => true 114 | ]); 115 | 116 | $cli->arguments->add('attributes_only', [ 117 | 'prefix' => 'a', 118 | 'longPrefix' => 'attributes-only', 119 | 'description' => 'Only export Attribute data', 120 | 'defaultValue' => 0 121 | ]); 122 | 123 | $cli->arguments->add('products_data_only', [ 124 | 'prefix' => 'po', 125 | 'longPrefix' => 'products-data-only', 126 | 'description' => 'Only export product data', 127 | 'defaultValue' => 0 128 | ]); 129 | 130 | $cli->arguments->add('encode_data', [ 131 | 'prefix' => 'ed', 132 | 'longPrefix' => 'encode_data', 133 | 'description' => 'Bae 64 encode data to csv file', 134 | 'defaultValue' => 0 135 | ]); 136 | } 137 | } 138 | -------------------------------------------------------------------------------- /src/Feed.php: -------------------------------------------------------------------------------- 1 | csvFactory = $csvFactory; 44 | $this->eavMetadataImport = $eavMetadataImport; 45 | $this->categoryImport = $categoryImport; 46 | $this->productImport = $productImport; 47 | $this->customerImport = $customerImport; 48 | } 49 | 50 | public function importAttributesOnly() 51 | { 52 | $this->eavMetadataImport->importAttributes($this->csvFactory->createReader('attribute.csv')); 53 | $this->eavMetadataImport->importAttributeSets($this->csvFactory->createReader('attribute_set.csv')); 54 | $this->eavMetadataImport->importAttributeOptions($this->csvFactory->createReader('attribute_option.csv')); 55 | $this->eavMetadataImport->importAttributeOptionSwatches($this->csvFactory->createReader('attribute_option_swatch.csv')); 56 | } 57 | 58 | public function importAttributes() 59 | { 60 | $this->eavMetadataImport->importAttributes($this->csvFactory->createReader('attribute.csv')); 61 | $this->eavMetadataImport->importAttributeSets($this->csvFactory->createReader('attribute_set.csv')); 62 | $this->eavMetadataImport->importAttributeOptions($this->csvFactory->createReader('attribute_option.csv')); 63 | $this->eavMetadataImport->importAttributeOptionSwatches($this->csvFactory->createReader('attribute_option_swatch.csv')); 64 | 65 | } 66 | 67 | public function importCategories() 68 | { 69 | $this->categoryImport->importCategories($this->csvFactory->createReader('category.csv')); 70 | $this->categoryImport->importCategoryAttributes($this->csvFactory->createReader('category_data.csv')); 71 | } 72 | 73 | public function importProductsDataOnly() 74 | { 75 | $this->productImport->importProducts($this->csvFactory->createReader('product.csv')); 76 | $this->productImport->importProductData($this->csvFactory->createReader('product_data.csv')); 77 | } 78 | 79 | public function importProducts() 80 | { 81 | $this->productImport->importProducts($this->csvFactory->createReader('product.csv')); 82 | 83 | $this->productImport->importProductData($this->csvFactory->createReader('product_data.csv')); 84 | $this->productImport->importTierPrices($this->csvFactory->createReader('product_tier_prices.csv')); 85 | $this->productImport->importProductWebsite($this->csvFactory->createReader('product_website.csv')); 86 | $this->productImport->importProductCategory($this->csvFactory->createReader('product_category.csv')); 87 | $this->productImport->importStock($this->csvFactory->createReader('stock.csv')); 88 | $this->productImport->importGallery($this->csvFactory->createReader('product_image.csv')); 89 | $this->productImport->importGalleryValues($this->csvFactory->createReader('product_image_label.csv')); 90 | $this->productImport->importProductUrls($this->csvFactory->createReader('product_urls.csv')); 91 | $this->productImport->importProductConfigurableAttributes( 92 | $this->csvFactory->createReader('product_configurable_attribute.csv') 93 | ); 94 | $this->productImport->importProductConfigurableRelation( 95 | $this->csvFactory->createReader('product_configurable_relation.csv') 96 | ); 97 | $this->productImport->importGroupedProductRelation( 98 | $this->csvFactory->createReader('product_grouped_relation.csv') 99 | ); 100 | } 101 | 102 | public function importCustomers() 103 | { 104 | $this->customerImport->importCustomers($this->csvFactory->createReader('customer.csv')); 105 | $this->customerImport->importCustomerAddresses($this->csvFactory->createReader('customer_address.csv')); 106 | $this->customerImport->importCustomerBalance($this->csvFactory->createReader('customer_balance.csv')); 107 | } 108 | } 109 | -------------------------------------------------------------------------------- /src/ImportApplication.php: -------------------------------------------------------------------------------- 1 | dbFactory = $factory; 29 | $this->importFactory = $importFactory; 30 | } 31 | 32 | public static function create() 33 | { 34 | return new self(new ImportFactory(), new DbFactory()); 35 | } 36 | 37 | public function run(CLImate $cli) 38 | { 39 | $this->initializeArguments($cli); 40 | try { 41 | $cli->arguments->parse(); 42 | 43 | $adapter = $this->dbFactory->crateConnectionPool( 44 | $cli->arguments->get('mysql_host'), 45 | $cli->arguments->get('mysql_user'), 46 | $cli->arguments->get('mysql_password'), 47 | $cli->arguments->get('mysql_db') 48 | ); 49 | 50 | $path = $cli->arguments->get('target_path'); 51 | 52 | if (!is_dir($path)) { 53 | throw new InvalidArgumentException( 54 | sprintf('Import Data directory does not exists "%s" [target_path]', $path) 55 | ); 56 | } 57 | 58 | $import = $this->importFactory->create($path, $adapter, (bool) $cli->arguments->get('decode_data')); 59 | 60 | if ($cli->arguments->get('category_only')) { 61 | $import->importCategories(); 62 | } elseif($cli->arguments->get('attributes_only')) { 63 | $import->importAttributesOnly(); 64 | } elseif($cli->arguments->get('products_data_only')) { 65 | $import->importProductsDataOnly(); 66 | } else { 67 | $import->importAttributes(); 68 | $import->importCategories(); 69 | $import->importProducts(); 70 | $import->importCustomers(); 71 | } 72 | } catch (InvalidArgumentException $e) { 73 | $cli->error($e->getMessage()); 74 | $cli->usage(); 75 | } 76 | 77 | } 78 | 79 | private function createConnection(CLImate $cli): Adapter 80 | { 81 | return $this->dbFactory->createConnection( 82 | $cli->arguments->get('mysql_host'), 83 | $cli->arguments->get('mysql_user'), 84 | $cli->arguments->get('mysql_password'), 85 | $cli->arguments->get('mysql_db') 86 | ); 87 | } 88 | 89 | private function initializeArguments(CLImate $cli) 90 | { 91 | $cli->arguments->add('mysql_user', [ 92 | 'prefix' => 'u', 93 | 'longPrefix' => 'db-user', 94 | 'description' => 'Database User', 95 | 'defaultValue' => get_current_user() 96 | ]); 97 | 98 | $cli->arguments->add('mysql_host', [ 99 | 'prefix' => 'h', 100 | 'longPrefix' => 'db-host', 101 | 'description' => 'Database Host', 102 | 'defaultValue' => 'localhost' 103 | ]); 104 | 105 | $cli->arguments->add('mysql_password', [ 106 | 'prefix' => 'p', 107 | 'longPrefix' => 'db-password', 108 | 'description' => 'Database Password', 109 | 'defaultValue' => '' 110 | ]); 111 | 112 | $cli->arguments->add('mysql_db', [ 113 | 'description' => 'Database Name', 114 | 'required' => true 115 | ]); 116 | 117 | $cli->arguments->add('target_path', [ 118 | 'description' => 'Import Data Directory', 119 | 'required' => true 120 | ]); 121 | 122 | $cli->arguments->add('attributes_only', [ 123 | 'prefix' => 'a', 124 | 'longPrefix' => 'attributes-only', 125 | 'description' => 'Only export Attribute data', 126 | 'defaultValue' => 0 127 | ]); 128 | 129 | $cli->arguments->add('products_data_only', [ 130 | 'prefix' => 'po', 131 | 'longPrefix' => 'products-data-only', 132 | 'defaultValue' => 0 133 | ]); 134 | 135 | $cli->arguments->add('category_only', [ 136 | 'prefix' => 'co', 137 | 'longPrefix' => 'category-only', 138 | 'defaultValue' => 0 139 | ]); 140 | 141 | $cli->arguments->add('decode_data', [ 142 | 'prefix' => 'dd', 143 | 'longPrefix' => 'decode-data', 144 | 'defaultValue' => 0 145 | ]); 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /src/ImportFactory.php: -------------------------------------------------------------------------------- 1 | createConnection(); 19 | $readAdapter = $connectionPool->createConnection(); 20 | return new Import( 21 | new CsvFactory($directory, $decodeData), 22 | EavMetadataImport::createFromAdapter($adapter), 23 | CategoryImport::createFromAdapter($adapter, $readAdapter), 24 | ProductImport::createFromAdapter($adapter), 25 | CustomerImport::createFromAdapter($adapter) 26 | ); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/MagentoExport.php: -------------------------------------------------------------------------------- 1 | eavMetadataExport = $attributeExport; 38 | $this->categoryExport = $categoryExport; 39 | $this->productExport = $productExport; 40 | $this->customerExport = $customerExport; 41 | } 42 | 43 | public function exportAttributes() 44 | { 45 | $this->eavMetadataExport->exportAttributes('attribute.csv'); 46 | $this->eavMetadataExport->exportAttributeSets('attribute_set.csv'); 47 | $this->eavMetadataExport->exportAttributeOptions('attribute_option.csv'); 48 | } 49 | 50 | public function exportCategories() 51 | { 52 | $this->categoryExport->exportCategoryList('category.csv'); 53 | $this->categoryExport->exportCategoryData('category_data.csv'); 54 | } 55 | 56 | public function exportProducts() 57 | { 58 | $this->productExport->exportProductList('product.csv'); 59 | $this->productExport->exportProductData('product_data.csv'); 60 | $this->productExport->exportProductWebsite('product_website.csv'); 61 | $this->productExport->exportProductCategory('product_category.csv'); 62 | $this->productExport->exportProductStock('stock.csv'); 63 | $this->productExport->exportProductImages('product_image.csv'); 64 | $this->productExport->exportProductImageValues('product_image_label.csv'); 65 | $this->productExport->exportProductUrls('product_urls.csv'); 66 | $this->productExport->exportConfigurableAttributes('product_configurable_attribute.csv'); 67 | $this->productExport->exportConfigurableRelations('product_configurable_relation.csv'); 68 | $this->productExport->exportGroupedProductRelations('product_grouped_relation.csv'); 69 | } 70 | 71 | public function exportProductsDataOnly() 72 | { 73 | $this->productExport->exportProductList('product.csv'); 74 | $this->productExport->exportProductData('product_data.csv'); 75 | } 76 | 77 | public function exportCustomers() 78 | { 79 | $this->customerExport->exportCustomers('customer.csv'); 80 | $this->customerExport->exportCustomerAddresses('customer_address.csv'); 81 | // TODO: add a check for customer balance existence and enable it back 82 | // $this->customerExport->exportCustomerBalance('customer_balance.csv'); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/MagentoExportFactory.php: -------------------------------------------------------------------------------- 1 | configuration as $fileName => $options) { 28 | if (!isset($options['skip']) && !isset($options['map'])) { 29 | continue; 30 | } 31 | 32 | $skip = $options['skip'] ?? []; 33 | foreach ($skip as $condition) { 34 | $csvFactory = $csvFactory->withSkip($fileName, $condition); 35 | } 36 | 37 | $map = $options['map'] ?? []; 38 | 39 | foreach ($map as $column => $values) { 40 | $csvFactory = $csvFactory->withMap($fileName, $column, $values); 41 | } 42 | } 43 | 44 | $categoryFactory = CategoryInfoFactory::createFromAdapter($adapter) 45 | ->withStoreFilter($this->configuration['category']['active_filter'] ?? []) 46 | ->withStoreMap($this->configuration['category']['stores'] ?? []); 47 | 48 | $productFactory = ProductInfoFactory::createFromAdapter($adapter) 49 | ->withStoreMap($this->configuration['category']['stores'] ?? []) 50 | ->withIgnoredAttributes($this->configuration['product']['ignored_attributes'] ?? []); 51 | 52 | 53 | $customerFactory = CustomerFeedFactory::createFromAdapter($adapter); 54 | 55 | /** @var FeedFactory[] $mapperTypes */ 56 | $mapperTypes = [ 57 | 'product' => $productFactory, 58 | 'customer' => $customerFactory 59 | ]; 60 | 61 | foreach ($mapperTypes as $type => $feedFactory) { 62 | $mappers = $this->configuration[$type]['mappers'] ?? []; 63 | $mapperTypes[$type] = $this->applyMapperConfiguration($mappers, $feedFactory); 64 | } 65 | 66 | $productFactory = $mapperTypes['product']; 67 | $customerFactory = $mapperTypes['customer']; 68 | 69 | 70 | return new MagentoExport( 71 | new EavMetadataExport($eavInfo, $csvFactory), 72 | new CategoryExport($categoryFactory->create(), $csvFactory), 73 | new ProductExport( 74 | $productFactory->create(), 75 | TableRangeConditionGeneratorFactory::createFromAdapter($adapter) 76 | ->createForTable('catalog_product_entity', 'entity_id'), 77 | $csvFactory 78 | ), 79 | new CustomerExport( 80 | $customerFactory->create(), 81 | TableRangeConditionGeneratorFactory::createFromAdapter($adapter) 82 | ->createForTable('customer_entity', 'entity_id'), 83 | $csvFactory 84 | ) 85 | ); 86 | } 87 | 88 | public function withConfiguration(array $configuration): self 89 | { 90 | $factory = clone $this; 91 | $factory->configuration = $configuration; 92 | return $factory; 93 | } 94 | 95 | private function createMapper(array $mapperConfiguration): RowMapper 96 | { 97 | $mapperClass = $mapperConfiguration['class']; 98 | $mapper = new $mapperClass(); 99 | $setup = $mapperConfiguration['setup'] ?? []; 100 | foreach ($setup as $call) { 101 | $method = array_shift($call); 102 | $mapper = $mapper->{$method}(...$call); 103 | } 104 | 105 | return $mapper; 106 | } 107 | 108 | /** 109 | * @return ProductInfoFactory|CustomerFeedFactory 110 | */ 111 | protected function applyMapperConfiguration(array $mappers, FeedFactory $feedFactory): FeedFactory 112 | { 113 | foreach ($mappers as $mapperCode => $mapperList) { 114 | foreach ($mapperList as $mapperConfiguration) { 115 | $feedFactory = $feedFactory->withRowMapper( 116 | $mapperCode, 117 | $this->createMapper($mapperConfiguration) 118 | ); 119 | } 120 | } 121 | 122 | return $feedFactory; 123 | } 124 | } 125 | -------------------------------------------------------------------------------- /src/MergedRowMapper.php: -------------------------------------------------------------------------------- 1 | csvReader = $csvReader ?? new CsvReader(); 23 | } 24 | 25 | public function apply(iterable $rows): iterable 26 | { 27 | foreach ($rows as $row) { 28 | yield $row; 29 | } 30 | 31 | foreach ($this->csvFiles as $additionalRows) { 32 | foreach ($additionalRows as $row) { 33 | yield $row; 34 | } 35 | } 36 | } 37 | 38 | public function withCsvFile(string $fileName): self 39 | { 40 | $mapper = clone $this; 41 | $mapper->csvFiles[] = $this->csvReader->readFile($fileName); 42 | return $mapper; 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/ProductExport.php: -------------------------------------------------------------------------------- 1 | productInfo = $productInfo; 34 | $this->conditionGenerator = $conditionGenerator; 35 | $this->csvFactory = $csvFactory; 36 | } 37 | 38 | public function exportProductList(string $fileName) 39 | { 40 | $writer = $this->csvFactory->createWriter($fileName, ['sku', 'type', 'set']); 41 | 42 | foreach ($this->productInfo->fetchProducts($this->conditionGenerator) as $row) { 43 | $writer->write($row); 44 | } 45 | } 46 | 47 | public function exportProductData(string $fileName) 48 | { 49 | $writer = $this->csvFactory->createWriter($fileName, ['sku', 'attribute', 'store', 'value']); 50 | 51 | foreach ($this->productInfo->fetchProductAttributes($this->conditionGenerator) as $row) { 52 | $writer->write($row); 53 | } 54 | } 55 | 56 | public function exportProductWebsite(string $fileName) 57 | { 58 | $writer = $this->csvFactory->createWriter($fileName, ['sku', 'store']); 59 | 60 | foreach ($this->productInfo->fetchProductWebsite($this->conditionGenerator) as $row) { 61 | $writer->write($row); 62 | } 63 | } 64 | 65 | public function exportProductCategory(string $fileName) 66 | { 67 | $writer = $this->csvFactory->createWriter($fileName, ['sku', 'category', 'position']); 68 | 69 | foreach ($this->productInfo->fetchProductCategories($this->conditionGenerator) as $row) { 70 | $writer->write($row); 71 | } 72 | } 73 | 74 | public function exportProductStock(string $fileName) 75 | { 76 | $writer = $this->csvFactory->createWriter($fileName, ['sku', 'stock', 'in_stock', 'qty']); 77 | 78 | foreach ($this->productInfo->fetchProductStock($this->conditionGenerator) as $row) { 79 | $writer->write($row); 80 | } 81 | } 82 | 83 | public function exportProductImages(string $fileName) 84 | { 85 | $writer = $this->csvFactory->createWriter($fileName, ['sku', 'image']); 86 | 87 | foreach ($this->productInfo->fetchProductGallery($this->conditionGenerator) as $row) { 88 | $writer->write($row); 89 | } 90 | } 91 | 92 | public function exportProductImageValues(string $fileName) 93 | { 94 | $writer = $this->csvFactory->createWriter($fileName, ['sku', 'image', 'store', 'label', 'position']); 95 | 96 | foreach ($this->productInfo->fetchProductGalleryValues($this->conditionGenerator) as $row) { 97 | $writer->write($row); 98 | } 99 | } 100 | 101 | public function exportProductUrls(string $fileName) 102 | { 103 | $writer = $this->csvFactory->createWriter($fileName, ['sku', 'store', 'url']); 104 | 105 | foreach ($this->productInfo->fetchProductUrls($this->conditionGenerator) as $row) { 106 | $writer->write($row); 107 | } 108 | } 109 | 110 | public function exportConfigurableAttributes(string $fileName) 111 | { 112 | $writer = $this->csvFactory->createWriter($fileName, ['sku', 'attribute', 'position', 'label']); 113 | 114 | foreach ($this->productInfo->fetchProductConfigurableAttributes($this->conditionGenerator) as $row) { 115 | $writer->write($row); 116 | } 117 | } 118 | 119 | public function exportConfigurableRelations(string $fileName) 120 | { 121 | $writer = $this->csvFactory->createWriter($fileName, ['sku', 'child_sku']); 122 | 123 | foreach ($this->productInfo->fetchProductConfigurableRelations($this->conditionGenerator) as $row) { 124 | $writer->write($row); 125 | } 126 | } 127 | 128 | public function exportGroupedProductRelations(string $fileName) 129 | { 130 | $writer = $this->csvFactory->createWriter($fileName, ['product_sku', 'linked_product_sku']); 131 | 132 | foreach ($this->productInfo->fetchGroupedProductRelations($this->conditionGenerator) as $row) { 133 | $writer->write($row); 134 | } 135 | } 136 | } 137 | -------------------------------------------------------------------------------- /src/ProductInfoFactory.php: -------------------------------------------------------------------------------- 1 | eavInfo = $eavInfo; 40 | $this->sql = $sql; 41 | } 42 | 43 | public static function createFromAdapter(Adapter $adapter): self 44 | { 45 | return new self( 46 | MagentoEavInfo::createFromAdapter($adapter), 47 | new Sql($adapter), 48 | Factory::createSourceFromAdapter($adapter) 49 | ); 50 | } 51 | 52 | /** @return ProductInfo */ 53 | public function create(): Feed 54 | { 55 | return new ProductInfo($this->eavInfo, $this->sql, $this->storeMap, $this->ignoredAttributes, $this->rowMappers); 56 | } 57 | 58 | public function withStoreMap(array $storeMap): self 59 | { 60 | $factory = clone $this; 61 | $factory->storeMap = $storeMap; 62 | return $factory; 63 | } 64 | 65 | public function withIgnoredAttributes(array $attributeCodes) 66 | { 67 | $factory = clone $this; 68 | $factory->ignoredAttributes = $attributeCodes; 69 | 70 | return $factory; 71 | } 72 | 73 | /** 74 | * @return $this 75 | */ 76 | public function withRowMapper(string $feedCode, RowMapper $rowMapper): FeedFactory 77 | { 78 | $factory = clone $this; 79 | 80 | $piped = $factory->rowMappers[$feedCode] ?? new CombinedRowMapper(); 81 | $factory->rowMappers[$feedCode] = $piped->pipe($rowMapper); 82 | 83 | return $factory; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/RowMapper.php: -------------------------------------------------------------------------------- 1 | values = $values; 22 | } 23 | 24 | public function apply(string $field, Where $where): void 25 | { 26 | if (!$this->values) { 27 | $where->equalTo($field, null) 28 | ->notEqualTo($field, null); 29 | 30 | return; 31 | } 32 | 33 | $where->in($field, $this->values); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/SelectRangeCondition.php: -------------------------------------------------------------------------------- 1 | from = $from; 29 | $this->to = $to; 30 | } 31 | 32 | public function apply(string $field, Where $where): void 33 | { 34 | $where->greaterThanOrEqualTo($field, $this->from); 35 | $where->lessThanOrEqualTo($field, $this->to); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/Sql/CombinedTableResolver.php: -------------------------------------------------------------------------------- 1 | sql = $sql; 57 | $this->tableName = $tableName; 58 | $this->sourceField = $sourceField; 59 | $this->targetField = $targetField; 60 | $this->foreignResolver = $foreignResolver; 61 | $this->foreignField = $foreignField; 62 | } 63 | 64 | /** 65 | * @throws IdentifierNotResolved when value does not exists and cannot be generated 66 | */ 67 | public function resolve(Identifier $value): int 68 | { 69 | $this->resolveValues(); 70 | 71 | return $value->findValue($this->resolvedValues); 72 | } 73 | 74 | private function resolveValues() 75 | { 76 | if (!$this->unresolvedValues) { 77 | return; 78 | } 79 | 80 | $unresolvedValueList = $this->unresolvedValues; 81 | 82 | $resolveValue = array_map( 83 | function ($value) { 84 | return explode('|', $value, 2)[0]; 85 | }, 86 | array_keys($unresolvedValueList) 87 | ); 88 | 89 | $foreignValueList = array_filter(array_map( 90 | function (Identifier $id) { 91 | try { 92 | return $this->foreignResolver->resolve($id); 93 | } catch (IdentifierNotResolved $exception) { 94 | return null; 95 | } 96 | }, 97 | $this->foreignValues 98 | )); 99 | 100 | $this->unresolvedValues = []; 101 | $this->foreignValues = []; 102 | 103 | if (!$foreignValueList || !$unresolvedValueList) { 104 | return; 105 | } 106 | 107 | $select = $this->sql->select($this->tableName) 108 | ->columns([ 109 | $this->targetField, $this->sourceField, $this->foreignField 110 | ]) 111 | ->where([$this->sourceField => $resolveValue, $this->foreignField => $foreignValueList]); 112 | 113 | $foreignMap = array_flip($foreignValueList); 114 | 115 | foreach ($this->sql->prepareStatementForSqlObject($select)->execute() as $row) { 116 | $key = implode('|', [$row[$this->sourceField], $foreignMap[$row[$this->foreignField]]]); 117 | $this->resolvedValues[$key] = (int)$row[$this->targetField]; 118 | unset($unresolvedValueList[$key]); 119 | } 120 | 121 | $this->sql->getAdapter()->getDriver()->getConnection()->beginTransaction(); 122 | 123 | foreach (array_keys($unresolvedValueList) as $key) { 124 | list($source, $foreignValue) = explode('|', $key, 2); 125 | 126 | if (!isset($foreignValueList[$foreignValue])) { 127 | continue; 128 | } 129 | 130 | $this->resolvedValues[$key] = (int)$this->sql->prepareStatementForSqlObject( 131 | $this->sql->insert($this->tableName) 132 | ->values([$this->sourceField => $source, $this->foreignField => $foreignValueList[$foreignValue]]) 133 | )->execute()->getGeneratedValue(); 134 | } 135 | 136 | $this->sql->getAdapter()->getDriver()->getConnection()->rollback(); 137 | } 138 | 139 | /** 140 | * Creates a new resolvable id 141 | */ 142 | public function unresolved($value): Identifier 143 | { 144 | $key = implode('|', $value); 145 | 146 | if (isset($this->resolvedValues[$key])) { 147 | return new ResolvedIdentifier($this->resolvedValues[$key]); 148 | } 149 | 150 | list(, $foreignKey) = $value; 151 | 152 | $this->foreignValues[$foreignKey] = $this->foreignValues[$foreignKey] ?? $this->foreignResolver->unresolved($foreignKey); 153 | $this->unresolvedValues[$key] = $this->unresolvedValues[$key] 154 | ?? new UnresolvedIdentifier($key, [$this, 'removeId']); 155 | 156 | return $this->unresolvedValues[$key]; 157 | } 158 | 159 | public function removeId(string $value): void 160 | { 161 | unset($this->resolvedValues[$value], $this->unresolvedValues[$value]); 162 | } 163 | } 164 | -------------------------------------------------------------------------------- /src/Sql/IdResolver.php: -------------------------------------------------------------------------------- 1 | columnNames = $columns; 60 | $insert->columns($columns); 61 | return $insert; 62 | } 63 | 64 | public function withRow(...$row): self 65 | { 66 | $this->rows[] = $row; 67 | return $this; 68 | } 69 | 70 | public function onDuplicate(array $columnsToUpdate): self 71 | { 72 | $this->onDuplicate = $columnsToUpdate; 73 | return $this; 74 | } 75 | 76 | 77 | private function resolveRows() 78 | { 79 | foreach ($this->rows as $row) { 80 | foreach ($row as $index => $value) { 81 | if ($value instanceof Identifier) { 82 | foreach ($this->resolver as $resolver) { 83 | try { 84 | $row[$index] = $value = $resolver->resolve($value); 85 | 86 | break; 87 | } catch (IdentifierNotResolved $e) { 88 | continue; 89 | } 90 | } 91 | 92 | if ($value instanceof Identifier) { 93 | if ($this->nullOnUnresolved) { 94 | $row[$index] = null; 95 | continue; 96 | } 97 | 98 | continue 2; 99 | } 100 | 101 | 102 | 103 | } 104 | } 105 | 106 | $this->resolvedCount++; 107 | foreach ($row as $index => $value) { 108 | $columnName = $this->columnNames[$index]; 109 | 110 | if (isset($this->formatted[$columnName]) && $value !== null) { 111 | $value = sprintf($this->formatted[$columnName], $value); 112 | } 113 | 114 | $this->resolvedParams[] = $value; 115 | } 116 | } 117 | 118 | $this->rows = []; 119 | } 120 | 121 | public function flushIfLimitReached(Sql $sql, $limit = 2000): self 122 | { 123 | if (count($this->rows) + $this->resolvedCount > $limit) { 124 | $this->resolveRows(); 125 | $this->executeResolvedValues($limit, $sql->getAdapter()); 126 | } 127 | 128 | return $this; 129 | } 130 | 131 | public function executeIfNotEmpty(Sql $sql): self 132 | { 133 | $this->resolveRows(); 134 | if ($this->resolvedCount) { 135 | $this->executeResolvedValues($this->resolvedCount, $sql->getAdapter()); 136 | } 137 | 138 | return $this; 139 | } 140 | 141 | private function limitStatement($limit, AdapterInterface $adapter): StatementInterface 142 | { 143 | if (!isset($this->statements[$limit])) { 144 | $this->statements[$limit] = $adapter->getDriver()->createStatement( 145 | $this->generateInsertSQL($limit, $adapter) 146 | ); 147 | } 148 | 149 | return $this->statements[$limit]; 150 | } 151 | 152 | 153 | public function withResolver(IdResolver $resolver): self 154 | { 155 | $insert = clone $this; 156 | $insert->resolver[] = $resolver; 157 | return $insert; 158 | } 159 | 160 | public function withNullOnUnresolved(): self 161 | { 162 | $insert = clone $this; 163 | $insert->nullOnUnresolved = true; 164 | return $insert; 165 | } 166 | 167 | public function withFormatted(string $field, string $format): self 168 | { 169 | $insert = clone $this; 170 | $insert->formatted[$field] = $format; 171 | return $insert; 172 | } 173 | 174 | public function withAssocRow(array $row) 175 | { 176 | $flatRow = []; 177 | foreach ($this->columnNames as $name) { 178 | $flatRow[] = $row[$name] ?? null; 179 | } 180 | 181 | $this->rows[] = $flatRow; 182 | return $this; 183 | } 184 | 185 | private function generateInsertSQL(int $rowsCount, AdapterInterface $adapter) 186 | { 187 | $rowTemplate = sprintf('(%s)', implode(',', array_fill(0, count($this->columnNames), '?'))); 188 | 189 | $statement = sprintf( 190 | 'INSERT INTO %s (%s) VALUES %s%s', 191 | $this->resolveTable($this->table, $adapter->getPlatform(), $adapter->getDriver()), 192 | implode(',', array_map([$adapter->getPlatform(), 'quoteIdentifier'], $this->columnNames)), 193 | str_repeat( 194 | sprintf('%s, ', $rowTemplate), 195 | $rowsCount - 1 196 | ), 197 | $rowTemplate 198 | ); 199 | 200 | if (!$this->onDuplicate) { 201 | return $statement; 202 | } 203 | 204 | $onDuplicateStatements = []; 205 | 206 | foreach ($this->onDuplicate as $columnName) { 207 | $columnName = $adapter->getPlatform()->quoteIdentifier($columnName); 208 | $onDuplicateStatements[] = sprintf('%1$s = VALUES(%1$s)', $columnName); 209 | } 210 | 211 | return sprintf('%s ON DUPLICATE KEY UPDATE %s', $statement, implode(', ', $onDuplicateStatements)); 212 | } 213 | 214 | /** 215 | * @param $limit 216 | * 217 | */ 218 | private function executeResolvedValues($limit, AdapterInterface $adapter): void 219 | { 220 | while ($this->resolvedCount >= $limit) { 221 | $paramCount = $limit * count($this->columnNames); 222 | $parameters = array_slice($this->resolvedParams, 0, $paramCount); 223 | $this->resolvedParams = array_slice($this->resolvedParams, $paramCount); 224 | $this->resolvedCount -= $limit; 225 | $adapter->getDriver()->createStatement('SET FOREIGN_KEY_CHECKS=0')->execute(); 226 | $this->limitStatement($limit, $adapter)->execute($parameters); 227 | $adapter->getDriver()->createStatement('SET FOREIGN_KEY_CHECKS=1')->execute(); 228 | } 229 | } 230 | } 231 | -------------------------------------------------------------------------------- /src/Sql/ResolvedIdentifier.php: -------------------------------------------------------------------------------- 1 | value = $value; 22 | } 23 | 24 | public function findValue(array $resolved): int 25 | { 26 | return $this->value; 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Sql/SingleTableResolver.php: -------------------------------------------------------------------------------- 1 | sql = $sql; 49 | $this->tableName = $tableName; 50 | $this->sourceField = $sourceField; 51 | $this->targetField = $targetField; 52 | } 53 | 54 | public function withAutoIncrement(array $defaultRow): self 55 | { 56 | $resolver = clone $this; 57 | $resolver->incrementRow = $defaultRow; 58 | return $resolver; 59 | } 60 | 61 | /** 62 | * @throws IdentifierNotResolved when value does not exists and cannot be generated 63 | */ 64 | public function resolve(Identifier $value): int 65 | { 66 | $this->resolveValues(); 67 | 68 | return $value->findValue($this->resolvedValues); 69 | } 70 | 71 | private function resolveValues() 72 | { 73 | if (!$this->unresolvedValues) { 74 | return; 75 | } 76 | 77 | $resolveValue = $this->unresolvedValues; 78 | $this->unresolvedValues = []; 79 | 80 | $inCondition = []; 81 | foreach ($resolveValue as $key => $value) { 82 | unset($value); 83 | $inCondition[] = (string)$key; 84 | } 85 | 86 | $select = $this->sql->select($this->tableName) 87 | ->columns([$this->targetField, $this->sourceField]) 88 | ->where([$this->sourceField => $inCondition] + $this->filter); 89 | 90 | foreach ($this->sql->prepareStatementForSqlObject($select)->execute() as $row) { 91 | $this->resolvedValues[$row[$this->sourceField]] = (int)$row[$this->targetField]; 92 | unset($resolveValue[$row[$this->sourceField]]); 93 | } 94 | 95 | if ($this->incrementRow && $resolveValue) { 96 | $this->sql->getAdapter()->getDriver()->getConnection()->beginTransaction(); 97 | 98 | $incrementRow = $this->incrementRow; 99 | unset($incrementRow[$this->sourceField]); 100 | 101 | $columns = array_keys($incrementRow); 102 | $columns[] = $this->sourceField; 103 | $baseRow = array_values($incrementRow); 104 | 105 | $insert = InsertOnDuplicate::create($this->tableName, $columns); 106 | $inCondition = []; 107 | 108 | foreach (array_keys($resolveValue) as $key) { 109 | $row = $baseRow; 110 | $row[] = $key; 111 | $inCondition[] = (string)$key; 112 | $insert = $insert->withRow(...$row); 113 | } 114 | 115 | $insert->executeIfNotEmpty($this->sql); 116 | 117 | $select = $this->sql->select($this->tableName) 118 | ->columns([$this->targetField, $this->sourceField]) 119 | ->where([$this->sourceField => $inCondition] + $this->filter); 120 | 121 | foreach ($this->sql->prepareStatementForSqlObject($select)->execute() as $row) { 122 | $this->resolvedValues[$row[$this->sourceField]] = (int)$row[$this->targetField]; 123 | } 124 | 125 | $this->sql->getAdapter()->getDriver()->getConnection()->rollback(); 126 | } 127 | } 128 | 129 | /** 130 | * Creates a new resolvable id 131 | */ 132 | public function unresolved($value): Identifier 133 | { 134 | if (isset($this->resolvedValues[$value])) { 135 | return new ResolvedIdentifier($this->resolvedValues[$value]); 136 | } 137 | 138 | $this->unresolvedValues[$value] = $this->unresolvedValues[$value] 139 | ?? new UnresolvedIdentifier($value, [$this, 'removeId']); 140 | 141 | return $this->unresolvedValues[$value]; 142 | } 143 | 144 | public function removeId(string $value): void 145 | { 146 | unset($this->resolvedValues[$value], $this->unresolvedValues[$value]); 147 | } 148 | 149 | public function withFilter(array $filter): self 150 | { 151 | $resolver = clone $this; 152 | $resolver->filter = $filter; 153 | return $resolver; 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /src/Sql/TableResolverFactory.php: -------------------------------------------------------------------------------- 1 | sql = $sql; 25 | } 26 | 27 | public static function createFromAdapter(Adapter $adapter) 28 | { 29 | return new self(new Sql($adapter)); 30 | } 31 | 32 | public function createSingleValueResolver(string $tableName, string $searchField, string $targetField): SingleTableResolver 33 | { 34 | return new SingleTableResolver($this->sql, $tableName, $searchField, $targetField); 35 | } 36 | 37 | public function createCombinedValueResolver( 38 | string $tableName, 39 | string $targetField, 40 | string $searchField, 41 | string $foreignField, 42 | IdResolver $foreignResolver 43 | ) { 44 | return new CombinedTableResolver( 45 | $this->sql, 46 | $tableName, 47 | $searchField, 48 | $targetField, 49 | $foreignField, 50 | $foreignResolver 51 | ); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/Sql/UnresolvedIdentifier.php: -------------------------------------------------------------------------------- 1 | onDestroy = $onDestroy; 25 | $this->value = $value; 26 | } 27 | 28 | public function findValue(array $resolved): int 29 | { 30 | if (!isset($resolved[$this->value])) { 31 | throw new IdentifierNotResolved(); 32 | } 33 | 34 | return $resolved[$this->value]; 35 | } 36 | 37 | public function __destruct() 38 | { 39 | ($this->onDestroy)($this->value); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/TableRangeConditionGenerator.php: -------------------------------------------------------------------------------- 1 | conditionFactory = $conditionFactory; 43 | $this->minValue = $minValue; 44 | $this->maxValue = $maxValue; 45 | $this->ranges = $ranges; 46 | } 47 | 48 | public function conditions(): \Generator 49 | { 50 | $start = $this->minValue; 51 | 52 | $currentRange = 0; 53 | 54 | do { 55 | $rangeMax = min($this->ranges[$currentRange] ?? ($start + self::STEP + 1), $this->maxValue); 56 | yield $this->conditionFactory->range($start, $rangeMax); 57 | $start = $rangeMax + 1; 58 | $currentRange ++; 59 | } while ($start <= $this->maxValue); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/TableRangeConditionGeneratorFactory.php: -------------------------------------------------------------------------------- 1 | sql = $sql; 30 | $this->conditionFactory = $conditionFactory; 31 | } 32 | 33 | public static function createFromAdapter(Adapter $adapter) 34 | { 35 | return new self(new Sql($adapter), new SelectConditionFactory()); 36 | } 37 | 38 | public function createForTable(string $tableName, string $fieldName): TableRangeConditionGenerator 39 | { 40 | $select = $this->sql->select($tableName) 41 | ->columns([ 42 | 'min' => new Literal(sprintf('MIN(%s)', $fieldName)), 43 | 'max' => new Literal(sprintf('MAX(%s)', $fieldName)), 44 | ]); 45 | 46 | list($minValue, $maxValue) = array_values( 47 | $this->sql->prepareStatementForSqlObject($select)->execute()->current() 48 | ); 49 | 50 | 51 | $range = new Literal(sprintf('CEIL(%s / 2000) * 2000', $fieldName)); 52 | 53 | $select = $this->sql->select($tableName) 54 | ->columns([ 55 | 'range' => $range 56 | ]) 57 | ->group($range); 58 | ; 59 | 60 | $ranges = []; 61 | foreach ($this->sql->prepareStatementForSqlObject($select)->execute() as $row) { 62 | $ranges[] = (int)$row['range']; 63 | } 64 | 65 | return new TableRangeConditionGenerator($this->conditionFactory, (int)$minValue, (int)$maxValue, $ranges); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /tests/ArrayConditionGeneratorTest.php: -------------------------------------------------------------------------------- 1 | assertEquals( 20 | [], 21 | iterator_to_array(ArrayConditionGenerator::create()->conditions()) 22 | ); 23 | } 24 | 25 | /** @test */ 26 | public function returnsProvidedConditions() 27 | { 28 | $conditionFactory = new SelectConditionFactory(); 29 | 30 | $this->assertEquals( 31 | [ 32 | $conditionFactory->range(1, 100), 33 | $conditionFactory->range(1, 200), 34 | ], 35 | iterator_to_array(ArrayConditionGenerator::create( 36 | $conditionFactory->range(1, 100), 37 | $conditionFactory->range(1, 200) 38 | )->conditions()) 39 | ); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /tests/CombinedRowMapperTest.php: -------------------------------------------------------------------------------- 1 | rowMapper = new CombinedRowMapper(); 22 | } 23 | 24 | /** @test */ 25 | public function passesAsIsNoMappersProvided() 26 | { 27 | $this->assertEquals( 28 | [ 29 | ['sku' => 'sku1', 'attribute' => 'name', 'store' => '', 'value' => 'Sku1 Name'], 30 | ['sku' => 'sku2', 'attribute' => 'name', 'store' => '', 'value' => 'Sku2 Name'] 31 | ], 32 | $this->asArray( 33 | $this->rowMapper->apply($this->arrayIteratable( 34 | ['sku' => 'sku1', 'attribute' => 'name', 'store' => '', 'value' => 'Sku1 Name'], 35 | ['sku' => 'sku2', 'attribute' => 'name', 'store' => '', 'value' => 'Sku2 Name'] 36 | )) 37 | ) 38 | ); 39 | } 40 | 41 | /** @test */ 42 | public function pipesRowMappersTogether() 43 | { 44 | $rowMapper = $this->rowMapper 45 | ->pipe((new MergedRowMapper())->withCsvFile(__DIR__ . '/fixture/file1.csv')) 46 | ->pipe((new MergedRowMapper())->withCsvFile(__DIR__ . '/fixture/file2.csv')) 47 | ; 48 | 49 | $this->assertEquals( 50 | [ 51 | ['sku' => 'sku1', 'attribute' => 'name', 'store' => '', 'value' => 'Sku1 Name'], 52 | ['sku' => 'sku2', 'attribute' => 'name', 'store' => '', 'value' => 'Sku2 Name'], 53 | ['sku' => 'sku1', 'attribute' => 'attribute1', 'store' => '', 'value' => 'value1'], 54 | ['sku' => 'sku1', 'attribute' => 'attribute1', 'store' => 'en_us', 'value' => 'value2'], 55 | ['sku' => 'sku2', 'attribute' => 'attribute1', 'store' => 'en_uk', 'value' => 'value3'], 56 | ['sku' => 'sku1', 'attribute' => 'attribute2', 'store' => '', 'value' => 'value1.1'], 57 | ['sku' => 'sku1', 'attribute' => 'attribute2', 'store' => 'en_us', 'value' => 'value1.2'], 58 | ['sku' => 'sku2', 'attribute' => 'attribute2', 'store' => 'en_uk', 'value' => 'value1.3'] 59 | ], 60 | $this->asArray( 61 | $rowMapper->apply($this->arrayIteratable( 62 | ['sku' => 'sku1', 'attribute' => 'name', 'store' => '', 'value' => 'Sku1 Name'], 63 | ['sku' => 'sku2', 'attribute' => 'name', 'store' => '', 'value' => 'Sku2 Name'] 64 | )) 65 | ) 66 | ); 67 | } 68 | 69 | private function asArray(iterable $traversable): array 70 | { 71 | return iterator_to_array($traversable); 72 | } 73 | 74 | private function arrayIteratable(array ...$items): iterable 75 | { 76 | return new \ArrayIterator($items); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /tests/CsvFactoryTest.php: -------------------------------------------------------------------------------- 1 | testDirectory = TestDirectory::create(); 24 | $this->factory = new CsvFactory($this->testDirectory->resolvePath('')); 25 | } 26 | 27 | /** @test */ 28 | public function createsWriterWithNewLineAndCommaSeparatedContent() 29 | { 30 | $writer = $this->factory->createWriter('file1.csv', ['one', 'two', 'three']); 31 | $writer->write(['one' => 'value1', 'two' => 'value2', 'three' => 'value3']); 32 | 33 | $this->assertStringEqualsFile( 34 | $this->testDirectory->resolvePath('file1.csv'), 35 | 'one,two,three' . PHP_EOL . 'value1,value2,value3' . PHP_EOL 36 | ); 37 | } 38 | 39 | /** @test */ 40 | public function outputsCsvFileWithMultiLineValues() 41 | { 42 | $writer = $this->factory->createWriter('file1.csv', ['one', 'two', 'three']); 43 | $writer->write(['one' => 'value1', 'two' => "value2\nnewline\nanother one", 'three' => 'value3']); 44 | 45 | 46 | $this->assertStringEqualsFile( 47 | $this->testDirectory->resolvePath('file1.csv'), 48 | 'one,two,three' . PHP_EOL . 'value1,"value2 49 | newline 50 | another one",value3' . PHP_EOL 51 | ); 52 | } 53 | 54 | /** @test */ 55 | public function enclosesEscapedCharsInOutput() 56 | { 57 | $writer = $this->factory->createWriter('file1.csv', ['one', 'two', 'three']); 58 | $writer->write(['one' => 'value1', 'two' => "value2\\\"\nnewline\nanother one", 'three' => 'value3']); 59 | 60 | 61 | $this->assertStringEqualsFile( 62 | $this->testDirectory->resolvePath('file1.csv'), 63 | 'one,two,three' . PHP_EOL . 'value1,"value2\\"" 64 | newline 65 | another one",value3' . PHP_EOL 66 | ); 67 | } 68 | 69 | /** @test */ 70 | public function readsEscpaedCharOutput() 71 | { 72 | file_put_contents( 73 | $this->testDirectory->resolvePath('file2.csv'), 74 | 'one,two,three' . PHP_EOL . 'value1,"value2\\"" 75 | newline 76 | another one",value3' . PHP_EOL 77 | ); 78 | 79 | 80 | $this->assertEquals( 81 | [ 82 | ['one' => 'value1', 'two' => "value2\\\"\nnewline\nanother one", 'three' => 'value3'] 83 | ], 84 | iterator_to_array($this->factory->createReader('file2.csv')) 85 | ); 86 | } 87 | 88 | 89 | /** @test */ 90 | public function createsReadCsvInstance() 91 | { 92 | file_put_contents( 93 | $this->testDirectory->resolvePath('file2.csv'), 94 | 'item1,item2,item3' . PHP_EOL . 'value1,value2,value3' . PHP_EOL 95 | ); 96 | 97 | $this->assertEquals( 98 | [ 99 | [ 100 | 'item1' => 'value1', 101 | 'item2' => 'value2', 102 | 'item3' => 'value3' 103 | ], 104 | ], 105 | 106 | iterator_to_array($this->factory->createReader('file2.csv')) 107 | ); 108 | } 109 | 110 | /** @test */ 111 | public function readsEmptyFileWhenFileDoesNotExists() 112 | { 113 | $this->assertEquals( 114 | [], 115 | iterator_to_array( 116 | $this->factory->createReader($this->testDirectory->resolvePath('file_not-exists.csv')) 117 | ) 118 | ); 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /tests/CsvWriterTest.php: -------------------------------------------------------------------------------- 1 | testDirectory = TestDirectory::create(); 25 | $this->factory = new CsvFactory($this->testDirectory->resolvePath('')); 26 | } 27 | 28 | /** @test */ 29 | public function writesDataIntoCsvFileBasedOnHeaderSpecification() 30 | { 31 | $writer = $this->factory->createWriter('file_with_data.csv', ['name', 'status', 'value']); 32 | $writer->write(['name' => 'Foo', 'value' => 'bar', 'status' => 0, 'other' => 'stuff']); 33 | 34 | $this->assertEquals( 35 | [['name' => 'Foo', 'value' => 'bar', 'status' => 0]], 36 | iterator_to_array($this->factory->createReader('file_with_data.csv')) 37 | ); 38 | } 39 | 40 | /** @test */ 41 | public function filtersOutputBasedOnSkipFilters() 42 | { 43 | $writer = $this->factory 44 | ->withSkip('file_with_data.csv', ['value' => ['bar2'], 'status' => [1]]) 45 | ->withSkip('file_with_data.csv', ['name' => ['Foo2.3']]) 46 | ->createWriter('file_with_data.csv', ['name', 'value', 'status']); 47 | ; 48 | 49 | $writer->write(['name' => 'Foo1', 'value' => 'bar1', 'status' => 0]); 50 | $writer->write(['name' => 'Foo2', 'value' => 'bar2', 'status' => 1]); 51 | $writer->write(['name' => 'Foo3', 'value' => 'bar3', 'status' => 1]); 52 | $writer->write(['name' => 'Foo2.1', 'value' => 'bar3', 'status' => 1]); 53 | $writer->write(['name' => 'Foo2.3', 'value' => 'bar3', 'status' => 1]); 54 | $writer->write(['name' => 'Foo4', 'value' => 'bar4', 'status' => 0]); 55 | 56 | $this->assertEquals( 57 | [ 58 | ['name' => 'Foo1', 'value' => 'bar1', 'status' => 0], 59 | ['name' => 'Foo3', 'value' => 'bar3', 'status' => 1], 60 | ['name' => 'Foo2.1', 'value' => 'bar3', 'status' => 1], 61 | ['name' => 'Foo4', 'value' => 'bar4', 'status' => 0], 62 | ], 63 | iterator_to_array( 64 | $this->factory->createReader('file_with_data.csv') 65 | ) 66 | ); 67 | } 68 | 69 | /** @test */ 70 | public function allowsToSpecifyIncludeRulesForColumns() 71 | { 72 | $writer = $this->factory 73 | ->withSkip('file_with_data.csv', ['!value' => ['bar2']]) 74 | ->createWriter('file_with_data.csv', ['name', 'value', 'status']); 75 | ; 76 | 77 | $writer->write(['name' => 'Foo1', 'value' => 'bar1', 'status' => 0]); 78 | $writer->write(['name' => 'Foo2', 'value' => 'bar2', 'status' => 1]); 79 | $writer->write(['name' => 'Foo3', 'value' => 'bar2', 'status' => 1]); 80 | $writer->write(['name' => 'Foo2.1', 'value' => 'bar3', 'status' => 1]); 81 | $writer->write(['name' => 'Foo2.3', 'value' => 'bar3', 'status' => 1]); 82 | $writer->write(['name' => 'Foo4', 'value' => 'bar4', 'status' => 0]); 83 | 84 | $this->assertEquals( 85 | [ 86 | ['name' => 'Foo2', 'value' => 'bar2', 'status' => 1], 87 | ['name' => 'Foo3', 'value' => 'bar2', 'status' => 1], 88 | ], 89 | iterator_to_array( 90 | $this->factory->createReader('file_with_data.csv') 91 | ) 92 | ); 93 | } 94 | 95 | /** @test */ 96 | public function mapsValuesInOutput() 97 | { 98 | $writer = $this->factory 99 | ->withMap('file_with_data.csv', 'name', ['Foo2' => 'Foo2222']) 100 | ->withMap('file_with_data.csv', 'status', [-1 => 1]) 101 | ->createWriter('file_with_data.csv', ['name', 'value', 'status']); 102 | ; 103 | 104 | $writer->write(['name' => 'Foo1', 'value' => 'bar1', 'status' => 0]); 105 | $writer->write(['name' => 'Foo2', 'value' => 'bar2', 'status' => 1]); 106 | $writer->write(['name' => 'Foo3', 'value' => 'bar2', 'status' => -1]); 107 | $writer->write(['name' => 'Foo4', 'value' => 'bar4', 'status' => 0]); 108 | 109 | $this->assertEquals( 110 | [ 111 | ['name' => 'Foo1', 'value' => 'bar1', 'status' => 0], 112 | ['name' => 'Foo2222', 'value' => 'bar2', 'status' => 1], 113 | ['name' => 'Foo3', 'value' => 'bar2', 'status' => 1], 114 | ['name' => 'Foo4', 'value' => 'bar4', 'status' => 0], 115 | ], 116 | iterator_to_array( 117 | $this->factory->createReader('file_with_data.csv') 118 | ) 119 | ); 120 | } 121 | 122 | } 123 | -------------------------------------------------------------------------------- /tests/CustomMappers/BaseCostMapperTest.php: -------------------------------------------------------------------------------- 1 | baseCostMapper = new BaseCostMapper(); 21 | } 22 | 23 | /** @test */ 24 | public function filtersOutBaseCostWhenCurrencyAttributeIsMissingCurrency() 25 | { 26 | $this->assertEquals( 27 | [ 28 | ['sku' => 'sku1', 'attribute' => 'name', 'store' => '', 'value' => 'Sku1 Name'], 29 | ['sku' => 'sku1', 'attribute' => 'description', 'store' => '', 'value' => 'Sku1 Description'], 30 | ['sku' => 'sku1', 'attribute' => 'price', 'store' => '', 'value' => 150], 31 | ['sku' => 'sku2', 'attribute' => 'name', 'store' => '', 'value' => 'Sku2 Name'], 32 | ['sku' => 'sku2', 'attribute' => 'description', 'store' => '', 'value' => 'Sku2 Description'], 33 | ['sku' => 'sku1', 'attribute' => 'price', 'store' => '', 'value' => 200] 34 | ], 35 | $this->asArray( 36 | $this->baseCostMapper->apply($this->arrayIteratable( 37 | ['sku' => 'sku1', 'attribute' => 'name', 'store' => '', 'value' => 'Sku1 Name'], 38 | ['sku' => 'sku1', 'attribute' => 'description', 'store' => '', 'value' => 'Sku1 Description'], 39 | ['sku' => 'sku1', 'attribute' => 'base_cost', 'store' => '', 'value' => 100], 40 | ['sku' => 'sku1', 'attribute' => 'price', 'store' => '', 'value' => 150], 41 | ['sku' => 'sku2', 'attribute' => 'name', 'store' => '', 'value' => 'Sku2 Name'], 42 | ['sku' => 'sku2', 'attribute' => 'description', 'store' => '', 'value' => 'Sku2 Description'], 43 | ['sku' => 'sku1', 'attribute' => 'price', 'store' => '', 'value' => 200] 44 | )) 45 | ) 46 | ); 47 | } 48 | 49 | /** @test */ 50 | public function putsBaseCostIntoCostInAdminAndDefaultStore() 51 | { 52 | $this->baseCostMapper = $this->baseCostMapper->withDefaultStore('us'); 53 | 54 | $this->assertEquals( 55 | [ 56 | ['sku' => 'sku1', 'attribute' => 'name', 'store' => '', 'value' => 'Sku1 Name'], 57 | ['sku' => 'sku1', 'attribute' => 'description', 'store' => '', 'value' => 'Sku1 Description'], 58 | ['sku' => 'sku1', 'attribute' => 'cost', 'store' => '', 'value' => 100], 59 | ['sku' => 'sku1', 'attribute' => 'cost', 'store' => 'us', 'value' => 100], 60 | ['sku' => 'sku1', 'attribute' => 'price', 'store' => '', 'value' => 150], 61 | 62 | ['sku' => 'sku2', 'attribute' => 'name', 'store' => '', 'value' => 'Sku2 Name'], 63 | ['sku' => 'sku2', 'attribute' => 'description', 'store' => '', 'value' => 'Sku2 Description'], 64 | ['sku' => 'sku1', 'attribute' => 'price', 'store' => '', 'value' => 200] 65 | ], 66 | $this->asArray( 67 | $this->baseCostMapper->apply($this->arrayIteratable( 68 | ['sku' => 'sku1', 'attribute' => 'name', 'store' => '', 'value' => 'Sku1 Name'], 69 | ['sku' => 'sku1', 'attribute' => 'description', 'store' => '', 'value' => 'Sku1 Description'], 70 | ['sku' => 'sku1', 'attribute' => 'base_cost', 'store' => '', 'value' => 100], 71 | ['sku' => 'sku1', 'attribute' => 'base_cost_currency', 'store' => '', 'value' => ''], 72 | ['sku' => 'sku1', 'attribute' => 'price', 'store' => '', 'value' => 150], 73 | ['sku' => 'sku2', 'attribute' => 'name', 'store' => '', 'value' => 'Sku2 Name'], 74 | ['sku' => 'sku2', 'attribute' => 'description', 'store' => '', 'value' => 'Sku2 Description'], 75 | ['sku' => 'sku1', 'attribute' => 'price', 'store' => '', 'value' => 200] 76 | )) 77 | ) 78 | ); 79 | } 80 | 81 | /** @test */ 82 | public function createsConvertedValueForTargetStoreFromBaseCost() 83 | { 84 | $this->baseCostMapper = $this->baseCostMapper->withStore('uk', 0.77) 85 | ->withStore('eu', 0.83) 86 | ; 87 | 88 | $this->assertEquals( 89 | [ 90 | ['sku' => 'sku1', 'attribute' => 'name', 'store' => '', 'value' => 'Sku1 Name'], 91 | ['sku' => 'sku1', 'attribute' => 'description', 'store' => '', 'value' => 'Sku1 Description'], 92 | ['sku' => 'sku1', 'attribute' => 'cost', 'store' => '', 'value' => 100], 93 | ['sku' => 'sku1', 'attribute' => 'cost', 'store' => 'uk', 'value' => 77], 94 | ['sku' => 'sku1', 'attribute' => 'cost', 'store' => 'eu', 'value' => 83], 95 | ['sku' => 'sku1', 'attribute' => 'price', 'store' => '', 'value' => 150], 96 | 97 | ['sku' => 'sku2', 'attribute' => 'name', 'store' => '', 'value' => 'Sku2 Name'], 98 | ['sku' => 'sku2', 'attribute' => 'description', 'store' => '', 'value' => 'Sku2 Description'], 99 | ['sku' => 'sku2', 'attribute' => 'price', 'store' => '', 'value' => 200] 100 | ], 101 | $this->asArray( 102 | $this->baseCostMapper->apply($this->arrayIteratable( 103 | ['sku' => 'sku1', 'attribute' => 'name', 'store' => '', 'value' => 'Sku1 Name'], 104 | ['sku' => 'sku1', 'attribute' => 'description', 'store' => '', 'value' => 'Sku1 Description'], 105 | ['sku' => 'sku1', 'attribute' => 'base_cost', 'store' => '', 'value' => 100], 106 | ['sku' => 'sku1', 'attribute' => 'base_cost_currency', 'store' => '', 'value' => ''], 107 | ['sku' => 'sku1', 'attribute' => 'price', 'store' => '', 'value' => 150], 108 | ['sku' => 'sku2', 'attribute' => 'name', 'store' => '', 'value' => 'Sku2 Name'], 109 | ['sku' => 'sku2', 'attribute' => 'description', 'store' => '', 'value' => 'Sku2 Description'], 110 | ['sku' => 'sku2', 'attribute' => 'price', 'store' => '', 'value' => 200] 111 | )) 112 | ) 113 | ); 114 | } 115 | 116 | /** @test */ 117 | public function convertsBaseCostUsingCurrencyMap() 118 | { 119 | $this->baseCostMapper = $this->baseCostMapper->withCurrencyConversion('GBP', 1.23) 120 | ->withCurrencyConversion('EUR', 1.17) 121 | ; 122 | 123 | $this->assertEquals( 124 | [ 125 | ['sku' => 'sku1', 'attribute' => 'name', 'store' => '', 'value' => 'Sku1 Name'], 126 | ['sku' => 'sku1', 'attribute' => 'description', 'store' => '', 'value' => 'Sku1 Description'], 127 | ['sku' => 'sku1', 'attribute' => 'cost', 'store' => '', 'value' => 123], 128 | ['sku' => 'sku1', 'attribute' => 'price', 'store' => '', 'value' => 150], 129 | 130 | ['sku' => 'sku2', 'attribute' => 'name', 'store' => '', 'value' => 'Sku2 Name'], 131 | ['sku' => 'sku2', 'attribute' => 'description', 'store' => '', 'value' => 'Sku2 Description'], 132 | ['sku' => 'sku2', 'attribute' => 'cost', 'store' => '', 'value' => 117], 133 | ['sku' => 'sku2', 'attribute' => 'price', 'store' => '', 'value' => 200], 134 | 135 | ['sku' => 'sku3', 'attribute' => 'name', 'store' => '', 'value' => 'Sku3 Name'], 136 | ['sku' => 'sku3', 'attribute' => 'description', 'store' => '', 'value' => 'Sku3 Description'], 137 | ['sku' => 'sku3', 'attribute' => 'cost', 'store' => '', 'value' => 100], 138 | ['sku' => 'sku3', 'attribute' => 'price', 'store' => '', 'value' => 200] 139 | ], 140 | $this->asArray( 141 | $this->baseCostMapper->apply($this->arrayIteratable( 142 | ['sku' => 'sku1', 'attribute' => 'name', 'store' => '', 'value' => 'Sku1 Name'], 143 | ['sku' => 'sku1', 'attribute' => 'description', 'store' => '', 'value' => 'Sku1 Description'], 144 | ['sku' => 'sku1', 'attribute' => 'base_cost', 'store' => '', 'value' => 100], 145 | ['sku' => 'sku1', 'attribute' => 'base_cost_currency', 'store' => '', 'value' => 'GBP'], 146 | ['sku' => 'sku1', 'attribute' => 'price', 'store' => '', 'value' => 150], 147 | ['sku' => 'sku2', 'attribute' => 'name', 'store' => '', 'value' => 'Sku2 Name'], 148 | ['sku' => 'sku2', 'attribute' => 'description', 'store' => '', 'value' => 'Sku2 Description'], 149 | ['sku' => 'sku2', 'attribute' => 'base_cost', 'store' => '', 'value' => 100], 150 | ['sku' => 'sku2', 'attribute' => 'base_cost_currency', 'store' => '', 'value' => 'EUR'], 151 | ['sku' => 'sku2', 'attribute' => 'price', 'store' => '', 'value' => 200], 152 | ['sku' => 'sku3', 'attribute' => 'name', 'store' => '', 'value' => 'Sku3 Name'], 153 | ['sku' => 'sku3', 'attribute' => 'description', 'store' => '', 'value' => 'Sku3 Description'], 154 | ['sku' => 'sku3', 'attribute' => 'base_cost', 'store' => '', 'value' => 100], 155 | ['sku' => 'sku3', 'attribute' => 'base_cost_currency', 'store' => '', 'value' => ''], 156 | ['sku' => 'sku3', 'attribute' => 'price', 'store' => '', 'value' => 200] 157 | )) 158 | ) 159 | ); 160 | } 161 | 162 | /** @test */ 163 | public function waitsForCompleteBaseCurrencyEvenWhenItIsApartInTheDataSet() 164 | { 165 | $this->baseCostMapper = $this->baseCostMapper->withCurrencyConversion('GBP', 1.23) 166 | ->withCurrencyConversion('EUR', 1.17) 167 | ; 168 | 169 | $this->assertEquals( 170 | [ 171 | ['sku' => 'sku1', 'attribute' => 'name', 'store' => '', 'value' => 'Sku1 Name'], 172 | ['sku' => 'sku1', 'attribute' => 'description', 'store' => '', 'value' => 'Sku1 Description'], 173 | ['sku' => 'sku1', 'attribute' => 'price', 'store' => '', 'value' => 150], 174 | 175 | ['sku' => 'sku2', 'attribute' => 'name', 'store' => '', 'value' => 'Sku2 Name'], 176 | ['sku' => 'sku2', 'attribute' => 'description', 'store' => '', 'value' => 'Sku2 Description'], 177 | ['sku' => 'sku2', 'attribute' => 'price', 'store' => '', 'value' => 200], 178 | 179 | ['sku' => 'sku3', 'attribute' => 'name', 'store' => '', 'value' => 'Sku3 Name'], 180 | ['sku' => 'sku3', 'attribute' => 'description', 'store' => '', 'value' => 'Sku3 Description'], 181 | ['sku' => 'sku3', 'attribute' => 'price', 'store' => '', 'value' => 200], 182 | 183 | ['sku' => 'sku1', 'attribute' => 'cost', 'store' => '', 'value' => 123], 184 | ['sku' => 'sku2', 'attribute' => 'cost', 'store' => '', 'value' => 117], 185 | ['sku' => 'sku3', 'attribute' => 'cost', 'store' => '', 'value' => 100], 186 | ], 187 | $this->asArray( 188 | $this->baseCostMapper->apply($this->arrayIteratable( 189 | ['sku' => 'sku1', 'attribute' => 'name', 'store' => '', 'value' => 'Sku1 Name'], 190 | ['sku' => 'sku1', 'attribute' => 'description', 'store' => '', 'value' => 'Sku1 Description'], 191 | ['sku' => 'sku1', 'attribute' => 'base_cost', 'store' => '', 'value' => 100], 192 | ['sku' => 'sku1', 'attribute' => 'price', 'store' => '', 'value' => 150], 193 | ['sku' => 'sku2', 'attribute' => 'name', 'store' => '', 'value' => 'Sku2 Name'], 194 | ['sku' => 'sku2', 'attribute' => 'description', 'store' => '', 'value' => 'Sku2 Description'], 195 | ['sku' => 'sku2', 'attribute' => 'base_cost', 'store' => '', 'value' => 100], 196 | ['sku' => 'sku2', 'attribute' => 'price', 'store' => '', 'value' => 200], 197 | ['sku' => 'sku3', 'attribute' => 'name', 'store' => '', 'value' => 'Sku3 Name'], 198 | ['sku' => 'sku3', 'attribute' => 'description', 'store' => '', 'value' => 'Sku3 Description'], 199 | ['sku' => 'sku3', 'attribute' => 'price', 'store' => '', 'value' => 200], 200 | ['sku' => 'sku1', 'attribute' => 'base_cost_currency', 'store' => '', 'value' => 'GBP'], 201 | ['sku' => 'sku2', 'attribute' => 'base_cost_currency', 'store' => '', 'value' => 'EUR'], 202 | ['sku' => 'sku3', 'attribute' => 'base_cost_currency', 'store' => '', 'value' => ''], 203 | ['sku' => 'sku3', 'attribute' => 'base_cost', 'store' => '', 'value' => 100] 204 | )) 205 | ) 206 | ); 207 | } 208 | 209 | private function asArray(iterable $traversable): array 210 | { 211 | return iterator_to_array($traversable); 212 | } 213 | 214 | private function arrayIteratable(array ...$items): iterable 215 | { 216 | return new \ArrayIterator($items); 217 | } 218 | } 219 | -------------------------------------------------------------------------------- /tests/CustomMappers/CustomerBalanceMapperTest.php: -------------------------------------------------------------------------------- 1 | mapper = new CustomerBalanceMapper(); 22 | } 23 | 24 | 25 | 26 | /** @test */ 27 | public function withoutMergingConfigurationReturnsDataAsIs() 28 | { 29 | $this->assertEquals( 30 | [ 31 | [ 32 | 'email' => 'customer@name.nl', 33 | 'website' => 'int_en', 34 | 'currency' => '', 35 | 'value' => '120.0000', 36 | ], 37 | [ 38 | 'email' => 'customer@name.nl', 39 | 'website' => 'uk_en', 40 | 'currency' => '', 41 | 'value' => '11.0653', 42 | ], 43 | [ 44 | 'email' => 'customer@name.nl', 45 | 'website' => 'us_en', 46 | 'currency' => '', 47 | 'value' => '0.0000', 48 | ], 49 | [ 50 | 'email' => 'another_customer@website.nl', 51 | 'website' => 'int_en', 52 | 'currency' => '', 53 | 'value' => '50.3600', 54 | ], 55 | [ 56 | 'email' => 'another_customer@website.nl', 57 | 'website' => 'uk_en', 58 | 'currency' => '', 59 | 'value' => '55.7931', 60 | ], 61 | [ 62 | 'email' => 'another_customer@website.nl', 63 | 'website' => 'us_en', 64 | 'currency' => '', 65 | 'value' => '31.2600', 66 | ], 67 | [ 68 | 'email' => 'another_customer@website.nl', 69 | 'website' => 'eu_en', 70 | 'currency' => '', 71 | 'value' => '0.0000', 72 | ], 73 | ], 74 | 75 | iterator_to_array( 76 | $this->mapper->apply( 77 | [ 78 | [ 79 | 'email' => 'customer@name.nl', 80 | 'website' => 'int_en', 81 | 'currency' => '', 82 | 'value' => '120.0000', 83 | ], 84 | [ 85 | 'email' => 'customer@name.nl', 86 | 'website' => 'uk_en', 87 | 'currency' => '', 88 | 'value' => '11.0653', 89 | ], 90 | [ 91 | 'email' => 'customer@name.nl', 92 | 'website' => 'us_en', 93 | 'currency' => '', 94 | 'value' => '0.0000', 95 | ], 96 | [ 97 | 'email' => 'another_customer@website.nl', 98 | 'website' => 'int_en', 99 | 'currency' => '', 100 | 'value' => '50.3600', 101 | ], 102 | [ 103 | 'email' => 'another_customer@website.nl', 104 | 'website' => 'uk_en', 105 | 'currency' => '', 106 | 'value' => '55.7931', 107 | ], 108 | [ 109 | 'email' => 'another_customer@website.nl', 110 | 'website' => 'us_en', 111 | 'currency' => '', 112 | 'value' => '31.2600', 113 | ], 114 | [ 115 | 'email' => 'another_customer@website.nl', 116 | 'website' => 'eu_en', 117 | 'currency' => '', 118 | 'value' => '0.0000', 119 | ], 120 | ] 121 | ) 122 | ) 123 | ); 124 | } 125 | 126 | /** @test */ 127 | public function mergesWebsiteIntoTargetOne() 128 | { 129 | $this->assertEquals( 130 | [ 131 | [ 132 | 'email' => 'customer@name.nl', 133 | 'website' => 'uk_en', 134 | 'currency' => '', 135 | 'value' => '11.0653', 136 | ], 137 | [ 138 | 'email' => 'customer@name.nl', 139 | 'website' => 'us_en', 140 | 'currency' => '', 141 | 'value' => 0.00 + 120.00, 142 | ], 143 | [ 144 | 'email' => 'another_customer@website.nl', 145 | 'website' => 'uk_en', 146 | 'currency' => '', 147 | 'value' => '55.7931', 148 | ], 149 | [ 150 | 'email' => 'another_customer@website.nl', 151 | 'website' => 'us_en', 152 | 'currency' => '', 153 | 'value' => 31.26 + 50.36, 154 | ], 155 | [ 156 | 'email' => 'another_customer@website.nl', 157 | 'website' => 'eu_en', 158 | 'currency' => '', 159 | 'value' => '0.0000', 160 | ], 161 | ], 162 | 163 | iterator_to_array( 164 | $this->mapper->withMerge('int_en', 'us_en') 165 | ->apply( 166 | [ 167 | [ 168 | 'email' => 'customer@name.nl', 169 | 'website' => 'int_en', 170 | 'currency' => '', 171 | 'value' => '120.0000', 172 | ], 173 | [ 174 | 'email' => 'customer@name.nl', 175 | 'website' => 'uk_en', 176 | 'currency' => '', 177 | 'value' => '11.0653', 178 | ], 179 | [ 180 | 'email' => 'customer@name.nl', 181 | 'website' => 'us_en', 182 | 'currency' => '', 183 | 'value' => '0.0000', 184 | ], 185 | [ 186 | 'email' => 'another_customer@website.nl', 187 | 'website' => 'int_en', 188 | 'currency' => '', 189 | 'value' => '50.3600', 190 | ], 191 | [ 192 | 'email' => 'another_customer@website.nl', 193 | 'website' => 'uk_en', 194 | 'currency' => '', 195 | 'value' => '55.7931', 196 | ], 197 | [ 198 | 'email' => 'another_customer@website.nl', 199 | 'website' => 'us_en', 200 | 'currency' => '', 201 | 'value' => '31.2600', 202 | ], 203 | [ 204 | 'email' => 'another_customer@website.nl', 205 | 'website' => 'eu_en', 206 | 'currency' => '', 207 | 'value' => '0.0000', 208 | ], 209 | ] 210 | ) 211 | ) 212 | ); 213 | } 214 | 215 | /** @test */ 216 | public function convertsValuesForSpecifiedStoreRates() 217 | { 218 | $this->assertEquals( 219 | [ 220 | [ 221 | 'email' => 'customer@name.nl', 222 | 'website' => 'uk_en', 223 | 'currency' => '', 224 | 'value' => 7.7, 225 | ], 226 | [ 227 | 'email' => 'customer@name.nl', 228 | 'website' => 'us_en', 229 | 'currency' => '', 230 | 'value' => '120.0000', 231 | ], 232 | [ 233 | 'email' => 'another_customer@website.nl', 234 | 'website' => 'uk_en', 235 | 'currency' => '', 236 | 'value' => 77, 237 | ], 238 | [ 239 | 'email' => 'another_customer@website.nl', 240 | 'website' => 'eu_en', 241 | 'currency' => '', 242 | 'value' => 41.5, 243 | ], 244 | ], 245 | 246 | iterator_to_array( 247 | $this->mapper->withRate('eu_en', 0.83) 248 | ->withRate('uk_en', 0.77) 249 | ->apply( 250 | [ 251 | [ 252 | 'email' => 'customer@name.nl', 253 | 'website' => 'uk_en', 254 | 'currency' => '', 255 | 'value' => '10', 256 | ], 257 | [ 258 | 'email' => 'customer@name.nl', 259 | 'website' => 'us_en', 260 | 'currency' => '', 261 | 'value' => '120.0000', 262 | ], 263 | [ 264 | 'email' => 'another_customer@website.nl', 265 | 'website' => 'uk_en', 266 | 'currency' => '', 267 | 'value' => '100', 268 | ], 269 | [ 270 | 'email' => 'another_customer@website.nl', 271 | 'website' => 'eu_en', 272 | 'currency' => '', 273 | 'value' => '50', 274 | ], 275 | ] 276 | ) 277 | ) 278 | ); 279 | } 280 | } 281 | -------------------------------------------------------------------------------- /tests/CustomMappers/DescriptionMapperTest.php: -------------------------------------------------------------------------------- 1 | descriptionMapper = new DescriptionMapper(); 21 | } 22 | 23 | /** @test */ 24 | public function replacesPStrongWithH2() 25 | { 26 | $this->assertEquals( 27 | [ 28 | ['sku' => 'sku1', 'attribute' => 'short_description', 'store' => '', 'value' => 'Sku1 Short Desc'], 29 | ['sku' => 'sku1', 'attribute' => 'description', 'store' => '', 'value' => '

Sku1 Description

'], 30 | ['sku' => 'sku2', 'attribute' => 'short_description', 'store' => '', 'value' => 'Sku2 Short Desc'], 31 | ['sku' => 'sku2', 'attribute' => 'description', 'store' => '', 'value' => 'Sku2 Description'], 32 | ], 33 | $this->asArray( 34 | $this->descriptionMapper->apply($this->arrayIteratable( 35 | ['sku' => 'sku1', 'attribute' => 'short_description', 'store' => '', 'value' => 'Sku1 Short Desc'], 36 | ['sku' => 'sku1', 'attribute' => 'description', 'store' => '', 'value' => '

Sku1 Description

'], 37 | ['sku' => 'sku2', 'attribute' => 'short_description', 'store' => '', 'value' => 'Sku2 Short Desc'], 38 | ['sku' => 'sku2', 'attribute' => 'description', 'store' => '', 'value' => 'Sku2 Description'] 39 | )) 40 | ) 41 | ); 42 | } 43 | 44 | /** @test */ 45 | public function replacesH4withH3() 46 | { 47 | $this->assertEquals( 48 | [ 49 | ['sku' => 'sku1', 'attribute' => 'description', 'store' => '', 'value' => '

Sku1 Description

'], 50 | ], 51 | $this->asArray( 52 | $this->descriptionMapper->apply($this->arrayIteratable( 53 | ['sku' => 'sku1', 'attribute' => 'description', 'store' => '', 'value' => '

Sku1 Description

'] 54 | )) 55 | ) 56 | ); 57 | } 58 | 59 | /** @test */ 60 | public function replacesPBwithH2() 61 | { 62 | $this->assertEquals( 63 | [ 64 | ['sku' => 'sku1', 'attribute' => 'short_description', 'store' => '', 'value' => '

Sku1 Description

'], 65 | ], 66 | $this->asArray( 67 | $this->descriptionMapper->apply($this->arrayIteratable( 68 | ['sku' => 'sku1', 'attribute' => 'short_description', 'store' => '', 'value' => '

Sku1 Description

'] 69 | )) 70 | ) 71 | ); 72 | } 73 | 74 | 75 | private function asArray(iterable $traversable): array 76 | { 77 | return iterator_to_array($traversable); 78 | } 79 | 80 | private function arrayIteratable(array ...$items): iterable 81 | { 82 | return new \ArrayIterator($items); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /tests/CustomMappers/PriceMapperTest.php: -------------------------------------------------------------------------------- 1 | priceMapper = new PriceMapper(); 21 | } 22 | 23 | /** @test */ 24 | public function whenNoStoreConversionIsProvidedDataReturnedWithoutSpecialPrice() 25 | { 26 | $this->assertEquals( 27 | [ 28 | ['sku' => 'sku1', 'attribute' => 'name', 'store' => '', 'value' => 'Sku1 Name'], 29 | ['sku' => 'sku1', 'attribute' => 'description', 'store' => '', 'value' => 'Sku1 Description'], 30 | ['sku' => 'sku1', 'attribute' => 'price', 'store' => '', 'value' => 150], 31 | ['sku' => 'sku2', 'attribute' => 'name', 'store' => '', 'value' => 'Sku2 Name'], 32 | ['sku' => 'sku2', 'attribute' => 'description', 'store' => '', 'value' => 'Sku2 Description'], 33 | ['sku' => 'sku2', 'attribute' => 'price', 'store' => '', 'value' => 200] 34 | ], 35 | $this->asArray( 36 | $this->priceMapper->apply($this->arrayIteratable( 37 | ['sku' => 'sku1', 'attribute' => 'name', 'store' => '', 'value' => 'Sku1 Name'], 38 | ['sku' => 'sku1', 'attribute' => 'description', 'store' => '', 'value' => 'Sku1 Description'], 39 | ['sku' => 'sku1', 'attribute' => 'price', 'store' => '', 'value' => 150], 40 | ['sku' => 'sku1', 'attribute' => 'special_price', 'store' => '', 'value' => 120], 41 | ['sku' => 'sku2', 'attribute' => 'name', 'store' => '', 'value' => 'Sku2 Name'], 42 | ['sku' => 'sku2', 'attribute' => 'description', 'store' => '', 'value' => 'Sku2 Description'], 43 | ['sku' => 'sku2', 'attribute' => 'price', 'store' => '', 'value' => 200], 44 | ['sku' => 'sku2', 'attribute' => 'special_price', 'store' => '', 'value' => 160] 45 | )) 46 | ) 47 | ); 48 | } 49 | 50 | /** @test */ 51 | public function whenStoreConversionRateIsProvidedPriceAndSpecialPriceGetsConverted() 52 | { 53 | $this->priceMapper = $this->priceMapper->withStore('us', 1) 54 | ->withStore('eu', 0.83) 55 | ->withStore('uk', 0.77) 56 | ; 57 | 58 | $this->assertEquals( 59 | [ 60 | ['sku' => 'sku1', 'attribute' => 'name', 'store' => '', 'value' => 'Sku1 Name'], 61 | ['sku' => 'sku1', 'attribute' => 'description', 'store' => '', 'value' => 'Sku1 Description'], 62 | ['sku' => 'sku1', 'attribute' => 'price', 'store' => '', 'value' => 150], 63 | ['sku' => 'sku1', 'attribute' => 'price', 'store' => 'us', 'value' => 150], 64 | ['sku' => 'sku1', 'attribute' => 'price', 'store' => 'eu', 'value' => 124.5], 65 | ['sku' => 'sku1', 'attribute' => 'price', 'store' => 'uk', 'value' => 115.5], 66 | ['sku' => 'sku1', 'attribute' => 'special_price', 'store' => 'us', 'value' => 120], 67 | ['sku' => 'sku1', 'attribute' => 'special_price', 'store' => 'eu', 'value' => 99.6], 68 | ['sku' => 'sku1', 'attribute' => 'special_price', 'store' => 'uk', 'value' => 92.4], 69 | ['sku' => 'sku2', 'attribute' => 'name', 'store' => '', 'value' => 'Sku2 Name'], 70 | ['sku' => 'sku2', 'attribute' => 'description', 'store' => '', 'value' => 'Sku2 Description'], 71 | ['sku' => 'sku2', 'attribute' => 'price', 'store' => '', 'value' => 200], 72 | ['sku' => 'sku2', 'attribute' => 'price', 'store' => 'us', 'value' => 200], 73 | ['sku' => 'sku2', 'attribute' => 'price', 'store' => 'eu', 'value' => 166], 74 | ['sku' => 'sku2', 'attribute' => 'price', 'store' => 'uk', 'value' => 154], 75 | ['sku' => 'sku2', 'attribute' => 'special_price', 'store' => 'us', 'value' => 160], 76 | ['sku' => 'sku2', 'attribute' => 'special_price', 'store' => 'eu', 'value' => 132.8], 77 | ['sku' => 'sku2', 'attribute' => 'special_price', 'store' => 'uk', 'value' => 123.2] 78 | ], 79 | $this->asArray( 80 | $this->priceMapper->apply($this->arrayIteratable( 81 | ['sku' => 'sku1', 'attribute' => 'name', 'store' => '', 'value' => 'Sku1 Name'], 82 | ['sku' => 'sku1', 'attribute' => 'description', 'store' => '', 'value' => 'Sku1 Description'], 83 | ['sku' => 'sku1', 'attribute' => 'price', 'store' => '', 'value' => 150], 84 | ['sku' => 'sku1', 'attribute' => 'special_price', 'store' => '', 'value' => 120], 85 | ['sku' => 'sku2', 'attribute' => 'name', 'store' => '', 'value' => 'Sku2 Name'], 86 | ['sku' => 'sku2', 'attribute' => 'description', 'store' => '', 'value' => 'Sku2 Description'], 87 | ['sku' => 'sku2', 'attribute' => 'price', 'store' => '', 'value' => 200], 88 | ['sku' => 'sku2', 'attribute' => 'special_price', 'store' => '', 'value' => 160] 89 | )) 90 | ) 91 | ); 92 | } 93 | 94 | private function asArray(iterable $traversable): array 95 | { 96 | return iterator_to_array($traversable); 97 | } 98 | 99 | private function arrayIteratable(array ...$items): iterable 100 | { 101 | return new \ArrayIterator($items); 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /tests/DbFactoryTest.php: -------------------------------------------------------------------------------- 1 | factory = new DbFactory(); 23 | } 24 | 25 | /** @test */ 26 | public function createsDatabaseConnection() 27 | { 28 | $connection = $this->createConnection(); 29 | $this->assertInstanceOf(Adapter::class, $connection); 30 | } 31 | 32 | /** @test */ 33 | public function executesDatabaseQuery() 34 | { 35 | $this->assertEquals( 36 | [['1+1' => 2]], 37 | iterator_to_array($this->createConnection()->query('SELECT 1+1')->execute()) 38 | ); 39 | } 40 | 41 | private function createConnection(): Adapter 42 | { 43 | return $this->factory->createConnection( 44 | $_ENV['DB_HOST'], 45 | $_ENV['DB_USER'], 46 | $_ENV['DB_PASSWORD'], 47 | $_ENV['DB_NAME'] 48 | ); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /tests/ExportFactoryTest.php: -------------------------------------------------------------------------------- 1 | factory = new MagentoExportFactory(); 29 | $this->testDirectory = TestDirectory::create(); 30 | $this->testDb = new TestDb(); 31 | } 32 | 33 | /** @test */ 34 | public function createdTargetExportDirectory() 35 | { 36 | $directory = $this->testDirectory->resolvePath('export-target/dir'); 37 | 38 | $this->factory->create($directory, $this->testDb->createMagentoOneConnection()); 39 | 40 | $this->assertDirectoryExists($directory); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /tests/MagentoTwoEavInfoTest.php: -------------------------------------------------------------------------------- 1 | info = MagentoEavInfo::createFromAdapter((new TestDb())->createMagentoTwoConnection()); 23 | } 24 | 25 | /** @test */ 26 | public function fetchesMappingOfEntityTypes() 27 | { 28 | $this->assertThat( 29 | $this->info->fetchEntityTypes(), 30 | $this->logicalAnd( 31 | $this->arrayHasKey('catalog_product'), 32 | $this->arrayHasKey('catalog_category'), 33 | $this->arrayHasKey('order'), 34 | $this->arrayHasKey('invoice'), 35 | $this->containsOnly('int') 36 | ) 37 | ); 38 | } 39 | 40 | /** @test */ 41 | public function fetchesBasicProductAttributeInformation() 42 | { 43 | $attributes = $this->info->fetchProductAttributes(); 44 | 45 | $this->assertThat( 46 | $attributes, 47 | $this->logicalAnd( 48 | $this->arrayHasKey('name'), 49 | $this->arrayHasKey('description'), 50 | $this->arrayHasKey('url_key'), 51 | $this->contains(['name' => 'Product Name', 'code' => 'name', 'type' => 'varchar']), 52 | $this->contains(['name' => 'Description', 'code' => 'description', 'type' => 'text']), 53 | $this->contains(['name' => 'URL Key', 'code' => 'url_key', 'type' => 'varchar']) 54 | ) 55 | ); 56 | } 57 | 58 | /** @test */ 59 | public function returnsExtendedAttributesInformationForStandardAttributes() 60 | { 61 | $this->assertEquals( 62 | [ 63 | 'name' => [ 64 | 'scope' => 'store', 65 | 'input' => 'text', 66 | 'default' => '', 67 | 'option' => 0, 68 | 'unique' => 0, 69 | 'required' => 1, 70 | 'validation' => 'validate-length maximum-length-255', 71 | 'searchable' => 1, 72 | 'layered' => 0, 73 | 'layered_search' => 0, 74 | 'promotion' => 0, 75 | 'product_list' => 1, 76 | 'product_page' => 0, 77 | 'sortable' => 1, 78 | 'advanced_search' => 1, 79 | 'comparable' => 0, 80 | 'apply_to' => '', 81 | 'html' => 0, 82 | 'position' => 0 83 | ], 84 | 'description' => [ 85 | 'scope' => 'store', 86 | 'input' => 'textarea', 87 | 'option' => 0, 88 | 'default' => '', 89 | 'unique' => 0, 90 | 'required' => 0, 91 | 'validation' => '', 92 | 'searchable' => 1, 93 | 'layered' => 0, 94 | 'layered_search' => 0, 95 | 'promotion' => 0, 96 | 'product_list' => 0, 97 | 'product_page' => 0, 98 | 'sortable' => 0, 99 | 'advanced_search' => 1, 100 | 'comparable' => 1, 101 | 'apply_to' => '', 102 | 'html' => 1, 103 | 'position' => 0 104 | ] 105 | ], 106 | iterator_to_array($this->info->fetchProductAttributeConfiguration(['name', 'description'])) 107 | ); 108 | } 109 | 110 | /** @test */ 111 | public function ignoresProductAttributesFromOutput() 112 | { 113 | $attributeCodes = array_keys($this->info->fetchProductAttributes(['name', 'description'])); 114 | 115 | $this->assertThat($attributeCodes, $this->logicalAnd( 116 | $this->logicalNot($this->contains('name')), 117 | $this->logicalNot($this->contains('description')), 118 | $this->contains('url_key') 119 | )); 120 | } 121 | 122 | /** @test */ 123 | public function exportsSingleSelectOptions() 124 | { 125 | $this->assertEquals( 126 | [ 127 | 'color' => [ 128 | 'scope' => 'global', 129 | 'input' => 'select', 130 | 'default' => '', 131 | 'option' => 1, 132 | 'unique' => 0, 133 | 'required' => 0, 134 | 'validation' => '', 135 | 'searchable' => 1, 136 | 'layered' => 1, 137 | 'layered_search' => 0, 138 | 'promotion' => 0, 139 | 'product_list' => 0, 140 | 'product_page' => 0, 141 | 'sortable' => 0, 142 | 'advanced_search' => 1, 143 | 'comparable' => 1, 144 | 'apply_to' => 'simple,virtual,configurable', 145 | 'html' => 0, 146 | 'position' => 0, 147 | ] 148 | ], 149 | iterator_to_array($this->info->fetchProductAttributeConfiguration(['color'])) 150 | ); 151 | } 152 | 153 | /** @test */ 154 | public function exportsSystemOptionAttributeAsNonOption() 155 | { 156 | $this->assertEquals( 157 | [ 158 | 'visibility' => [ 159 | 'scope' => 'store', 160 | 'input' => 'select', 161 | 'default' => '4', 162 | 'option' => 0, 163 | 'unique' => 0, 164 | 'required' => 0, 165 | 'validation' => '', 166 | 'searchable' => 0, 167 | 'layered' => 0, 168 | 'layered_search' => 0, 169 | 'promotion' => 0, 170 | 'product_list' => 0, 171 | 'product_page' => 0, 172 | 'sortable' => 0, 173 | 'advanced_search' => 0, 174 | 'comparable' => 0, 175 | 'apply_to' => '', 176 | 'html' => 0, 177 | 'position' => 0 178 | ] 179 | ], 180 | iterator_to_array($this->info->fetchProductAttributeConfiguration(['visibility'])) 181 | ); 182 | } 183 | 184 | /** @test */ 185 | public function fetchesMapOfStoreCodes() 186 | { 187 | $this->assertThat( 188 | $this->info->fetchStoreMap(), 189 | $this->logicalAnd( 190 | $this->arrayHasKey('us_en'), 191 | $this->arrayHasKey('eu_en'), 192 | $this->arrayHasKey('uk_en') 193 | ) 194 | ); 195 | } 196 | 197 | /** @test */ 198 | public function fetchesDefaultAttributeSet() 199 | { 200 | $this->assertEquals( 201 | [ 202 | 'catalog_product' => 4, 203 | 'catalog_category' => 3 204 | ], 205 | $this->info->fetchDefaultEntityAttributeSet('catalog_product', 'catalog_category') 206 | ); 207 | } 208 | 209 | /** @test */ 210 | public function detectsMagentoVersion() 211 | { 212 | $this->assertTrue($this->info->isMagentoTwo()); 213 | } 214 | } 215 | -------------------------------------------------------------------------------- /tests/MergedRowMapperTest.php: -------------------------------------------------------------------------------- 1 | mergedMapper = new MergedRowMapper(); 22 | } 23 | 24 | /** @test */ 25 | public function returnsDataStructureAsNoMergedDataProvided() 26 | { 27 | $this->assertEquals( 28 | [ 29 | ['sku' => 'sku10', 'attribute' => 'attribute10', 'store' => '', 'value' => '10'], 30 | ['sku' => 'sku20', 'attribute' => 'attribute20', 'store' => '', 'value' => '20'] 31 | ], 32 | iterator_to_array( 33 | $this->mergedMapper->apply([ 34 | ['sku' => 'sku10', 'attribute' => 'attribute10', 'store' => '', 'value' => '10'], 35 | ['sku' => 'sku20', 'attribute' => 'attribute20', 'store' => '', 'value' => '20'] 36 | ]) 37 | ) 38 | ); 39 | } 40 | 41 | /** @test */ 42 | public function appendsMergedStructureAppliedStructure() 43 | { 44 | $this->assertEquals( 45 | [ 46 | ['sku' => 'sku10', 'attribute' => 'attribute10', 'store' => '', 'value' => '10'], 47 | ['sku' => 'sku20', 'attribute' => 'attribute20', 'store' => '', 'value' => '20'], 48 | ['sku' => 'sku1', 'attribute' => 'attribute1', 'store' => '', 'value' => 'value1'], 49 | ['sku' => 'sku1', 'attribute' => 'attribute1', 'store' => 'en_us', 'value' => 'value2'], 50 | ['sku' => 'sku2', 'attribute' => 'attribute1', 'store' => 'en_uk', 'value' => 'value3'], 51 | ], 52 | iterator_to_array( 53 | $this->mergedMapper 54 | ->withCsvFile(__DIR__ . '/fixture/file1.csv') 55 | ->apply([ 56 | ['sku' => 'sku10', 'attribute' => 'attribute10', 'store' => '', 'value' => '10'], 57 | ['sku' => 'sku20', 'attribute' => 'attribute20', 'store' => '', 'value' => '20'] 58 | ]) 59 | ) 60 | ); 61 | } 62 | 63 | /** @test */ 64 | public function mergesAllAppendedRowsStructuresToAppliedStructure() 65 | { 66 | $this->assertEquals( 67 | [ 68 | ['sku' => 'sku10', 'attribute' => 'attribute10', 'store' => '', 'value' => '10'], 69 | ['sku' => 'sku20', 'attribute' => 'attribute20', 'store' => '', 'value' => '20'], 70 | ['sku' => 'sku1', 'attribute' => 'attribute1', 'store' => '', 'value' => 'value1'], 71 | ['sku' => 'sku1', 'attribute' => 'attribute1', 'store' => 'en_us', 'value' => 'value2'], 72 | ['sku' => 'sku2', 'attribute' => 'attribute1', 'store' => 'en_uk', 'value' => 'value3'], 73 | ['sku' => 'sku1', 'attribute' => 'attribute2', 'store' => '', 'value' => 'value1.1'], 74 | ['sku' => 'sku1', 'attribute' => 'attribute2', 'store' => 'en_us', 'value' => 'value1.2'], 75 | ['sku' => 'sku2', 'attribute' => 'attribute2', 'store' => 'en_uk', 'value' => 'value1.3'], 76 | ], 77 | iterator_to_array( 78 | $this->mergedMapper 79 | ->withCsvFile( 80 | __DIR__ . '/fixture/file1.csv' 81 | ) 82 | ->withCsvFile( 83 | __DIR__ . '/fixture/file2.csv' 84 | ) 85 | ->apply([ 86 | ['sku' => 'sku10', 'attribute' => 'attribute10', 'store' => '', 'value' => '10'], 87 | ['sku' => 'sku20', 'attribute' => 'attribute20', 'store' => '', 'value' => '20'] 88 | ]) 89 | ) 90 | ); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /tests/SelectConditionTest.php: -------------------------------------------------------------------------------- 1 | factory = new SelectConditionFactory(); 23 | } 24 | 25 | /** @test */ 26 | public function appliesImpossibleConditionWhenEmptyListOfValuesProvided() 27 | { 28 | $condition = $this->factory->in([]); 29 | 30 | $where = new Where(); 31 | $condition->apply('some_field', $where); 32 | 33 | $this->assertEquals( 34 | (new Where()) 35 | ->equalTo('some_field', null) 36 | ->notEqualTo('some_field', null), 37 | $where 38 | ); 39 | } 40 | 41 | /** @test */ 42 | public function appliesListOfValuesAsInConditionOnWhereInstance() 43 | { 44 | $condition = $this->factory->in([1, 2, 3]); 45 | 46 | 47 | $where = new Where(); 48 | $condition->apply('field_name', $where); 49 | 50 | $this->assertEquals( 51 | (new Where()) 52 | ->in('field_name', [1, 2, 3]), 53 | $where 54 | ); 55 | } 56 | 57 | /** 58 | * @test 59 | * @testWith [1, 1000] 60 | * [1000, 2000] 61 | * [400, 1567] 62 | */ 63 | public function appliesRangeConditionIntoWhere(int $startRange, int $endRange) 64 | { 65 | $condition = $this->factory->range($startRange, $endRange); 66 | 67 | $where = new Where(); 68 | $condition->apply('field', $where); 69 | 70 | $this->assertEquals( 71 | (new Where()) 72 | ->greaterThanOrEqualTo('field', $startRange) 73 | ->lessThanOrEqualTo('field', $endRange), 74 | $where 75 | ); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /tests/TableRangeConditionGeneratorTest.php: -------------------------------------------------------------------------------- 1 | createMagentoOneConnection(); 34 | self::$testDb = $connection; 35 | $sql = new Sql($connection); 36 | 37 | $tables = [ 38 | 'sample_ranged_table_10k' => 10001, 39 | 'sample_ranged_table_5k' => 5123, 40 | 'sample_ranged_table_2k' => 2000 41 | ]; 42 | 43 | foreach ($tables as $tableName => $maxValue) { 44 | $table = new CreateTable($tableName, true); 45 | $table->addColumn(new Integer('entity_id')) 46 | ->addConstraint(new PrimaryKey(['entity_id'])); 47 | 48 | $connection->query($sql->buildSqlString($table))->execute(); 49 | $insert = InsertOnDuplicate::create($tableName, ['entity_id']); 50 | 51 | foreach (range(1, $maxValue) as $id) { 52 | $insert->values(['entity_id' => $id], InsertOnDuplicate::VALUES_MERGE); 53 | $insert = $insert->flushIfLimitReached($sql); 54 | } 55 | 56 | $insert->executeIfNotEmpty($sql); 57 | } 58 | 59 | 60 | } 61 | 62 | public static function tearDownAfterClass() 63 | { 64 | self::$testDb = null; 65 | } 66 | 67 | protected function setUp() 68 | { 69 | $this->factory = TableRangeConditionGeneratorFactory::createFromAdapter(self::$testDb); 70 | $this->conditionFactory = new SelectConditionFactory(); 71 | } 72 | 73 | /** @test */ 74 | public function generatesConditions2KTable() 75 | { 76 | $generator = $this->factory->createForTable('sample_ranged_table_2k', 'entity_id'); 77 | 78 | $this->assertEquals( 79 | [$this->conditionFactory->range(1, 2000)], 80 | iterator_to_array($generator->conditions()) 81 | ); 82 | } 83 | 84 | /** @test */ 85 | public function generatesConditionsFor5KTable() 86 | { 87 | $generator = $this->factory->createForTable('sample_ranged_table_5k', 'entity_id'); 88 | 89 | $this->assertEquals( 90 | [ 91 | $this->conditionFactory->range(1, 2000), 92 | $this->conditionFactory->range(2001, 4000), 93 | $this->conditionFactory->range(4001, 5123), 94 | ], 95 | iterator_to_array($generator->conditions()) 96 | ); 97 | } 98 | 99 | /** @test */ 100 | public function generatesConditionsFor10KTable() 101 | { 102 | $generator = $this->factory->createForTable('sample_ranged_table_10k', 'entity_id'); 103 | 104 | $this->assertEquals( 105 | [ 106 | $this->conditionFactory->range(1, 2000), 107 | $this->conditionFactory->range(2001, 4000), 108 | $this->conditionFactory->range(4001, 6000), 109 | $this->conditionFactory->range(6001, 8000), 110 | $this->conditionFactory->range(8001, 10000), 111 | $this->conditionFactory->range(10001, 10001), 112 | ], 113 | iterator_to_array($generator->conditions()) 114 | ); 115 | } 116 | } 117 | -------------------------------------------------------------------------------- /tests/TestDb.php: -------------------------------------------------------------------------------- 1 | dbFactory = $dbFactory ?? new DbFactory(); 30 | } 31 | 32 | public function createMagentoOneConnection(): Adapter 33 | { 34 | list($host, $user, $password) = $this->databaseConnectionSettings(); 35 | 36 | return $this->dbFactory->createConnection( 37 | $host, 38 | $user, 39 | $password, 40 | $this->magentoOneDbName() 41 | ); 42 | } 43 | 44 | public function createMagentoTwoConnection(): Adapter 45 | { 46 | 47 | list($host, $user, $password) = $this->databaseConnectionSettings(); 48 | 49 | return $this->dbFactory->createConnection( 50 | $host, 51 | $user, 52 | $password, 53 | $this->magentoTwoDbName() 54 | ); 55 | } 56 | 57 | public function resetMagentoTwoDb(): void 58 | { 59 | list($host, $user, $password) = $this->databaseConnectionSettings(); 60 | 61 | $command = sprintf( 62 | 'gunzip -c %s | mysql -u %s -p%s -h%s %s 2>/dev/null', 63 | __DIR__ . '/test.sql.gz', 64 | $user, $password, $host, $this->magentoTwoDbName() 65 | ); 66 | 67 | shell_exec($command); 68 | 69 | } 70 | 71 | public function listTablesLike(string $pattern): array 72 | { 73 | list($host, $user, $password) = $this->databaseConnectionSettings(); 74 | 75 | $tables = explode( 76 | PHP_EOL, 77 | shell_exec(sprintf('mysql -u %s -p%s -h%s %s -N -e "SHOW TABLES;" 2>/dev/null', $user, $password, $host, $this->magentoTwoDbName())) 78 | ); 79 | 80 | return array_filter($tables, function ($table) use ($pattern) { 81 | return preg_match($pattern, $table); 82 | }); 83 | } 84 | 85 | public function createSnapshot(array $tables): void 86 | { 87 | list($host, $user, $password) = $this->databaseConnectionSettings(); 88 | 89 | $this->snapshot = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('database-snapshot'); 90 | 91 | $command = sprintf( 92 | 'mysqldump -u %s -p%s -h%s %s %s 2>/dev/null | gzip -c > %s', 93 | $user, $password, $host, $this->magentoTwoDbName(), implode(' ', $tables), $this->snapshot 94 | ); 95 | 96 | shell_exec($command); 97 | } 98 | 99 | public function restoreSnapshot(): void 100 | { 101 | if (!$this->snapshot) { 102 | return; 103 | } 104 | 105 | list($host, $user, $password) = $this->databaseConnectionSettings(); 106 | 107 | $command = sprintf( 108 | 'gunzip -c %s | mysql -u %s -p%s -h%s %s 2>/dev/null', 109 | $this->snapshot, 110 | $user, $password, $host, $this->magentoTwoDbName() 111 | ); 112 | 113 | shell_exec($command); 114 | } 115 | 116 | private function databaseConnectionSettings() 117 | { 118 | return [ 119 | (string)$_ENV['DB_HOST'], 120 | (string)$_ENV['DB_USER'], 121 | (string)$_ENV['DB_PASSWORD'] 122 | ]; 123 | } 124 | 125 | private function magentoTwoDbName(): string 126 | { 127 | return (string)$_ENV['DB_NAME_M2']; 128 | } 129 | 130 | private function magentoOneDbName(): string 131 | { 132 | return (string)$_ENV['DB_NAME']; 133 | } 134 | } 135 | -------------------------------------------------------------------------------- /tests/TestDirectory.php: -------------------------------------------------------------------------------- 1 | path = $path; 19 | } 20 | 21 | public static function create(): self 22 | { 23 | $directory = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('test_directory'); 24 | mkdir($directory, 0777, true); 25 | 26 | return new self($directory); 27 | } 28 | 29 | public function copyFrom(string $path) 30 | { 31 | foreach ($this->createDirectoryIterator($path, \RecursiveIteratorIterator::SELF_FIRST) as $item) { 32 | $relativePath = substr($item->getPathname(), strlen($path) + 1); 33 | if ($item->isDir()) { 34 | mkdir($this->resolvePath($relativePath), 0777, true); 35 | continue; 36 | } 37 | 38 | copy($item->getPathname(), $this->resolvePath($relativePath)); 39 | } 40 | } 41 | 42 | public function resolvePath(string $path): string 43 | { 44 | return $this->path . ($path ? DIRECTORY_SEPARATOR . $path : ''); 45 | } 46 | 47 | public function __destruct() 48 | { 49 | if (!is_dir($this->path)) { 50 | return; 51 | } 52 | 53 | // Remove all temporary files and directories 54 | foreach ($this->listAllItems() as $item) { 55 | if ($item->isDir()) { 56 | rmdir($item->getRealPath()); 57 | continue; 58 | } 59 | 60 | unlink($item->getRealPath()); 61 | } 62 | 63 | rmdir($this->path); 64 | } 65 | 66 | 67 | /** 68 | * Lists all entries in the directory 69 | * @return \SplFileInfo[] 70 | */ 71 | public function listAllItems(): \Traversable 72 | { 73 | return $this->createDirectoryIterator($this->path, \RecursiveIteratorIterator::CHILD_FIRST); 74 | } 75 | 76 | /** 77 | * 78 | * @return \SplFileInfo[] 79 | */ 80 | private function createDirectoryIterator(string $path, int $mode): \Traversable 81 | { 82 | return new \RecursiveIteratorIterator( 83 | new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS), 84 | $mode 85 | ); 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /tests/fixture/balance-file.csv: -------------------------------------------------------------------------------- 1 | email,website,value,currency 2 | ivan@ecomdev.org,en_en,1000, 3 | -------------------------------------------------------------------------------- /tests/fixture/file1.csv: -------------------------------------------------------------------------------- 1 | sku,attribute,store,value 2 | sku1,attribute1,,value1 3 | sku1,attribute1,en_us,value2 4 | sku2,attribute1,en_uk,value3 5 | -------------------------------------------------------------------------------- /tests/fixture/file2.csv: -------------------------------------------------------------------------------- 1 | sku,attribute,store,value 2 | sku1,attribute2,,value1.1 3 | sku1,attribute2,en_us,value1.2 4 | sku2,attribute2,en_uk,value1.3 5 | -------------------------------------------------------------------------------- /tests/test.sql.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EcomDev/sync-magento-2-migration/bdb765c4edb3ac4b7eafb420f91a48b1c39d06d8/tests/test.sql.gz --------------------------------------------------------------------------------