Unit Testing Laravel

73b0a25ed03f649ec63ded82ce4ac7cb?s=47 nunulk
November 14, 2018

Unit Testing Laravel

73b0a25ed03f649ec63ded82ce4ac7cb?s=128

nunulk

November 14, 2018
Tweet

Transcript

  1. 8.

    Ϣχοτςετͷྫʢܭࢉʣ // test public function testRemainingHousrs() { $today = Carbon::today();

    $task = factory(Task::class)->make([ 'due' => Carbon::tomorrow() ]); $this->assertSame(24, $task->remainingHours($today)); } // production public function remainingHours(Carbon $datetime): int { return $this->due->diffInHours($datetime); }
  2. 9.

    Ϣχοτςετͷྫʢ৚݅൑ఆʣ // test public function testIsOverdue() { $now = Carbon::now();

    $task = factory(Task::class)->make([ 'due' => Carbon::yesterday() ]); $this->assertTrue($task->isOverdue($now)); } // production public function isOverdue(Carbon $datetime): bool { return $this->due->lessThan($datetime); }
  3. 11.
  4. 12.

    PHPUnit w ΞαʔγϣϯBTTFSU&RVBMT BTTFSU4BNF BTTFSU5SVF  BTTFSU'BMTF BTTFSU$POUBJOT BTTFSU/VMM BTTFSU5IBU

    FUD w Ξϊςʔγϣϯ!EBUB1SPWJEFS !FYQFDUFE&YDFQUJPO  !HSPVQ FUD w ίʔυΧόϨοδ
  5. 14.

    PHPUnit in Laravel w .PDLFSZ 'BLFSΛόϯυϧ w .PEFM'BDUPSZ 'BLFS w

    'BDBEF .PDLFSZ w %*ίϯςφΛ࢖ͬͨ.PDLJOH w ศརτϨΠτ *OUFSBDUT8JUI"VUIFOUJDBUJPO  .PDLT"QQMJDBUJPO4FSWJDFT  *OUFSBDUT8JUI%BUBCBTF3FGSFTI%BUBCBTF  %BUBCBTF5SBOTBDUJPOT FUD
  6. 15.

    Mockery $task = factory(Task::class)
 ->states(['doing', 'high'])
 ->create(); Mockery::mock(TaskRepository::class) ->shouldReceive('find')
 ->once()


    ->andReturn($task); Mockery::mock(Log::class) ->shouldReceive(‘debug')
 ->once() ->andReturnUsing(function ($subject, $body) { // ͜ͷதͰΞαʔγϣϯ͕Ͱ͖·͢ $this->assertContains('Laravel', $body); });
  7. 16.

    Model Factory, Faker $factory->define(Task::class, function (Faker $faker) { return [

    'subject' => $faker->sentence, 'priority' => Priority::default(), 'status' => Status::default(), ]; }); $factory->state(Task::class, 'doing', function () { return [ 'status' => Status::DOING(), ]; }); factory(Task::class)->states(['doing', 'high'])->create();