SlideShare une entreprise Scribd logo
1  sur  196
Télécharger pour lire hors ligne
Beginning PHPUnit
Jace Ju / jaceju /

 PHP Smarty




       Plurk: http://www.plurk.com/jaceju   >////<
•   PHPUnit
•   PHPUnit
•   PHPUnit
•   PHPUnit
•   PHPUnit
•
•   PHPUnit
•   PHPUnit
•
•
Question 1
Web
Question 2
Question 3
bug
bugs
Bugs
Example
1 + 2 + 3 + ... + N = ?
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';
# php run_test.php
# php run_test.php
Test 1 OK!
Test 2 OK!
...
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
project
├── application
└── library
    └── Math.php
project
├── application
├── library
│   └── Math.php
└── tests
project
                ├── application
                ├── library
                │   └── Math.php
                └── tests
                    ├── application
(   tests   )
                    └── library
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
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)
SQL    Select
ORM Framework
•   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)
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
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)
bug
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)
“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
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.
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
Test Suite
tests
 phpunit tests
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
Slides
Beginning PHPUnit
Beginning PHPUnit

Contenu connexe

Tendances

New Features PHPUnit 3.3 - Sebastian Bergmann
New Features PHPUnit 3.3 - Sebastian BergmannNew Features PHPUnit 3.3 - Sebastian Bergmann
New Features PHPUnit 3.3 - Sebastian Bergmanndpc
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownpartsBastian Feder
 
Unit testing PHP apps with PHPUnit
Unit testing PHP apps with PHPUnitUnit testing PHP apps with PHPUnit
Unit testing PHP apps with PHPUnitMichelangelo van Dam
 
Introduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitIntroduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitMichelangelo van Dam
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnitvaruntaliyan
 
Test your code like a pro - PHPUnit in practice
Test your code like a pro - PHPUnit in practiceTest your code like a pro - PHPUnit in practice
Test your code like a pro - PHPUnit in practiceSebastian Marek
 
Unit testing with PHPUnit - there's life outside of TDD
Unit testing with PHPUnit - there's life outside of TDDUnit testing with PHPUnit - there's life outside of TDD
Unit testing with PHPUnit - there's life outside of TDDPaweł Michalik
 
Unit Testing Presentation
Unit Testing PresentationUnit Testing Presentation
Unit Testing Presentationnicobn
 
PHPUnit: from zero to hero
PHPUnit: from zero to heroPHPUnit: from zero to hero
PHPUnit: from zero to heroJeremy Cook
 
Advanced PHPUnit Testing
Advanced PHPUnit TestingAdvanced PHPUnit Testing
Advanced PHPUnit TestingMike Lively
 
Test in action week 2
Test in action   week 2Test in action   week 2
Test in action week 2Yi-Huan Chan
 
Test Driven Development with PHPUnit
Test Driven Development with PHPUnitTest Driven Development with PHPUnit
Test Driven Development with PHPUnitMindfire Solutions
 
Workshop quality assurance for php projects - ZendCon 2013
Workshop quality assurance for php projects - ZendCon 2013Workshop quality assurance for php projects - ZendCon 2013
Workshop quality assurance for php projects - ZendCon 2013Michelangelo van Dam
 
UA testing with Selenium and PHPUnit - PFCongres 2013
UA testing with Selenium and PHPUnit - PFCongres 2013UA testing with Selenium and PHPUnit - PFCongres 2013
UA testing with Selenium and PHPUnit - PFCongres 2013Michelangelo van Dam
 
Testing Code and Assuring Quality
Testing Code and Assuring QualityTesting Code and Assuring Quality
Testing Code and Assuring QualityKent Cowgill
 
Unlock The Mystery Of PHPUnit (Wave PHP 2018)
Unlock The Mystery Of PHPUnit (Wave PHP 2018)Unlock The Mystery Of PHPUnit (Wave PHP 2018)
Unlock The Mystery Of PHPUnit (Wave PHP 2018)ENDelt260
 
Workshop quality assurance for php projects tek12
Workshop quality assurance for php projects tek12Workshop quality assurance for php projects tek12
Workshop quality assurance for php projects tek12Michelangelo van Dam
 
Test in action week 3
Test in action   week 3Test in action   week 3
Test in action week 3Yi-Huan Chan
 
Test in action week 4
Test in action   week 4Test in action   week 4
Test in action week 4Yi-Huan Chan
 

Tendances (20)

New Features PHPUnit 3.3 - Sebastian Bergmann
New Features PHPUnit 3.3 - Sebastian BergmannNew Features PHPUnit 3.3 - Sebastian Bergmann
New Features PHPUnit 3.3 - Sebastian Bergmann
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownparts
 
Unit testing PHP apps with PHPUnit
Unit testing PHP apps with PHPUnitUnit testing PHP apps with PHPUnit
Unit testing PHP apps with PHPUnit
 
Introduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnitIntroduction to Unit Testing with PHPUnit
Introduction to Unit Testing with PHPUnit
 
Unit Testing using PHPUnit
Unit Testing using  PHPUnitUnit Testing using  PHPUnit
Unit Testing using PHPUnit
 
Test your code like a pro - PHPUnit in practice
Test your code like a pro - PHPUnit in practiceTest your code like a pro - PHPUnit in practice
Test your code like a pro - PHPUnit in practice
 
Unit testing with PHPUnit - there's life outside of TDD
Unit testing with PHPUnit - there's life outside of TDDUnit testing with PHPUnit - there's life outside of TDD
Unit testing with PHPUnit - there's life outside of TDD
 
Unit Testing Presentation
Unit Testing PresentationUnit Testing Presentation
Unit Testing Presentation
 
PHPUnit: from zero to hero
PHPUnit: from zero to heroPHPUnit: from zero to hero
PHPUnit: from zero to hero
 
PHPUnit testing to Zend_Test
PHPUnit testing to Zend_TestPHPUnit testing to Zend_Test
PHPUnit testing to Zend_Test
 
Advanced PHPUnit Testing
Advanced PHPUnit TestingAdvanced PHPUnit Testing
Advanced PHPUnit Testing
 
Test in action week 2
Test in action   week 2Test in action   week 2
Test in action week 2
 
Test Driven Development with PHPUnit
Test Driven Development with PHPUnitTest Driven Development with PHPUnit
Test Driven Development with PHPUnit
 
Workshop quality assurance for php projects - ZendCon 2013
Workshop quality assurance for php projects - ZendCon 2013Workshop quality assurance for php projects - ZendCon 2013
Workshop quality assurance for php projects - ZendCon 2013
 
UA testing with Selenium and PHPUnit - PFCongres 2013
UA testing with Selenium and PHPUnit - PFCongres 2013UA testing with Selenium and PHPUnit - PFCongres 2013
UA testing with Selenium and PHPUnit - PFCongres 2013
 
Testing Code and Assuring Quality
Testing Code and Assuring QualityTesting Code and Assuring Quality
Testing Code and Assuring Quality
 
Unlock The Mystery Of PHPUnit (Wave PHP 2018)
Unlock The Mystery Of PHPUnit (Wave PHP 2018)Unlock The Mystery Of PHPUnit (Wave PHP 2018)
Unlock The Mystery Of PHPUnit (Wave PHP 2018)
 
Workshop quality assurance for php projects tek12
Workshop quality assurance for php projects tek12Workshop quality assurance for php projects tek12
Workshop quality assurance for php projects tek12
 
Test in action week 3
Test in action   week 3Test in action   week 3
Test in action week 3
 
Test in action week 4
Test in action   week 4Test in action   week 4
Test in action week 4
 

Similaire à Beginning PHPUnit

Php Unit With Zend Framework Zendcon09
Php Unit With Zend Framework   Zendcon09Php Unit With Zend Framework   Zendcon09
Php Unit With Zend Framework Zendcon09Michelangelo van Dam
 
13 PHPUnit #burningkeyboards
13 PHPUnit #burningkeyboards13 PHPUnit #burningkeyboards
13 PHPUnit #burningkeyboardsDenis Ristic
 
Load Testing with PHP and RedLine13
Load Testing with PHP and RedLine13Load Testing with PHP and RedLine13
Load Testing with PHP and RedLine13Jason Lotito
 
How to write not breakable unit tests
How to write not breakable unit testsHow to write not breakable unit tests
How to write not breakable unit testsRafal Ksiazek
 
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnitinternational PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnitsmueller_sandsmedia
 
PHPunit and you
PHPunit and youPHPunit and you
PHPunit and youmarkstory
 
PHP 5.3 Overview
PHP 5.3 OverviewPHP 5.3 Overview
PHP 5.3 Overviewjsmith92
 
Unit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBeneluxUnit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBeneluxMichelangelo van Dam
 
Developer testing 101: Become a Testing Fanatic
Developer testing 101: Become a Testing FanaticDeveloper testing 101: Become a Testing Fanatic
Developer testing 101: Become a Testing FanaticLB Denker
 
Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4Jeff Carouth
 
Writing Test Cases with PHPUnit
Writing Test Cases with PHPUnitWriting Test Cases with PHPUnit
Writing Test Cases with PHPUnitShouvik Chatterjee
 
Unit testing with zend framework tek11
Unit testing with zend framework tek11Unit testing with zend framework tek11
Unit testing with zend framework tek11Michelangelo van Dam
 
An introduction to Test Driven Development on MapReduce
An introduction to Test Driven Development on MapReduceAn introduction to Test Driven Development on MapReduce
An introduction to Test Driven Development on MapReduceAnanth PackkilDurai
 
