简体   繁体   English

如何在PhpSpec中访问Laravel Controller的静态属性?

[英]How to access static property of Laravel Controller in PhpSpec?

I am trying to write a class which will read the routes given by the constructor it's constructed with. 我正在尝试编写一个类,它将读取由它构造的构造函数给出的路由。 However, even after 1h of googling I didn't find anything how to access the getRouter method of the Laravel Controller - because it's a static function . 然而,即使在谷歌搜索1小时后,我也没有找到任何方法来访问Laravel控制器的getRouter方法 - 因为它是一个静态函数 I've tried many things but most of the time I got following error: 我尝试了很多东西,但大部分时间我都遇到了以下错误:

Uncaught Error: Using $this when not in object context in 未捕获错误:在不在对象上下文中时使用$ this
vendor/phpspec/prophecy/src/Prophecy/Doubler/Generator/ClassCreator.php(49) : vendor / phpspec / prophecy / src / Prophecy / Doubler / Generator / ClassCreator.php(49):
eval()'d code:13 eval()'代码:13
Stack trace: #0 [internal function]: Double\\Illuminate\\Routing\\Controller\\P4::getRouter() 堆栈跟踪:#0 [内部功能]:Double \\ Illuminate \\ Routing \\ Controller \\ P4 :: getRouter()

How can I achieve this or is this just not possible with PhpSpec? 我怎样才能实现这一目标,或者这对PhpSpec来说是不可能的?

My Spec: 我的规格:

use Illuminate\Routing\Controller;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class OptionDescriberSpec extends ObjectBehavior
{

    function let(Controller $controller)
    {
      $this->beConstructedWith($controller);
    }

    function it_should_read_the_aviable_routes_of_the_controller()
    {
        $this->getController()->getRouter()->shouldReturn('Router');
        $this->render()->shouldReturn('Router');
    }
}

My Class: 我的课:

use Illuminate\Routing\Controller;

class OptionDescriber
{

    /**
     * @var Controller
     */
    protected $controller;

    /**
     * OptionDescriber constructor.
     *
     * @param Controller $controller
     */
    public function __construct(Controller $controller)
    {
        $this->controller = $controller;
    }

    public function render()
    {
        return $this->controller->getRouter();
    }
}
function it_should_read_the_aviable_routes_of_the_controller(Controller $controller, Router $router)
{
    $this->getController()->willReturn($controller);
    $controller->getRouter()->willReturn($router);

    $this->render()->shouldReturn($router);
}

Alltough it's not recommended to spec controllers. 尽管不建议使用规格控制器。 Make the controller as thin as possible and move the 'meat' into a service that is used by the controller. 使控制器尽可能薄,并将“肉”移动到控制器使用的服务中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM