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
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
Test and API-driven development of CakePHP Behaviors
1. Test [and API] driven development
of CakePHP Behaviors
held by Alexander Morland
at CakeFest 2009
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. 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. 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
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. 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. 6. Example - ColourBehavior
What:
add colour on save
easy find by colour
How:
beforeSave()
colour('red')
Tests:
save()
colour()
find()
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. 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. 7. CakePHP and SimpleTest
Cake uses SimpleTest as vendor (www.simpletest.org)
domain.com/test.php
Run Example
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. In closing
Be your
own giant!
thanks for listening