【发布时间】:2018-06-02 06:39:14
【问题描述】:
刚开始使用 PHPUnit 框架,我正在尝试将其合并到现有代码中。
假设我是一个班级Math,我的写法如下,
<?php
/**
*
* Date: 19/12/2017
* Time: 16:22
*/
include_once 'include/testing_namespace.php';
class Math extends TestCase {
function isEven($x) {
if ($this->assertInternalType(IsType::TYPE_NUMERIC)) {
if ($x % 2 == 0) {
return true;
} else {
return false;
}
}
}
function isOdd($y) {
if ($this->assertInternalType(IsType::TYPE_NUMERIC)) {
if ($y % 2 >= 1) {
return true;
} else {
return false;
}
}
}
}
我有一个像这样的 php 文件 include/testing_namespace.php,
<?php
namespace testing;
use PHPUnit\Util\PHP;
use PHPUnit\Framework\Constraint;
use PHPUnit\Framework\Constraint\IsType;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Assert;
$path = $_SERVER['APPL_PHYSICAL_PATH'];
// class includes, may need to branch these off into separate include files
include_once $path . '\vendor\phpunit\phpunit\src\Framework\Assert\Functions.php';
include_once $path . '\vendor\phpunit\phpunit\src\Framework\TestCase.php';
include_once $path . 'vendor\phpunit\phpunit\src\Framework\Constraint\IsEqual.php';
include_once $path . 'vendor\phpunit\phpunit\src\Framework\Constraint\IsFalse.php';
include_once $path . 'vendor\phpunit\phpunit\src\Framework\Constraint\IsEmpty.php';
include_once $path . 'vendor\phpunit\phpunit\src\Framework\Constraint\IsNull.php';
?>
假设我正在这样测试,
$m = new testing\Math();
$integer1 = 8;
var_dump($m->isEven($integer1));
die();
(这似乎工作了一段时间)
但是假设我正在测试的文件有一个声明的函数也称为isEven,它不在命名空间中,但也包含在文件中。
假设oldfuncs/OldMath.php 也有一个同名的isEven() 函数。
第一个问题是,我在这里正确使用命名空间吗?我也可以包含一个带有命名空间声明的文件吗?是否会将包含它的文件放入该命名空间?
【问题讨论】:
标签: php namespaces phpunit web-development-server