PHPUnit でよりよくテストを書くために

12,832 views

Published on

第56回PHP勉強会@関東で PHPUnit について話してきた
http://blog.yuyat.jp/archives/1386

Published in: Technology, Business

PHPUnit でよりよくテストを書くために

  1. 1. [begin, [define, fib, [lambda, [x], [if, [<, :x, 2], :x, [+, [fib, [-, :x, 2]], [fib, [-, :x, 1]]]]]], [print, fib(10) = ], [println, [fib, 10]]] => fib(10) = 55
  2. 2. class CalculatorTest extends PHPUnit_Framework_TestCase{    public function setUp()    {        $this->calc = new Calculator;    }    public function test_add_ ()    {        $result = $this->calc->add(1, 2);        $this->assertSame(3, $result);    }}
  3. 3. class CalculatorTest extends PHPUnit_Framework_TestCase{    public function setUp()    {        $this->calc = new Calculator;    }    public function test_add_ ()    {        $result = $this->calc->add(1, 2);        $this->assertSame(3, $result);    }}
  4. 4. class CalculatorTest extends PHPUnit_Framework_TestCase{    public function setUp()    {        $this->calc = new Calculator;    }    public function test_add_ ()    {        $result = $this->calc->add(1, 2);        $this->assertSame(3, $result);    }}
  5. 5. class CalculatorTest extends PHPUnit_Framework_TestCase{    public function setUp()    {        $this->calc = new Calculator;    }    public function test_add_ ()    {        $result = $this->calc->add(1, 2);        $this->assertSame(3, $result);    }}
  6. 6. public function test_isValid_ true(){    $user = new User;    $user->setName(Yuya);    $user->setUrl(http://yuyat.jp/);    $user->setAge(24);    $this->assertTrue($user->isValid());}public function test_isValid_ false(){    $user = new User;    $user->setName(Yuya);    $user->setUrl(http://yuyat.jp/);    $user->setAge(NULL);    $this->assertFalse($user->isValid());}
  7. 7. public function test_isValid_ true(){    $user = new User;    $user->setName(Yuya);    $user->setUrl(http://yuyat.jp/);    $user->setAge(24);    $this->assertTrue($user->isValid());}public function test_isValid_ false(){    $user = new User;    $user->setName(Yuya);    $user->setUrl(http://yuyat.jp/);    $user->setAge(NULL);    $this->assertFalse($user->isValid());}
  8. 8. public function test_isValid_ true(){    $user = $this->createValidUser();    $this->assertTrue($user->isValid());}public function test_isValid_ false(){    $user = $this->createValidUser();    $user->setAge(NULL);    $this->assertFalse($user->isValid());}
  9. 9. public function test_isValid_ true(){    $user = $this->createValidUser();    $this->assertTrue($user->isValid());}public function test_isValid_ false(){    $user = $this->createValidUser();    $user->setAge(NULL);    $this->assertFalse($user->isValid());}
  10. 10. public function test_isValid_ true(){    $user = $this->createValidUser();    $this->assertTrue($user->isValid());}public function test_isValid_ false(){    $user = $this->createValidUser();    $user->setAge(NULL);    $this->assertFalse($user->isValid());}
  11. 11. public function test_isValid_ false(){    $user = $this->createValidUser();    $user->setAge(NULL);    $this->assertFalse($user->isValid());    $user = $this->createValidUser();    $user->setName(NULL);    $this->assertFalse($user->isValid());}
  12. 12. public function test_isValid_ false(){    $user = $this->createValidUser();    $user->setAge(NULL);    $this->assertFalse($user->isValid());    $user = $this->createValidUser();    $user->setName(NULL);    $this->assertFalse($user->isValid());}
  13. 13. public function test_isValid_ false(){    $user = $this->createValidUser();    $user->setAge(NULL);    $this->assertFalse($user->isValid());    $user = $this->createValidUser();    $user->setName(NULL);    $this->assertFalse($user->isValid());}
  14. 14. public function test_isValid_ Null false(){    $user = $this->createValidUser();    $user->setAge(NULL);    $this->assertFalse($user->isValid());}public function test_isValid_ Null false(){    $user = $this->createValidUser();    $user->setName(NULL);    $this->assertFalse($user->isValid());}
  15. 15. public function test_isValid_ Null false(){    $user = $this->createValidUser();    $user->setAge(NULL);    $this->assertFalse($user->isValid());}public function test_isValid_ Null false(){    $user = $this->createValidUser();    $user->setName(NULL);    $this->assertFalse($user->isValid());}
  16. 16. class Request{    public function isSsl()    {        return $_SERVER[HTTPS] === on;    }}
  17. 17. class Request{    public function isSsl()    {        return $_SERVER[HTTPS] === on;    }}
  18. 18. public function test_isSsl_HTTPS true(){    $_SERVER[HTTPS] = on;    $this->assertTrue($this->request->isSsl());}public function test_isSsl_HTTPS false(){    unset($_SERVER[HTTPS]);    $this->assertFalse($this->request->isSsl());}
  19. 19. public function test_isSsl_HTTPS true(){    $_SERVER[HTTPS] = on;    $this->assertTrue($this->request->isSsl());}public function test_isSsl_HTTPS false(){    unset($_SERVER[HTTPS]);    $this->assertFalse($this->request->isSsl());}
  20. 20. public function test_isSsl_HTTPS true(){    $_SERVER[HTTPS] = on;    $this->assertTrue($this->request->isSsl());}public function test_isSsl_HTTPS false(){    unset($_SERVER[HTTPS]);    $this->assertFalse($this->request->isSsl());}
  21. 21. public function test_isSsl_HTTPS true(){    $_SERVER[HTTPS] = on;    $this->assertTrue($this->request->isSsl());}public function test_isSsl_HTTPS false(){    unset($_SERVER[HTTPS]);    $this->assertFalse($this->request->isSsl());}
  22. 22. class Request{ public function __construct($server) {     $this->_server = $server; } public function isSsl() {     return $this->_server === on; }}
  23. 23. class Request{ public function __construct($server) {     $this->_server = $server; } public function isSsl() {     return $this->_server === on; }}
  24. 24. public function test_isSsl_HTTPS true(){    $request = new Request(array(HTTPS => on));    $this->assertTrue($request->isSsl());}public function test_isSsl_HTTPS false(){    $request = new Request(array());    $this->assertFalse($request->isSsl());}
  25. 25. public function test_isSsl_HTTPS true(){    $request = new Request(array(HTTPS => on));    $this->assertTrue($request->isSsl());}public function test_isSsl_HTTPS false(){    $request = new Request(array());    $this->assertFalse($request->isSsl());}
  26. 26. class User{    protected $_name;    protected $_birthday;    public function setName($name) { /*~*/ }    public function getName() { /*~*/ }    public function getAge() { /*~*/ }    public static function findByName($name) { /*~*/ }    public function save() { /*~*/ }}
  27. 27. $user = User::findByName(Bob);$user->setName(Alice);$user->save();
  28. 28. class User{    protected $_name;    protected $_birthday;    public function setName($name) { /*~*/ }    public function getName() { /*~*/ }    public function getAge() { /*~*/ }}class UserMapper{    public function findByName($name) { /*~*/ }    public function save(User $user) { /*~*/ }}
  29. 29. $mapper = new UserMapper;$user = $mapper->findByName(Bob);$user->setName(Alice);$mapper->save($user);
  30. 30. class User{    protected $_name;    protected $_birthday;    public function setName($name) { /*~*/ }    public function getName() { /*~*/ }    public function getAge() { /*~*/ }    public static function findByName($name) { /*~*/ }    public function save() { /*~*/ }}
  31. 31. class User{    protected $_name;    protected $_birthday;    public function setName($name) { /*~*/ }    public function getName() { /*~*/ }    public function getAge() { /*~*/ }    public static function findByName($name) { /*~*/ }    public function save() { /*~*/ }}
  32. 32. class User{    protected $_name;    protected $_birthday;    public function setName($name) { /*~*/ }    public function getName() { /*~*/ }    public function getAge() { /*~*/ }    public static function findByName($name) { /*~*/ }    public function save() { /*~*/ }}
  33. 33. class User{    protected $_name;    protected $_birthday;    public function setName($name) { /*~*/ }    public function getName() { /*~*/ }    public function getAge() { /*~*/ }    public static function findByName($name) { /*~*/ }    public function save() { /*~*/ }}
  34. 34. class User{    protected $_name;    protected $_birthday;    public function setName($name) { /*~*/ }    public function getName() { /*~*/ }    public function getAge() { /*~*/ }}class UserMapper{    public function findByName($name) { /*~*/ }    public function save(User $user) { /*~*/ }}
  35. 35. class User{    protected $_name;    protected $_birthday;    public function setName($name) { /*~*/ }    public function getName() { /*~*/ }    public function getAge() { /*~*/ }}class UserMapper{    public function findByName($name) { /*~*/ }    public function save(User $user) { /*~*/ }}
  36. 36. class User{    protected $_name;    protected $_birthday;    public function setName($name) { /*~*/ }    public function getName() { /*~*/ }    public function getAge() { /*~*/ }}class UserMapper{    public function findByName($name) { /*~*/ }    public function save(User $user) { /*~*/ }}
  37. 37. class User{    protected $_name;    protected $_birthday;    public function setName($name) { /*~*/ }    public function getName() { /*~*/ }    public function getAge() { /*~*/ }}class UserMapper{    public function findByName($name) { /*~*/ }    public function save(User $user) { /*~*/ }}
  38. 38. class Config{    protected static $_instance;    private function __construct() {}    public static function getInstance()    {        if (empty(self::$_instance)) {            self::$_instance = new self;        }        return self::$_instance;    }}
  39. 39. public function __clone(){    throw new BadMethodCallException( Clone is not allowed. );}
  40. 40. public function test_get_ (){    $config = Config::getInstance();    $config->set(message, Hello);    $this->assertSame( Hello, $config->get(message) );}
  41. 41. public function test_get_ Null(){    $config = Config::getInstance();    $this->assertNull( $config->get(message) );}
  42. 42. public function test_get_ Null(){    $config = Config::getInstance();    $this->assertNull( $config->get(message) );}
  43. 43. public function test_get_ Null(){    $config = Config::getInstance();    $config->init();    $this->assertNull( $config->get(message) );}
  44. 44. public function test_get_ Null(){    $config = Config::getInstance();    $config->init();    $this->assertNull( $config->get(message) );}
  45. 45. public function test_get_ Null(){    $config = Config::getInstance();    $config->init();    $this->assertNull( $config->get(message) );}

×