test [and api] driven development of cakephp behaviors held by alexander morland at cakefest 2009
TRANSCRIPT
![Page 1: Test [and API] driven development of CakePHP Behaviors held by Alexander Morland at CakeFest 2009](https://reader036.vdocuments.mx/reader036/viewer/2022082816/56649cfe5503460f949ce236/html5/thumbnails/1.jpg)
Test [and API] driven developmentof CakePHP Behaviors
held by Alexander Morlandat CakeFest 2009
![Page 2: Test [and API] driven development of CakePHP Behaviors held by Alexander Morland at CakeFest 2009](https://reader036.vdocuments.mx/reader036/viewer/2022082816/56649cfe5503460f949ce236/html5/thumbnails/2.jpg)
1. Introduction
• Alexander Morland aka 'alkemann'• Web technology since 2005• CakePHP since 2007• illustrata.no 2009
• Revision Behavior• Ordered Behavior• Logable Behavior• and others
![Page 3: Test [and API] driven development of CakePHP Behaviors held by Alexander Morland at CakeFest 2009](https://reader036.vdocuments.mx/reader036/viewer/2022082816/56649cfe5503460f949ce236/html5/thumbnails/3.jpg)
1. Introduction2. Unit Testing3. Focus on the API 4. Why we did it5. The Devide and Conquer6. Example7. CakePHP and SimpleTest8. Lessons learned9. Summary
Goal:
Share some of the excitement I discovered in moving the logic to the right place and code it in a way that makes sense.
Talk overview
![Page 4: Test [and API] driven development of CakePHP Behaviors held by Alexander Morland at CakeFest 2009](https://reader036.vdocuments.mx/reader036/viewer/2022082816/56649cfe5503460f949ce236/html5/thumbnails/4.jpg)
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
2. Unit Testing
![Page 5: Test [and API] driven development of CakePHP Behaviors held by Alexander Morland at CakeFest 2009](https://reader036.vdocuments.mx/reader036/viewer/2022082816/56649cfe5503460f949ce236/html5/thumbnails/5.jpg)
1. Follow existing api 2. Implement abstract classes3. Hook into Cake callbacks4. Keep it simple
Model::delete( $id = NULL, $cascade = true )
delete( $id = NULL, $custom = '', $cascade = true ) vs.delete( $id = NULL, $cascade = true, $custom = '' )
3. API writing
![Page 6: Test [and API] driven development of CakePHP Behaviors held by Alexander Morland at CakeFest 2009](https://reader036.vdocuments.mx/reader036/viewer/2022082816/56649cfe5503460f949ce236/html5/thumbnails/6.jpg)
1. Move from single developer to team2. Consistent code, in-house, core and community3. Cyclic development
4. Why we did it?
![Page 7: Test [and API] driven development of CakePHP Behaviors held by Alexander Morland at CakeFest 2009](https://reader036.vdocuments.mx/reader036/viewer/2022082816/56649cfe5503460f949ce236/html5/thumbnails/7.jpg)
1. Brainstorm function2. Brainstorm implementation3. Specifications of features4. Write the API5. Parallel or sequential :
o Writing a test caseo Implement code against test
– Write tests for feature specWrite tests trying to use wrongly / error
– Code Review
5. The Divide and Conquer
![Page 8: Test [and API] driven development of CakePHP Behaviors held by Alexander Morland at CakeFest 2009](https://reader036.vdocuments.mx/reader036/viewer/2022082816/56649cfe5503460f949ce236/html5/thumbnails/8.jpg)
What: • add colour on save• easy find by colour
How:• beforeSave()• colour('red')
Tests: • save()• colour()• find()
6. Example - ColourBehavior
![Page 9: Test [and API] driven development of CakePHP Behaviors held by Alexander Morland at CakeFest 2009](https://reader036.vdocuments.mx/reader036/viewer/2022082816/56649cfe5503460f949ce236/html5/thumbnails/9.jpg)
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');
![Page 10: Test [and API] driven development of CakePHP Behaviors held by Alexander Morland at CakeFest 2009](https://reader036.vdocuments.mx/reader036/viewer/2022082816/56649cfe5503460f949ce236/html5/thumbnails/10.jpg)
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');
![Page 11: Test [and API] driven development of CakePHP Behaviors held by Alexander Morland at CakeFest 2009](https://reader036.vdocuments.mx/reader036/viewer/2022082816/56649cfe5503460f949ce236/html5/thumbnails/11.jpg)
Cake uses SimpleTest as vendor (www.simpletest.org)
domain.com/test.php
Run Example
7. CakePHP and SimpleTest
![Page 12: Test [and API] driven development of CakePHP Behaviors held by Alexander Morland at CakeFest 2009](https://reader036.vdocuments.mx/reader036/viewer/2022082816/56649cfe5503460f949ce236/html5/thumbnails/12.jpg)
1. Tests could be written blindly2. Usage in focus produced better api3. Complete project just with test4. Shared code early, feedback early5. Test, Brute-force, Optimize6. Start simple and expand7. Test compatibility with other Behaviors8. It was fun!
8. Lessons learned
![Page 13: Test [and API] driven development of CakePHP Behaviors held by Alexander Morland at CakeFest 2009](https://reader036.vdocuments.mx/reader036/viewer/2022082816/56649cfe5503460f949ce236/html5/thumbnails/13.jpg)
thanks for listening
Be yourown giant!
In closing