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

Unit Testing Laravel

nunulk
November 14, 2018

Unit Testing Laravel

nunulk

November 14, 2018
Tweet

More Decks by nunulk

Other Decks in Programming

Transcript

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  4. Ϣχοτςετ

    View full-size slide

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

    View full-size slide

  6. Ϣχοτςετʹదͨ͠γνϡΤʔγϣϯ
    w ܭࢉ
    w ৚݅൑ఆ
    w ม׵

    View full-size slide

  7. Ϣχοτςετͷྫʢܭࢉʣ
    // 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);
    }

    View full-size slide

  8. Ϣχοτςετͷྫʢ৚݅൑ఆʣ
    // 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);
    }

    View full-size slide

  9. Ϣχοτςετͷྫʢม׵ʣ
    public function testToArrayInvisiblePassword()
    {
    $user = factory(User::class)->make();
    $this->assertArrayNotHasKey('password', $user->toArray());
    $this->assertArrayHasKey('email', $user->toArray());
    }

    View full-size slide

  10. PHPUnit
    w ΞαʔγϣϯBTTFSU&RVBMT BTTFSU4BNF BTTFSU5SVF
    BTTFSU'BMTF BTTFSU$POUBJOT BTTFSU/VMM BTTFSU5IBU FUD
    w Ξϊςʔγϣϯ!EBUB1SPWJEFS !FYQFDUFE&YDFQUJPO
    !HSPVQ FUD
    w ίʔυΧόϨοδ

    View full-size slide

  11. LaravelʹΑΔ֦ு

    View full-size slide

  12. 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

    View full-size slide

  13. 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);
    });

    View full-size slide

  14. 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();

    View full-size slide

  15. ςετͷ༏ઌॱҐ

    View full-size slide

  16. ςετͷ༏ઌॱҐ
    ΞϓϦέʔγϣϯͷେࣄͳ෦෼ʢ͓ۚͱ͔ʣʹؔΘΔॲ
    ཧ

    ˠ࣮֬ʹνΣοΫ͢ΔͨΊʹࣗಈԽ
    ೖग़ྗ΍ܭࢉͷύλʔϯ͕ͨ͘͞Μ͋Δॲཧ

    ˠͨ͘͞Μςετ͢Δͷ͕ΊΜͲ͍͘͞ͷͰࣗಈԽ
    ࢓༷มߋ͕ೖΓ΍͍͢ॲཧ

    ˠมΘΔͨͼʹςετ͢Δͷ͕ΊΜͲ͍͘͞ͷͰࣗಈԽ

    View full-size slide

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

    View full-size slide