SlideShare a Scribd company logo
Beginning PHPUnit
Jace Ju / jaceju /

 PHP Smarty




       Plurk: http://www.plurk.com/jaceju   >////<
Beginning PHPUnit
Beginning PHPUnit
•   PHPUnit
•   PHPUnit
•   PHPUnit
•   PHPUnit
•   PHPUnit
•
•   PHPUnit
•   PHPUnit
•
•
Beginning PHPUnit
Question 1
Web
Beginning PHPUnit
Beginning PHPUnit
Beginning PHPUnit
Question 2
Beginning PHPUnit
Beginning PHPUnit
Beginning PHPUnit
Question 3
bug
bugs
Bugs
Beginning PHPUnit
Beginning PHPUnit
Example
1 + 2 + 3 + ... + N = ?
Beginning PHPUnit
project
├── application
└── library
│   └── Math.php
└── run_test.php
project
├── application
└── library
│   └── Math.php
└── run_test.php
project
├── application
└── library
│   └── Math.php
└── run_test.php
project
├── application
└── library
│   └── Math.php
└── run_test.php
project
├── application
└── library
│   └── Math.php
└── run_test.php
Math.php
Math.php
Math.php
           <?php
           class Math
           {




           }
Math.php
               <?php
               class Math
               {
   Math::sum       public static function sum($min, $max)
                   {
                       $sum = 0;
                       for ($i = $min; $i <= $max; $i++) {
                           $sum += $i;
                       }
                       return $sum;
                   }
               }
Math.php
               //
   TEST_MODE   if (defined('TEST_MODE')) {




               }
Math.php
              //
              if (defined('TEST_MODE')) {
                  // Test 1
                  $result = Math::sum(1, 10);
     1   10       if (55 !== $result) {
                      echo "Test 1 failed!n";
                  } else {
                      echo "Test 1 OK!n";
                  }




              }
Math.php
              //
              if (defined('TEST_MODE')) {
                  // Test 1
                  $result = Math::sum(1, 10);
                  if (55 !== $result) {
                      echo "Test 1 failed!n";
                  } else {
                      echo "Test 1 OK!n";
                  }

                    // Test 2
                    $result = Math::sum(1, 100);
                    if (5050 !== $result) {
    1   100             echo "Test 2 failed!n";
                    } else {
                        echo "Test 2 OK!n";
                    }
              }
run_test.php
run_test.php
run_test.php
                <?php
    TEST_MODE   define('TEST_MODE', true);
run_test.php
               <?php
               define('TEST_MODE', true);
               require_once __DIR__ . '/library/Math.php';
Beginning PHPUnit
# php run_test.php
# php run_test.php
Test 1 OK!
Test 2 OK!
Beginning PHPUnit
Beginning PHPUnit
Beginning PHPUnit
Beginning PHPUnit
Beginning PHPUnit
Beginning PHPUnit
Beginning PHPUnit
...
Beginning PHPUnit
PHPUnit
 by Sebastian Bergmann




                         http://phpunit.de
JUnit


        http://www.junit.org/
xUnit


http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks
#   pear   channel-discover pear.symfony-project.com
#   pear   install symfony/YAML
#   pear   channel-discover pear.phpunit.de
#   pear   channel-discover components.ez.no
#   pear   install -o phpunit/phpunit
PHPUnit
Beginning PHPUnit
project
├── application
└── library
    └── Math.php
project
├── application
├── library
│   └── Math.php
└── tests
project
                ├── application
                ├── library
                │   └── Math.php
                └── tests
                    ├── application
(   tests   )
                    └── library
Beginning PHPUnit
project
├── application
├── library
│   └── Math.php
└── tests
    ├── application
    └── library
project
                 ├── application
                 ├── library
                 │   └── Math.php
                 └── tests
                     ├── application
                     └── library
library
  MathTest.php             └── MathTest.php
MathTest.php
MathTest.php
               <?php

               class MathTest
   Test Case   {




               }
MathTest.php
               <?php

               class MathTest
               {




               }
MathTest.php
               <?php
               require_once __DIR__ . '/Math.php';
               class MathTest
               {




               }
MathTest.php
                <?php
                require_once __DIR__ . '/Math.php';
      PHPUnit   class MathTest extends PHPUnit_Framework_TestCase
   TestCase     {




                }
MathTest.php
               <?php
               require_once __DIR__ . '/Math.php';
               class MathTest extends PHPUnit_Framework_TestCase
               {
                   public function testSum()
                   {


                   }
               }
MathTest.php
               <?php
               require_once __DIR__ . '/Math.php';
               class MathTest extends PHPUnit_Framework_TestCase
               {
                   public function testSum()
       test        {


                   }
               }
MathTest.php
               <?php
               require_once __DIR__ . '/Math.php';
               class MathTest extends PHPUnit_Framework_TestCase
               {
   test            public function testSum()
                   {

 CamelCase
                   }
               }
MathTest.php
                  <?php
                  require_once __DIR__ . '/Math.php';
                  class MathTest extends PHPUnit_Framework_TestCase
                  {
                      public function testSum()
                      {
                          $this->assertEquals(55, Math::sum(1, 10));
    PHPUnit               $this->assertEquals(5050, Math::sum(1, 100));
     assertions       }
                  }
MathTest.php
               <?php
               require_once __DIR__ . '/Math.php';
               class MathTest extends PHPUnit_Framework_TestCase
               {
                   public function testSum()
                   {
                       $this->assertEquals(55, Math::sum(1, 10));
                       $this->assertEquals(5050, Math::sum(1, 100));
                   }
               }
MathTest.php
               <?php
               require_once __DIR__ . '/Math.php';
               class MathTest extends PHPUnit_Framework_TestCase
               {
                   public function testSum()
                   {
                       $this->assertEquals(55, Math::sum(1, 10));
                       $this->assertEquals(5050, Math::sum(1, 100));
                   }
               }
# phpunit tests/library/MathTest
 console


php
# phpunit tests/library/MathTest
PHPUnit 3.5.15 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 5.25Mb

OK (1 test, 2 assertions)
# phpunit tests/library/MathTest
             PHPUnit 3.5.15 by Sebastian Bergmann.

             .

             Time: 0 seconds, Memory: 5.25Mb

             OK (1 test, 2 assertions)
assertions
Beginning PHPUnit
phpunit

PHPUnit
Test Case
             Tests
  testXxxx
Test


