Creating unit tests

This chapter assumes that we have PHPUnit configured and available on the command line. If this is not the case, PHPUnit can be installed using instructions from the https://phpunit.de/ website.

To build and run tests using the PHPUnit testing framework, we need to define test locations and other configuration options via an XML file. Magento defines this XML configuration file under dev/tests/unit/phpunit.xml.dist. Let's make a copy of that file under dev/tests/unit/phpunit-foggyline-helpdesk.xml, with adjustments as follows:

<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1 /phpunit.xsd"
         colors="true"
         bootstrap="./framework/bootstrap.php"
        >
    <testsuite name="Foggyline_Helpdesk - Unit Tests">
        <directory suffix="Test.php"> ../../../app/code/Foggyline/Helpdesk/Test/Unit </directory>
    </testsuite>
    <php>
        <ini name="date.timezone" value="Europe/Zagreb"/>
        <ini name="xdebug.max_nesting_level" value="200"/>
    </php>
    <filter>
        <whitelist addUncoveredFilesFromWhiteList="true">
            <directory suffix=".php"> ../../../app/code/Foggyline/Helpdesk/*</directory>
            <exclude>
                <directory> ../../../app/code/Foggyline/Form/Helpdesk </directory>
            </exclude>
        </whitelist>
    </filter>
    <logging>
        <log type="coverage-html" target="coverage_dir/Foggyline_Helpdesk/test- reports/coverage" charset="UTF-8" yui="true" highlight="true"/>
    </logging>
</phpunit>

Tip

We are making a special XML configuration file for our module alone because we want to quickly run a few of the tests contained within our module alone and not the entire Magento app/code folder.

Given that the actual art of writing unit tests is beyond the scope of this book and writing the full unit test with 100 percent code coverage for this simple module would require at least a dozen more pages, we will only write a single test, one that covers the Ticket entity model class.

We define our Ticket entity model class test under the app/code/Foggyline/Helpdesk/Test/Unit/Model/TicketTest.php file as follows:

<?php

namespace FoggylineHelpdeskTestUnitModel;

class TicketTest extends PHPUnit_Framework_TestCase
{
    protected $objectManager;
    protected $ticket;

    public function setUp()
    {
        $this->objectManager = new MagentoFrameworkTestFrameworkUnitHelper ObjectManager($this);
        $this->ticket = $this->objectManager-> getObject('FoggylineHelpdeskModelTicket');
    }

    public function testGetSeveritiesOptionArray()
    {
        $this-> assertNotEmpty(Foggyline HelpdeskModelTicket::getSeveritiesOptionArray());
    }

    public function testGetStatusesOptionArray()
    {
        $this->assertNotEmpty(Foggyline HelpdeskModelTicket::getStatusesOptionArray());
    }

    public function testGetStatusAsLabel()
    {
        $this->ticket->setStatus(FoggylineHelpdesk ModelTicket::STATUS_CLOSED);

        $this->assertEquals(
            FoggylineHelpdeskModelTicket::$statusesOptions [FoggylineHelpdeskModelTicket::STATUS_CLOSED],
            $this->ticket->getStatusAsLabel()
        );
    }

    public function testGetSeverityAsLabel()
    {
        $this->ticket->setSeverity(Foggyline HelpdeskModelTicket::SEVERITY_MEDIUM);

        $this->assertEquals(
            FoggylineHelpdeskModelTicket::$severitiesOptions [FoggylineHelpdeskModelTicket::SEVERITY_MEDIUM],
            $this->ticket->getSeverityAsLabel()
        );
    }
}

The location of test files should map those of the files being tested. The naming of the test file should also follow the naming of the file being tested with the suffix Test attached to it. This means that if our Ticket model is located under the modules Model/Ticket.php file, then our test should be located under Test/Unit/TicketTest.php.

Our FoggylineHelpdeskTestUnitModelTicketTest extends the PHPUnit_Framework_TestCase class. There is a setUp method we need to define, which acts like a constructor, where we set up the variables and everything that requires initializing.

Using Magento ObjectManager, we instantiate the Ticket model, which is then used within the test methods. The actual test methods follow a simple naming pattern, where the name of the method from the Ticket model matches the {test}+{method name} from the TicketTest class.

We defined four test methods: testGetSeveritiesOptionArray, testGetStatusesOptionArray, testGetStatusAsLabel, and testGetSeverityAsLabel. Within the test methods, we are using only assertEquals and assertNotEmpty methods from the PHPUnit testing framework library to do basic checks.

We can now open a console, change the directory to our Magento installation directory, and execute the following command:

phpunit -c dev/tests/unit/phpunit-foggyline-helpdesk.xml

After the command executes, the console should show an output as shown:

PHPUnit 4.7.6 by Sebastian Bergmann and contributors.
....

Time: 528 ms, Memory: 11.50Mb

OK (4 tests, 4 assertions)

Generating code coverage report in HTML format ... done

Looking back at our dev/tests/unit/phpunit-foggyline-helpdesk.xml file, under the target attribute of the phpunit > logging > log element, we can see that the test report is dumped into the coverage_dir/Foggyline_Helpdesk/test-reports/coverage folder relative to the XML file.

If we open the dev/tests/unit/coverage_dir/Foggyline_Helpdesk/test-reports/coverage/ folder, we should see a whole lot of files generated there, as shown in the following screenshot:

Creating unit tests

Opening the index.html file in the browser should give us a page as shown in the following screenshot:

Creating unit tests

We can see the code coverage report showing 60% on lines and methods for our Model folder and 0% for the rest. This is because we only wrote the test for the Ticket entity model class, whereas the rest remain untested.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.51.117