【问题标题】:How to debug updating an instance variable of a php Trait without using setters如何在不使用 setter 的情况下调试更新 php Trait 的实例变量
【发布时间】:2019-12-29 11:32:47
【问题描述】:

背景

我正在尝试按照here 的推荐使用jwt-auth 的 leeway 属性来规避奇怪的时区差异。

jwt 中的代码非常简单,DateTimeTrait trait 是 used,首先带有一个名为 leeway 的实例变量:

trait DatetimeTrait
{
    /**
     * Time leeway in seconds.
     *
     * @var int
     */
    protected $leeway = 0;

稍后使用此帮助方法is used 来确定给定的时间戳是否在未来:

/**
 * 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 类,所以我使用 spl_object_hash 来了解我们是否使用相同的对象,结果证明我们这样做了:

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>"

在哪里:

  • &lt;obj-hash-1&gt;000000007386f4100000000038d49b05
  • &lt;obj-hash-2&gt;000000007386f4160000000038d49b05

问题

如果没有使用 setter 更新该实例变量,同一 trait 怎么可能有时具有相同的实例变量 0,然后是 500?我如何至少调试正在更新的属性(如果不是通过设置器完成)

【问题讨论】:

    标签: php laravel laravel-5 jwt


    【解决方案1】:

    更多调试可以找到here。但是在深入研究了身份验证之后,我想我还是按照孙子的单元测试advice最好不战而胜

    所以我像这样嘲笑了整个身份验证

    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
                                 ]);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-26
      • 2020-06-12
      相关资源
      最近更新 更多