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

Testing on Laravel

nunulk
January 31, 2019

Testing on Laravel

nunulk

January 31, 2019
Tweet

More Decks by nunulk

Other Decks in Technology

Transcript

  1. Laravel Testing Tips w 1)16OJU w BTTFSU&RVBMT BTTFSU4BNF BTTFSU5SVF BTTFSU'BMTF

    BTTFSU$POUBJOT  BTTFSU/VMM BTTFSU5IBU BTTFSU"SSBZ)BT,FZ FUD w !EBUB1SPWJEFS !FYQFDUFE&YDFQUJPO !HSPVQ FUD w -BSBWFM w .PDLFSZ 'BLFS w .PEFM'BDUPSZ 'BDLFS w 'BDBEF .PDLFSZ w %*$POUBJOFS w *OUFSBDUT8JUI"VUIFOUJDBUJPO .PDLT"QQMJDBUJPO4FSWJDFT  *OUFSBDUT8JUI%BUBCBTF3FGSFTI%BUBCBTF %BUBCBTF5SBOTBDUJPOT FUD w BDUJOH"T BTTFSU%BUBCBTF)BT BTTFSU%BUBCBTF.JTTJOH FUD
  2. Assertions $this->assertNotSame(1, ‘1'); $this->assertTrue(true); $this->assertContains('PHP', 'PHPUnit'); $this->assertNotContains(2, [1, 3, 5,

    7, 9]); $this->assertNotNull(1); $this->assertArrayHasKey('Laravel', [
 'PHP' => '7.2',
 'Laravel' => ‘5.7'
 ]);
  3. DataProvider /** * @param string $key1 * @param string $key2

    * @return void * @dataProvider dataSomething */ public function testSomething(string $key1, string $key2) {
 }
 public function dataSomething() { return [ 'OK' => [ 'key1' => 'value1', 'key2' => 'value2', ], 'NG:έʔε1' => [ 'key1' => 'value3', 'key2' => 'value4', ], ]; }
  4. Model Factory + Faker $factory->define(App\Task::class, function (Faker $faker) { return

    [ 'subject' => $faker->text(), 'status' => 'open', ‘priority' => 'normal', ]; }); $factory->state(App\Task::class, 'doing', [ 'status' => 'doing', ]); $factory->state(App\Task::class, ‘high', [ 'priority' => 'high', ]); $task = factory(Task::class)->states([‘doing’,’high'])->make();
  5. Mocking DI $mockService = \Mockery::mock(‘SomeService')
 ->shouldReturn('someMethod')
 ->once()
 ->andReturn(1)
 ->getMock(); 


    $this->app->bind(SomeService::class,
 function () use ($mockService) { return $mockService; }
 );