|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Directory\Setup; |
| 9 | + |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use Magento\TestFramework\Helper\Bootstrap; |
| 12 | +use Magento\Directory\Setup\DataInstaller; |
| 13 | +use Magento\Framework\App\ResourceConnection; |
| 14 | + |
| 15 | +/** |
| 16 | + * Provide test for DataInstaller |
| 17 | + */ |
| 18 | +class DataInstallerTest extends TestCase |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var DataInstaller |
| 22 | + */ |
| 23 | + private $dataInstaller; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var ResourceConnection |
| 27 | + */ |
| 28 | + private $resourceConnection; |
| 29 | + |
| 30 | + /** |
| 31 | + * @inheritDoc |
| 32 | + */ |
| 33 | + protected function setUp(): void |
| 34 | + { |
| 35 | + $objectManager = Bootstrap::getObjectManager(); |
| 36 | + $this->dataInstaller = $objectManager->create(DataInstaller::class); |
| 37 | + $this->resourceConnection = $objectManager->create(ResourceConnection::class); |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * @return void |
| 42 | + */ |
| 43 | + public function testAddCountryRegions(): void |
| 44 | + { |
| 45 | + $adapter = $this->resourceConnection->getConnection(); |
| 46 | + $expectedCountries = $this->getCountries(true); |
| 47 | + |
| 48 | + $regionsBefore = $this->getTableRowsCount('directory_country_region'); |
| 49 | + $regionsNamesBefore = $this->getTableRowsCount('directory_country_region_name'); |
| 50 | + |
| 51 | + $this->dataInstaller->addCountryRegions( |
| 52 | + $adapter, |
| 53 | + $this->getDataForRegions() |
| 54 | + ); |
| 55 | + |
| 56 | + $regionsAfter = $this->getTableRowsCount('directory_country_region'); |
| 57 | + $regionsNamesAfter = $this->getTableRowsCount('directory_country_region_name'); |
| 58 | + |
| 59 | + $this->assertEquals(4, ($regionsAfter - $regionsBefore)); |
| 60 | + $this->assertEquals(4, ($regionsNamesAfter - $regionsNamesBefore)); |
| 61 | + $this->assertEquals($expectedCountries, $this->getCountries()); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Count table rows |
| 66 | + * |
| 67 | + * @param string $tableName |
| 68 | + * @return int |
| 69 | + */ |
| 70 | + private function getTableRowsCount(string $tableName): int |
| 71 | + { |
| 72 | + $connection = $this->resourceConnection->getConnection(); |
| 73 | + $select = $connection->select()->from( |
| 74 | + $this->resourceConnection->getTableName($tableName), |
| 75 | + ['count(*)'] |
| 76 | + ); |
| 77 | + |
| 78 | + return (int)$connection->fetchOne($select); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Return required countries with regions |
| 83 | + * |
| 84 | + * @param bool $isConfig |
| 85 | + * @return string |
| 86 | + */ |
| 87 | + private function getCountries(bool $isConfig = false): string |
| 88 | + { |
| 89 | + $connection = $this->resourceConnection->getConnection(); |
| 90 | + $select = $connection->select() |
| 91 | + ->from($connection->getTableName('core_config_data'), 'value') |
| 92 | + ->where('path = ?', 'general/region/state_required') |
| 93 | + ->where('scope = ?', 'default') |
| 94 | + ->where('scope_id = ?', 0); |
| 95 | + |
| 96 | + $countries = $connection->fetchOne($select); |
| 97 | + $countries = (!empty($countries)) ? explode(',', $countries) : []; |
| 98 | + |
| 99 | + if (!$isConfig) { |
| 100 | + return implode(',', $countries); |
| 101 | + } |
| 102 | + |
| 103 | + $countryCodes = ['JP', 'UA']; |
| 104 | + foreach ($countryCodes as $country) { |
| 105 | + if (!in_array($country, $countries)) { |
| 106 | + $countries[] = $country; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return implode(',', $countries); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Return test data for new regions |
| 115 | + * |
| 116 | + * @return array[] |
| 117 | + */ |
| 118 | + private function getDataForRegions(): array |
| 119 | + { |
| 120 | + return [ |
| 121 | + ['JP', 'JP-01', 'State1'], |
| 122 | + ['JP', 'JP-02', 'State2'], |
| 123 | + ['JP', 'JP-03', 'State3'], |
| 124 | + ['UA', 'UA-410', 'State4'], |
| 125 | + ]; |
| 126 | + } |
| 127 | +} |
0 commit comments