diff --git a/metadata.php b/metadata.php index e34f761..8081e33 100755 --- a/metadata.php +++ b/metadata.php @@ -30,7 +30,6 @@ // Core \OxidEsales\Eshop\Core\ViewConfig::class => \OxidEsales\MediaLibrary\Core\ViewConfig::class, \OxidEsales\Eshop\Core\Language::class => \OxidEsales\MediaLibrary\Transition\Core\Language::class, - \OxidEsales\Eshop\Core\Utils::class => \OxidEsales\MediaLibrary\Core\Utils::class, ], 'controllers' => [ // Lang diff --git a/src/Application/Controller/MediaLangJs.php b/src/Application/Controller/MediaLangJs.php index f2a99e5..fc9e772 100755 --- a/src/Application/Controller/MediaLangJs.php +++ b/src/Application/Controller/MediaLangJs.php @@ -8,9 +8,8 @@ namespace OxidEsales\MediaLibrary\Application\Controller; use OxidEsales\Eshop\Application\Controller\FrontendController; -use OxidEsales\Eshop\Core\Registry; -use OxidEsales\MediaLibrary\Core\Utils; use OxidEsales\MediaLibrary\Transition\Core\LanguageInterface; +use OxidEsales\MediaLibrary\Transition\Core\ResponseInterface; /** * Class MediaLangJs @@ -23,12 +22,9 @@ class MediaLangJs extends FrontendController public function init() { $languages = $this->getService(LanguageInterface::class); + $responseService = $this->getService(ResponseInterface::class); - header('Content-Type: application/javascript'); - - /** @var Utils $oUtils */ - $oUtils = Registry::getUtils(); - $sJson = json_encode($languages->getLanguageStringsArray()); - $oUtils->showMessageAndExit(";( function(g){ g.i18n = " . $sJson . "; })(window);"); + $jsonValue = json_encode($languages->getLanguageStringsArray()); + $responseService->responseAsJavaScript(";( function(g){ g.i18n = " . $jsonValue . "; })(window);"); } } diff --git a/src/Core/Utils.php b/src/Core/Utils.php deleted file mode 100644 index 5f64891..0000000 --- a/src/Core/Utils.php +++ /dev/null @@ -1,22 +0,0 @@ -showMessageAndExit(json_encode($mMsg)); - } -} diff --git a/src/Transition/Core/Response.php b/src/Transition/Core/Response.php new file mode 100644 index 0000000..16d613a --- /dev/null +++ b/src/Transition/Core/Response.php @@ -0,0 +1,29 @@ +utils = $utils; + } + + public function responseAsJson(array $valueArray): void + { + $this->utils->setHeader('Content-Type: application/json'); + $this->utils->showMessageAndExit(json_encode($valueArray)); + } + + public function responseAsJavaScript(string $value): void + { + $this->utils->setHeader('Content-Type: application/javascript'); + $this->utils->showMessageAndExit($value); + } +} \ No newline at end of file diff --git a/src/Transition/Core/ResponseInterface.php b/src/Transition/Core/ResponseInterface.php new file mode 100644 index 0000000..80fc2d1 --- /dev/null +++ b/src/Transition/Core/ResponseInterface.php @@ -0,0 +1,10 @@ + 'someValue']; + $jsonValue = json_encode($exampleData); + + $utilsMock = $this->createPartialMock(Utils::class, ['setHeader', 'showMessageAndExit']); + $utilsMock->expects($this->once()) + ->method('showMessageAndExit') + ->with($jsonValue); + + $correctHeaderSet = false; + $utilsMock->method('setHeader')->willReturnCallback(function ($value) use (&$correctHeaderSet){ + if (preg_match("@Content-Type:\s?application/json@i", $value)) { + $correctHeaderSet = true; + } + }); + + $sut = new Response($utilsMock); + $sut->responseAsJson($exampleData); + + $this->assertTrue($correctHeaderSet); + } + + public function testRespondAsJavaScript(): void + { + $exampleData = 'someJavaScriptCodeExample'; + + $utilsMock = $this->createPartialMock(Utils::class, ['setHeader', 'showMessageAndExit']); + $utilsMock->expects($this->once()) + ->method('showMessageAndExit') + ->with($exampleData); + + $correctHeaderSet = false; + $utilsMock->method('setHeader')->willReturnCallback(function ($value) use (&$correctHeaderSet){ + if (preg_match("@Content-Type:\s?application/javascript@i", $value)) { + $correctHeaderSet = true; + } + }); + + $sut = new Response($utilsMock); + $sut->responseAsJavaScript($exampleData); + + $this->assertTrue($correctHeaderSet); + } +}