Reading list Switch to dark mode

    Integrate PHPUnit with Opencart

    Updated 15 June 2016

    Here, we will learn how to implement the PHPUnit testing framework with Opencart. As Opencart already provides some examples of tests case so I will provide a procedure to learn, how to write test cases.

    For that, we have to first install the Opencart and then in the root of Opencart, we have to create a folder. Here, I have created a folder named “tests”. In the folder, we have to download the composer installer using this command:

    curl -s http://getcomposer.org/installer | php

    Then we need to create a file named composer.json in the folder itself and write the following in the composer.json file.

    {
        "require": {
            "beyondit/opencart-test-suite": "0.2.1"
        }
    }

    “beyondit” provides the Opencart test suite for PHPUnit testing. Here is the GitHub link.

    Searching for an experienced
    Opencart Company ?
    Find out More

    After writing the above lines in the composer.json file, we have to install the dependencies using the command:

    php composer.phar install

    After running the above command, we will have a folder named vendor in the ‘tests’ folder. We will have a class OpenCartTest in file OpenCartTest.php which extends the PHPUnit framework. The OpenCartTest class will be extended by each test class we will write. The constructor of OpenCartTest class needed to be changed according to the Opencart version you are using. It is basically created for OC version 2.0.x.x. It actually contains the stuff we have in index.php. This class also contains the method for loading model and controller. We can create new methods as well.

    So, here I have created a file named “firsttest” in the tests folder. In this file, we have “Firsttest” class which will extend the OpenCartTest class. As per PHPUnit, the file name must have “test” as postfix and each function must have the “test” word as the prefix. So, here I put this simple code:

    <?php 
    class Firsttest extends OpenCartTest { 
        public function testOne() {
        	$this->assertEquals('1', '2');
        }   
        public function testTwo() {
        	$this->assertEquals('1', '2');
        }   
        public function testThree() {
        	$this->assertEquals('2', '2');
        }   
        public function testFour() {
        	$this->assertEquals('1', '2');
        }
    }
    ?>

    On running the following command, we will have an output like in below image:

    vendor/bin/phpunit firsttest

    firstphpunittest

    This is showing 3 failures as only testThree is correct.testThree is correct.testThree is correct.testThree is correct.testThree is correct.testThree is correct.

    Here, in this another example, we are accessing the model in the front end. Here’s the test, I wrote in file customertest.php:

    <?php
    
    class ModelCatalogCustomerTest extends OpenCartTest {
        public function testCheckCustomerName() {
    
            // load the customer model
            $model = $this->loadModelByRoute("account/customer");
            $customer = $model->getCustomer(1);
    
            // test a specific assertion
            $this->assertEquals('Vikhyat', $customer['firstname']);
            $this->assertTrue($customer['status']);
        }
    }
    ?>

    Here, there are two assertions in this file. On running the following command, we will have the given output.

    vendor/bin/phpunit customertest

    customertest
    Here, in this test, there is one failure, as the status was 1 which was not equal to true.

    Similarly, we can make tests over the controllers as well using the loadControllerByRoute and dispatchAction functions. Hope, this will help you writing your test cases. You will find a list of assertions here on PHPUnit website.

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    2 comments

  • Stefan Huber
  • Testing Opencart Admin End Files By PHPUnit
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home