assertions
assertions
Data Provider
MathTest.php
               <?php
               require_once dirname(dirname(__DIR__)) . '/library/Math.php';
               class MathTest extends PHPUnit_Framework_TestCase
               {



                     public function testSum()
   MathTest          {
                         $this->assertEquals(55, Math::sum(1, 10 ));
                     }




               }
MathTest.php
               <?php
               require_once dirname(dirname(__DIR__)) . '/library/Math.php';
               class MathTest extends PHPUnit_Framework_TestCase
               {



                     public function testSum($expected, $min, $max)
                     {
                         $this->assertEquals(55, Math::sum(1, 10 ));
                     }




               }
MathTest.php
               <?php
               require_once dirname(dirname(__DIR__)) . '/library/Math.php';
               class MathTest extends PHPUnit_Framework_TestCase
               {



                     public function testSum($expected, $min, $max)
                     {
                         $this->assertEquals($expected, Math::sum($min, $max));
                     }




               }
MathTest.php
               <?php
               require_once dirname(dirname(__DIR__)) . '/library/Math.php';
               class MathTest extends PHPUnit_Framework_TestCase
               {



                     public function testSum($expected, $min, $max)
                     {
                         $this->assertEquals($expected, Math::sum($min, $max));
                     }

                     public function myDataProvider()
    public           {
                         return array(
                             array(55, 1, 10),
                             array(5050, 1, 100)
                         );
                     }
               }
MathTest.php
               <?php
               require_once dirname(dirname(__DIR__)) . '/library/Math.php';
               class MathTest extends PHPUnit_Framework_TestCase
               {



                     public function testSum($expected, $min, $max)
                     {
                         $this->assertEquals($expected, Math::sum($min, $max));
                     }

                     public function myDataProvider()
                     {
                         return array(
                             array(55, 1, 10),
                             array(5050, 1, 100)
                         );
                     }
               }
MathTest.php
                   <?php
                   require_once dirname(dirname(__DIR__)) . '/library/Math.php';
                   class MathTest extends PHPUnit_Framework_TestCase
                   {
                       /**
     PHPUnit            * @dataProvider myDataProvider
   @dataProvider        */
      annotation       public function testSum($expected, $min, $max)
   data provider       {
                           $this->assertEquals($expected, Math::sum($min, $max));
                       }

                         public function myDataProvider()
                         {
                             return array(
                                 array(55, 1, 10),
                                 array(5050, 1, 100)
                             );
                         }
                   }
# phpunit tests/library/MathTest
# phpunit tests/library/MathTest
        PHPUnit 3.5.15 by Sebastian Bergmann.

        ..

        Time: 0 seconds, Memory: 5.25Mb

        OK (2 tests, 2 assertions)
tests
Provider
           Test
Situation
Math::sum
            ...
Math::sum
            ...
1 + 2 + 3 + ... + N
        =
Math::sum
Math.php
                <?php
     Math.php   class Math
                {
                    public static function sum($min, $max)
                    {
                        $sum = 0;
                        for ($i = $min; $i <= $max; $i++) {
                            $sum += $i;
                        }
                        return $sum;
                    }
                }
Math.php
           <?php
           class Math
           {
               public static function sum($min, $max)
               {
                   $sum = 0;



                   return $sum;
               }
           }
Math.php
           <?php
           class Math
           {
               public static function sum($min, $max)
               {
                   $sum = $min + $max * $max / 2;



                   return $sum;
               }
           }
# phpunit tests/library/MathTest
MathTest.php
# phpunit tests/library/MathTest
PHPUnit 3.5.15 by Sebastian Bergmann.

F

Time: 0 seconds, Memory: 6.00Mb

There was 1 failure:

1) MathTest03::testSum
Failed asserting that <integer:51> matches expected <integer:
55>.

/path/to/tests/library/MathTest.php:7

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
# phpunit tests/library/MathTest
     PHPUnit 3.5.15 by Sebastian Bergmann.

     F

     Time: 0 seconds, Memory: 6.00Mb

     There was 1 failure:

     1) MathTest03::testSum
     Failed asserting that <integer:51> matches expected <integer:
55
51
     55>.

     /path/to/tests/library/MathTest.php:7

     FAILURES!
     Tests: 1, Assertions: 1, Failures: 1.
Math.php
           <?php
           class Math
           {
               /**
                 *
                 */
               public static function sum($min, $max)
               {
                    $sum = $min + $max * $max / 2;



                   return $sum;
               }
           }
Math.php
           <?php
           class Math
           {
               /**
                 *
                 */
               public static function sum($min, $max)
               {
                    $sum = ($min + $max) * $max / 2;



                   return $sum;
               }
           }
# phpunit tests/library/MathTest
PHPUnit 3.5.15 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 5.25Mb

OK (1 test, 2 assertions)
Beginning PHPUnit
Beginning PHPUnit
SQL    Select
ORM Framework
Beginning PHPUnit
•   DbSelect
•   DbSelect
•
•       DbSelect
•
 ‣ from          SELECT   FROM
•       DbSelect
•
 ‣ from          SELECT   FROM

 ‣ cols        SELECT            *
Example 1



        $select = new DbSelect();
        echo $select->from(‘table’);




            SELECT * FROM table
Example 2


  $select = new DbSelect();
  echo $select->from(‘table’)->cols(array(
      ‘col_a’, ‘col_b’));




       SELECT col_a, col_b FROM table
project
├── application
├── library
│   └── DbSelect.php
└── tests
    ├── application
    └── library
          └── DbSelectTest.php
DbSelect.php
                <?php
     DbSelect
                class DbSelect
                {




                }
DbSelectTest.php
             <?php
             require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
             class DbSelectTest extends PHPUnit_Framework_TestCase
             {




             }
DbSelectTest.php
             <?php
             require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
             class DbSelectTest extends PHPUnit_Framework_TestCase
             {
                 public function testFrom()
                 {
     from
                 



                 }




             }
DbSelectTest.php
             <?php
             require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
             class DbSelectTest extends PHPUnit_Framework_TestCase
             {
                 public function testFrom()
                 {
                     $select = new DbSelect();
                     $select->from('test');
                     $this->assertEquals('SELECT * FROM test', 
                                         $select->__toString());
                 }




             }
DbSelect.php
                   <?php
    DbSelect.php   class DbSelect
                   {




                   }
DbSelect.php
               <?php
               class DbSelect
               {

                   

                   
                   public function from($table)
     from
                   {




                   }
                   




                   




               }
