我有一个特征(由另一个特征使用)和一个非抽象方法,我需要模拟,但是当我尝试这样做时,我收到错误消息:
尝试
getOfficeDays配置由于不存在、未指定、最终或静态而无法配置的方法
这是我要测试的代码:
trait OfficeDaysTrait {
public function getOfficeDays(): array {
// This normally calls the database, that's why I want to mock it in tests
return [
[
'day' => 'mon',
'openingTime' => '08:00:00',
'closingTime' => '17:00:00'
]
];
}
}
trait EmployeeAttendenceTrait {
use OfficeDaysTrait;
public function isLate($today)
{
$late = false;
$officeDays = $this->getOfficeDays();
// compare logic with today.. whether the employee is late or not
return $late;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的测试方法主体:
$mock = $this->getMockForTrait(EmployeeAttendenceTrait::class);
$mock->expects($this->once()) …Run Code Online (Sandbox Code Playgroud) 我有以下代码作为示例.
trait sampletrait{
function hello(){
echo "hello from trait";
}
}
class client{
use sampletrait;
function hello(){
echo "hello from class";
//From within here, how do I call traits hello() function also?
}
}
Run Code Online (Sandbox Code Playgroud)
我可以把所有细节都说明为什么这是必要的,但我想让这个问题变得简单.由于我的特殊情况,从班级客户端延伸不是答案.
是否有可能让一个特征与使用它的类具有相同的函数名称,但除了类函数之外还调用特征函数?
目前它只会使用类函数(因为它似乎覆盖了特征)