PhpUnit - The most unknown Parts
PhpUnit - The most unknown PartsPhpUnit - The most unknown Parts
PhpUnit - The most unknown PartsBastian Feder
 
Object Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPObject Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPWildan Maulana
 
Practical approach for testing your software with php unit
Practical approach for testing your software with php unitPractical approach for testing your software with php unit
Practical approach for testing your software with php unitMario Bittencourt
 

Similaire à Beginning PHPUnit (20)

Php Unit With Zend Framework Zendcon09
Php Unit With Zend Framework   Zendcon09Php Unit With Zend Framework   Zendcon09
Php Unit With Zend Framework Zendcon09
 
13 PHPUnit #burningkeyboards
13 PHPUnit #burningkeyboards13 PHPUnit #burningkeyboards
13 PHPUnit #burningkeyboards
 
PHPSpec BDD for PHP
PHPSpec BDD for PHPPHPSpec BDD for PHP
PHPSpec BDD for PHP
 
PHPSpec BDD Framework
PHPSpec BDD FrameworkPHPSpec BDD Framework
PHPSpec BDD Framework
 
Load Testing with PHP and RedLine13
Load Testing with PHP and RedLine13Load Testing with PHP and RedLine13
Load Testing with PHP and RedLine13
 
How to write not breakable unit tests
How to write not breakable unit testsHow to write not breakable unit tests
How to write not breakable unit tests
 
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnitinternational PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
 
PHPunit and you
PHPunit and youPHPunit and you
PHPunit and you
 
Unit testing
Unit testingUnit testing
Unit testing
 
PHP 5.3 Overview
PHP 5.3 OverviewPHP 5.3 Overview
PHP 5.3 Overview
 
Unit testing zend framework apps
Unit testing zend framework appsUnit testing zend framework apps
Unit testing zend framework apps
 
Unit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBeneluxUnit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBenelux
 
Developer testing 101: Become a Testing Fanatic
Developer testing 101: Become a Testing FanaticDeveloper testing 101: Become a Testing Fanatic
Developer testing 101: Become a Testing Fanatic
 
Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4
 
Writing Test Cases with PHPUnit
Writing Test Cases with PHPUnitWriting Test Cases with PHPUnit
Writing Test Cases with PHPUnit
 
Unit testing with zend framework tek11
Unit testing with zend framework tek11Unit testing with zend framework tek11
Unit testing with zend framework tek11
 
An introduction to Test Driven Development on MapReduce
An introduction to Test Driven Development on MapReduceAn introduction to Test Driven Development on MapReduce
An introduction to Test Driven Development on MapReduce
 
PhpUnit - The most unknown Parts
PhpUnit - The most unknown PartsPhpUnit - The most unknown Parts
PhpUnit - The most unknown Parts
 
Object Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPObject Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOP
 
Practical approach for testing your software with php unit
Practical approach for testing your software with php unitPractical approach for testing your software with php unit
Practical approach for testing your software with php unit
 

Plus de Jace Ju

What happens in laravel 4 bootstraping
What happens in laravel 4 bootstrapingWhat happens in laravel 4 bootstraping
What happens in laravel 4 bootstrapingJace Ju
 
深入淺出 MVC
深入淺出 MVC深入淺出 MVC
深入淺出 MVCJace Ju
 
如何打造 WebMVC Framework - 基礎概念篇
如何打造 WebMVC Framework - 基礎概念篇如何打造 WebMVC Framework - 基礎概念篇
如何打造 WebMVC Framework - 基礎概念篇Jace Ju
 
Refactoring with Patterns in PHP
Refactoring with Patterns in PHPRefactoring with Patterns in PHP
Refactoring with Patterns in PHPJace Ju
 
Patterns in Library Design (套件設計裡的模式)
Patterns in Library Design (套件設計裡的模式)Patterns in Library Design (套件設計裡的模式)
Patterns in Library Design (套件設計裡的模式)Jace Ju
 
購物車程式架構簡介
購物車程式架構簡介購物車程式架構簡介
購物車程式架構簡介Jace Ju
 
Patterns in Zend Framework
Patterns in Zend FrameworkPatterns in Zend Framework
Patterns in Zend FrameworkJace Ju
 
Head First Zend Framework - Part 1 Project & Application
Head First Zend Framework - Part 1 Project & ApplicationHead First Zend Framework - Part 1 Project & Application
Head First Zend Framework - Part 1 Project & ApplicationJace Ju
 
常見設計模式介紹
常見設計模式介紹常見設計模式介紹
常見設計模式介紹Jace Ju
 
PHPUnit 入門介紹
PHPUnit 入門介紹PHPUnit 入門介紹
PHPUnit 入門介紹Jace Ju
 
Web Refactoring
Web RefactoringWeb Refactoring
Web RefactoringJace Ju
 