DbSelect.php
                  <?php
                  class DbSelect
                  {



                      
                      public function from($table)
                      {




                      }
                      




                      
                      public function __toString()
     __toString
                      {


                      }
                  }
DbSelect.php
               <?php
               class DbSelect
               {



                   
                   public function from($table)
                   {




                   }
                   




                   
                   public function __toString()
                   {

                       return 'SELECT * FROM test';
                   }
               }
# phpunit tests/library/DbSelectTest
PHPUnit 3.5.15 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 5.25Mb

OK (1 test, 1 assertions)
Beginning PHPUnit
DbSelect.php
               <?php
               class DbSelect
               {

                   

                   
                   public function from($table)
                   {




                   }
                   




                   
                   public function __toString()
                   {
     table
                       return 'SELECT * FROM ' . $this->_table;
                   }
               }
DbSelect.php
               <?php
               class DbSelect
               {
    $_table        protected $_table = 'table';
                   

                   
                   public function from($table)
                   {




                   }
                   




                   
                   public function __toString()
                   {

                       return 'SELECT * FROM ' . $this->_table;
                   }
               }
DbSelect.php
                  <?php
                  class DbSelect
                  {
                      protected $_table = 'table';
                      

                      
                      public function from($table)
                      {
                          if (!preg_match('/[0-9a-z]+/i', $table)) {
                              throw new IllegalNameException('Illegal Table Name');
  from
                          }
         setter
                          $this->_table = $table;
                          return $this;
                      }
                      




                      
                      public function __toString()
                      {

                          return 'SELECT * FROM ' . $this->_table;
                      }
                  }
# phpunit tests/library/DbSelectTest
PHPUnit 3.5.15 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 5.25Mb

OK (1 test, 1 assertions)
Design    →   Write Test   →   Coding

Direction   → Find Target →      Fire
Beginning PHPUnit
DbSelectTest.php
                     <?php
                     require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
                     class DbSelectTest extends PHPUnit_Framework_TestCase
                     {
  DbSelectTest.php       public function testFrom()
                         {
                             $select = new DbSelect();
                             $select->from('test');
                             $this->assertEquals('SELECT * FROM test',
                                                 $select->__toString());
                         }




                     }
DbSelectTest.php
             <?php
             require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
             class DbSelectTest extends PHPUnit_Framework_TestCase
             {
                 public function testFrom()
                 {
                     $select = new DbSelect();
                     $select->from('test');
                     $this->assertEquals('SELECT * FROM test',
                                         $select->__toString());
                 }
                 
                 public function testCols()
                 {
  cols               $select = new DbSelect();
                     $select->from('test')->cols(array(
                         'col_a',
                         'col_b',
                     ));
                     $this->assertEquals('SELECT col_a, col_b FROM test',
                                         $select->__toString());
                 }
             }
DbSelect.php
                   <?php
    DbSelect.php   class DbSelect
                   {
                       protected $_table = 'table';
                       

                       
                       public function from($table)
                       {
                           if (!preg_match('/[0-9a-z]+/i', $table)) {
                               throw new IllegalNameException('Illegal Table Name');
                           }
                           $this->_table = $table;
                           return $this;
                       }
                       




                       
                       public function __toString()
                       {

                           return 'SELECT * FROM ' . $this->_table;
                       }
                   }
DbSelect.php
               <?php
               class DbSelect
               {
                   protected $_table = 'table';
                   

                   
                   public function from($table)
                   {
                       if (!preg_match('/[0-9a-z]+/i', $table)) {
                           throw new IllegalNameException('Illegal Table Name');
                       }
                       $this->_table = $table;
                       return $this;
                   }
                   
     cols          public function cols($cols)
                   {
                       $this->_cols = (array) $cols;
                       return $this;
                   }
                 
                   public function __toString()
                   {

                       return 'SELECT * FROM ' . $this->_table;
                   }
               }
DbSelect.php
               <?php
               class DbSelect
               {
                   protected $_table = 'table';
                   
    $_cols         protected $_cols = '*';
                   
                   public function from($table)
                   {
                       if (!preg_match('/[0-9a-z]+/i', $table)) {
                           throw new IllegalNameException('Illegal Table Name');
                       }
                       $this->_table = $table;
                       return $this;
                   }
                   
                   public function cols($cols)
                   {
                       $this->_cols = (array) $cols;
                       return $this;
                   }
                  
                   public function __toString()
                   {

                       return 'SELECT * FROM ' . $this->_table;
                   }
               }
DbSelect.php
                 <?php
                 class DbSelect
                 {
                     protected $_table = 'table';
                     
                     protected $_cols = '*';
                     
                     public function from($table)
                     {
                         if (!preg_match('/[0-9a-z]+/i', $table)) {
                             throw new IllegalNameException('Illegal Table Name');
                         }
                         $this->_table = $table;
                         return $this;
                     }
                     
                     public function cols($cols)
                     {
                         $this->_cols = (array) $cols;
                         return $this;
                     }
                    
                     public function __toString()
                     {
    $_cols               $cols = implode(', ', (array) $this->_cols);
             *           return 'SELECT ' . $cols . ' FROM ' . $this->_table;
                     }
                 }
# phpunit tests/library/DbSelectTest
PHPUnit 3.5.15 by Sebastian Bergmann.

.

Time: 0 seconds, Memory: 5.25Mb

OK (2 test, 2 assertions)
Beginning PHPUnit
bug
Beginning PHPUnit
DbSelectTest.php
                     <?php
                     require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
  DbSelectTest.php   class DbSelectTest extends PHPUnit_Framework_TestCase
                     {
                         public function testFrom()
                         {
                             $select = new DbSelect();
                             $select->from('test');
                             $this->assertEquals('SELECT * FROM test',
                                                 $select->__toString());
                         }
                         
                         public function testCols()
                         {
                             $select = new DbSelect();
                             $select->from('test')->cols(array(
                                 'col_a',
                                 'col_b',
                             ));
                             $this->assertEquals('SELECT col_a, col_b FROM test',
                                                 $select->__toString());
                         }
                     }
DbSelectTest.php
                    <?php
                    require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
                    class DbSelectTest extends PHPUnit_Framework_TestCase
                    {
                        public function testFrom()
                        {
                            $select = new DbSelect();
                            $select->from('test');
                            $this->assertEquals('SELECT * FROM test',
                                                $select->__toString());
                        }
                        
   new DbSelect()       public function testCols()
                        {
                            $select = new DbSelect();
                            $select->from('test')->cols(array(
                                'col_a',
                                'col_b',
                            ));
                            $this->assertEquals('SELECT col_a, col_b FROM test',
                                                $select->__toString());
                        }
                    }
