Skip to content

Implement ActionInterface for /robots/index/index #27393

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 5 additions & 7 deletions app/code/Magento/Robots/Controller/Index/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,31 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Robots\Controller\Index;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;

/**
* Processes request to robots.txt file and returns robots.txt content as result
*/
class Index extends Action
class Index implements HttpGetActionInterface
{
/**
* @var PageFactory
*/
private $resultPageFactory;

/**
* @param Context $context
* @param PageFactory $resultPageFactory
*/
public function __construct(
Context $context,
PageFactory $resultPageFactory
) {
$this->resultPageFactory = $resultPageFactory;

parent::__construct($context);
}

/**
Expand All @@ -44,6 +41,7 @@ public function execute()
$resultPage = $this->resultPageFactory->create(true);
$resultPage->addHandle('robots_index_index');
$resultPage->setHeader('Content-Type', 'text/plain');

return $resultPage;
}
}
45 changes: 23 additions & 22 deletions app/code/Magento/Robots/Test/Unit/Controller/Index/IndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,42 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Robots\Test\Unit\Controller\Index;

class IndexTest extends \PHPUnit\Framework\TestCase
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\View\Result\Page;
use Magento\Framework\View\Result\PageFactory;
use Magento\Robots\Controller\Index\Index;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class IndexTest extends TestCase
{
/**
* @var \Magento\Framework\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject
* @var Index
*/
private $contextMock;
private $controller;

/**
* @var \Magento\Framework\Controller\Result\RawFactory|\PHPUnit_Framework_MockObject_MockObject
* @var PageFactory|MockObject
*/
private $resultPageFactory;

/**
* @var \Magento\Robots\Controller\Index\Index
*/
private $controller;
private $resultPageFactoryMock;

protected function setUp()
{
$this->contextMock = $this->getMockBuilder(\Magento\Framework\App\Action\Context::class)
->disableOriginalConstructor()
->getMock();

$this->resultPageFactory = $this->getMockBuilder(\Magento\Framework\View\Result\PageFactory::class)
$this->resultPageFactoryMock = $this->getMockBuilder(PageFactory::class)
->disableOriginalConstructor()
->setMethods(['create'])
->getMock();

$this->controller = new \Magento\Robots\Controller\Index\Index(
$this->contextMock,
$this->resultPageFactory
$objectManager = new ObjectManager($this);
$this->controller = $objectManager->getObject(
Index::class,
[
'resultPageFactory' => $this->resultPageFactoryMock
]
);
}

Expand All @@ -45,7 +47,7 @@ protected function setUp()
*/
public function testExecute()
{
$resultPageMock = $this->getMockBuilder(\Magento\Framework\View\Result\Page::class)
$resultPageMock = $this->getMockBuilder(Page::class)
->disableOriginalConstructor()
->getMock();
$resultPageMock->expects($this->once())
Expand All @@ -55,13 +57,12 @@ public function testExecute()
->method('setHeader')
->with('Content-Type', 'text/plain');

$this->resultPageFactory->expects($this->any())
->method('create')
$this->resultPageFactoryMock->method('create')
->with(true)
->willReturn($resultPageMock);

$this->assertInstanceOf(
\Magento\Framework\View\Result\Page::class,
Page::class,
$this->controller->execute()
);
}
Expand Down