Skip to content
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Theme\Test\Unit\Controller\Result;

use Magento\Theme\Controller\Result\JsFooterPlugin;
use Magento\Framework\App\Response\Http;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;

/**
* Unit test for Magento\Theme\Test\Unit\Controller\Result\JsFooterPlugin.
*/
class JsFooterPluginTest extends TestCase
{
const STUB_XML_PATH_DEV_MOVE_JS_TO_BOTTOM = 'dev/js/move_script_to_bottom';

/**
* @var JsFooterPlugin
*/
private $plugin;

/**
* @var ScopeConfigInterface|MockObject
*/
private $scopeConfigMock;

/**
* @var Http|MockObject
*/
private $httpMock;

/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
->setMethods(['isSetFlag'])
->disableOriginalConstructor()
->getMockForAbstractClass();

$this->httpMock = $this->createMock(Http::class);

$objectManager = new ObjectManagerHelper($this);
$this->plugin = $objectManager->getObject(
JsFooterPlugin::class,
[
'scopeConfig' => $this->scopeConfigMock
]
);
}

/**
* Data Provider for testBeforeSendResponse()
*
* @return array
*/
public function sendResponseDataProvider(): array
{
return [
[
"content" => "<body><h1>Test Title</h1>" .
"<script type=\"text/x-magento-init\">test</script>" .
"<script type=\"text/x-magento-template\">test</script>" .
"<p>Test Content</p></body>",
"flag" => true,
"result" => "<body><h1>Test Title</h1>" .
"<script type=\"text/x-magento-template\">test</script>" .
"<p>Test Content</p>" .
"<script type=\"text/x-magento-init\">test</script>" .
"\n</body>"
],
[
"content" => "<body><p>Test Content</p></body>",
"flag" => false,
"result" => "<body><p>Test Content</p></body>"
],
[
"content" => "<body><p>Test Content</p></body>",
"flag" => true,
"result" => "<body><p>Test Content</p>\n</body>"
]
];
}

/**
* Test beforeSendResponse
*
* @param string $content
* @param bool $isSetFlag
* @param string $result
* @return void
* @dataProvider sendResponseDataProvider
*/
public function testBeforeSendResponse($content, $isSetFlag, $result): void
{
$this->httpMock->expects($this->once())
->method('getContent')
->willReturn($content);

$this->scopeConfigMock->expects($this->once())
->method('isSetFlag')
->with(
self::STUB_XML_PATH_DEV_MOVE_JS_TO_BOTTOM,
ScopeInterface::SCOPE_STORE
)
->willReturn($isSetFlag);

$this->httpMock->expects($this->any())
->method('setContent')
->with($result);

$this->plugin->beforeSendResponse($this->httpMock);
}

/**
* Data Provider for testBeforeSendResponseIfGetContentIsNotAString()
*
* @return array
*/
public function ifGetContentIsNotAStringDataProvider(): array
{
return [
[
'content' => []
],
[
'content' => NULL
]
Copy link
Contributor

@lbajsarowicz lbajsarowicz Feb 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can replace this with:

'empty_array' => ['content' => []],
'null' => ['content' => NULL]

As a result - when one of them fail - You'll know on which data set it failed (key of dataProvider is added to error message (Test failed with: X))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lbajsarowicz Thanks for the feedback, I have updated the keys.

];
}

/**
* Test BeforeSendResponse if content is not a string
*
* @param string $content
* @return void
* @dataProvider ifGetContentIsNotAStringDataProvider
*/
public function testBeforeSendResponseIfGetContentIsNotAString($content): void
{
$this->httpMock->expects($this->once())
->method('getContent')
->willReturn($content);

$this->scopeConfigMock->expects($this->never())
->method('isSetFlag')
->with(
self::STUB_XML_PATH_DEV_MOVE_JS_TO_BOTTOM,
ScopeInterface::SCOPE_STORE
)
->willReturn(false);

$this->plugin->beforeSendResponse($this->httpMock);
}
}