SlideShare une entreprise Scribd logo
1  sur  13
Télécharger pour lire hors ligne
Test [and API] driven development
       of CakePHP Behaviors




        held by Alexander Morland
             at CakeFest 2009
1. Introduction

 Alexander Morland aka 'alkemann'
 Web technology since 2005
 CakePHP since 2007
 illustrata.no 2009

 Revision Behavior
 Ordered Behavior
 Logable Behavior
 and others
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
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
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 = '' )
4. Why we did it?

1. Move from single developer to team
2. Consistent code, in-house, core and community
3. Cyclic development
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
6. Example - ColourBehavior

What: 
   add colour on save
   easy find by colour

How:
   beforeSave()
   colour('red')

Tests: 
   save()
   colour()
   find()
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');
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');
7. CakePHP and SimpleTest

Cake uses SimpleTest as vendor (www.simpletest.org)

                     domain.com/test.php




                       Run Example
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!
In closing

  Be your
  own giant!




             thanks for listening

Contenu connexe

Tendances

php 2 Function creating, calling, PHP built-in function
php 2 Function creating, calling,PHP built-in functionphp 2 Function creating, calling,PHP built-in function
php 2 Function creating, calling, PHP built-in functiontumetr1
 
Programming Primer Encapsulation CS
Programming Primer Encapsulation CSProgramming Primer Encapsulation CS
Programming Primer Encapsulation CSsunmitraeducation
 
jQuery Plugin
jQuery PluginjQuery Plugin
jQuery Pluginrbiggs
 
RSpec 3: The new, the old, the good
RSpec 3: The new, the old, the goodRSpec 3: The new, the old, the good
RSpec 3: The new, the old, the goodmglrnm
 
Ruby on Rails testing with Rspec
Ruby on Rails testing with RspecRuby on Rails testing with Rspec
Ruby on Rails testing with RspecBunlong Van
 
Create a web-app with Cgi Appplication
Create a web-app with Cgi AppplicationCreate a web-app with Cgi Appplication
Create a web-app with Cgi Appplicationolegmmiller
 
Introduction to Python decorators
Introduction to Python decoratorsIntroduction to Python decorators
Introduction to Python decoratorsrikbyte
 
Think Generic - Add API's To Your Custom Modules
Think Generic - Add API's To Your Custom ModulesThink Generic - Add API's To Your Custom Modules
Think Generic - Add API's To Your Custom ModulesJens Sørensen
 
Object oriented php
Object oriented phpObject oriented php
Object oriented phpjwperry
 
Driving Design with PhpSpec
Driving Design with PhpSpecDriving Design with PhpSpec
Driving Design with PhpSpecCiaranMcNulty
 
Arrays & functions in php
Arrays & functions in phpArrays & functions in php
Arrays & functions in phpAshish Chamoli
 
Php using variables-operators
Php using variables-operatorsPhp using variables-operators
Php using variables-operatorsKhem Puthea
 
Symfony 4 Workshop - Limenius
Symfony 4 Workshop - LimeniusSymfony 4 Workshop - Limenius
Symfony 4 Workshop - LimeniusIgnacio Martín
 
Beginning Perl
Beginning PerlBeginning Perl
Beginning PerlDave Cross
 
BDD with Behat and Symfony2
BDD with Behat and Symfony2BDD with Behat and Symfony2
BDD with Behat and Symfony2katalisha
 

Tendances (20)

php 2 Function creating, calling, PHP built-in function
php 2 Function creating, calling,PHP built-in functionphp 2 Function creating, calling,PHP built-in function
php 2 Function creating, calling, PHP built-in function
 
Programming Primer Encapsulation CS
Programming Primer Encapsulation CSProgramming Primer Encapsulation CS
Programming Primer Encapsulation CS
 
jQuery Plugin
jQuery PluginjQuery Plugin
jQuery Plugin
 
RSpec 3: The new, the old, the good
RSpec 3: The new, the old, the goodRSpec 3: The new, the old, the good
RSpec 3: The new, the old, the good
 
Ruby on Rails testing with Rspec
Ruby on Rails testing with RspecRuby on Rails testing with Rspec
Ruby on Rails testing with Rspec
 
PHP-Part2
PHP-Part2PHP-Part2
PHP-Part2
 
Create a web-app with Cgi Appplication
Create a web-app with Cgi AppplicationCreate a web-app with Cgi Appplication
Create a web-app with Cgi Appplication
 
PHP-Part3
PHP-Part3PHP-Part3
PHP-Part3
 
Introduction to Python decorators
Introduction to Python decoratorsIntroduction to Python decorators
Introduction to Python decorators
 
Functions in php
Functions in phpFunctions in php
Functions in php
 