DRY
Don't Repeat Yourself
Fixture
DbSelectTest.php
                    <?php
                    require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
                    class DbSelectTest extends PHPUnit_Framework_TestCase
                    {




                          public function testFrom()
                          {
                              $select->from('test');
                              $this->assertEquals('SELECT * FROM test',
   new DbSelect()                                 $select->__toString());
                          }
                          
                          public function testCols()
                          {
                              $select->from('test')->cols(array('col_a', 'col_b'));
                              $this->assertEquals('SELECT col_a, col_b FROM test',
                                                  $select->__toString());
                          }
                          




                    }
DbSelectTest.php
               <?php
               require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
               class DbSelectTest extends PHPUnit_Framework_TestCase
               {
      fixture       protected $_select;
                   




                     
                     public function testFrom()
                     {
                         $select->from('test');
                         $this->assertEquals('SELECT * FROM test',
                                             $select->__toString());
                     }
                     
                     public function testCols()
                     {
                         $select->from('test')->cols(array('col_a', 'col_b'));
                         $this->assertEquals('SELECT col_a, col_b FROM test',
                                             $select->__toString());
                     }
                     




               }
DbSelectTest.php
                    <?php
                    require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
                    class DbSelectTest extends PHPUnit_Framework_TestCase
                    {
                        protected $_select;
                        
                        protected function setUp()
                        {
   setUp                    $this->_select = new DbSelect();
           fixture       }
                        
                        public function testFrom()
                        {
                            $select->from('test');
                            $this->assertEquals('SELECT * FROM test',
                                                $select->__toString());
                        }
                        
                        public function testCols()
                        {
                            $select->from('test')->cols(array('col_a', 'col_b'));
                            $this->assertEquals('SELECT col_a, col_b FROM test',
                                                $select->__toString());
                        }




                    }
DbSelectTest.php
                  <?php
                  require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
                  class DbSelectTest extends PHPUnit_Framework_TestCase
                  {
                      protected $_select;
                      
                      protected function setUp()
                      {
                          $this->_select = new DbSelect();
                      }
                      
                      public function testFrom()
                      {
                          $select->from('test');
                          $this->assertEquals('SELECT * FROM test',
                                              $select->__toString());
                      }
                      
                      public function testCols()
                      {
                          $select->from('test')->cols(array('col_a', 'col_b'));
                          $this->assertEquals('SELECT col_a, col_b FROM test',
                                              $select->__toString());
                      }
                      
                      protected function tearDown()
                      {
                          $this->_select = null;
     tearDown
                      }
         fixture   }
DbSelectTest.php
                    <?php
                    require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
                    class DbSelectTest extends PHPUnit_Framework_TestCase
                    {
                        protected $_select;
                        
                        protected function setUp()
                        {
                            $this->_select = new DbSelect();
                        }
                        
                        public function testFrom()
                        {
                            $this->_select->from('test');
                            $this->assertEquals('SELECT * FROM test',
                                                $this->_select->__toString());
    $select             }
   $this->_select       
                        public function testCols()
                        {
                            $this->_select->from('test')->cols(array('col_a', 'col_b'));
                            $this->assertEquals('SELECT col_a, col_b FROM test',
                                                $this->_select->__toString());
                        }
                        
                        protected function tearDown()
                        {
                            $this->_select = null;
                        }
                    }
setUp() → testFrom() → tearDown()

setUp() → testCols() → tearDown()
# phpunit tests/library/DbSelectTest
DbSelectTest   PHPUnit 3.5.15 by Sebastian Bergmann.

               .

               Time: 0 seconds, Memory: 5.25Mb

               OK (2 test, 2 assertions)
Beginning PHPUnit
“Houston, we have a problem.”
DbSelect
           bug
$select = new DbSelect();
        $select->from('table')->where('id = 1');
where
$select = new DbSelect();
$select->from('table')->where('id = 1');

//
// SELECT * FROM table WHERE id = 1
$select = new DbSelect();
$select->from('table WHERE id = 1');

//
// SELECT * FROM table WHERE id = 1
Bug
Beginning PHPUnit
DbSelectTest.php
                     <?php
                     require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
  DbSelectTest.php   class DbSelectTest extends PHPUnit_Framework_TestCase
                     {
                         // ...    ...




                     }
DbSelectTest.php
             <?php
             require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
             class DbSelectTest extends PHPUnit_Framework_TestCase
             {
                 // ...    ...




                   public function testIllegalTableName()
                   {
                       try {
                           $this->_select->from('test WHERE id = 1');
                       }
                       catch (IllegalNameException $e) {
                           throw $e;
                       }
                   }
             }
DbSelectTest.php
                     <?php
                     require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php';
                     class DbSelectTest extends PHPUnit_Framework_TestCase
                     {
                         // ...    ...

                           /**
       PHPUnit              * @expectedException IllegalNameException
@expectedException          */
 annotation                public function testIllegalTableName()
                           {
                               try {
                                   $this->_select->from('test WHERE id = 1');
                               }
                               catch (IllegalNameException $e) {
                                   throw $e;
                               }
                           }
                     }
# phpunit tests/library/DbSelectTest
                       PHPUnit 3.5.15 by Sebastian Bergmann.

                       .F.

                       Time: 0 seconds, Memory: 6.00Mb

                       There was 1 failure:

                       1) DbSelectTest::testIllegalTableName
IllegalNameException   Expected exception IllegalNameException


                       FAILURES!
                       Tests: 3, Assertions: 3, Failures: 1.
Beginning PHPUnit
DbSelectTest.php
                   <?php
    DbSelect.php   class DbSelect
                   {
                       // ...    ...

                       public function from($table)
                       {
                           if (!preg_match('/[0-9a-z]+/i', $table)) {
                               throw new Exception('Illegal Table Name: ' . $table);
                           }
                           $this->_table = $table;
                           return $this;
                       }

                       // ...   ...
                   }
DbSelectTest.php
             <?php
             class DbSelect
             {
                 // ...    ...

                 public function from($table)
                 {
                     if (!preg_match('/[0-9a-z]+/i', $table)) {
                         throw new Exception('Illegal Table Name: ' . $table);
                     }
                     $this->_table = $table;
                     return $this;
                 }

                 // ...   ...
             }
