Slide 1

Slide 1 text

© - BASE, Inc. X \ ⾃作してみよう / xUnitフレームワーク . . PHP Conference 懇親会LT - @hgsgtk - Let’s make xUnit framework -

Slide 2

Slide 2 text

© - BASE, Inc. About me BASE BANK, Inc. Dev Division Tech Lead 『みんなのPHP 現場で役⽴つ最新 ノウハウ!』共著者 (PHPにおけるユニットテスト) https://www.amazon.co.jp/ gp/product/ @hgsgtk Kazuki Higashiguchi

Slide 3

Slide 3 text

© - BASE, Inc. Do you love PHPUnit?

Slide 4

Slide 4 text

© - BASE, Inc. I LOVE PHPUnit

Slide 5

Slide 5 text

© - BASE, Inc. But need more

Slide 6

Slide 6 text

© - BASE, Inc. If you make xUnit framework

Slide 7

Slide 7 text

© - BASE, Inc. You will love PHPUnit more!

Slide 8

Slide 8 text

© - BASE, Inc. Let’s make it!

Slide 9

Slide 9 text

© - BASE, Inc. By the way, PHPUnit is https://phpunit.de/

Slide 10

Slide 10 text

© - BASE, Inc. About xUnit Generic name of testing framework Kent Beck: SUnit (Smalltalk) -> JUnit (Java) Sebastian Bergmann: PHPUnit (PHP) https://en.wikipedia.org/wiki/XUnit

Slide 11

Slide 11 text

© - BASE, Inc. Main features of xUnit family • Test Method • Assertion Method • Test Suites • Run tests, Get report Refs:Meszaros, Gerard. xUnit Test Patterns: Refactoring Test Code』

Slide 12

Slide 12 text

© - BASE, Inc. Today’s topic • Test Method • Assertion Method • Test Suites • Run tests, Get report Refs:Meszaros, Gerard. xUnit Test Patterns: Refactoring Test Code』

Slide 13

Slide 13 text

© - BASE, Inc. Let’s go

Slide 14

Slide 14 text

© - BASE, Inc. hgsgtk/mpunit https://github.com/hgsgtk/mpunit

Slide 15

Slide 15 text

© - BASE, Inc. The Goal is to execute this test

Slide 16

Slide 16 text

© - BASE, Inc. • Test Method • Assertion Method • Test Suites • Run tests, Get report Refs:Meszaros, Gerard. xUnit Test Patterns: Refactoring Test Code』 Today’s topic

Slide 17

Slide 17 text

© - BASE, Inc. Make executable command /bin/mpunit

Slide 18

Slide 18 text

© - BASE, Inc. Find test methods and execute them .Find test files .Find test classes .Find test methods .Execute test methods

Slide 19

Slide 19 text

© - BASE, Inc. . Find test files /src/Command.php Collect files “*Test.php”

Slide 20

Slide 20 text

© - BASE, Inc. . Find test classes /src/Command.php 定義済みクラスから、ユーザー定義型で MPUnit\TestCaseを継承して いるを探し出す

Slide 21

Slide 21 text

© - BASE, Inc. . Find test classes /src/Command.php Get declared classes

Slide 22

Slide 22 text

© - BASE, Inc. . Find test classes /src/Command.php Is user defined?

Slide 23

Slide 23 text

© - BASE, Inc. . Find test classes /src/Command.php Is extended MPUnit\TestCase?

Slide 24

Slide 24 text

© - BASE, Inc. (Tips)PHP feature to collect classes • get_declared_classes() • 定義済みのクラスの名前を配列として返す • ReflectionClass::isUserDefined() • 内部クラスかどうかを検査 • ReflectionClass::getParentClass() • 親クラスの情報を取得 https://www.php.net/manual/ja/reflectionclass.isuserdefined.php https://www.php.net/manual/ja/function.get-declared-classes.php https://www.php.net/manual/ja/reflectionclass.getparentclass.php

Slide 25

Slide 25 text

© - BASE, Inc. . Find test methods /src/Command.php テストクラスのPUBLICメソッドから、先頭 が test で始まるメソッドを探す

Slide 26

Slide 26 text

© - BASE, Inc. . Find test methods /src/Command.php テストクラスのPUBLICメソッドから、先頭 が test で始まるメソッドを探す Find public method which name starts ʻtest’

Slide 27

Slide 27 text

© - BASE, Inc. (Tips)Gather functions • ReflectionClass::getMethods • クラスのメソッド名を取得 • ReflectionMethod::IS_PUBLIC etc https://www.php.net/manual/ja/reflectionclass.getmethods.php

Slide 28

Slide 28 text

© - BASE, Inc. . Execute test methods /src/Command.php If AssertionError => Failed (F) Else => Success (.)

Slide 29

Slide 29 text

© - BASE, Inc. Understand xUnit • Two strategies to find test functions • Test Discovery‧Test Enumeration • In Test Discovery, user does not need to register manually • Almost xUnit families’ strategy is Test Discovery

Slide 30

Slide 30 text

© - BASE, Inc. • Test Method • Assertion Method • Test Suites • Run tests, Get report Refs:Meszaros, Gerard. xUnit Test Patterns: Refactoring Test Code』 Today’s topic

Slide 31

Slide 31 text

© - BASE, Inc. Ways to assert • assert function(PHP) • (Choice it, because I want to implement with minimum dependencies) • beberlei/assert • webmozart/assert • hamcrest/hamcrest-php etc

Slide 32

Slide 32 text

© - BASE, Inc. Setting assertion options /bin/mpunit

Slide 33

Slide 33 text

© - BASE, Inc. (Tips)Setting directive for assertion • zend.assertions (default: ) • assert()を⽣成するか‧実⾏するか • 1 の場合、コードは⽣成され実⾏される • assert.exception (default: ) • アサーション失敗に指定したオブジェクトをス ロー • 指定なしだとAssertionError https://www.php.net/manual/ja/function.assert.php https://www.php.net/manual/ja/class.assertionerror.php

Slide 34

Slide 34 text

© - BASE, Inc. (Tips)Options for assertion • 様々なassertフラグを設定できる • ASSERT_ACTIVE • ASSERT_WARNING • ASSERT_BAIL • ASSERT_QUIET_EVAL • ASSERT_CALLBACK https://www.php.net/manual/ja/function.assert-options.php

Slide 35

Slide 35 text

© - BASE, Inc. assertion Make assertion method “assertSame”

Slide 36

Slide 36 text

© - BASE, Inc. • Test Method • Assertion Method • Test Suites • Run tests, Get report Refs:Meszaros, Gerard. xUnit Test Patterns: Refactoring Test Code』 Today’s topic

Slide 37

Slide 37 text

© - BASE, Inc. Run tests, Get report TestCaseを継承したクラスから assertSame が使えるように Use assertion method in test method

Slide 38

Slide 38 text

© - BASE, Inc. hgsgtk/mpunit https://github.com/hgsgtk/mpunit Demonstration

Slide 39

Slide 39 text

© - BASE, Inc. Get Report

Slide 40

Slide 40 text

© - BASE, Inc. By making xUnit, • PHPUnit has a lot of features, it’s amazing!! • I also red internal implementations of PHPUnit to make it. I learned a lot.

Slide 41

Slide 41 text

© - BASE, Inc. LOVE to PHPUnit became STRONGER!! By making xUnit,

Slide 42

Slide 42 text

© - BASE, Inc. X Thanks!