uoz*_*Aho 11 php joomla unit-testing joomla2.5
有人可以举例说明如何对Joomla 2.5组件进行单元测试吗?我正在研究这个joomla mvc组件示例,它不包含单元测试,我无法在任何地方找到完整的示例.我的主要问题是:
Joomla文档似乎不完整和支离破碎 - 我已经阅读了我可以在单元测试中找到的内容(由于低代表而无法发布链接),这似乎是关于测试Joomla本身而不是扩展.我发现的最接近的是这个,我是基于我的虚拟测试.
组件目录结构:
要运行测试,我将组件安装/复制到我的Joomla安装.然后我从~joomla/administration/components/com_helloworld/tests运行以下命令:
php phpunit-4.2.phar --bootstrap bootstrap.php .
Run Code Online (Sandbox Code Playgroud)
从我收到
Fatal error: Class 'ContentController' not found in C:\inetpub\wwwroot\ws_cairnstest\administrator\components\com_helloworld\tests\modelsHelloWorldsTest.php on line 5
Run Code Online (Sandbox Code Playgroud)
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';
Run Code Online (Sandbox Code Playgroud)
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);
}
}
Run Code Online (Sandbox Code Playgroud)
phpunit.xml:
<phpunit bootstrap="bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
verbose="true">
</phpunit>
Run Code Online (Sandbox Code Playgroud)
Joomla 的单元测试通常是使用 PHP 单元框架编写的
回答您的问题 在哪里放置组件测试代码:在存储库的顶层,如下所示
组件仓库目录结构:
如何运行组件单元测试:测试执行遵循此模式
vendor/bin/phpunit --configuration phpunit.xml
其他详细信息可以在 php 单元文档中找到https://phpunit.de/documentation.html
您的测试代码是否正确:否
您未能使用getMockBuilder()
测试来模拟您的类和方法。
以下是一些可供查看的示例