DbSelectTest.php
              <?php
              class DbSelect
              {
                  // ...    ...

                  public function from($table)
                  {
      ^   $           if (!preg_match('/^[0-9a-z]+$/i', $table)) {
                          throw new Exception('Illegal Table Name: ' . $table);
                      }
                      $this->_table = $table;
                      return $this;
                  }

                  // ...   ...
              }
# phpunit tests/library/DbSelectTest
PHPUnit 3.5.15 by Sebastian Bergmann.

...

Time: 1 second, Memory: 5.50Mb

OK (3 tests, 3 assertions)
bug
Beginning PHPUnit
Test Suite
tests
 phpunit tests
Beginning PHPUnit
phpunit.xml
project
              ├── application
              ├── library
              └── tests
phpunit.xml
    tests
                  └── phpunit.xml
phpunit.xml
                      <phpunit>
 root tag   phpunit




                      </phpunit>
phpunit.xml
                 <phpunit colors=”true”>
      root tag




                 </phpunit>
phpunit.xml
                   <phpunit colors=”true”>
     test suites     <testsuite name="Application Test Suite">
                       <directory>./application</directory>
                     </testsuite>
                     <testsuite name="Library Test Suite">
                       <directory>./library</directory>
                     </testsuite>
                   </phpunit>
phpunit.xml
                 # phpunit -c tests/phpunit.xml
                 PHPUnit 3.5.15 by Sebastian Bergmann.

                 ....

                 Time: 0 seconds, Memory: 6.00Mb

 colors=”true”   OK (4 tests, 4 assertions)
phpunit.xml
              <phpunit colors=”true” bootstrap=”./bootstrap.php”>
                <testsuite name="Application Test Suite">
     PHP          <directory>./application</directory>
                </testsuite>
                <testsuite name="Library Test Suite">
                  <directory>./library</directory>
                </testsuite>
              </phpunit>
bootstrap.php
                <?php
                define('PROJECT_PATH', realpath(dirname(__DIR__)));

                set_include_path(implode(PATH_SEPARATOR, array(
                    realpath(PROJECT_PATH . '/library'),
                    realpath(PROJECT_PATH . '/application'),
                    get_include_path()
                )));

                function autoload($className)
                {
                    $className = str_replace('_', '/', $className);
                    require_once "$className.php";
                }

                spl_autoload_register('autoload');
phpunit.xml



              http://goo.gl/tvmq4
Beginning PHPUnit
Beginning PHPUnit
Beginning PHPUnit
Beginning PHPUnit
Beginning PHPUnit
Slides
Beginning PHPUnit
Beginning PHPUnit

More Related Content