Think Generic - Add API's To Your Custom Modules
Think Generic - Add API's To Your Custom ModulesThink Generic - Add API's To Your Custom Modules
Think Generic - Add API's To Your Custom Modules
 
Object oriented php
Object oriented phpObject oriented php
Object oriented php
 
Driving Design with PhpSpec
Driving Design with PhpSpecDriving Design with PhpSpec
Driving Design with PhpSpec
 
Arrays & functions in php
Arrays & functions in phpArrays & functions in php
Arrays & functions in php
 
Php using variables-operators
Php using variables-operatorsPhp using variables-operators
Php using variables-operators
 
Rspec 101
Rspec 101Rspec 101
Rspec 101
 
Symfony 4 Workshop - Limenius
Symfony 4 Workshop - LimeniusSymfony 4 Workshop - Limenius
Symfony 4 Workshop - Limenius
 
Php Basic
Php BasicPhp Basic
Php Basic
 
Beginning Perl
Beginning PerlBeginning Perl
Beginning Perl
 
BDD with Behat and Symfony2
BDD with Behat and Symfony2BDD with Behat and Symfony2
BDD with Behat and Symfony2
 

Similaire à Test and API-driven development of CakePHP Behaviors

Ch ch-changes cake php2
Ch ch-changes cake php2Ch ch-changes cake php2
Ch ch-changes cake php2markstory
 
PHPunit and you
PHPunit and youPHPunit and you
PHPunit and youmarkstory
 
Crossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end FrameworkCrossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end FrameworkDaniel Spector
 
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasmineSingle Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasminePaulo Ragonha
 
10 Groovy Little JavaScript Tips
10 Groovy Little JavaScript Tips10 Groovy Little JavaScript Tips
10 Groovy Little JavaScript TipsTroy Miles
 
Advanced Php - Macq Electronique 2010
Advanced Php - Macq Electronique 2010Advanced Php - Macq Electronique 2010
Advanced Php - Macq Electronique 2010Michelangelo van Dam
 
Testing in Laravel Framework
Testing in Laravel FrameworkTesting in Laravel Framework
Testing in Laravel FrameworkAnis Ahmad
 
Quality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStormQuality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStormMichelangelo van Dam
 
How To Test Everything
How To Test EverythingHow To Test Everything
How To Test Everythingnoelrap
 
Working Effectively With Legacy Perl Code
Working Effectively With Legacy Perl CodeWorking Effectively With Legacy Perl Code
Working Effectively With Legacy Perl Codeerikmsp
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to javaciklum_ods
 
.NET Coding Standards For The Real World (2012)
.NET Coding Standards For The Real World (2012).NET Coding Standards For The Real World (2012)
.NET Coding Standards For The Real World (2012)David McCarter
 
SproutCore and the Future of Web Apps
SproutCore and the Future of Web AppsSproutCore and the Future of Web Apps
SproutCore and the Future of Web AppsMike Subelsky
 

Similaire à Test and API-driven development of CakePHP Behaviors (20)

Dutch PHP Conference 2013: Distilled
Dutch PHP Conference 2013: DistilledDutch PHP Conference 2013: Distilled
Dutch PHP Conference 2013: Distilled
 
Ch ch-changes cake php2
Ch ch-changes cake php2Ch ch-changes cake php2
Ch ch-changes cake php2
 
PHPunit and you
PHPunit and youPHPunit and you
PHPunit and you
 
Crossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end FrameworkCrossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end Framework
 
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and JasmineSingle Page Web Applications with CoffeeScript, Backbone and Jasmine
Single Page Web Applications with CoffeeScript, Backbone and Jasmine
 
Cucumber testing
Cucumber testingCucumber testing
Cucumber testing
 
Cucumber testing
Cucumber testingCucumber testing
Cucumber testing
 
10 Groovy Little JavaScript Tips
10 Groovy Little JavaScript Tips10 Groovy Little JavaScript Tips
10 Groovy Little JavaScript Tips
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Advanced Php - Macq Electronique 2010
Advanced Php - Macq Electronique 2010Advanced Php - Macq Electronique 2010
Advanced Php - Macq Electronique 2010
 
Fatc
FatcFatc
Fatc
 
Testing in Laravel Framework
Testing in Laravel FrameworkTesting in Laravel Framework
Testing in Laravel Framework
 
PerlTesting
PerlTestingPerlTesting
PerlTesting
 
Quality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStormQuality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStorm
 
Test your modules
Test your modulesTest your modules
Test your modules
 
How To Test Everything
How To Test EverythingHow To Test Everything
How To Test Everything
 
Working Effectively With Legacy Perl Code
Working Effectively With Legacy Perl CodeWorking Effectively With Legacy Perl Code
Working Effectively With Legacy Perl Code
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to java
 
