SlideShare uma empresa Scribd logo
1 de 13
Baixar para ler offline
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

Mais conteúdo relacionado

Mais procurados

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
 

Mais procurados (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
 

Semelhante a 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
 

Semelhante a 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
 

Mais 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
 

Mais 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
 

Último

TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusZilliz
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...apidays
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 

Último (20)

TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 

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