運行環境zend studio 7.1.1
此版本ide對phpunit支持還不夠好,配置文件phpunit.xml還不能支持.得看看7.2如何.
沒辦法只有修改JLoaderTest_Class 此類了.
add :
protected function setUp() {
include_once 'tests\unit\bootstrap.php';
}
主要測試如下兩個方法:
調用庫類比較簡單:
JLoader::import( 'joomla.error.error' );
JLoader::import( 'joomla.error.exception' );
一定要以'joomla'開頭,實際上JLoader::import還有另外兩個參數
$base指路徑,$key只是個key,會同$key+$filePath 連接起來,作為數組的key緩存.
/**
* Loads a class from specified directories.
*
* @param string $name The class name to look for ( dot notation ).
* @param string $base Search this directory for the class.
* @param string $key String used as a prefix to denote the full path of the file ( dot notation ).
* @return void
* @since 1.5
*/
function import( $filePath, $base = null, $key = 'libraries.' )
function test_import()
{
$r = JLoader::import('joomla.factory');
$this -> assertTrue($r);
}
/** function import($filePath, $base = test dir, $key = null) */
function test_import_base()
{
// $testLib = 'joomla._testdata.loader-data';
// $this -> assertFalse(defined('JUNIT_DATA_JLOADER'), 'Test set up failure.');
// $r = JLoader::import($testLib, dirname(__FILE__));
// if ($this -> assertTrue($r)) {
// $this -> assertTrue(defined('JUNIT_DATA_JLOADER'));
// }
//
// // retry
// $r = JLoader::import($testLib, dirname(__FILE__));
// $this->assertTrue($r);
if(!defined('DS')) {
define( 'DS', DIRECTORY_SEPARATOR );
}
$r = JLoader::import("tests.unit.suite.libraries.index2");
$this->assertFalse($r);
$r = JLoader::import("index2");
$this->assertFalse($r);
$r = JLoader::import("unit.suite.libraries.index2",'tests','random.tests.');
// $this->assertTrue($r);
$this->assertEquals(1,$r);
$r = JLoader::import("tests.unit.suite.libraries.index2",'','custom0.');
$this->assertFalse($r);
$r = JLoader::import("tests.unit.suite.libraries.index2",null,'custom1.');
$this->assertFalse($r);
//這個地方需要注意
//import 自己的腳本會比較麻煩,得至少設置$base,總感覺怪怪的.本來覺得綠色的部分是可以的,還是返回false.
}
其實問題就出在:
function import( $filePath, $base = null, $key = 'libraries.' )
{
static $paths;
if (!isset($paths)) {
$paths = array();
}
$keyPath = $key ? $key . $filePath : $filePath;
if (!isset($paths[$keyPath]))
{
if ( ! $base ) {//問題在這里
$base = dirname( __FILE__ );
}
$parts = explode( '.', $filePath );
$classname = array_pop( $parts );
switch($classname)
{
case 'helper' :
$classname = ucfirst(array_pop( $parts )).ucfirst($classname);
break;
default :
$classname = ucfirst($classname);
break;
}
//條件語句的問題,準確說是boolean的問題.寫一測試腳本如下:
<?php
if(!'')
echo "chedan0\n";
if(!"")
echo "chedan1\n";
if(!null)
echo "chedan2\n";
if(!0)
echo "bie chedan0\n";
if(!1)
echo "bie chedan1\n";
if(!false)
echo "bie chedan2\n";
結果如下:
Content-type: text/html
chedan0
chedan1
chedan2
bie chedan0
bie chedan2
看來import方法考慮還不是挺周全.難道都要這樣子作$r = JLoader::import("unit.suite.libraries.index2",'tests','random.tests.');
本來我的包名是tests.unit.suite.libraries 腳本是index2.php,真是有噶怪.
另外include()方法成功返回1,所以上面就有 $this->assertEquals(1,$r); JLoader::import 既有可能return true,又有可能return 1,看來腳本還是太靈活了,容易迷惑人.