unit testing - Confused about PhpSpec stubs and mocks again -
unit testing - Confused about PhpSpec stubs and mocks again -
i'm building laravel 5 application @ moment , have gotten myself confused how mock things in phpspec.
i'm building schedule times validator requires intended schedule checked against current schedules , see if there's overlap (events not allowed overlap).
i need pull in schedules in question can test against them. @ moment it's basic wherebetween query, it's going lot more complicated there'll recurring schedules check against well.
so here's stripped downwards class. want test doesnotoverlap function.
use app\schedule; class scheduletimesvalidator { protected $schedule; public function __construct(schedule $schedule) { $this->schedule = $schedule; } public function doesnotoverlap($slug, $intended) { $schedules = $this->getschedulesbetween($slug, $intended); if(empty($schedules)) homecoming true; homecoming false; } protected function getschedulesbetween($slug, $intended) { // casting array create testing little easier homecoming $this->schedule->whereisrecurring(false) ->ofchurch($slug) ->wherebetween('start', [$intended['start'], $intended['end']]) ->get()->toarray(); }
and here's spec
use phpspec\objectbehavior; utilize prophecy\argument; class scheduletimesvalidatorspec extends objectbehavior { protected $validintended = [ 'start' => '2015-12-01 12:00:00', 'end' => '2015-12-01 13:00:00' ]; protected $churchnonrecurringschedules = [ ['start' => '2014-11-20 13:00:00', 'end' => '2014-11-21 14:00:00'], ['start' => '2014-11-23 10:36:07', 'end' => '2014-11-23 11:36:07'], ]; function let($schedule) { $schedule->beadoubleof('app\schedule'); $this->beconstructedwith($schedule); } function it_is_initializable() { $this->shouldhavetype('app\validation\scheduletimesvalidator'); } function it_should_return_true_if_it_does_not_overlap($schedule) { // $schedule->any()->willreturn([]); // $schedule->whereisrecurring()->shouldbecalled(); // $schedule->whereisrecurring(false)->ofchurch()->wherebetween()->get()->toarray()->willreturn([]); // $schedule->willreturn([]); // $this->getschedulesbetween('slug', $this->validintended)->willreturn([]); $this->doesnotoverlap('slug', $this->validintended)->shouldreturn(true); } // tear downwards function letgo() {} }
if run get:
! should homecoming true if not overlap method 'double\app\schedule\p8::whereisrecurring()' not found.
i tried (as can see) various commented out things mock $schedule return, doesn't seem work.
so guess want mock protected getschedulesbetween
method in class, doing things $this->getschedulesbetween($arg, $arg)->willreturn(blah)
doesn't work.
do need pull getschedulesbetween() out of class , move class , mock that? or need force $this->schedule->blah doestnotoverlap method can mock $schedule return?
i don't want test app\schedule laravel model - want mock it's returning , hardcoding variety of queries run different model results.
end of long day here brain little zonked.
update 2014-10-23so created scope on schedule model
public function scopeschedulesbetween($query, $slug, $intended) { homecoming $query->whereisrecurring(false) ->ofchurch($slug) ->wherebetween('start', [$intended['start'], $intended['end']]); }
then created new app\helpers\schedulequeryhelper instantiated app\schedule variable , added method:
public function getschedulesbetween($slug, $intended) { homecoming $this->schedule->schedulesbetween($slug, $intended)->get()->toarray(); }
then updated spec do
function let($schedulequeryhelper) { $schedulequeryhelper->beadoubleof('app\helpers\schedulequeryhelper'); $this->beconstructedwith($schedulequeryhelper); } function it_should_return_true_if_it_does_not_overlap($schedulequeryhelper) { $schedulequeryhelper->getschedulesbetween('slug', $this->validintended)->willreturn([]); $this->doesnotoverlap('slug', $this->validintended)->shouldreturn(true); }
and in scheduletimesvalidator class did
public function doesnotoverlap($slug, $intended) { $schedules = $this->schedulequeryhelper->getschedulesbetween($slug, $intended); if(empty($schedules)) { homecoming true; } homecoming false; }
and phpspec mocking other class ok. seems roundabout way doing things.
unit-testing model mocking phpspec laravel-5
Comments
Post a Comment