在D-7中有phpunit模块吗?
到目前为止,正在使用details Core框架(简单测试),它有DrupalWebTestCase类,
我想让D7上的TDD程序,使用PHP类,请建议。
发布于 2015-12-14 17:07:19
还没有准备好在Drupal 7中使用集成类进行PHPUnit测试。
据我所见,社区中对于TDD的普遍共识是使用PHPunit编写不依赖于TDD中Drupal的API的代码。由Drupal中的轻量级集成层完成,无需使用TDD。这样做的目的是减少测试/编写代码与Drupal之间的摩擦。
这通常意味着将Drupal抽象到一组不同的服务或功能中,注入到您开发的业务逻辑类中。
作为一个简单的例子,不要在代码中使用cache_get()
和cache_set()
函数,而是编写代码来使用DrupalCacheInterface
实例。以同样的方式,您不必在代码中使用Drupal的数据库API,而是依赖于所需opé口粮的抽象。在测试中,您可以提供所需接口的模拟实现(直接从Drupal,或者特定于您自己的代码)。您的Drupal集成代码负责注入实际的实现。
例如,使用以下代码--虽然MODULE_entity_load
不是PHP的teastbale (因为对数据库的访问和缓存的使用)--您可以在PHP中测试_MODULE_entity_load
的行为,因为它实际上不依赖于引导的Drupal应用程序。
<?php
/**
* Implements hook_entity_load().
*/
function MODULE_entity_load($entities, $type) {
return _MODULE_entity_load(
$entities,
$type,
function($entity) {
return db_query(...);
},
_cache_get_object($bin)
);
}
/**
* "Pure" function to handle entities load.
*/
function _MODULE_entity_load($entities, $type, $queryData, DrupalCacheInterfac $cacheObject) {
if ($type != 'node') return;
forEach($entities as $entity) {
$cache = $cacheObject->get('MODULE:' . $entity->nid);
if ($cache && (time() < $cache->expire)) {
$entity->MODULE_data = $queryData($entity->field_xyz);
$cacheObject->set('MODULE:' . $node->nid, $entity->MODULE_data);
}
else {
$entity->MODULE_data = $cache->data;
}
}
}
发布于 2015-12-15 05:23:00
是的,@Pierre Buyle现在没有专门支持的模块可用于phpunit,
我在根目录中创建了test文件夹,并添加了几个测试代码,它已开始工作,我可以使用drupal函数进行登录身份验证,我使用了user_authenticate('root', 'admin123');
。
看上去phpunit的开局很好,将在tdd中开始下一关。
单位动作项目
现在我可以在drupal中运行phpunit了
代码样本
<?php
define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
// Bootstrap Drupal.
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
class test extends PHPUnit_Framework_TestCase
{
public function testLoginChk(){
print_r(user_authenticate('root', 'admin123'));
$this->assertEquals(1,user_authenticate('root', 'admin123'));
}
}
请分享你的想法,我在同一条道路上的drupal phpunit测试方法。
https://stackoverflow.com/questions/34260831
复制相似问题