Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Unit Testing Laravel

73b0a25ed03f649ec63ded82ce4ac7cb?s=47 nunulk
November 14, 2018

Unit Testing Laravel

73b0a25ed03f649ec63ded82ce4ac7cb?s=128

nunulk

November 14, 2018
Tweet

Transcript

  1. Unit Testing Laravel nunulk Laravel Meetup Okinawa ୈ7ճ

  2. ຊ೔࿩͢͜ͱ w ࣗݾ঺հ w Ϣχοτςετͱ͸ w 1)16OJUʹ͍ͭͯ w -BSBWFMʹΑΔ1)16OJUͷ֦ுػೳʹ͍ͭͯ w

    ςετͷ༏ઌॱҐʹ͍ͭͯ
  3. ࣗݾ঺հ

  4. ࣗݾ঺հ • nunulkʢ͵͵Δ͘ʣ • ౦ژ͔Βདྷ·ͨ͠ʢ4ധ5೔ʣ • ϑϦʔϥϯεͷϓϩάϥϚʢࡾྲྀʣ • ޷͖ͳٕज़: PHP/Laravel/TypeScript/Vue.js/Elixir/

    Scala/Nim
  5. Ϣχοτςετ

  6. Ϣχοτςετͱ͸ w Ϋϥε΍ؔ਺ʢϝιουʣ୯ҐͰ࡞੒͠ɺͦͷΫϥε΍ ؔ਺ͷॲཧΛςετ͢Δ w ϗϫΠτϘοΫεςετΛ࡞੒͢Δʢ৚݅෼ذɺೖྗύϥ ϝʔλͷ಺༰ʹґଘ͢Δॲཧʣ w ϒϥοΫϘοΫεςετΛ࡞੒͢Δʢ૝ఆ͞ΕΔೖྗύϥ ϝʔλ͔ΒύλʔϯΛબͿɻڥք஋ɺҟৗ஋ɺFUDʣ

    w ґଘؔ܎͸ϞοΫ΍ελϒͰஔ͖׵͑Δ
  7. Ϣχοτςετʹదͨ͠γνϡΤʔγϣϯ w ܭࢉ w ৚݅൑ఆ w ม׵

  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); }
  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); }
  10. Ϣχοτςετͷྫʢม׵ʣ public function testToArrayInvisiblePassword() { $user = factory(User::class)->make(); $this->assertArrayNotHasKey('password', $user->toArray());

    $this->assertArrayHasKey('email', $user->toArray()); }
  11. PHPUnit

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

    FUD w Ξϊςʔγϣϯ!EBUB1SPWJEFS !FYQFDUFE&YDFQUJPO  !HSPVQ FUD w ίʔυΧόϨοδ
  13. LaravelʹΑΔ֦ு

  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
  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); });
  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();
  17. ςετͷ༏ઌॱҐ

  18. ςετͷ༏ઌॱҐ  ΞϓϦέʔγϣϯͷେࣄͳ෦෼ʢ͓ۚͱ͔ʣʹؔΘΔॲ ཧ
 ˠ࣮֬ʹνΣοΫ͢ΔͨΊʹࣗಈԽ  ೖग़ྗ΍ܭࢉͷύλʔϯ͕ͨ͘͞Μ͋Δॲཧ
 ˠͨ͘͞Μςετ͢Δͷ͕ΊΜͲ͍͘͞ͷͰࣗಈԽ  ࢓༷มߋ͕ೖΓ΍͍͢ॲཧ


    ˠมΘΔͨͼʹςετ͢Δͷ͕ΊΜͲ͍͘͞ͷͰࣗಈԽ
  19. ·ͱΊ w -BSBWFMͳΒΦʔϧΠϯϫϯύοέʔδͳςετ͕Ͱ͖Δ w ܭࢉɺ൑ఆɺม׵Λ΍ΔͳΒϢχοτςετΛॻ͜͏ w ΞϓϦέʔγϣϯͷେࣄͳ෦෼ʢ͓ۚͱ͔ʣʹؔΘΔॲཧɺ ೖग़ྗ΍ܭࢉͷύλʔϯ͕ͨ͘͞Μ͋Δॲཧɺ࢓༷มߋ ͕ೖΓ΍͍͢ॲཧΛॻ͘ͳΒϢχοτςετΛॻ͜͏