Skip to content

Commit

Permalink
Add and use the ResponseInterface
Browse files Browse the repository at this point in the history
Instead of direct shop Utils usages

Signed-off-by: Anton Fedurtsya <[email protected]>
  • Loading branch information
Sieg committed Oct 2, 2023
1 parent 2763f43 commit 164bf79
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 31 deletions.
1 change: 0 additions & 1 deletion metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 4 additions & 8 deletions src/Application/Controller/MediaLangJs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);");
}
}
22 changes: 0 additions & 22 deletions src/Core/Utils.php

This file was deleted.

29 changes: 29 additions & 0 deletions src/Transition/Core/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace OxidEsales\MediaLibrary\Transition\Core;

use OxidEsales\MediaLibrary\Core\Utils;

class Response implements ResponseInterface
{
/** @var Utils $language */
private $utils;

public function __construct(\OxidEsales\Eshop\Core\Utils $utils)
{
/** @var Utils $utils */
$this->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);
}
}
10 changes: 10 additions & 0 deletions src/Transition/Core/ResponseInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace OxidEsales\MediaLibrary\Transition\Core;

interface ResponseInterface
{
public function responseAsJson(array $valueArray): void;

public function responseAsJavaScript(string $value): void;
}
5 changes: 5 additions & 0 deletions src/Transition/Core/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ services:
autowire: true
bind:
OxidEsales\Eshop\Core\Language: '@=service("OxidEsales\\MediaLibrary\\Core\\Registry").getLang()'
OxidEsales\Eshop\Core\Utils: '@=service("OxidEsales\\MediaLibrary\\Core\\Registry").getUtils()'

OxidEsales\MediaLibrary\Transition\Core\LanguageInterface:
class: OxidEsales\MediaLibrary\Transition\Core\LanguageProxy
public: true

OxidEsales\MediaLibrary\Transition\Core\ResponseInterface:
class: OxidEsales\MediaLibrary\Transition\Core\Response
public: true
62 changes: 62 additions & 0 deletions tests/Integration/Transition/Core/ResponseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/**
* Copyright © OXID eSales AG. All rights reserved.
* See LICENSE file for license details.
*/

declare(strict_types=1);

namespace OxidEsales\MediaLibrary\Tests\Integration\Transition\Core;

use OxidEsales\Eshop\Core\Utils;
use OxidEsales\MediaLibrary\Transition\Core\Response;
use PHPUnit\Framework\TestCase;

class ResponseTest extends TestCase
{
public function testRespondAsJson(): void
{
$exampleData = ['somekey' => '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);
}
}

0 comments on commit 164bf79

Please sign in to comment.