标签: php-traits

如何使用 phpunit 模拟非抽象特征方法?

我有一个特征(由另一个特征使用)和一个非抽象方法,我需要模拟,但是当我尝试这样做时,我收到错误消息:

尝试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)

php phpunit php-traits

6
推荐指数
1
解决办法
1132
查看次数

使用与特征函数相同名称的PHP类

我有以下代码作为示例.

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)

我可以把所有细节都说明为什么这是必要的,但我想让这个问题变得简单.由于我的特殊情况,从班级客户端延伸不是答案.

是否有可能让一个特征与使用它的类具有相同的函数名称,但除了类函数之外还调用特征函数?

目前它只会使用类函数(因为它似乎覆盖了特征)

php class traits php-7 php-traits

5
推荐指数
1
解决办法
984
查看次数

标签 统计

php ×2

php-traits ×2

class ×1

php-7 ×1

phpunit ×1

traits ×1