Test and API-driven development of CakePHP Behaviors

2,896 views

Published on

Slides from the talk given by Alexander Morland during the CakeFest #3 - July 2009

Note: the original pdf and the code related to this talk can be found on cakephp.org

Published in: Technology, Business
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,896
On SlideShare
0
From Embeds
0
Number of Embeds
32
Actions
Shares
0
Downloads
38
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Test and API-driven development of CakePHP Behaviors

  1. 1. Test [and API] driven development of CakePHP Behaviors held by Alexander Morland at CakeFest 2009
  2. 2. 1. Introduction Alexander Morland aka 'alkemann' Web technology since 2005 CakePHP since 2007 illustrata.no 2009 Revision Behavior Ordered Behavior Logable Behavior and others
  3. 3. Talk overview 1. Introduction Goal: 2. Unit Testing 3. Focus on the API  Share some of the excitement I 4. Why we did it discovered in moving the logic 5. The Devide and Conquer to the right place and code it in 6. Example a way that makes sense.  7. CakePHP and SimpleTest 8. Lessons learned 9. Summary
  4. 4. 2. Unit Testing In computer programming, unit testing is a software verification and validation method where the programmer gains confidence that individual units of source code are fit for use. A unit is the smallest testable part of an application. [..] in object-oriented programming, the smallest unit is a method,[..] from: http://en.wikipedia.org/wiki/Unit_test Design Documentation Simplifies integration Facilitates change Enables Divide and Conquer development
  5. 5. 3. API writing 1. Follow existing api  2. Implement abstract classes 3. Hook into Cake callbacks 4. Keep it simple Model::delete( $id = NULL, $cascade = true ) delete( $id = NULL, $custom = '', $cascade = true ) vs. delete( $id = NULL, $cascade = true, $custom = '' )
  6. 6. 4. Why we did it? 1. Move from single developer to team 2. Consistent code, in-house, core and community 3. Cyclic development
  7. 7. 5. The Divide and Conquer 1. Brainstorm function 2. Brainstorm implementation 3. Specifications of features 4. Write the API 5. Parallel or sequential : Writing a test case Implement code against test 6. Write tests for feature spec Write tests trying to use wrongly / error   7. Code Review
  8. 8. 6. Example - ColourBehavior What:  add colour on save easy find by colour How: beforeSave() colour('red') Tests:  save() colour() find()
  9. 9. Behavior method: 1. /** 2. * Returns a findByColour of the given colour 3. * 4. * @param Object $Model 5. * @param $colour Colour to filter model by 6. * @return string a english worded colour 7. */ 8. public function colour(&$Model, $colour) { 9. return $Model->findAllByColour($colour); 10. } Test code: 1. $findBy = $Nose->findByColour('red'); 2. $expected = array('Nose' => array(0 => array('id' => 3, 'owner' => 'Rudolf', 'colour' => 'Red' ))); 3. $this->assertEqual($findBy, $expected, 'Data corrupt : %s'); 4. /**/ 5. $check = $Nose->colour('red'); 6. $this->assertEqual($check, $findBy, 'Different result than findByColour : %s'); 7. $this->assertEqual($check, $expected, 'Incorrect result : %s');
  10. 10. Behavior method: 1. /** 2. * When saving new rows and a colour is not set, insert colour into dataset 3. * It does this by checking if neither id or colour field is in the dataset 4. */ 5. public function beforeSave(&$Model) { 6. if ( 7. !isset($Model->data[$Model->alias][$Model->primaryKey]) 8. && 9. !isset($Model->data[$Model->alias]['colour']) 10. ) { 11. $Model->data[$Model->alias]['colour'] = $this->random($Model); 12. } 13. return true; 14. } Test code: 1. $Nose->create(array('owner' => 'Alexander'); 2. $Nose->save(); 3. $result = $Nose->read(); 4. $this->assertNotNull($result['Nose']['colour'], 'Did not get a colour : %s'); 5. /**/ 6. $Nose->create(array('owner' => 'Superman', 'colour' => 'Cryptonite'); 7. $Nose->save(); 8. $result = $Nose->read(); 9. $this->assertEqual($result['Nose']['colour'],'Cryptonite', 'Interference : %s');
  11. 11. 7. CakePHP and SimpleTest Cake uses SimpleTest as vendor (www.simpletest.org) domain.com/test.php Run Example
  12. 12. 8. Lessons learned 1. Tests could be written blindly 2. Usage in focus produced better api 3. Complete project just with test 4. Shared code early, feedback early 5. Test, Brute-force, Optimize 6. Start simple and expand 7. Test compatibility with other Behaviors 8. It was fun!
  13. 13. In closing Be your own giant! thanks for listening

×