Your SlideShare is downloading. ×
Tdd Presentation
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Tdd Presentation

2,237
views

Published on

Test Driven Development from December's PHP Israel and Tapuz Forum meeting at Zend's offices

Test Driven Development from December's PHP Israel and Tapuz Forum meeting at Zend's offices

Published in: Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,237
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1.  
  • 2.  
  • 3.  
  • 4. <?php $a = 1; do_smething_to_var( $a ); if ( $a ===1) die ( 'test failed' ); echo 'test did not fail. Yey!' ; function do_something_to_var( & $var ){ $var += 1; }
  • 5.  
  • 6.
    • interface SomeBehaviour{
      • public function doOneThing( $var );
      • public function doOtherThing( $var );
      • public function doThirdThing( $var );
    • }
  • 7.
    • class SomeBehaviour{
      • public function doOneThing( $var ){ /* ... */ }
      • public function doOtherThing( $var ){ /* ... */ }
      • public function doThirdThing( $var ){ /* ... */ }
      • protected function willDoItThing( $var ){ /* ... */ }
      • protected function willDoAnotherThing( $var ){ /* ... */ }
    • }
  • 8.
    • class SomeTest extends PHPUnit_Framework_Testcase {
      • public function testSomeBehaviuor() {
        • /*
        • some assertions
        • */
      • }
      • public function testSomeOtherBehaviour() {
        • /*
        • some assertions
        • */
      • }
    • {
    • class Some {
      • public function someBehaviuor( $var ) {
        • /*
          • some expected action
        • */
      • }
      • public function someOtherBehaviour( $var ) {
        • /*
          • some expected action
        • */
      • {
    • }
  • 9.  
  • 10. Active Record : מייצג את הישות &quot; הודעה &quot; באתר . אחראי על כל הפעולות שנעשה על עם הודעה אחראי על מנגנון ההרשאות באתר . מימוש יחודי לכל אתר בנפרד
  • 11.
    • //From MessagePermissionHandlerTest
    • public function testOpenPermission () {
      • user::setId(1);
      • $permissionHandler = new MessagePermissionHandler();
      • $this -> assertTrue ($permissionHandler ->doesHavePermission( 'open' ,1));
      • user::setId(2);
      • $this -> assertFalse ($permissionHandler ->doesHavePermission( 'open' ,1));
    • }
    • //From MessagePermissionHandler
    • public function doesHavePermission( $type , $forum_id = false , $message_id = false ){
      • if (user :: getInstance()->canOpenForum( $forum_id )) return true ;
      • return false ;
    • }
  • 12.
    • / /From MessagePermissionHandlerTest
    • public function testOpenPermission () {
      • user::setId(1);
      • $permissionHandler = new MessagePermissionHandler();
      • $this -> assertTrue ($permissionHandler ->doesHavePermission( 'open' ,1));
      • user::setId(2);
      • $this -> assertFalse ($permissionHandler ->doesHavePermission( 'open' ,1));
    • }
    • public function testCreatePermission () {
      • user::setId(1);
      • $permissionHandler = new MessagePermissionHandler();
      • $this -> assertTrue ($permissionHandler ->doesHavePermission( ‘create' ,1));
      • user::setId(2);
      • $this -> assertFalse ($permissionHandler ->doesHavePermission( ‘create' ,1));
    • }
    • public function testUpdatePermission () {
      • user::setId(1);
      • $permissionHandler = new MessagePermissionHandler();
      • $this -> assertTrue ($permissionHandler ->doesHavePermission( ‘update' ,1,3));
      • user::setId(2);
      • $this -> assertFalse ($permissionHandler ->doesHavePermission( ‘update' ,1,3));
    • }
  • 13.
    • //From PermissionHandler
    • public function doesHavePermission( $type , $forum_id = false , $message_id = false ){
      • if (user:::getId() === 1) return true ;
      • return false ;
    • }
  • 14.
    • class a{
      • private $b = null ;
      • public function __construct(){
      • $this ->b = new B();
      • }
    • }
    • class a{
      • private $b = null ;
      • public function __construct(B $b ){
      • $this ->b = $b;
      • }
    • }
    • $b = new B();
    • $a = new a( $b );
    הוצאנו את הבנאי מחוץ למחלקה – ו &quot; הזרקנו &quot; את המחלקה המוכנה . Type hinting – בד &quot; כ לממשק
  • 15.  
  • 16.
    • public function setParent(Message $parent ){
      • if ( null != $this ->parent){
        • throw new Exception( ‘parent set’ );
      • }
      • $this ->parent = $parent ;
    • }
      • public function testSetParent(){
        • $ph = $this ->getMock( ‘PermissionHandler’ );
        • $pr = $this ->getMock( ‘Message’ );
        • $this ->message = new Message( $ph );
        • $this ->message->setParent( $pr );
        • $this ->assertTrue( $this ->message instanceof Message);
      • }
    • //from Message
    • public function __construct( PermissionHandler $ph ){
      • $this ->handler = $ph ;
    • }
    שם המחלקה אותה נרצה לזייף הזרקת תלות
  • 17.
    • //from MessageTest
    • /**
    • * @expectedException NoPermissionException
    • */
    • public function testSetIdNoPermission(){
      • $ph = $this
      • ->getMock( ‘PermissionHandler’ , array ( ‘doesHavePermission’ ));
      • $ph ->expects( $this ->once())
        • ->method( ‘doesHavePermission’ )
        • ->will( $this ->returnValue( false ));
      • $message = new Message( $ph );
      • $message ->setId(1);
    • }
    • //from Message
    • public function setId( $id ){
      • if (! $this ->handler->doesHavePermission( ‘open’ , $this ->getForumId())){
        • throw new NoPermissionException( ‘open’ );
      • }
      • $this ->openMessage( $id );
    • }
    כמה פעמים לצפות שהמתודה תיקרא יכול לקבל : once, any, exactly( int ) . שם המתודה איזה ערך להחזיר . יכול גם לקבל מערך של ערכים ( למקרה שנרצה שהמתודה תיקרא מספר פעמים ) מערך של מתודות שנרצה שידומו Annotaitions : שימוש ב Dock-Block כדי להגדיר עוד תנאים למבחן – במקרה הזה – לצפות לחריגה מסוג מסויים
  • 18.  
  • 19.
    • public function setUp(){
      • $this ->initDB();
    • }
    • // some tests
    • public function tearDown(){
      • $this ->emptyDB();
    • }
    • protected function initDB(){
      • $pdo = new PDO( 'mysql:host:=localhost;dbname=test_db’ , ’root’ , ’1234’ );
      • $sql = file_get_contents ( dirname (__FILE__). '/../db/test_sql.sql' );
      • $sql = explode ( ';' , $sql );
      • foreach ( $sql as $stmt ){
      • try {
      • $pdo ->query( $stmt );
      • } catch (Exception $e ){}
      • }
    • }
    INSERT INTO `messages` VALUES ( '1' , '1' , '1' , '1' ); INSERT INTO `messages` VALUES ( '2' , '1' , '1.2' , '1' ); INSERT INTO `messages` VALUES ( '3' , '1' , '1.3' , '1' ); INSERT INTO `messages` VALUES ( '4' , '1' , '1.2.4' , '1' ); INSERT INTO `messages` VALUES ( '5' , '1' , '5' , '5' ); INSERT INTO `messages` VALUES ( '7' , '1' , '1.3.7' , '1' ); INSERT INTO `messages` VALUES ( '8' , '1' , '5.8' , '5' ); TRUNCATE TABLE `forums` ; TRUNCATE TABLE `message_contents` ; TRUNCATE TABLE `message_extras` ; TRUNCATE TABLE `messages` ; TRUNCATE TABLE `users` ;
  • 20.  
  • 21.
    • public function testProtectedMethod(){
      • $tester = new MessageTester();
      • $this ->assertTrue (
        • $tester -> public _ protectedMethodReturnsTrue()
      • );
    • }
    אם המתודה מתחילה ב _public וקיימת במחלקה תבדוק אם למחלקה יש כבר __call
  • 22.  
  • 23.