.NET Coding Standards For The Real World (2012)
.NET Coding Standards For The Real World (2012).NET Coding Standards For The Real World (2012)
.NET Coding Standards For The Real World (2012)
 
SproutCore and the Future of Web Apps
SproutCore and the Future of Web AppsSproutCore and the Future of Web Apps
SproutCore and the Future of Web Apps
 

Plus de Pierre MARTIN

Introduction à CakePHP
Introduction à CakePHPIntroduction à CakePHP
Introduction à CakePHPPierre MARTIN
 
Using and reusing CakePHP plugins
Using and reusing CakePHP pluginsUsing and reusing CakePHP plugins
Using and reusing CakePHP pluginsPierre MARTIN
 
Building custom APIs
Building custom APIsBuilding custom APIs
Building custom APIsPierre MARTIN
 
Recipes for successful CakePHP projects
Recipes for successful CakePHP projectsRecipes for successful CakePHP projects
Recipes for successful CakePHP projectsPierre MARTIN
 
The CakePHP Media Plugin
The CakePHP Media PluginThe CakePHP Media Plugin
The CakePHP Media PluginPierre MARTIN
 
Internationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsInternationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsPierre MARTIN
 

Plus de Pierre MARTIN (6)

Introduction à CakePHP
Introduction à CakePHPIntroduction à CakePHP
Introduction à CakePHP
 
Using and reusing CakePHP plugins
Using and reusing CakePHP pluginsUsing and reusing CakePHP plugins
Using and reusing CakePHP plugins
 
Building custom APIs
Building custom APIsBuilding custom APIs
Building custom APIs
 
Recipes for successful CakePHP projects
Recipes for successful CakePHP projectsRecipes for successful CakePHP projects
Recipes for successful CakePHP projects
 
The CakePHP Media Plugin
The CakePHP Media PluginThe CakePHP Media Plugin
The CakePHP Media Plugin
 
Internationalizing CakePHP Applications
Internationalizing CakePHP ApplicationsInternationalizing CakePHP Applications
Internationalizing CakePHP Applications
 

Dernier

COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Websitedgelyza
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfDaniel Santiago Silva Capera
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfAijun Zhang
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfinfogdgmi
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Adtran
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URLRuncy Oommen
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfJamie (Taka) Wang
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaborationbruanjhuli
 
OpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureOpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureEric D. Schabell
 
VoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXVoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXTarek Kalaji
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6DianaGray10
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7DianaGray10
 
AI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarAI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarPrecisely
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPathCommunity
 
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...Aggregage
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfDianaGray10
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024D Cloud Solutions
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Commit University
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IES VE
 

Dernier (20)

COMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a WebsiteCOMPUTER 10 Lesson 8 - Building a Website
COMPUTER 10 Lesson 8 - Building a Website
 
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdfIaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
IaC & GitOps in a Nutshell - a FridayInANuthshell Episode.pdf
 
Machine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdfMachine Learning Model Validation (Aijun Zhang 2024).pdf
Machine Learning Model Validation (Aijun Zhang 2024).pdf
 
Videogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdfVideogame localization & technology_ how to enhance the power of translation.pdf
Videogame localization & technology_ how to enhance the power of translation.pdf
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URL
 
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
activity_diagram_combine_v4_20190827.pdfactivity_diagram_combine_v4_20190827.pdf
 
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online CollaborationCOMPUTER 10: Lesson 7 - File Storage and Online Collaboration
COMPUTER 10: Lesson 7 - File Storage and Online Collaboration
 
OpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability AdventureOpenShift Commons Paris - Choose Your Own Observability Adventure
OpenShift Commons Paris - Choose Your Own Observability Adventure
 
VoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBXVoIP Service and Marketing using Odoo and Asterisk PBX
VoIP Service and Marketing using Odoo and Asterisk PBX
 
UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6UiPath Studio Web workshop series - Day 6
UiPath Studio Web workshop series - Day 6
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7
 
201610817 - edge part1
201610817 - edge part1201610817 - edge part1
201610817 - edge part1
 
AI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity WebinarAI You Can Trust - Ensuring Success with Data Integrity Webinar
AI You Can Trust - Ensuring Success with Data Integrity Webinar
 
UiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation DevelopersUiPath Community: AI for UiPath Automation Developers
UiPath Community: AI for UiPath Automation Developers
 
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
 
Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024Artificial Intelligence & SEO Trends for 2024
Artificial Intelligence & SEO Trends for 2024
 
Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)Crea il tuo assistente AI con lo Stregatto (open source python framework)
Crea il tuo assistente AI con lo Stregatto (open source python framework)
 
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019IESVE Software for Florida Code Compliance Using ASHRAE 90.1-2019
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
  • 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. 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