Beginning PHPUnit

  • 2. Jace Ju / jaceju / PHP Smarty Plurk: http://www.plurk.com/jaceju >////<
  • 5. PHPUnit
  • 6. PHPUnit • PHPUnit
  • 7. PHPUnit • PHPUnit •
  • 8. PHPUnit • PHPUnit • •
  • 11. Web
  • 21. Bugs
  • 25. 1 + 2 + 3 + ... + N = ?
  • 27. project ├── application └── library │ └── Math.php └── run_test.php
  • 28. project ├── application └── library │ └── Math.php └── run_test.php
  • 29. project ├── application └── library │ └── Math.php └── run_test.php
  • 30. project ├── application └── library │ └── Math.php └── run_test.php
  • 31. project ├── application └── library │ └── Math.php └── run_test.php
  • 34. Math.php <?php class Math { }
  • 35. Math.php <?php class Math { Math::sum     public static function sum($min, $max)     {         $sum = 0;         for ($i = $min; $i <= $max; $i++) {             $sum += $i;         }         return $sum;     } }
  • 36. Math.php // TEST_MODE if (defined('TEST_MODE')) { }
  • 37. Math.php // if (defined('TEST_MODE')) {     // Test 1     $result = Math::sum(1, 10); 1 10     if (55 !== $result) {         echo "Test 1 failed!n";     } else {         echo "Test 1 OK!n";     } }
  • 38. Math.php // if (defined('TEST_MODE')) {     // Test 1     $result = Math::sum(1, 10);     if (55 !== $result) {         echo "Test 1 failed!n";     } else {         echo "Test 1 OK!n";     }     // Test 2     $result = Math::sum(1, 100);     if (5050 !== $result) { 1 100         echo "Test 2 failed!n";     } else {         echo "Test 2 OK!n";     } }
  • 41. run_test.php <?php TEST_MODE define('TEST_MODE', true);
  • 42. run_test.php <?php define('TEST_MODE', true); require_once __DIR__ . '/library/Math.php';
  • 45. # php run_test.php Test 1 OK! Test 2 OK!
  • 53. ...
  • 55. PHPUnit by Sebastian Bergmann http://phpunit.de
  • 56. JUnit http://www.junit.org/
  • 58. # pear channel-discover pear.symfony-project.com # pear install symfony/YAML # pear channel-discover pear.phpunit.de # pear channel-discover components.ez.no # pear install -o phpunit/phpunit
  • 62. project ├── application ├── library │ └── Math.php └── tests
  • 63. project ├── application ├── library │ └── Math.php └── tests ├── application ( tests ) └── library
  • 65. project ├── application ├── library │ └── Math.php └── tests ├── application └── library
  • 66. project ├── application ├── library │ └── Math.php └── tests ├── application └── library library MathTest.php └── MathTest.php
  • 68. MathTest.php <?php class MathTest Test Case { }
  • 69. MathTest.php <?php class MathTest { }
  • 70. MathTest.php <?php require_once __DIR__ . '/Math.php'; class MathTest { }
  • 71. MathTest.php <?php require_once __DIR__ . '/Math.php'; PHPUnit class MathTest extends PHPUnit_Framework_TestCase TestCase { }
  • 72. MathTest.php <?php require_once __DIR__ . '/Math.php'; class MathTest extends PHPUnit_Framework_TestCase {     public function testSum()     {     } }
  • 73. MathTest.php <?php require_once __DIR__ . '/Math.php'; class MathTest extends PHPUnit_Framework_TestCase {     public function testSum() test     {     } }
  • 74. MathTest.php <?php require_once __DIR__ . '/Math.php'; class MathTest extends PHPUnit_Framework_TestCase { test     public function testSum()     { CamelCase     } }
  • 75. MathTest.php <?php require_once __DIR__ . '/Math.php'; class MathTest extends PHPUnit_Framework_TestCase {     public function testSum()     {         $this->assertEquals(55, Math::sum(1, 10)); PHPUnit         $this->assertEquals(5050, Math::sum(1, 100)); assertions     } }
  • 76. MathTest.php <?php require_once __DIR__ . '/Math.php'; class MathTest extends PHPUnit_Framework_TestCase {     public function testSum()     {         $this->assertEquals(55, Math::sum(1, 10));         $this->assertEquals(5050, Math::sum(1, 100));     } }
  • 77. MathTest.php <?php require_once __DIR__ . '/Math.php'; class MathTest extends PHPUnit_Framework_TestCase {     public function testSum()     {         $this->assertEquals(55, Math::sum(1, 10));         $this->assertEquals(5050, Math::sum(1, 100));     } }
  • 79. # phpunit tests/library/MathTest PHPUnit 3.5.15 by Sebastian Bergmann. . Time: 0 seconds, Memory: 5.25Mb OK (1 test, 2 assertions)
  • 80. # phpunit tests/library/MathTest PHPUnit 3.5.15 by Sebastian Bergmann. . Time: 0 seconds, Memory: 5.25Mb OK (1 test, 2 assertions) assertions
  • 83. Test Case Tests testXxxx
  • 87. MathTest.php <?php require_once dirname(dirname(__DIR__)) . '/library/Math.php'; class MathTest extends PHPUnit_Framework_TestCase {     public function testSum() MathTest     {         $this->assertEquals(55, Math::sum(1, 10 ));     } }
  • 88. MathTest.php <?php require_once dirname(dirname(__DIR__)) . '/library/Math.php'; class MathTest extends PHPUnit_Framework_TestCase {     public function testSum($expected, $min, $max)     {         $this->assertEquals(55, Math::sum(1, 10 ));     } }
  • 89. MathTest.php <?php require_once dirname(dirname(__DIR__)) . '/library/Math.php'; class MathTest extends PHPUnit_Framework_TestCase {     public function testSum($expected, $min, $max)     {         $this->assertEquals($expected, Math::sum($min, $max));     } }
  • 90. MathTest.php <?php require_once dirname(dirname(__DIR__)) . '/library/Math.php'; class MathTest extends PHPUnit_Framework_TestCase {     public function testSum($expected, $min, $max)     {         $this->assertEquals($expected, Math::sum($min, $max));     }     public function myDataProvider() public     {         return array(             array(55, 1, 10),             array(5050, 1, 100)         );     } }
  • 91. MathTest.php <?php require_once dirname(dirname(__DIR__)) . '/library/Math.php'; class MathTest extends PHPUnit_Framework_TestCase {     public function testSum($expected, $min, $max)     {         $this->assertEquals($expected, Math::sum($min, $max));     }     public function myDataProvider()     {         return array(             array(55, 1, 10),             array(5050, 1, 100)         );     } }
  • 92. MathTest.php <?php require_once dirname(dirname(__DIR__)) . '/library/Math.php'; class MathTest extends PHPUnit_Framework_TestCase {     /** PHPUnit      * @dataProvider myDataProvider @dataProvider      */ annotation     public function testSum($expected, $min, $max) data provider     {         $this->assertEquals($expected, Math::sum($min, $max));     }     public function myDataProvider()     {         return array(             array(55, 1, 10),             array(5050, 1, 100)         );     } }
  • 94. # phpunit tests/library/MathTest PHPUnit 3.5.15 by Sebastian Bergmann. .. Time: 0 seconds, Memory: 5.25Mb OK (2 tests, 2 assertions) tests
  • 95. Provider Test
  • 97. Math::sum ...
  • 98. Math::sum ...
  • 99. 1 + 2 + 3 + ... + N =
  • 101. Math.php <?php Math.php class Math { public static function sum($min, $max) { $sum = 0; for ($i = $min; $i <= $max; $i++) { $sum += $i; } return $sum; } }
  • 102. Math.php <?php class Math { public static function sum($min, $max) { $sum = 0; return $sum; } }
  • 103. Math.php <?php class Math { public static function sum($min, $max) { $sum = $min + $max * $max / 2; return $sum; } }
  • 105. # phpunit tests/library/MathTest PHPUnit 3.5.15 by Sebastian Bergmann. F Time: 0 seconds, Memory: 6.00Mb There was 1 failure: 1) MathTest03::testSum Failed asserting that <integer:51> matches expected <integer: 55>. /path/to/tests/library/MathTest.php:7 FAILURES! Tests: 1, Assertions: 1, Failures: 1.
  • 106. # phpunit tests/library/MathTest PHPUnit 3.5.15 by Sebastian Bergmann. F Time: 0 seconds, Memory: 6.00Mb There was 1 failure: 1) MathTest03::testSum Failed asserting that <integer:51> matches expected <integer: 55 51 55>. /path/to/tests/library/MathTest.php:7 FAILURES! Tests: 1, Assertions: 1, Failures: 1.
  • 107. Math.php <?php class Math { /** * */ public static function sum($min, $max) { $sum = $min + $max * $max / 2; return $sum; } }
  • 108. Math.php <?php class Math { /** * */ public static function sum($min, $max) { $sum = ($min + $max) * $max / 2; return $sum; } }
  • 109. # phpunit tests/library/MathTest PHPUnit 3.5.15 by Sebastian Bergmann. . Time: 0 seconds, Memory: 5.25Mb OK (1 test, 2 assertions)
  • 112. SQL Select ORM Framework
  • 114. DbSelect
  • 115. DbSelect •
  • 116. DbSelect • ‣ from SELECT FROM
  • 117. DbSelect • ‣ from SELECT FROM ‣ cols SELECT *
  • 118. Example 1 $select = new DbSelect(); echo $select->from(‘table’); SELECT * FROM table
  • 119. Example 2 $select = new DbSelect(); echo $select->from(‘table’)->cols(array(     ‘col_a’, ‘col_b’)); SELECT col_a, col_b FROM table
  • 120. project ├── application ├── library │ └── DbSelect.php └── tests ├── application └── library └── DbSelectTest.php
  • 121. DbSelect.php <?php DbSelect class DbSelect { }
  • 122. DbSelectTest.php <?php require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php'; class DbSelectTest extends PHPUnit_Framework_TestCase { }
  • 123. DbSelectTest.php <?php require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php'; class DbSelectTest extends PHPUnit_Framework_TestCase {     public function testFrom()     { from          } }
  • 124. DbSelectTest.php <?php require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php'; class DbSelectTest extends PHPUnit_Framework_TestCase {     public function testFrom()     {         $select = new DbSelect();         $select->from('test');         $this->assertEquals('SELECT * FROM test',  $select->__toString());     } }
  • 125. DbSelect.php <?php DbSelect.php class DbSelect { }
  • 126. DbSelect.php <?php class DbSelect {               public function from($table) from     {     }           }
  • 127. DbSelect.php <?php class DbSelect {          public function from($table)     {     }               public function __toString() __toString     {     } }
  • 128. DbSelect.php <?php class DbSelect {          public function from($table)     {     }               public function __toString()     {         return 'SELECT * FROM test';     } }
  • 129. # phpunit tests/library/DbSelectTest PHPUnit 3.5.15 by Sebastian Bergmann. . Time: 0 seconds, Memory: 5.25Mb OK (1 test, 1 assertions)
  • 131. DbSelect.php <?php class DbSelect {               public function from($table)     {     }               public function __toString()     { table         return 'SELECT * FROM ' . $this->_table;     } }
  • 132. DbSelect.php <?php class DbSelect { $_table     protected $_table = 'table';               public function from($table)     {     }               public function __toString()     {         return 'SELECT * FROM ' . $this->_table;     } }
  • 133. DbSelect.php <?php class DbSelect {     protected $_table = 'table';               public function from($table)     { if (!preg_match('/[0-9a-z]+/i', $table)) {      throw new IllegalNameException('Illegal Table Name'); from } setter         $this->_table = $table;         return $this;     }               public function __toString()     {         return 'SELECT * FROM ' . $this->_table;     } }
  • 134. # phpunit tests/library/DbSelectTest PHPUnit 3.5.15 by Sebastian Bergmann. . Time: 0 seconds, Memory: 5.25Mb OK (1 test, 1 assertions)
  • 135. Design → Write Test → Coding Direction → Find Target → Fire
  • 137. DbSelectTest.php <?php require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php'; class DbSelectTest extends PHPUnit_Framework_TestCase { DbSelectTest.php     public function testFrom()     {         $select = new DbSelect();         $select->from('test');         $this->assertEquals('SELECT * FROM test', $select->__toString());     } }
  • 138. DbSelectTest.php <?php require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php'; class DbSelectTest extends PHPUnit_Framework_TestCase {     public function testFrom()     {         $select = new DbSelect();         $select->from('test');         $this->assertEquals('SELECT * FROM test',  $select->__toString());     }          public function testCols()     { cols         $select = new DbSelect();         $select->from('test')->cols(array(             'col_a',             'col_b',         ));         $this->assertEquals('SELECT col_a, col_b FROM test',  $select->__toString());     } }
  • 139. DbSelect.php <?php DbSelect.php class DbSelect {     protected $_table = 'table';               public function from($table)     { if (!preg_match('/[0-9a-z]+/i', $table)) {      throw new IllegalNameException('Illegal Table Name'); }         $this->_table = $table;         return $this;     }               public function __toString()     {         return 'SELECT * FROM ' . $this->_table;     } }
  • 140. DbSelect.php <?php class DbSelect {     protected $_table = 'table';               public function from($table)     { if (!preg_match('/[0-9a-z]+/i', $table)) {      throw new IllegalNameException('Illegal Table Name'); }         $this->_table = $table;         return $this;     }      cols     public function cols($cols)     {         $this->_cols = (array) $cols;         return $this;     }       public function __toString()     {         return 'SELECT * FROM ' . $this->_table;     } }
  • 141. DbSelect.php <?php class DbSelect {     protected $_table = 'table';      $_cols     protected $_cols = '*';          public function from($table)     { if (!preg_match('/[0-9a-z]+/i', $table)) {      throw new IllegalNameException('Illegal Table Name'); }         $this->_table = $table;         return $this;     }          public function cols($cols)     {         $this->_cols = (array) $cols;         return $this;     }       public function __toString()     {         return 'SELECT * FROM ' . $this->_table;     } }
  • 142. DbSelect.php <?php class DbSelect {     protected $_table = 'table';          protected $_cols = '*';          public function from($table)     { if (!preg_match('/[0-9a-z]+/i', $table)) {      throw new IllegalNameException('Illegal Table Name'); }         $this->_table = $table;         return $this;     }          public function cols($cols)     {         $this->_cols = (array) $cols;         return $this;     }       public function __toString()     { $_cols         $cols = implode(', ', (array) $this->_cols); *         return 'SELECT ' . $cols . ' FROM ' . $this->_table;     } }
  • 143. # phpunit tests/library/DbSelectTest PHPUnit 3.5.15 by Sebastian Bergmann. . Time: 0 seconds, Memory: 5.25Mb OK (2 test, 2 assertions)
  • 145. bug
  • 147. DbSelectTest.php <?php require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php'; DbSelectTest.php class DbSelectTest extends PHPUnit_Framework_TestCase {     public function testFrom() ��   {         $select = new DbSelect();         $select->from('test');         $this->assertEquals('SELECT * FROM test',  $select->__toString());     }          public function testCols()     {         $select = new DbSelect();         $select->from('test')->cols(array(             'col_a',             'col_b',         ));         $this->assertEquals('SELECT col_a, col_b FROM test',  $select->__toString());     } }
  • 148. DbSelectTest.php <?php require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php'; class DbSelectTest extends PHPUnit_Framework_TestCase {     public function testFrom()     {         $select = new DbSelect();         $select->from('test');         $this->assertEquals('SELECT * FROM test', $select->__toString());     }      new DbSelect()     public function testCols()     {         $select = new DbSelect();         $select->from('test')->cols(array(             'col_a',             'col_b',         ));         $this->assertEquals('SELECT col_a, col_b FROM test', $select->__toString());     } }
  • 151. DbSelectTest.php <?php require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php'; class DbSelectTest extends PHPUnit_Framework_TestCase {     public function testFrom()     {         $select->from('test');         $this->assertEquals('SELECT * FROM test', new DbSelect()  $select->__toString());     }          public function testCols()     {         $select->from('test')->cols(array('col_a', 'col_b'));         $this->assertEquals('SELECT col_a, col_b FROM test',   $select->__toString());     }      }
  • 152. DbSelectTest.php <?php require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php'; class DbSelectTest extends PHPUnit_Framework_TestCase { fixture     protected $_select;               public function testFrom()     {         $select->from('test');         $this->assertEquals('SELECT * FROM test', $select->__toString());     }          public function testCols()     {         $select->from('test')->cols(array('col_a', 'col_b'));         $this->assertEquals('SELECT col_a, col_b FROM test',   $select->__toString());     }      }
  • 153. DbSelectTest.php <?php require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php'; class DbSelectTest extends PHPUnit_Framework_TestCase {     protected $_select;          protected function setUp()     { setUp         $this->_select = new DbSelect(); fixture     }          public function testFrom()     {         $select->from('test');         $this->assertEquals('SELECT * FROM test', $select->__toString());     }          public function testCols()     {         $select->from('test')->cols(array('col_a', 'col_b'));         $this->assertEquals('SELECT col_a, col_b FROM test',   $select->__toString());     } }
  • 154. DbSelectTest.php <?php require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php'; class DbSelectTest extends PHPUnit_Framework_TestCase {     protected $_select;          protected function setUp()     {         $this->_select = new DbSelect();     }          public function testFrom()     {         $select->from('test');         $this->assertEquals('SELECT * FROM test',  $select->__toString());     }          public function testCols()     {         $select->from('test')->cols(array('col_a', 'col_b'));         $this->assertEquals('SELECT col_a, col_b FROM test',   $select->__toString());     }          protected function tearDown()     {         $this->_select = null; tearDown     } fixture }
  • 155. DbSelectTest.php <?php require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php'; class DbSelectTest extends PHPUnit_Framework_TestCase {     protected $_select;          protected function setUp()     {         $this->_select = new DbSelect();     }          public function testFrom()     {         $this->_select->from('test');         $this->assertEquals('SELECT * FROM test',  $this->_select->__toString()); $select     } $this->_select          public function testCols()     {         $this->_select->from('test')->cols(array('col_a', 'col_b'));         $this->assertEquals('SELECT col_a, col_b FROM test',   $this->_select->__toString());     }          protected function tearDown()     {         $this->_select = null;     } }
  • 156. setUp() → testFrom() → tearDown() setUp() → testCols() → tearDown()
  • 157. # phpunit tests/library/DbSelectTest DbSelectTest PHPUnit 3.5.15 by Sebastian Bergmann. . Time: 0 seconds, Memory: 5.25Mb OK (2 test, 2 assertions)
  • 159. “Houston, we have a problem.”
  • 160. DbSelect bug
  • 161. $select = new DbSelect(); $select->from('table')->where('id = 1'); where
  • 164. Bug
  • 166. DbSelectTest.php <?php require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php'; DbSelectTest.php class DbSelectTest extends PHPUnit_Framework_TestCase {     // ... ... }
  • 167. DbSelectTest.php <?php require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php'; class DbSelectTest extends PHPUnit_Framework_TestCase {     // ... ...     public function testIllegalTableName()     {         try {             $this->_select->from('test WHERE id = 1');         }         catch (IllegalNameException $e) {             throw $e;         }     } }
  • 168. DbSelectTest.php <?php require_once dirname(dirname(__DIR__)) . '/library/DbSelect.php'; class DbSelectTest extends PHPUnit_Framework_TestCase {     // ... ...     /** PHPUnit      * @expectedException IllegalNameException @expectedException      */ annotation     public function testIllegalTableName()     {         try {             $this->_select->from('test WHERE id = 1');         }         catch (IllegalNameException $e) {             throw $e;         }     } }
  • 169. # phpunit tests/library/DbSelectTest PHPUnit 3.5.15 by Sebastian Bergmann. .F. Time: 0 seconds, Memory: 6.00Mb There was 1 failure: 1) DbSelectTest::testIllegalTableName IllegalNameException Expected exception IllegalNameException FAILURES! Tests: 3, Assertions: 3, Failures: 1.
  • 171. DbSelectTest.php <?php DbSelect.php class DbSelect {     // ... ...     public function from($table)     { if (!preg_match('/[0-9a-z]+/i', $table)) {      throw new Exception('Illegal Table Name: ' . $table); }         $this->_table = $table;         return $this;     }     // ... ... }
  • 172. DbSelectTest.php <?php class DbSelect {     // ... ...     public function from($table)     { if (!preg_match('/[0-9a-z]+/i', $table)) {      throw new Exception('Illegal Table Name: ' . $table); }         $this->_table = $table;         return $this;     }     // ... ... }
  • 173. DbSelectTest.php <?php class DbSelect {     // ... ...     public function from($table)     { ^ $ if (!preg_match('/^[0-9a-z]+$/i', $table)) {      throw new Exception('Illegal Table Name: ' . $table); }         $this->_table = $table;         return $this;     }     // ... ... }
  • 174. # phpunit tests/library/DbSelectTest PHPUnit 3.5.15 by Sebastian Bergmann. ... Time: 1 second, Memory: 5.50Mb OK (3 tests, 3 assertions)
  • 175. bug
  • 181. project ├── application ├── library └── tests phpunit.xml tests └── phpunit.xml
  • 182. phpunit.xml <phpunit> root tag phpunit </phpunit>
  • 183. phpunit.xml <phpunit colors=”true”> root tag </phpunit>
  • 184. phpunit.xml <phpunit colors=”true”> test suites <testsuite name="Application Test Suite"> <directory>./application</directory> </testsuite> <testsuite name="Library Test Suite"> <directory>./library</directory> </testsuite> </phpunit>
  • 185. phpunit.xml # phpunit -c tests/phpunit.xml PHPUnit 3.5.15 by Sebastian Bergmann. .... Time: 0 seconds, Memory: 6.00Mb colors=”true” OK (4 tests, 4 assertions)
  • 186. phpunit.xml <phpunit colors=”true” bootstrap=”./bootstrap.php”> <testsuite name="Application Test Suite"> PHP <directory>./application</directory> </testsuite> <testsuite name="Library Test Suite"> <directory>./library</directory> </testsuite> </phpunit>
  • 187. bootstrap.php <?php define('PROJECT_PATH', realpath(dirname(__DIR__))); set_include_path(implode(PATH_SEPARATOR, array(     realpath(PROJECT_PATH . '/library'),     realpath(PROJECT_PATH . '/application'),     get_include_path() ))); function autoload($className) {     $className = str_replace('_', '/', $className);     require_once "$className.php"; } spl_autoload_register('autoload');
  • 188. phpunit.xml http://goo.gl/tvmq4
  • 194. Slides