Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Unit testing

3,332 views

Published on

EPHPC Webinar
Unit Testing

Merangkak Menuju Pengembangan Aplikasi PHP yang Berkualitas dan Tahan Banting.

Date: Wednesday, April 6, 2011
Time: 1:00 pm, Bangkok Time (Bangkok, GMT+07:00)

Published in: Technology
  • Be the first to comment

Unit testing

  1. 1. Unit Testing Merangkak Menuju Pengembangan Aplikasi PHP yang Berkualitas dan Tahan Banting Arthur Purnama arthur [at] purnama.de http://arthur.purnama.de 06.04.2011
  2. 2. Arthur Purnama <ul><li>Diplom Informatiker Hochschule Fulda </li></ul><ul><li>Sejak 2004 bekerja sebagai PHP Programmer </li></ul><ul><li>Sejak 2008 bekerja sebagai ITConsultant divisi Portal Solutions </li></ul><ul><li>Saat ini tinggal di Frankfurt am Main, Jerman </li></ul>
  3. 3. Tujuan <ul><li>Memperkenalkan Unit Testing </li></ul><ul><li>Mendorong atau memotivasi penggunaan Unit Testing </li></ul><ul><li>Memperkenalkan Unit Testing Framework PHPUnit </li></ul>
  4. 4. Rutinitas Test Programer PHP <ul><li>Berjam-jam mencari bug </li></ul><ul><li>Debugger? apaan tuh? </li></ul><ul><li>Teman setia: echo, print_r, var_dump </li></ul><ul><li>Mati satu tumbuh seribu (bug baru) </li></ul><ul><li>Bug Lama Bersemi Kembali </li></ul>
  5. 5. Unit Testing <ul><li>In computer programming, unit testing is a method by which individual units of source code are tested to determine if they are fit for use. </li></ul><ul><li>A unit is the smallest testable part of an application. </li></ul><ul><li>In object-oriented programming a unit is usually a method. </li></ul>http://en.wikipedia.org/wiki/Unit_testing
  6. 6. Unit Testing <ul><li>Dalam pemrograman komputer, unit testing merupakan suatu metode yang mana satu unit dalam source code diuji untuk menunjukkan bahwa code tersebut bekerja sebagaimana mestinya. </li></ul><ul><li>Unit adalah bagian terkecil dari satu aplikasi yang dapat diuji. </li></ul><ul><li>Dalam pemrograman berorientasi obyek, unit biasanya adalah method. </li></ul>
  7. 7. Contoh function multiplicate($a, $b){ return $a*$b; } echo multiplicate(1, 2) === 2 ? true : false ; echo PHP_EOL; echo multiplicate(2, 2) === 4 ? true : false ; echo PHP_EOL; echo multiplicate(3, 2) !== 5 ? true : false ;
  8. 8. Kelebihan Unit Testing <ul><li>Yakin setiap saat bahwa code berfungsi sebagaimana mestinya. </li></ul><ul><li>Yakin setiap kali aplikasi berkembang, code tetap berfungsi sebagaimana mestinya. </li></ul><ul><li>Yakin setiap kali melakukan re-design atau refakturisasi, code tetap berfungsi sebagaimana mestinya. </li></ul>
  9. 9. Kelebihan Unit Testing <ul><li>Menjadi bagian dari dokumentasi code </li></ul><ul><li>Mempermudah Integrasi antar komponen </li></ul><ul><li>Meningkatkan kualitas code </li></ul>
  10. 10. Unsur Unit Testing <ul><li>Harus di otomatisasi </li></ul><ul><li>Dapat dijalankan dengan sekali klick </li></ul><ul><li>Dapat dengan mudah diulang </li></ul><ul><li>Mudah diimplementasikan </li></ul><ul><li>Setelah ditulis dapat dijalankan kapan saja </li></ul><ul><li>Setiap orang dapat menjalankannya </li></ul><ul><li>Berjalan dengan cepat </li></ul>
  11. 11. Unit Test != Integration Test <ul><li>Unit Test </li></ul><ul><ul><li>Tidak berinteraksi dengan basis data </li></ul></ul><ul><ul><li>Tidak berinteraksi dengan Jaringan </li></ul></ul><ul><ul><li>Tidak berinteraksi dengan File System </li></ul></ul><ul><ul><li>Tidak membutuhkan konfigurasi khusus untuk menjalankannya </li></ul></ul><ul><ul><li>Terisolasi dari komponen atau kelas konkrit lainnya </li></ul></ul>
  12. 12. Unit Testing Framework <ul><li>PHPUnit </li></ul><ul><ul><li>Unit Testing Framework </li></ul></ul><ul><ul><li>Ditulis oleh Sebastian Bergmann </li></ul></ul><ul><ul><li>De facto standard untuk PHP </li></ul></ul><ul><ul><li>Banyak didukung oleh Tools dan Framework lain </li></ul></ul>
  13. 13. Contoh Unit Testing Multiplikasi <ul><li><?php </li></ul><ul><li>//MultiplikasiTest.php </li></ul><ul><li>require_once 'multiplikasi.php' ; </li></ul><ul><li>class BelajarUnitTest extends PHPUnit_Framework_TestCase{ </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  14. 14. Contoh Unit Testing Multiplikasi <ul><li><?php </li></ul><ul><li>//MultiplikasiTest.php </li></ul><ul><li>require_once 'multiplikasi.php' ; </li></ul><ul><li>class BelajarUnitTest extends PHPUnit_Framework_TestCase{ </li></ul><ul><li>//Nama fungsi menggunakan awalan “test“ </li></ul><ul><ul><li>public function testMultiplicate(){ </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  15. 15. Contoh Unit Testing Multiplikasi <ul><li><?php </li></ul><ul><li>//MultiplikasiTest.php </li></ul><ul><li>require_once 'multiplikasi.php' ; </li></ul><ul><li>class BelajarUnitTest extends PHPUnit_Framework_TestCase{ </li></ul><ul><li>//Nama fungsi menggunakan awalan “test“ </li></ul><ul><ul><li>public function testMultiplicate(){ </li></ul></ul><ul><ul><li>$this->assertEquals(2, multiplicate(1, 2)); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  16. 16. Contoh Unit Testing Multiplikasi <ul><li>$>phpunit BelajarUnitTest MultiplikasiTest.php </li></ul><ul><li>PHPUnit 3.5.12 by Sebastian Bergmann. </li></ul><ul><li>. </li></ul><ul><li>Time: 0 seconds, Memory: 3.00Mb </li></ul><ul><li>OK (1 test, 1 assertion) </li></ul>
  17. 17. Contoh Unit Testing Multiplikasi <ul><li><?php </li></ul><ul><li>//MultiplikasiTest.php </li></ul><ul><li>require_once 'multiplikasi.php' ; </li></ul><ul><li>class BelajarUnitTest extends PHPUnit_Framework_TestCase{ </li></ul><ul><ul><li>public function testMultiplicate(){ </li></ul></ul><ul><ul><li>//Sangat tidak dianjurkan. Pastikan hanya selalu menguji SATU hal </li></ul></ul><ul><ul><ul><li>$this->assertEquals(2, multiplicate(1, 2)); </li></ul></ul></ul><ul><ul><ul><li>$this->assertEquals(5, multiplicate(3, 2)); </li></ul></ul></ul><ul><ul><ul><li>$this->assertEquals(4, multiplicate(2, 2)); </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  18. 18. Contoh Unit Testing Multiplikasi <ul><li>$>phpunit BelajarUnitTest MultiplikasiTest.php </li></ul><ul><li>PHPUnit 3.5.12 by Sebastian Bergmann. </li></ul><ul><li>F </li></ul><ul><li>Time: 0 seconds, Memory: 3.00Mb </li></ul><ul><li>There was 1 failure: </li></ul><ul><li>1) BelajarUnitTest::testMultiplicate </li></ul><ul><li>Failed asserting that <integer:6> matches expected <integer:5>. </li></ul><ul><li>$MultiplikasiTest.php:10 </li></ul><ul><li>FAILURES! </li></ul><ul><li>Tests: 1, Assertions: 2, Failures: 1. </li></ul>
  19. 19. Contoh Unit Testing Multiplikasi <ul><li><?php </li></ul><ul><li>//MultiplikasiTest.php </li></ul><ul><li>require_once 'multiplikasi.php' ; </li></ul><ul><li>class BelajarUnitTest extends PHPUnit_Framework_TestCase{ </li></ul><ul><ul><li>public function testMultiplicate1And2(){ </li></ul></ul><ul><ul><li>$this->assertEquals(2, multiplicate(1, 2)); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public function testMultiplicate3And2(){ </li></ul></ul><ul><ul><li>$this->assertEquals(5, multiplicate(3, 2)); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public function testMultiplicate2And2(){ </li></ul></ul><ul><ul><li>$this->assertEquals(4, multiplicate(2, 2)); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  20. 20. Contoh Unit Testing Multiplikasi <ul><li>$>phpunit MultiplikasiTest.php </li></ul><ul><li>PHPUnit 3.5.12 by Sebastian Bergmann. </li></ul><ul><li>.F. </li></ul><ul><li>Time: 0 seconds, Memory: 3.00Mb </li></ul><ul><li>There was 1 failure: </li></ul><ul><li>1) BelajarUnitTest::testMultiplicate3And2 </li></ul><ul><li>Failed asserting that <integer:6> matches expected <integer:5>. </li></ul><ul><li>$MultiplikasiTest.php:12 </li></ul><ul><li>FAILURES! </li></ul><ul><li>Tests: 3, Assertions: 3, Failures: 1. </li></ul>
  21. 21. Contoh Unit Testing Multiplikasi <ul><li><?php </li></ul><ul><li>//MultiplikasiTest.php </li></ul><ul><li>require_once 'multiplikasi.php' ; </li></ul><ul><li>class BelajarUnitTest extends PHPUnit_Framework_TestCase{ </li></ul><ul><ul><li>public function testMultiplicate1And2(){ </li></ul></ul><ul><ul><li>$this->assertEquals(2, multiplicate(1, 2)); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public function testMultiplicate3And2(){ </li></ul></ul><ul><ul><li>$this->assertNotEquals(5, multiplicate(3, 2)); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public function testMultiplicate2And2(){ </li></ul></ul><ul><ul><li>$this->assertEquals(4, multiplicate(2, 2)); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  22. 22. Contoh Unit Testing Multiplikasi <ul><li>$>phpunit BelajarUnitTest MultiplikasiTest.php </li></ul><ul><li>PHPUnit 3.5.12 by Sebastian Bergmann. </li></ul><ul><li>... </li></ul><ul><li>Time: 0 seconds, Memory: 3.00Mb </li></ul><ul><li>OK (3 tests, 3 assertions) </li></ul>
  23. 23. Contoh Unit Testing Multiplikasi <ul><li>$>phpunit --testdox BelajarUnitTest MultiplikasiTest.php </li></ul><ul><li>PHPUnit 3.5.12 by Sebastian Bergmann. </li></ul><ul><li>BelajarUnitTest </li></ul><ul><li>[x] Multiplicate 1 and 2 </li></ul><ul><li>[x] Multiplicate 3 and 2 </li></ul><ul><li>[x] Multiplicate 2 and 2 </li></ul>
  24. 24. Tapi.. Code saya seperti ini.. <ul><li><?php </li></ul><ul><li>// Connects to your Database </li></ul><ul><li>mysql_connect ( &quot;your.hostaddress.com&quot; , &quot;username&quot; , &quot;password&quot; ) or die ( mysql_error () ); </li></ul><ul><li>mysql_select_db ( &quot;Database_Name&quot; ) or die ( mysql_error () ); </li></ul><ul><li>//checks cookies to make sure they are logged in </li></ul><ul><li>if ( isset ( $_SESSION [ 'ID_my_site' ] )) { </li></ul><ul><ul><li>$username = $_ SESSION [ 'ID_my_site' ]; </li></ul></ul><ul><ul><li>$pass = $_ SESSION [ 'Key_my_site' ]; </li></ul></ul><ul><ul><li>$check = mysql_query ( &quot;SELECT * FROM users WHERE username = ' $username '&quot; ) or die ( mysql_error () ); </li></ul></ul><ul><ul><li>$result = mysql_fetch_array ( $check ); </li></ul></ul><ul><ul><li>foreach ( $result as $info ) { </li></ul></ul><ul><ul><ul><li>//if the cookie has the wrong password, they are taken to the login page </li></ul></ul></ul><ul><ul><ul><li>if ($pass != $info [ 'password' ]) { </li></ul></ul></ul><ul><ul><ul><li>header ( &quot;Location: login.php&quot; ); </li></ul></ul></ul><ul><ul><ul><li>} else { //otherwise they are shown the admin area </li></ul></ul></ul><ul><ul><ul><ul><li>echo &quot;<p>Admin Area</p>&quot; ; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>echo &quot;<p>Your Content<p>&quot; ; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>echo &quot;<a href=logout.php>Logout</a>&quot; ; </li></ul></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} else { </li></ul><ul><ul><li>//if the cookie does not exist, they are taken to the login screen </li></ul></ul><ul><ul><li>header ( &quot;Location: login.php&quot; ); </li></ul></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  25. 25. Core techniques <ul><li>Refactoring </li></ul><ul><li>Separation of Concern </li></ul><ul><li>Inversion of Control (Dependency Injection) </li></ul><ul><li>Design by Contract </li></ul><ul><li>Design Patterns </li></ul>
  26. 26. Contoh Refactoring <ul><li><?php </li></ul><ul><li>namespace myappsecurity; </li></ul><ul><li>use myappdaosinterfacesUser; </li></ul><ul><li>use myapputilinterfacesSession; </li></ul><ul><li>class Authorize implements interfacesAuthorize{ </li></ul><ul><ul><li>private $userDAO ; </li></ul></ul><ul><ul><li>private $session ; </li></ul></ul><ul><ul><li>public function __construct(User $userDAO, Session $session) { </li></ul></ul><ul><ul><ul><li>$this-> userDAO = $userDAO; </li></ul></ul></ul><ul><ul><ul><li>$this-> session = $session; </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public function isAuthorize(){ </li></ul></ul><ul><ul><ul><li>if ($this-> session ->isAuthenticate()){ </li></ul></ul></ul><ul><ul><ul><ul><li>$user = $this-> userDAO ->getByUsername($this-> session ->getUsername()); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>return $this-> session ->getPassword() === $user->getPassword(); </li></ul></ul></ul></ul><ul><ul><ul><li>} else { </li></ul></ul></ul><ul><ul><ul><li>return false ; </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  27. 27. Mocks & Stubs <ul><li><?php </li></ul><ul><li>namespace myappmocks; </li></ul><ul><li>use myappdaosinterfacesUser; </li></ul><ul><li>class UserPalsu implements User{ </li></ul><ul><ul><li>public function getById($id){ </li></ul></ul><ul><ul><li>return null ; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public function getByUsername($username){ </li></ul></ul><ul><ul><li>return null ; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public function getAll(){ </li></ul></ul><ul><ul><li>return null ; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul><ul><li>?> </li></ul><ul><li><?php </li></ul><ul><li>namespace myappmocks; </li></ul><ul><li>use myapputilinterfacesSession; </li></ul><ul><li>class SessionPalsu implements Session{ </li></ul><ul><ul><li>public function getUsername(){ </li></ul></ul><ul><ul><li>return null ; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public function getPassword(){ </li></ul></ul><ul><ul><li>return null ; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public function isAuthenticate(){ </li></ul></ul><ul><ul><li>return false ; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  28. 28. Unit Test Authorize <ul><li><?php </li></ul><ul><li>use myappsecurityAuthorize; </li></ul><ul><li>use myappmocksSessionPalsu; </li></ul><ul><li>use myappmocksUserPalsu; </li></ul><ul><li>class AuthorizeTest extends PHPUnit_Framework_TestCase { </li></ul><ul><ul><li>public function testIsAuthorizeSessionNotAuthenticate(){ </li></ul></ul><ul><ul><ul><li>$userDAO = new UserPalsu(); </li></ul></ul></ul><ul><ul><ul><li>$session = new SessionPalsu(); </li></ul></ul></ul><ul><ul><ul><li>$authorize = new Authorize($userDAO, $session); </li></ul></ul></ul><ul><ul><ul><li>$this->assertFalse($authorize->isAuthorize()); </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  29. 29. Mocks & Stubs <ul><li><?php </li></ul><ul><li>namespace myappmocks; </li></ul><ul><li>use myappmodels; </li></ul><ul><li>use myappdaosinterfacesUser; </li></ul><ul><li>class UserPalsu implements User{ </li></ul><ul><ul><li>private $user ; </li></ul></ul><ul><ul><li>public function getById($id){ </li></ul></ul><ul><ul><li>return null ; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public function getByUsername($username){ </li></ul></ul><ul><ul><li>return $this-> user ; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public function getAll(){ </li></ul></ul><ul><ul><li>return null ; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public function setUser(modelsinterfacesUser $user){ </li></ul></ul><ul><ul><li>$this-> user = $user; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul><ul><li>?> </li></ul><ul><li><?php </li></ul><ul><li>namespace myappmocks; </li></ul><ul><li>use myapputilinterfacesSession; </li></ul><ul><li>class SessionPalsu implements Session{ </li></ul><ul><ul><li>private $username ; </li></ul></ul><ul><ul><li>private $password ; </li></ul></ul><ul><ul><li>private $authenticate ; </li></ul></ul><ul><ul><li>public function getUsername() { </li></ul></ul><ul><ul><li>return $this-> username ; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public function setUsername($username) { </li></ul></ul><ul><ul><li>$this-> username = $username; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public function getPassword() { </li></ul></ul><ul><ul><li>return $this-> password ; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public function setPassword($password) { </li></ul></ul><ul><ul><li>$this-> password = $password; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public function isAuthenticate(){ </li></ul></ul><ul><ul><li>return $this-> authenticate ; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public function setAuthenticate($authenticate){ </li></ul></ul><ul><ul><li>$this-> authenticate = $authenticate; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  30. 30. Unit Test Authorize <ul><li><?php </li></ul><ul><li>use myappmodelsUser; </li></ul><ul><li>use myappsecurityAuthorize; </li></ul><ul><li>use myappmocksSessionPalsu; </li></ul><ul><li>use myappmocksUserPalsu; </li></ul><ul><li>class AuthorizeTest extends PHPUnit_Framework_TestCase { </li></ul><ul><ul><li>//public function testIsAuthorizeSessionNotAuthenticate(){... </li></ul></ul><ul><ul><li>public function testIsAuthorizeSessionAuthenticateButWrongPassword(){ </li></ul></ul><ul><ul><ul><li>$userDAO = new UserPalsu(); </li></ul></ul></ul><ul><ul><ul><li>$user = new User( &quot;username&quot; , &quot;password&quot; ); </li></ul></ul></ul><ul><ul><ul><li>$userDAO->setUser($user); </li></ul></ul></ul><ul><ul><ul><li>$session = new SessionPalsu(); </li></ul></ul></ul><ul><ul><ul><li>$session->setPassword( &quot;wrongpassword&quot; ); </li></ul></ul></ul><ul><ul><ul><li>$authorize = new Authorize($userDAO, $session); </li></ul></ul></ul><ul><ul><ul><li>$this->assertFalse($authorize->isAuthorize()); </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public function testIsAuthorizeSessionAuthenticateCorrectPassword(){ </li></ul></ul><ul><ul><ul><li>$userDAO = new UserPalsu(); </li></ul></ul></ul><ul><ul><ul><li>$user = new User( &quot;username&quot; , &quot;password&quot; ); </li></ul></ul></ul><ul><ul><ul><li>$userDAO->setUser($user); </li></ul></ul></ul><ul><ul><ul><li>$session = new SessionPalsu(); </li></ul></ul></ul><ul><ul><ul><li>$session->setPassword( &quot;password&quot; ); </li></ul></ul></ul><ul><ul><ul><li>$authorize = new Authorize($userDAO, $session); </li></ul></ul></ul><ul><ul><ul><li>$this->assertTrue($authorize->isAuthorize()); </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  31. 31. Mengorganisir Unit Test <ul><li>Fixture </li></ul><ul><li>Bootstrap </li></ul><ul><li>Stubs & Mocks Tools </li></ul><ul><li>Test Suite </li></ul><ul><li>Code Coverage </li></ul>
  32. 32. Fixtures <ul><li><?php </li></ul><ul><li>use myappmodelsUser; </li></ul><ul><li>use myappsecurityAuthorize; </li></ul><ul><li>use myappmocksSessionPalsu; </li></ul><ul><li>use myappmocksUserPalsu; </li></ul><ul><li>class AuthorizeTest extends PHPUnit_Framework_TestCase { </li></ul><ul><ul><li>private $userDAO ; </li></ul></ul><ul><ul><li>private $session ; </li></ul></ul><ul><ul><li>public function setUp(){ </li></ul></ul><ul><ul><ul><li>$this-> userDAO = new UserPalsu(); </li></ul></ul></ul><ul><ul><ul><li>$user = new User( &quot;username&quot; , &quot;password&quot; ); </li></ul></ul></ul><ul><ul><ul><li>$this-> userDAO ->setUser($user); </li></ul></ul></ul><ul><ul><ul><li>$this-> session = new SessionPalsu(); </li></ul></ul></ul><ul><ul><ul><li>$this-> session ->setAuthenticate( true ); </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public function tearDown(){ </li></ul></ul><ul><ul><ul><li>$this-> userDAO = null ; </li></ul></ul></ul><ul><ul><ul><li>$this-> session = null ; </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>//public function testIsAuthorizeSessionNotAuthenticate(){.. </li></ul></ul><ul><ul><li>public function testIsAuthorizeSessionAuthenticateButWrongPassword(){ </li></ul></ul><ul><ul><ul><li>$this-> session ->setPassword( &quot;wrongpassword&quot; ); </li></ul></ul></ul><ul><ul><ul><li>$authorize = new Authorize($this-> userDAO , $this-> session ); </li></ul></ul></ul><ul><ul><ul><li>$this->assertFalse($authorize->isAuthorize()); </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public function testIsAuthorizeSessionAuthenticateCorrectPassword(){ </li></ul></ul><ul><ul><ul><li>$this-> session ->setPassword( &quot;password&quot; ); </li></ul></ul></ul><ul><ul><ul><li>$authorize = new Authorize($this-> userDAO , $this-> session ); </li></ul></ul></ul><ul><ul><ul><li>$this->assertTrue($authorize->isAuthorize()); </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} ?> </li></ul>
  33. 33. Bootstrap < phpunit bootstrap = &quot;/path/to/bootstrap.php&quot; colors = &quot;false&quot; convertErrorsToExceptions = &quot;true&quot; convertNoticesToExceptions = &quot;true&quot; convertWarningsToExceptions = &quot;true&quot; stopOnFailure = &quot;true&quot; > <!-- ... --> </ phpunit >
  34. 34. Stubs and Mocks Tools <ul><li><?php </li></ul><ul><li>use myappsecurityAuthorize; </li></ul><ul><li>class AuthorizeTest extends PHPUnit_Framework_TestCase { </li></ul><ul><ul><li>private $userDAO ; </li></ul></ul><ul><ul><li>private $session ; </li></ul></ul><ul><ul><li>public function setUp() { </li></ul></ul><ul><ul><li>$this-> userDAO = $this->getMock ( 'myappdaosUser' ); </li></ul></ul><ul><ul><li>$user = $this->getMock ( 'myappmodelsUser' , array (), array (), '' , false , false , false ); </li></ul></ul><ul><ul><li>$user->expects ( $this->any () )->method ( 'getUsername' )->will ( $this->returnValue ( 'username' ) ); </li></ul></ul><ul><ul><li>$user->expects ( $this->any () )->method ( 'getPassword' )->will ( $this->returnValue ( 'password' ) ); </li></ul></ul><ul><ul><li>$this-> userDAO ->expects ( $this->any () )->method ( 'getByUsername' )->will ( $this->returnValue ( $user ) ); </li></ul></ul><ul><ul><li>$this-> session = $this->getMock ( 'myapputilSession' , array (), array (), '' , false , false , false ); </li></ul></ul><ul><ul><li>$this-> session ->expects ( $this->any () )->method ( 'isAuthenticate' )->will ( $this->returnValue ( true ) ); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public function tearDown() { </li></ul></ul><ul><ul><ul><li>$this-> userDAO = null ; </li></ul></ul></ul><ul><ul><ul><li>$this-> session = null ; </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>//public function testIsAuthorizeSessionNotAuthenticate(){... </li></ul></ul><ul><ul><li>public function testIsAuthorizeSessionAuthenticateButWrongPassword() { </li></ul></ul><ul><ul><ul><li>$this-> session ->expects ( $this->any () )->method ( 'getPassword' )->will ( $this->returnValue ( 'wrongpassword' ) ); </li></ul></ul></ul><ul><ul><ul><li>$authorize = new Authorize ( $this-> userDAO , $this-> session ); </li></ul></ul></ul><ul><ul><ul><li>$this->assertFalse ( $authorize->isAuthorize () ); </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>public function testIsAuthorizeSessionAuthenticateCorrectPassword() { </li></ul></ul><ul><ul><ul><li>$this-> session ->expects ( $this->any () )->method ( 'getPassword' )->will ( $this->returnValue ( 'password' ) ); </li></ul></ul></ul><ul><ul><ul><li>$authorize = new Authorize ( $this-> userDAO , $this-> session ); </li></ul></ul></ul><ul><ul><ul><li>$this->assertTrue ( $authorize->isAuthorize () ); </li></ul></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>} </li></ul><ul><li>?> </li></ul>
  35. 35. Test Suite File System Object Tests |-- Freezer |-- Freezer | |-- HashGenerator | |-- HashGenerator | | `-- NonRecursiveSHA1.php | | `-- NonRecursiveSHA1Test.php | |-- HashGenerator.php | | | |-- IdGenerator | |-- IdGenerator | | `-- UUID.php | | `-- UUIDTest.php | |-- IdGenerator.php | | | |-- LazyProxy.php | | | |-- Storage | |-- Storage | | `-- CouchDB.php | | `-- CouchDB | | | | |-- WithLazyLoadTest.php | | | | `-- WithoutLazyLoadTest.php | |-- Storage.php | |-- StorageTest.php | `-- Util.php | `-- UtilTest.php `-- Freezer.php `-- FreezerTest.php $>phpunit Tests PHPUnit 3.5.13 by Sebastian Bergmann. ............................................................ 60 / 75 ............... Time: 0 seconds OK (75 tests, 164 assertions)
  36. 36. Test Suite XML < phpunit > < testsuites > < testsuite name = &quot;Object_Freezer&quot; > < file > Tests/Freezer/HashGenerator/NonRecursiveSHA1Test.php < / file > < file > Tests/Freezer/IdGenerator/UUIDTest.php < / file > < file > Tests/Freezer/UtilTest.php < / file > < file > Tests/FreezerTest.php < / file > < file > Tests/Freezer/StorageTest.php < / file > < file > Tests/Freezer/Storage/CouchDB/WithLazyLoadTest.php < / file > < file > Tests/Freezer/Storage/CouchDB/WithoutLazyLoadTest.php < / file > < / testsuite > < / testsuites > < / phpunit >
  37. 37. Code Coverage
  38. 38. Memulai Unit Testing <ul><li>Yakinkan diri sendiri </li></ul><ul><li>Kumpulkan Case Study atau Success Story </li></ul><ul><ul><li>http://biblio.gdinwiddie.com/biblio/StudiesOfTestDrivenDevelopment </li></ul></ul><ul><li>Cari Pendukung / Panggil Konsultan </li></ul><ul><li>Tentukan Tujuan </li></ul><ul><li>Catat Perkembangan </li></ul><ul><li>Bersiap menghadapi rintangan </li></ul>
  39. 39. Kemana Setelah Ini <ul><li>Metodologi </li></ul><ul><ul><li>Test Driven Development </li></ul></ul><ul><ul><li>Behaviour Driven Development </li></ul></ul><ul><li>Tools Pendukung </li></ul><ul><ul><li>IDE (Zend Studio, PHPStorm, PDT, NetBeans) </li></ul></ul><ul><ul><li>Build Automation (Ant, Maven, Phing) </li></ul></ul><ul><ul><li>Continuous Integration </li></ul></ul><ul><li>Test Lain menggunakan PHPUnit </li></ul><ul><ul><li>Database Testing </li></ul></ul><ul><ul><li>UI Testing (Selenium) </li></ul></ul><ul><li>Diskusi terbuka di Milist / Groups </li></ul>
  40. 40. Referensi <ul><li>Wikipedia </li></ul><ul><ul><li>http://en.wikipedia.org/wiki/Unit_testing </li></ul></ul><ul><li>„ PHPUnit Manual“ – Sebastian Bergmann </li></ul><ul><ul><li>http://www.phpunit.de/manual/current/en/ </li></ul></ul><ul><li>„ Getting Started With PHPUnit “ – Sebastian Bergmann </li></ul><ul><ul><li>http://www.slideshare.net/sebastian_bergmann/getting-started-with-phpunit </li></ul></ul><ul><li>„ The Art of Unit Testing“ - Roy Osherove </li></ul><ul><li>„ Unit Testing Fundamentals“ – Richard Paul </li></ul><ul><ul><li>http://www.slideshare.net/rapaul/unit-testing-fundamentals </li></ul></ul><ul><li>„ Embrace Unit Testing“ – Alessio Pace </li></ul><ul><ul><li>http://www.slideshare.net/alessiopace/embrace-unit-testing </li></ul></ul>
  41. 41. <ul><li>Web: http://arthur.purnama.de </li></ul><ul><li>Mail: arthur [at] purnama.de </li></ul><ul><li>Twitter: @purnama </li></ul><ul><li>Groups: </li></ul><ul><ul><li>[email_address] </li></ul></ul><ul><ul><li>[email_address] </li></ul></ul>

×