php - PHPSpec and Laravel - how to handle double method not found issues -
i appear having issues spec tests when comes stubs calling other methods.
i've been following laracasts 'hexagonal' approach controller ensure responsible 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 facebook * * @return void */ public function initialise() { // todo: not needed we'll control // whether controller called via filter or similar if(auth::user()) return redirect::to('/'); return $this->connect->loginorcreate(input::all()); } /** * user authenticated, return main game view * @return response */ public function facebookconnectsucceeds() { return redirect::to('/'); } }
so when route initialised construct new connect instance , pass instance of $this class connect class (to act listener) , call 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 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 focus of test if(!isset($input['code'])){ return $this->handleotherrequests($input); } // trying stub method main issue $facebookuserdata = $this->getfacebookuserdata($input['code']); $user = user::where('email', '=', $facebookuserdata->email)->first(); if(!$user){ // not focus of test $user = $this->createaccount($facebookuserdata); } auth::login($user, true); // want test method called return $this->listener->facebookconnectsucceeds(); } public function getfacebookuserdata($code) { // can't seem stub method because it's making method call $token = $this->facebookconnect->requestaccesstoken($code); return (object) json_decode($this->facebookconnect->request( '/me' ), true); } // various other methods not relevant question
i've tried trim down focus on methods under test , understanding far 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 spec i'm having issues with. first pretend i've got facebook token already. then, things failing, need fudge getfacebookuserdata method return sample user exists in users table.
however when run test get:
apes/utilities/connect 37 ! should login user method `double\artdarek\oauth\facade\oauth\p13::requestaccesstoken()` not found.
i had hoped 'willreturn' ignore whatever happening in getfacebookuserdata method i'm testing separately, seems not.
any recommendations on should doing?
do need pull of oauth class methods own class or something? seems strange me might need considering oauth own class. there way stub method in getfacebookuserdata?
update 1
so tried stubbing method that's being called inside getfacebookuserdata , updated spec looks this:
function it_should_login_the_user($listener, $facebookconnect) { $returncurrentuser = (object) [ 'email' => 'existinguser@domain.tld', ]; $input = ['code' => 'afacebooktoken']; // try stubbing methods 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 error has changed:
apes/utilities/connect 37 ! should login user method `double\artdarek\oauth\facade\oauth\p13::requestaccesstoken()` not defined.
interestingly if place these new stubs after $this->getfacebookuserdata stub error 'not found' instead of 'not defined'. don't understand inner workings @ hand :d
not everything, called methods in dependencies have mocked, because in fact called while testing classes:
... $facebookconnect->requestaccesstoken($input)->willreturn(<whatever should return>); $this->getfacebookuserdata($input)->willreturn($returncurrentuser); ...
if don't mock them, phpspec raise not found
.
Comments
Post a Comment