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

Let tested using a mock with PHPUnit

halt
March 30, 2015
2.8k

Let tested using a mock with PHPUnit

PHPUnitなんて当たり前に知ってるし、ユニットテストだってちょっとは書いたことある。
でも実務では面倒くさくて結局書けない。そんなあなたのためにモックの具体的な使い方を紹介した資料です。

halt

March 30, 2015
Tweet

Transcript

  1. PHPUnitΛ࢖͓͏ • ݱࡏ΋ͬͱ΋ϙϐϡϥʔͳPHPʹ͓͚ΔϢχοτ ςετϥΠϒϥϦ • Θ͕͠ए͍ࠒ͸ simpletest ͱ͔ lime ͱ͔ܹ͋ͬͯ

    ͍͠ઓ͍Λ܁Γ޿͓͛ͯͬͯͷ͎ΰϗΰϗ… • ࠓ͸ͱʹ͔͘ίϨΛ֮͑Ε͹OK(͔ͨͩ͠ͳΓԞ ͕ਂ͍)
  2. ςετͷͻͳܗΛ༻ҙ࣮ͯ͠ߦ class Member { }
 class MemberTest extends PHPUnit_Framework_TestCase
 {


    public function test_createInstance()
 {
 $member = new Member();
 $this->assertTrue($member instanceof Member);
 }
 
 }
  3. /** * ݱࡏ͕࣌ؒ༩͑ΒΕͨ࣌ؒΑΓલ͔Ͳ͏͔ * * Ҿ਺ͱͯ͠null ͕༩͑ΒΕͨͱ͖͸ɺfalse Λฦ͠·͢ɻ * @param

    $a unixtimeΛද͢੔਺ɺ·ͨ͸೔෇ͱͯ͠ղऍՄ ೳͳจࣈྻ * @return boolean ݱࡏ͕࣌ؒ༩͑ΒΕͨ࣌ؒΑΓલͷͱ͖ trueɺͦΕҎ֎ͷͱ͖ false */ public static function before($a) { if (is_null($a)) { return false; } $a = is_int($a) ? $a : strtotime($a, self::$time); return self::$time < $a; }
  4. public function test_before() { Time::set(strtotime('2011-11-01 15:00:00')); $this->assertEquals(Time::before(null), false); $this->assertEquals('2011-11-01 15:00:00',

    Time::now()); $this->assertTrue(Time::before('2011-11-01 16:00:00')); $this->assertTrue(Time::before('2011-11-01 15:00:01')); $this->assertFalse(Time::before('2011-11-01 15:00:00')); $this->assertFalse(Time::before('2011-11-01 14:59:59')); }
  5. phpunit.phar ࢖ͬͨΓ assert ϝιου࢖ͬͯ ݕূͨ͠Γɺ phpunit.xml Ͱςετέʔεࢦ ఆͨ͠Γɺ MVC Ͱ͍ͬͨΒ

    Model ͚ͩ΍Δ ͱ͍ͩͿҧ͏ͱ͔ fixture ॻ͘ͱ͔ xdebug ೖΕΔͱΧόϨοδͱΕΔͱ͔ͱ͔ͱ͔… " શ෦஌ͬͯΔʂ ͚Ͳॻ͔ͳ͍ΜͰ͢ʂ
  6. public function nop() { $xml = $this->callMethod("nop"); if (strpos($xml, 'stat="ok"')

    !== false) { return true; } else { return false; } } $this->callMethodͰAPI௨৴͕͸͠ΔͷͰ ωοτϫʔΫ͕ͭͳ͕ͬͯͳ͍ͱಈ͔ͳ͍
  7. public function testNop() { $response = <<<EOD <?xml version="1.0" encoding="UTF-8"

    ?> <rsp stat="ok"> <info><user_id>1111</user_id></info> </rsp> EOD; $photozou = new Services_Photozou($this- >user, $this->password); $this->injectMock($photozou, $response); $this->assertTrue($photozou->nop()); }
  8. public function injectMock(Services_Photozou $photozou, $response) { $mock = new \GuzzleHttp\Subscriber

    \Mock($response); $class = new ReflectionClass(‘Services_Photozou'); $property = $class->getProperty(‘client'); $property->setAccessible(true); $client = $property->getValue($photozou); $client->getEmitter()->attach($mock); } ͬ͘͟Γ͜Μͳײ͡ͰͰ͖Δ
  9. Guzzle->getEmitter()- >attach() • ϞμϯͳPHPͰͷఆ൪ʹͳ͖ͬͯͨ௨৴ϥΠ ϒϥϦ Guzzle ʹ͸ɺϞοΫΛ attach Ͱ͖Δ ػೳ͕͋Γɺ͜ΕΛར༻͢ΔࣄͰɺ

    Guzzleɹ Λར༻ͯ͠ωοτϫʔΫΞΫηεΛߦ͍ͬͯ Δίʔυ͸ɺ؆୯ʹςετΛॻ͘ࣄ͕Ͱ͖Δ Α͏ʹͳΔɻ
  10. public static function get($member_id) { $con = SimpleDBI::conn(); $sql =

    "SELECT * FROM member WHERE id = ?"; $row = $con->row($sql, array($member_id)); if ($row === false) { throw new AppException(); } return new self($row); }
  11. public function testGet() { $row = ['id' => 1,'name' =>

    'john']; " $mock = $this->getMock('DB', ['row'], [], '', false); $mock->expects($this->any()) ->method('row') ->will($this->returnValue($row)); " $class = new ReflectionClass(‘DB'); $property = $class->getProperty(‘instances'); $property->setAccessible(true); $property->setValue([null => $mock]); " $member = Member::getObject(1); $this->assertTrue($member instanceof Member); }