How to unit test a Joomla 2.5 component
Asked Answered
D

1

18

Can someone provide an example of how to unit test a Joomla 2.5 component? I'm working through this joomla mvc component example, which doesn't include unit tests, and I can't find a full example anywhere. My main questions are:

  • Where to put component test code
  • How to run component unit tests
  • Is my test code correct

The Joomla documentation seems incomplete and fragmented - I've read what I can find on unit testing (can't post links due to low rep), which seem to be about testing Joomla itself rather than extensions. The closest I've found is this, which I've based my dummy test on.

What I've done so far

Component directory structure:

  • helloworld/
    • admin/
      • ...
      • tests/
        • bootstrap.php
        • phpunit.xml
        • modelHelloWorldsTest.php
    • site/
      • ...
    • helloworld.xml

To run the tests, I install/copy the component to my Joomla installation. I then run the following command from ~joomla/administration/components/com_helloworld/tests:

php phpunit-4.2.phar --bootstrap bootstrap.php .

from which I receive

Fatal error: Class 'ContentController' not found in C:\inetpub\wwwroot\ws_cairnstest\administrator\components\com_helloworld\tests\modelsHelloWorldsTest.php on line 5

bootstrap.php:

<?php
error_reporting(E_ALL);

define('_JEXEC', 1);
define('BASEPATH',realpath(dirname(__FILE__).'/../../'));
define('JOOMLA_PATH',realpath(dirname(__FILE__).'/../../../../../'));
define('JOOMLA_ADMIN_PATH',realpath(dirname(__FILE__).'/../../../../'));
$_SERVER['HTTP_HOST'] = 'localhost';
$_SERVER['REQUEST_METHOD'] = 'GET';

if (file_exists(JOOMLA_ADMIN_PATH . '/defines.php'))
{
    include_once JOOMLA_ADMIN_PATH . '/defines.php';
}

if (!defined('_JDEFINES'))
{
    define('JPATH_BASE', JOOMLA_ADMIN_PATH);
    require_once JPATH_BASE . '/includes/defines.php';
}

require_once JPATH_BASE . '/includes/framework.php';
require_once JPATH_BASE . '/includes/helper.php';
require_once JPATH_BASE . '/includes/toolbar.php';
define('JPATH_COMPONENT',JOOMLA_ADMIN_PATH.'/components/com_content');
$app = JFactory::getApplication('administrator');
include BASEPATH.'/controller.php';

modelsHelloWorldsTest.php:

<?php
class HelloWorldsTest extends \PHPUnit_Framework_TestCase {

    public function testList(){
        $c = new ContentController();
        $model = $c->getModel('helloworlds');
        $worlds = $model->getItems();
        var_dump($worlds);
        $this->assertNotEmpty($worlds);
    }
}

phpunit.xml:

<phpunit bootstrap="bootstrap.php"
    colors="true"
    convertErrorsToExceptions="true"
    convertNoticesToExceptions="true"
    convertWarningsToExceptions="true"
    processIsolation="false"
    stopOnFailure="false"
    syntaxCheck="false"
    verbose="true">
</phpunit>
Dorton answered 22/9, 2014 at 6:11 Comment(2)
This question is about Joomla specific implementation details, you may get a better result if you, try asking on the Joomla Q&A StackExhange siteEbbarta
Done (link).Dorton
S
1

Unit tests for Joomla are typically written to use the PHP Unit framework

To answer your questions Where to put component test code: at the top level of your repo as follows

Component repo directory structure:

  • src
    • admin/
    • site/
  • tests

How to run component unit tests: test execution follows this pattern

vendor/bin/phpunit --configuration phpunit.xml

additional details can be found in the php unit documentation https://phpunit.de/documentation.html

Is your test code correct: no

You failed to mock your class and methods with the getMockBuilder() for testing.

Here are some examples to look at

Softener answered 7/12, 2016 at 0:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.