PHPSpec and Laravel - how to handle double method not found issues

岁酱吖の 提交于 2019-12-13 05:59:06

问题


I appear to be having issues with my spec tests when it comes to stubs that are calling other methods.

I've been following Laracasts 'hexagonal' approach for my controller to ensure it is only responsible for the HTTP layer.

Controller

<?php

use Apes\Utilities\Connect;
use \OAuth;

class FacebookConnectController extends \BaseController {


    /**
     * @var $connect
     */
    protected $connect;


    /**
     * Instantiates $connect
     *
     * @param $connect
     */
    function __construct()
    {
        $this->connect = new Connect($this, OAuth::consumer('Facebook'));
    }


    /**
     * Login user with facebook
     *
     * @return void
     */
    public function initialise() {

        // TODO: Actually probably not needed as we'll control
        // whether this controller is called via a filter or similar
        if(Auth::user()) return Redirect::to('/');

        return $this->connect->loginOrCreate(Input::all());
    }


    /**
     * User authenticated, return to main game view
     * @return Response
     */
    public function facebookConnectSucceeds()
    {
        return Redirect::to('/');
    }


}

So when the route is initialised I construct a new Connect instance and I pass an instance of $this class to my Connect class (to act as a listener) and call the loginOrCreate method.

Apes\Utilities\Connect

<?php

namespace Apes\Utilities;

use Apes\Creators\Account;
use Illuminate\Database\Eloquent\Model;
use \User;
use \Auth;
use \Carbon\Carbon as Carbon;

class Connect
{

    /**
     * @var $facebookConnect
     */
    protected $facebookConnect;


    /**
     * @var $account
     */
    protected $account;


    /**
     * @var $facebookAuthorizationUri
     */
    // protected $facebookAuthorizationUri;


    /**
     * @var $listener
     */
    protected $listener;


    public function __construct($listener, $facebookConnect)
    {
        $this->listener = $listener;
        $this->facebookConnect = $facebookConnect;
        $this->account = new Account();
    }


    public function loginOrCreate($input)
    {
        // Not the focus of this test
        if(!isset($input['code'])){
            return $this->handleOtherRequests($input);
        }

        // Trying to stub this method is my main issue
        $facebookUserData = $this->getFacebookUserData($input['code']);

        $user = User::where('email', '=', $facebookUserData->email)->first();

        if(!$user){
            // Not the focus of this test
            $user = $this->createAccount($facebookUserData);
        }

        Auth::login($user, true);
        // I want to test that this method is called
        return $this->listener->facebookConnectSucceeds();

    }


    public function getFacebookUserData($code)
    {
        // I can't seem to stub this method because it's making another method call
        $token = $this->facebookConnect->requestAccessToken($code);

        return (object) json_decode($this->facebookConnect->request( '/me' ), true);
    }

    // Various other methods not relevant to this question

I've tried to trim this down to focus on the methods under test and my understanding thus far as to what is going wrong.

Connect Spec

<?php

namespace spec\Apes\Utilities;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use \Illuminate\Routing\Controllers\Controller;
use \OAuth;
use \Apes\Creators\Account;


class ConnectSpec extends ObjectBehavior
{
    function let(\FacebookConnectController $listener, \OAuth $facebookConnect, \Apes\Creators\Account $account)
    {
        $this->beConstructedWith($listener, $facebookConnect, $account);
    }


    function it_should_login_the_user($listener)
    {
        $input = ['code' => 'afacebooktoken'];

        $returnCurrentUser = (object) [
            'email' => 'existinguser@domain.tld',
        ];

        $this->getFacebookUserData($input)->willReturn($returnCurrentUser);

        $listener->facebookConnectSucceeds()->shouldBeCalled();
        $this->loginOrCreate($input);
    }

So here's the spec that I'm having issues with. First I pretend that I've got a facebook token already. Then, where things are failing, is that I need to fudge that the getFacebookUserData method will return a sample user that exists in my users table.

However when I run the test I get:

Apes/Utilities/Connect                               
  37  ! it should login the user
      method `Double\Artdarek\OAuth\Facade\OAuth\P13::requestAccessToken()` not found.

I had hoped that 'willReturn' would just ignore whatever was happening in the getFacebookUserData method as I'm testing that separately, but it seems not.

Any recommendations on what I should be doing?

Do I need to pull all of the OAuth class methods into their own class or something? It seems strange to me that I might need to do that considering OAuth is already its own class. Is there some way to stub the method in getFacebookUserData?

Update 1

So I tried stubbing the method that's being called inside getFacebookUserData and my updated spec looks like this:

function it_should_login_the_user($listener, $facebookConnect)
{
    $returnCurrentUser = (object) [
        'email' => 'existinguser@domain.tld',
    ];
    $input = ['code' => 'afacebooktoken'];

    // Try stubbing any methods that are called in getFacebookUserData
    $facebookConnect->requestAccessToken($input)->willReturn('alongstring');
    $facebookConnect->request($input)->willReturn($returnCurrentUser);

    $this->getFacebookUserData($input)->willReturn($returnCurrentUser);


    $listener->facebookConnectSucceeds()->shouldBeCalled();
    $this->loginOrCreate($input);
}

The spec still fails but the error has changed:

Apes/Utilities/Connect                               
  37  ! it should login the user
      method `Double\Artdarek\OAuth\Facade\OAuth\P13::requestAccessToken()` is not defined.

Interestingly if I place these new stubs after the $this->getFacebookUserData stub then the error is 'not found' instead of 'not defined'. Clearly I don't fully understand the inner workings at hand :D


回答1:


Not everything, called methods in your dependencies have to be mocked, because they will in fact be called while testing your classes:

...

$facebookConnect->requestAccessToken($input)->willReturn(<whatever it should return>);

$this->getFacebookUserData($input)->willReturn($returnCurrentUser);

...

If you don't mock them, phpspec will raise a not found.




回答2:


I'm not familiar with the classes involved but that error implies there is not method Oauth:: requestAccessToken().

Prophecy will not let you stub non-existent methods.



来源:https://stackoverflow.com/questions/23002474/phpspec-and-laravel-how-to-handle-double-method-not-found-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!