Slide 1

Slide 1 text

࠶ֶश PHPUnitͰϞοΫΛ ࢖ͬͯςετ͠Α͏ 2015-03-30 ୈ88ճ PHPษڧձ ࠇҪय़ਓ

Slide 2

Slide 2 text

ςετॻ͍ͯ·͔͢ʁ

Slide 3

Slide 3 text

Ғ͍ਓ͸͜͏͍͏Θ͚Ͱ͕͢

Slide 4

Slide 4 text

࣮ࡍ໘౗͍͘͞

Slide 5

Slide 5 text

ςετͷॏཁੑ͸Θ͔͍ͬͯ Δ͠ɺϢχοτςετͱ͔ॻ ͍ͯ͋ΔͱͳΜ͔ΧοίΠΠ

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

ςετΛॻ͘ͱCIͰ͖Δ

Slide 9

Slide 9 text

΍Βͳ͍ΑΓ΍ͬͨ΄͏͕ ઈରྑ͍

Slide 10

Slide 10 text

Ͱ΋໘౗͍͘͞

Slide 11

Slide 11 text

ͳ͔ͥʁ

Slide 12

Slide 12 text

ͱ͍͏͜ͱΛࠓ೔͸ߟ͑ͯΈ ·͠ΐ͏ɻ

Slide 13

Slide 13 text

جຊͷ͓͞Β͍

Slide 14

Slide 14 text

PHPUnitΛ࢖͓͏

Slide 15

Slide 15 text

PHPUnitΛ࢖͓͏ • ݱࡏ΋ͬͱ΋ϙϐϡϥʔͳPHPʹ͓͚ΔϢχοτ ςετϥΠϒϥϦ • Θ͕͠ए͍ࠒ͸ simpletest ͱ͔ lime ͱ͔ܹ͋ͬͯ ͍͠ઓ͍Λ܁Γ޿͓͛ͯͬͯͷ͎ΰϗΰϗ… • ࠓ͸ͱʹ͔͘ίϨΛ֮͑Ε͹OK(͔ͨͩ͠ͳΓԞ ͕ਂ͍)

Slide 16

Slide 16 text

Install࣮͸௒؆୯ • wget https://phar.phpunit.de/phpunit.phar • chmod 775 phpunit.phar • mv phpunit.phar $HOME/bin/phpunit http://project-p.jp/halt/?p=1882

Slide 17

Slide 17 text

ςετͷͻͳܗΛ༻ҙ࣮ͯ͠ߦ class Member { }
 class MemberTest extends PHPUnit_Framework_TestCase
 {
 public function test_createInstance()
 {
 $member = new Member();
 $this->assertTrue($member instanceof Member);
 }
 
 }

Slide 18

Slide 18 text

php-timeͷ৔߹ https://github.com/ttsuruoka/php-time

Slide 19

Slide 19 text

