I've asked this question on StackOverflow, and it was suggested that I ask it here.
I'm familiar with unit/system/integration testing, and would like to be able to test my Joomla component. Is there a standard way of doing this?
I'm working through this joomla mvc component example, which doesn't include tests. All I can find in the Joomla documentation and on various websites is fragments of test code & bootstrap.php files. Specifically what I would like to know is:
- Where to put component test code
- Do I need to provide my own bootstrap.php, or is there some way to just 'include joomla' and run my tests
Ideally someone could direct me to an open source Joomla component that has tests & instructions on how to run them (had a quick look, first 5 or so didn't have tests).
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
- admin/
First attempt
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
I gather this means my bootstrap.php isn't correct and hasn't loaded the necessary Joomla classes. I'll investigate this at some point, but this seems like a lot of setup just to get some tests to run.
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>
Second attempt
After seeing this answer, I put my tests under test/unit under my joomla installation, copied phpunit.dist.xml and bootstrap.php from the joomla-cms repo to their appropriate locations, and still got the
Fatal error: Class 'ContentController' not found in C:\inetpub\wwwroot\ws_cairnstest\administrator\components\com_helloworld\tests\modelsHelloWorldsTest.php on line 5
error I was receiving before.