1

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. I've tried many things but most of the time I got following error:

Uncaught Error: Using $this when not in object context in
vendor/phpspec/prophecy/src/Prophecy/Doubler/Generator/ClassCreator.php(49) :
eval()'d code:13
Stack trace: #0 [internal function]: Double\Illuminate\Routing\Controller\P4::getRouter()

How can I achieve this or is this just not possible with 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();
    }
}
Krenor
  • 621
  • 10
  • 27

1 Answers1

0
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.

gvf
  • 1,039
  • 7
  • 6