2

I'm trying to understand the Unit Testing for Magento2.

public function getPostUrl()
{
    $ticketId = $this->getRequest()->getParam('ticket_id');
    $params   = ['ticket_id' => $ticketId];

    $url = $this->backendUrl->getUrl(self::QUOTE_MERGE_URL, $params);

    return $url;
}

Now i want to write a Unit Test for this code. But i have no clue how to proceed.

I have a unit test for

public function getTest()
{
    return 'Test';
}

and in the test page:

public function testGetTest()
{
    $this->assertEquals('Test', $this->block->getTest());
}

This works.

But how do i check the post url if that is how i expect?

3
  • Have you learned how to use PHPUnit test? Commented May 9, 2018 at 13:48
  • 1
    Yes, at least the basic use of it.
    – Johan
    Commented May 9, 2018 at 14:08
  • Learn about mocking assertions are not the only thing in PHPUnit phpunit.readthedocs.io/en/7.1 Commented May 14, 2018 at 14:41

1 Answer 1

0

Basically, you just need to mock the Request, Context and BackendUrl object of your block. Say your block looks like this:

<?php
namespace MyCompany\MyModule\Block;

use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Backend\Model\UrlInterface;

class Ticket extends Template
{

    const QUOTE_MERGE_URL = 'showTickets';

    protected $backendUrl;

    public function __construct(Context $context, UrlInterface $backendUrl, $data = [])
    {
        parent::__construct($context, $data);
        $this->backendUrl = $backendUrl;
    }

    public function getPostUrl()
    {
        $ticketId = $this->getRequest()->getParam('ticket_id');
        $params = [
            'ticket_id' => $ticketId
        ];

        $url = $this->backendUrl->getUrl(self::QUOTE_MERGE_URL, $params);

        return $url;
    }
}

A simple test of this block would look something like this:

<?php
namespace MyCompany\MyModule\Test\Unit\Block;

use Magento\Framework\App\RequestInterface;

class TicketTest extends \PHPUnit\Framework\TestCase
{

    protected $ticketBlock;

    protected function setUp(): void
    {
        $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);

        $request = $this->createMock(\Magento\Framework\App\RequestInterface::class);
        $request->method('getParam')
            ->with('ticket_id')
            ->willReturn('1234');

        $context = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
        $context->method('getRequest')->willReturn($request);

        $backendUrl = $this->createMock(\Magento\Backend\Model\UrlInterface::class);
        $backendUrl->method('getUrl')->willReturnCallback(function($route, $params){
            return 'https://www.mysite.local/'.$route.'?'.http_build_query($params);
        });

        $this->ticketBlock = $objectManager->getObject(\ISN\ThemeWhiteLabel\Block\Ticket::class, [
            'context' => $context,
            'backendUrl' => $backendUrl
        ]);
    }

    public function testGetPostUrl(): void
    {
        $url = $this->ticketBlock->getPostUrl();

        $this->assertStringContainsString(\MyCompany\MyModule\Block\Ticket::QUOTE_MERGE_URL.'?ticket_id=1234', $url);
    }
}

You have to mock Context because the AbstractBlock has a Request object injected into it from the Context object, so when your block calls getRequest(), it gets the Request object from the AbstractBlock. You have to mock Request so your test can simulate request parameters populated in the Request object. You have to mock BackendUrl because when you call getUrl it needs to get the base url from the configuration and tries to write to the session, causing phpunit to throw all sorts of errors. Since we as Magento developers all know what getUrl does, we can just mock what it returns rather than worry about what BackendUrl is doing.

By the way, run the test like this: vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/MyCompany/MyModule

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.