diff --git a/.gitignore b/.gitignore index f878d41..eb04423 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ Thumbs.db vendor .phpunit.result.cache .php-cs-fixer.cache +.codex \ No newline at end of file diff --git a/Controllers/Api/SwagMigrationTimezone.php b/Controllers/Api/SwagMigrationTimezone.php new file mode 100644 index 0000000..238bb13 --- /dev/null +++ b/Controllers/Api/SwagMigrationTimezone.php @@ -0,0 +1,64 @@ + + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +use SwagMigrationConnector\Controllers\SwagMigrationApiControllerBase; +use SwagMigrationConnector\Service\ControllerReturnStruct; + +class Shopware_Controllers_Api_SwagMigrationTimezone extends SwagMigrationApiControllerBase +{ + /** + * @return void + */ + public function indexAction() + { + $dbConfig = $this->container->getParameter('shopware.db'); + if (!\is_array($dbConfig)) { + $this->assignEmptyResult(); + + return; + } + + if (!$this->hasTimezoneConfig($dbConfig)) { + $this->assignEmptyResult(); + + return; + } + + $timezone = $dbConfig['timezone']; + if ($timezone === '') { + $timezone = null; + } + + $response = new ControllerReturnStruct([ + ['timezone' => $timezone], + ]); + + $this->view->assign($response->jsonSerialize()); + } + + /** + * @param array $dbConfig + * + * @return bool + */ + private function hasTimezoneConfig(array $dbConfig) + { + return isset($dbConfig['timezone']); + } + + /** + * @return void + */ + private function assignEmptyResult() + { + $response = new ControllerReturnStruct([ + ['timezone' => null], + ]); + + $this->view->assign($response->jsonSerialize()); + } +} diff --git a/Resources/services.xml b/Resources/services.xml index e49030d..bdcfcf7 100644 --- a/Resources/services.xml +++ b/Resources/services.xml @@ -15,6 +15,7 @@ %shopware.release.version% %shopware.release.version_text% %shopware.release.revision% + %shopware.db% diff --git a/Tests/Functional/Controllers/SwagMigrationTimezoneTest.php b/Tests/Functional/Controllers/SwagMigrationTimezoneTest.php new file mode 100644 index 0000000..7311c40 --- /dev/null +++ b/Tests/Functional/Controllers/SwagMigrationTimezoneTest.php @@ -0,0 +1,102 @@ + + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace SwagMigrationConnector\Tests\Functional\Controllers; + +use Shopware\Components\DependencyInjection\Container; +use Shopware_Controllers_Api_SwagMigrationTimezone as SwagMigrationTimezone; +use SwagMigrationConnector\Tests\Functional\ContainerTrait; +use SwagMigrationConnector\Tests\Functional\Controllers\ControllerFactory\Arguments; +use SwagMigrationConnector\Tests\Functional\Controllers\ControllerFactory\ControllerFactory; +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag; + +require __DIR__ . '/../../../Controllers/Api/SwagMigrationTimezone.php'; + +class SwagMigrationTimezoneTest extends \Enlight_Components_Test_Controller_TestCase +{ + use ContainerTrait; + + /** + * @return void + */ + public function testIndexActionReturnsConfiguredDatabaseTimezone() + { + $container = $this->getContainer(); + $dbConfig = $container->getParameter('shopware.db'); + static::assertTrue(\is_array($dbConfig)); + + $configuredTimezone = $dbConfig['timezone']; + $timezone = $configuredTimezone === '' ? null : $configuredTimezone; + + $controller = ControllerFactory::createController( + SwagMigrationTimezone::class, + new Arguments($container) + ); + + $controller->indexAction(); + + $this->assertTimezoneResponse($timezone, $controller); + } + + /** + * @return void + */ + public function testIndexActionReturnsNullTimezoneWhenDatabaseConfigHasNoTimezone() + { + $controller = $this->createControllerWithDatabaseConfig([]); + $controller->indexAction(); + + $this->assertTimezoneResponse(null, $controller); + } + + /** + * @return void + */ + public function testIndexActionReturnsNullTimezoneWhenDatabaseConfigIsNotArray() + { + $controller = $this->createControllerWithDatabaseConfig('invalid'); + $controller->indexAction(); + + $this->assertTimezoneResponse(null, $controller); + } + + /** + * @param array|string $dbConfig + * + * @return SwagMigrationTimezone + */ + private function createControllerWithDatabaseConfig($dbConfig) + { + $container = new Container(new ParameterBag([ + 'shopware.db' => $dbConfig, + ])); + + return ControllerFactory::createController( + SwagMigrationTimezone::class, + new Arguments($container) + ); + } + + /** + * @param string|null $timezone + * @param SwagMigrationTimezone $controller + * + * @return void + */ + private function assertTimezoneResponse($timezone, $controller) + { + static::assertSame([ + 'data' => [ + [ + 'timezone' => $timezone, + ], + ], + 'isLastRequest' => false, + 'success' => true, + ], $controller->View()->getAssign()); + } +}