/** * ݱࡏ͕࣌ؒ༩͑ΒΕͨ࣌ؒΑΓલ͔Ͳ͏͔ * * Ҿ਺ͱͯ͠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; }

Slide 20

Slide 20 text

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')); }

Slide 21

Slide 21 text

؆୯ʂ ΈΜͳ΋΍Ζ͏ʂ

Slide 22

Slide 22 text

͓͠·͍

Slide 23

Slide 23 text

phpunit.phar ࢖ͬͨΓ assert ϝιου࢖ͬͯ ݕূͨ͠Γɺ phpunit.xml Ͱςετέʔεࢦ ఆͨ͠Γɺ MVC Ͱ͍ͬͨΒ Model ͚ͩ΍Δ ͱ͍ͩͿҧ͏ͱ͔ fixture ॻ͘ͱ͔ xdebug ೖΕΔͱΧόϨοδͱΕΔͱ͔ͱ͔ͱ͔… " શ෦஌ͬͯΔʂ ͚Ͳॻ͔ͳ͍ΜͰ͢ʂ

Slide 24

Slide 24 text

ͳ͔ͥʁ

Slide 25

Slide 25 text

ݱ࣮ͷ࣮૷΋ͬͱ໘౗͍͘͞ • AͷதͰBΛࢀরͯ͠B͕CΛࢀর͍ͯ͠Δ • ֎෦ͱͷ௨৴Λߦͳ͏ • Ϣʔβʔ໊΍ύεϫʔυ͕ඞཁ • ؔ࿈σʔλ͕ͳ͍ͱಈ͔ͳ͍ • ݶఆ͞Εͨ؀ڥ͡Όͳ͍ͱಈ͔ͳ͍ • ͳͲͳͲ…

Slide 26

Slide 26 text

࣮ࡍ͸΋ͬͱ໘౗͍͘͞ Internet DB Library Local

Slide 27

Slide 27 text

ϞοΫΛ࢖͏ͱ͍͍ײ͡ʹς ετʹઐ೦Ͱ͖Δ

Slide 28

Slide 28 text

ϞοΫͱ͸ • ϞοΫ = ໦ܕ = ֎ݟͷݕ౼ͳͲͷͨΊʹ࡞Β ΕΔݪܕ • ࣮ࡍͷΦϒδΣΫτΛஔ͖׵͑ͯɺςετ༻ ʹઃఆͨ͠஋Λฦ͢ͱ͖ʹ࢖͏

Slide 29

Slide 29 text

͢΂ͯΛίϯτϩʔϧԼʹ͓͘ Mock Library Local

Slide 30

Slide 30 text

ԿΛݴ͍ͬͯΔͷ͔?

Slide 31

Slide 31 text

0͔Βॻ͚ͱݴΘΕΔͱ ໘౗͚ͩͲ ࣄྫ͕͋Ε͹ίϐϖͰઓ͑Δ

Slide 32

Slide 32 text

InternetΞΫηε Λ ϞοΫͰ୅༻͢Δ

Slide 33

Slide 33 text

Services_Photozouͷ৔߹ https://github.com/ha1t/php-Services_Photozou

Slide 34

Slide 34 text

public function nop() { $xml = $this->callMethod("nop"); if (strpos($xml, 'stat="ok"') !== false) { return true; } else { return false; } } $this->callMethodͰAPI௨৴͕͸͠ΔͷͰ ωοτϫʔΫ͕ͭͳ͕ͬͯͳ͍ͱಈ͔ͳ͍

Slide 35

Slide 35 text

public function testNop() { $response = << 1111 EOD; $photozou = new Services_Photozou($this- >user, $this->password); $this->injectMock($photozou, $response); $this->assertTrue($photozou->nop()); }

Slide 36

Slide 36 text

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); } ͬ͘͟Γ͜Μͳײ͡ͰͰ͖Δ

Slide 37

Slide 37 text

Guzzle->getEmitter()- >attach() • ϞμϯͳPHPͰͷఆ൪ʹͳ͖ͬͯͨ௨৴ϥΠ ϒϥϦ Guzzle ʹ͸ɺϞοΫΛ attach Ͱ͖Δ ػೳ͕͋Γɺ͜ΕΛར༻͢ΔࣄͰɺ Guzzleɹ Λར༻ͯ͠ωοτϫʔΫΞΫηεΛߦ͍ͬͯ Δίʔυ͸ɺ؆୯ʹςετΛॻ͘ࣄ͕Ͱ͖Δ Α͏ʹͳΔɻ

Slide 38

Slide 38 text

DBΞΫηε Λ ϞοΫͰ୅༻͢Δ

Slide 39

Slide 39 text

SimpleDBIͷ৔߹ https://github.com/ttsuruoka/php-simple-dbi

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

SimpleDBI::instance • SimpleDBI͸γϯάϧτϯΫϥεͳͷͰɺࣗ෼ࣗ਎ Λϝϯόʹ͍࣋ͬͯΔ • ͜ͷϝϯόΛϞοΫʹࠩ͠ସ͑ͯ͠·͏ࣄͰɺ೚ҙ ͷڍಈΛ࣮ݱ͢Δ • DBͷ৔߹ fixture ࢖͏ͷ΋͋Γ͚ͩͲίετͰ͔͍ ͷͰ಺෦ͷϩδοΫ͚ͩΛݕূ͍ͨ͠ͳΒϞοΫ࢖ ͏

Slide 43

Slide 43 text

·ͱΊ • IneternetΞΫηε΍DBΞΫηε͸ϞοΫ࢖͏ ͱ؆୯ʹ୅༻Ͱ͖ͯςετ͕ॻ͖΍͘͢ͳΔ Α • ͍ͬͯ͏͔ͳΜͰ΋ஔ͖׵͑Ͱ͖Δ͔Βຊ࣭ తͳ෦෼Ҏ֎͸ΨϯΨϯஔ͖׵͍͍͑ͯͱࢥ ͏Α

Slide 44

Slide 44 text

͋ͳ͕ͨॻ͍ͯΔ ͋ͷίʔυͰ΋ ͪΐͬͱςετॻ͚ͦ͏ͳ ؾ͕͖ͯ͠·ͤΜ͔ʁ

Slide 45

Slide 45 text

େࣄͳࣄ͸ manual ʹ શ෦ॻ͍ͯ͋Δ " https://phpunit.de/manual/current/ja/test- doubles.html#test-doubles.mock-objects

Slide 46

Slide 46 text

࣭ٙԠ౴(͋Ε͹)