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

自動テストの2歩目

chiroruxx
February 26, 2020

 自動テストの2歩目

2020/02/26のPHP勉強会で話した内容です。

chiroruxx

February 26, 2020
Tweet

More Decks by chiroruxx

Other Decks in Technology

Transcript

  1. ಉ͡ϩδοΫ function sum(array $array) { $sum = 0; foreach ($array

    as $value) { $sum += $value; } return $sum; } public function testSum() { $input = [1, 2, 3]; $expected = 0; foreach ($input as $value) { $expected += $value; } $this->assertEquals($expected, sum($input)); }
  2. ಉ͡ϩδοΫ function sum(array $array) { $sum = 0; foreach ($array

    as $value) { $sum += $value; } return $sum; } public function testSum() { $input = [1, 2, 3]; $expected = 0; foreach ($input as $value) { $expected += $value; } $this->assertEquals($expected, sum($input)); }
  3. ܾΊଧͪςετ public function testSum() { $this->assertEquals(6, sum([1, 2, 3])); $this->assertEquals(2,

    sum([2])); $this->assertEquals(0, sum([0, 0, 0])); $this->assertEquals(0, sum([])); }
  4. σʔλϓϩόΠμ public function testSum(array $input, bool $expected) { $this->assertEquals($expected, sum($input));

    } public function dataProvider(): array { return [ ['input' => [1, 2, 3], 'expected' => 6], ['input' => [2], 'expected' => 2], ['input' => [0, 0, 0], 'expected' => 0], ['input' => [], 'expected' => 0], ]; }
  5. σʔλϓϩόΠμͷ࢖͍·Θ͠ public function testGetQuestionsWithoutDate(array $input, array $expected) { $expected['created_at'] =

    null; $expected['updated_at'] = null; $expected['deleted_at'] = null; $service = new QuestionService(); $this->assertEquals($expected, $service->getQuestionsWithoutDate($input)); }
  6. σʔλϓϩόΠμͷ࢖͍·Θ͠ public function testGetQuestionsWithoutDate(array $input, array $expected) { $expected['created_at'] =

    null; $expected['updated_at'] = null; $expected['deleted_at'] = null; $service = new QuestionService(); $this->assertEquals($expected, $service->getQuestionsWithoutDate($input)); }
  7. ςετσʔλϓϩόΠμ public function getQuestionDataProvider(): array { return [ 'input' =>

    [ 'title' => 'question title', ], 'expected' => [ 'title' => 'question title', 'author' => ‘default author', 'created_at' => '2019-02-03 00:00:00', 'updated_at' => '2019-02-03 00:00:00', 'deleted_at' => null, ], ]; } public function getQuestionWithoutDateDataProvider(): array { return [ 'input' => [ 'title' => 'question title', ], 'expected' => [ 'title' => 'question title', 'author' => ‘default author', ], ]; }
  8. ςετσʔλϓϩόΠμ public function testGetQuestions(array $input, array $expected) { $service =

    new QuestionService(); $this->assertEquals($expected, $service->getQuestions($input)); } public function testGetQuestionsWithoutDate(array $input, array $expected) { $service = new QuestionService(); $this->assertEquals($expected, $service->getQuestionsWithoutDate($input)); }
  9. ςετσʔλϓϩόΠμ public function getQuestionDataProvider(): array { return [ 'input' =>

    [ 'title' => 'question title', ], 'expected' => $this->defaultQuestionAttributes(), ]; } public function defaultQuestionAttributes(array $overrides = []) { $default = [ 'title' => 'question title', 'author' => 'default author', 'created_at' => '2019-02-03 00:00:00', 'updated_at' => '2019-02-03 00:00:00', 'deleted_at' => null, ]; return array_merge($default, $overrides); }
  10. ϥϯμϜ஋ͷݕূ public function testDelete() { $question1 = factory(Question::class)->create(); $question2 =

    factory(Question::class)->create(); $service = new QuestionService(); $service->delete($question1); $this->assertDatabaseMissing(['title' => $question1->title]); $this->assertDatabaseHas(['title' => $question2->title]); }
  11. ϥϯμϜ஋ͷݕূ public function testDelete() { $question1 = factory(Question::class)->create(['title' => 'ফ͑Δ']);

    $question2 = factory(Question::class)->create(['title' => '࢒Δ']); $service = new QuestionService(); $service->delete($question1); $this->assertDatabaseMissing(['title' => 'ফ͑Δ']); $this->assertDatabaseHas(['title' => '࢒Δ']); }