如何调试在不使用setter的情况下更新php Trait的实例变量 背景
我正试图使用建议的leeway属性来规避奇怪的时区差异 jwt中的代码非常简单,一个特点是,首先有一个名为leeway的实例变量:如何调试在不使用setter的情况下更新php Trait的实例变量 背景,php,laravel,laravel-5,jwt,Php,Laravel,Laravel 5,Jwt,我正试图使用建议的leeway属性来规避奇怪的时区差异 jwt中的代码非常简单,一个特点是,首先有一个名为leeway的实例变量: trait DatetimeTrait { /** * Time leeway in seconds. * * @var int */ protected $leeway = 0; 稍后,使用此帮助器方法确定给定的时间戳是否在将来: /** * Determine whether the value is
trait DatetimeTrait
{
/**
* Time leeway in seconds.
*
* @var int
*/
protected $leeway = 0;
稍后,使用此帮助器方法确定给定的时间戳是否在将来:
/**
* Determine whether the value is in the future.
*
* @param mixed $value
*
* @return bool
*/
protected function isFuture($value)
{
return Utils::isFuture($value, $this->leeway);
}
我仔细看了一遍代码,发现leeway的值是随机变化的。在某个时候,我发现我们调用的是不同的trait类,所以我使用了
public function setLeeway($leeway)
{
$this->leeway = $leeway;
var_dump("set leeway: ".$this->leeway." on object: ".spl_object_hash($this));
return $this;
}
protected function isFuture($value)
{
var_dump("calling is future with leeway :".$this->leeway. " on object: ".spl_object_hash($this));
return Utils::isFuture($value, $this->leeway);
}
但请参阅日志:
string(60) "set leeway: 5000 on object: <obj-hash-1>"
/vendor/tymon/jwt-auth/src/Claims/DatetimeTrait.php:97:
string(60) "set leeway: 5000 on object: <obj-hash-2>"
/vendor/tymon/jwt-auth/src/Claims/DatetimeTrait.php:71:
string(76) "calling is future with leeway :0 on object: <obj-hash-1>"
string(60)“对象上的设置余地:5000:”
/vendor/tymon/jwt auth/src/Claims/DatetimeTrait.php:97:
字符串(60)“对象上的设置偏差:5000:”
/vendor/tymon/jwt auth/src/Claims/DatetimeTrait.php:71:
字符串(76)“调用是未来的,对象上有0的回旋余地:”
其中:
是000000007386F4100000038D49B05
是000000007386f41600000000038d49b05
如果不使用setter更新实例变量,同一个trait有时会有相同的实例变量0,然后是500,这怎么可能呢?如何至少调试正在更新的属性(如果不是通过setter完成的话)可以找到更多调试。但在深入研究了身份验证之后,我想我应该遵循孙子的单元测试:最好是不战而胜 所以我就这样嘲笑了整个认证
public function getFakeClient()
{
$data['type'] = 'client';
$client = factory(App\User::class)->create($data);
$this->be($client);
$this->client = $client;
Auth::shouldReceive('user')->andReturn($this->client);
Auth::shouldReceive('check')->andReturn(true);
return $this->client;
}
在每个场景之前(我使用的是Behat),我只是跳过了auth中间件:
public function beforeScenario(BeforeScenarioScope $scope)
{
parent::setUp();
$this->withoutMiddleware([\App\Http\Middleware\TokenAuthentication::class,
\App\Http\Middleware\TokenAuthorization::class
]);
}