【发布时间】:2015-04-22 23:51:01
【问题描述】:
我有几个要重写为 OOP 类的函数。我对 OOP 还是很陌生,并且正在使用它来学习 OOP 的基本概念。
这是我写的第一堂课,考虑到以下内容
单元测试/隔离测试
一个类应该只做一件事
可重用性
这个类接受 4 个用户设置的变量并根据 URL 中设置的参数对其进行测试,然后返回 4 个条件,每个变量集一个
这是类(我删除了一些与isAuthorReferrer() 方法相同的方法和属性)
namespace PG\Single\Post\Navigation;
/**
* Test set values against the super global given. Returns conditional properties
* which is boolean values. true is returned on success and false on failure
*
* @param $superGlobalVar Super global to test the values against
* @param (string) $authorReferrer
* @param (string) $dateReferrer
* @param (string) $searchReferrer
* @param (string) $taxReferrer
*
*/
class RequestReferrerHandler implements RequestReferrerHandlerInterface
{
/**
* @var (array) $superGlobalVar
*/
protected $superGlobalVar;
/**
* @var (bool) $isAuthorReferrer
*/
protected $isAuthorReferrer;
//OTHER PROPERTIES
/**
* Public constructor method
*
* @param $SuperGlobalVar Super global to get data from
*/
public function __construct($superGlobalVar = null, $authorReferrer= null, $dateReferrer = null, $searchReferrer = null, $taxReferrer = null )
{
/**
* Properties
*/
$this->superGlobalVar = $superGlobalVar;
$this->authorReferrer = $authorReferrer;
$this->dateReferrer = $dateReferrer;
$this->searchReferrer = $searchReferrer;
$this->taxReferrer = $taxReferrer;
/**
* Conditional methods, all returns boolean values
*/
$this->isAuthorReferrer();
//etc
}
/**
* Test $authorReferrer against $superGlobalVar
*
* @return (bool) true on success or false on failure
*/
public function isAuthorReferrer()
{
if ($this->authorReferrer && isset($this->superGlobalVar[$this->authorReferrer])) {
$isAuthorReferrer = true;
} else {
$isAuthorReferrer = false;
}
return $this->isAuthorReferrer = $isAuthorReferrer;
}
//OTHER METHODS, SAME AS isAuthorReferrer() METHOD
/**
* Returns an array of super global variables
* @return (array) $this->getRequest
*/
public function getSuperGlobalVar()
{
return $this->superGlobalVar;
}
}
我读过一些帖子说你应该使用构造函数注入,所以围绕它编写了我的类。
我确实有一些顾虑,一个是在构造函数中实例化我的方法
我的测试
$a = new PG\Single\Post\Navigation\RequestReferrerHandler($_GET, 'aq', 'dq', 'sq', 'tq');
?><pre><?php var_dump($a); ?></pre><?php
使用上面的代码,我得到以下输出
object(PG\Single\Post\Navigation\RequestReferrerHandler)#496 (9) {
["superGlobalVar":protected]=>
array(1) {
["tq"]=>
string(10) "category 1"
}
["isAuthorReferrer":protected]=>
bool(false)
["isDateReferrer":protected]=>
bool(false)
["isSearchReferrer":protected]=>
bool(false)
["isTaxReferrer":protected]=>
bool(true)
["authorReferrer"]=>
string(2) "aq"
["dateReferrer"]=>
string(2) "dq"
["searchReferrer"]=>
string(2) "sq"
["taxReferrer"]=>
string(2) "tq"
}
如果我从构造函数中删除我的方法,我的四个条件属性会返回 NULL,但如果我像这样直接调用方法,我会得到正确的布尔值
?><pre><?php var_dump($a->isAuthorReferrer()); ?></pre><?php
var_dump() 的结果是:
如果设置了aq
bool(true)
如果aq 未设置
bool(false)
我的问题
我是否在构造函数中正确使用了这些方法,我的类是否设置正确
【问题讨论】:
-
var_dump($a->isAuthorReferrer()); 的输出是什么??
-
@noob 我已经用这些细节更新了我的问题
-
所以它完全按预期工作有什么问题?
-
是的,我的问题是,我是否在构造函数中正确使用了这些方法
标签: php unit-testing oop constructor