jQuery 實戰經驗講座
jQuery 實戰經驗講座jQuery 實戰經驗講座
jQuery 實戰經驗講座Jace Ju
 
CSS 排版 - 基礎觀念篇
CSS 排版 - 基礎觀念篇CSS 排版 - 基礎觀念篇
CSS 排版 - 基礎觀念篇Jace Ju
 
PHP 防駭 - 基礎觀念篇
PHP 防駭 - 基礎觀念篇PHP 防駭 - 基礎觀念篇
PHP 防駭 - 基礎觀念篇Jace Ju
 
PHP 物件導向 - 基礎觀念篇
PHP 物件導向 - 基礎觀念篇PHP 物件導向 - 基礎觀念篇
PHP 物件導向 - 基礎觀念篇Jace Ju
 

Plus de Jace Ju (15)

What happens in laravel 4 bootstraping
What happens in laravel 4 bootstrapingWhat happens in laravel 4 bootstraping
What happens in laravel 4 bootstraping
 
深入淺出 MVC
深入淺出 MVC深入淺出 MVC
深入淺出 MVC
 
如何打造 WebMVC Framework - 基礎概念篇
如何打造 WebMVC Framework - 基礎概念篇如何打造 WebMVC Framework - 基礎概念篇
如何打造 WebMVC Framework - 基礎概念篇
 
Refactoring with Patterns in PHP
Refactoring with Patterns in PHPRefactoring with Patterns in PHP
Refactoring with Patterns in PHP
 
Patterns in Library Design (套件設計裡的模式)
Patterns in Library Design (套件設計裡的模式)Patterns in Library Design (套件設計裡的模式)
Patterns in Library Design (套件設計裡的模式)
 
購物車程式架構簡介
購物車程式架構簡介購物車程式架構簡介
購物車程式架構簡介
 
Patterns in Zend Framework
Patterns in Zend FrameworkPatterns in Zend Framework
Patterns in Zend Framework
 
Head First Zend Framework - Part 1 Project & Application
Head First Zend Framework - Part 1 Project & ApplicationHead First Zend Framework - Part 1 Project & Application
Head First Zend Framework - Part 1 Project & Application
 
常見設計模式介紹
常見設計模式介紹常見設計模式介紹
常見設計模式介紹
 
PHPUnit 入門介紹
PHPUnit 入門介紹PHPUnit 入門介紹
PHPUnit 入門介紹
 
Web Refactoring
Web RefactoringWeb Refactoring
Web Refactoring
 
jQuery 實戰經驗講座
jQuery 實戰經驗講座jQuery 實戰經驗講座
jQuery 實戰經驗講座
 
CSS 排版 - 基礎觀念篇
CSS 排版 - 基礎觀念篇CSS 排版 - 基礎觀念篇
CSS 排版 - 基礎觀念篇
 
PHP 防駭 - 基礎觀念篇
PHP 防駭 - 基礎觀念篇PHP 防駭 - 基礎觀念篇
PHP 防駭 - 基礎觀念篇
 
PHP 物件導向 - 基礎觀念篇
PHP 物件導向 - 基礎觀念篇PHP 物件導向 - 基礎觀念篇
PHP 物件導向 - 基礎觀念篇
 

Dernier

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 

Dernier (20)

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 

Beginning PHPUnit

  • 2. Jace Ju / jaceju / PHP Smarty Plurk: http://www.plurk.com/jaceju >////<
  • 3.
  • 4.
  • 5. PHPUnit
  • 6. PHPUnit • PHPUnit
  • 7. PHPUnit • PHPUnit •
  • 8. PHPUnit • PHPUnit • •
  • 9.
  • 11. Web
  • 12.
  • 13.
  • 14.
  • 16.
  • 17.
  • 18.
  • 21. Bugs
  • 22.
  • 23.
  • 25. 1 + 2 + 3 + ... + N = ?
  • 26.
  • 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';
  • 43.
  • 45. # php run_test.php Test 1 OK! Test 2 OK!
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53. ...
  • 54.
  • 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
  • 60.
  • 62. project ├── application ├── library │ └── Math.php └── tests
  • 63. project ├── application ├── library │ └── Math.php └── tests ├── application ( tests ) └── library
  • 64.
  • 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
  • 81.
  • 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)
  • 110.
  • 111.
  • 112. SQL Select ORM Framework
  • 113.
  • 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)
  • 130.
  • 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
  • 136.
  • 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)
  • 144.
  • 145. bug
  • 146.
  • 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)
  • 158.
  • 159. “Houston, we have a problem.”
  • 160. DbSelect bug
  • 161. $select = new DbSelect(); $select->from('table')->where('id = 1'); where
  • 164. Bug
  • 165.
  • 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.
  • 170.
  • 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
  • 176.
  • 179.
  • 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
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194. Slides