Technology
• We need good tools
• They enable our workflow
• They facilitate our achievements
8
Technology
• We need good tools
• They enable our workflow
• They facilitate our achievements
• They allow us to meet our deadlines
8
Technology
• We need good tools
• They enable our workflow
• They facilitate our achievements
• They allow us to meet our deadlines
• They are not the silver bullet (sorry)
8
The Main Ingredients
Preparation time: some years
Ingredients:
• Source control
• Development platforms
• Task tracking
• Automated testing
• Static analysis
• Automated deployment
• Continuous integration
10
Source Control: Key Ingredient
• Central, canonical version
• Collaboration point
• Historical information
• what changed
• when
• by whom
• Can include its own config
12
Database Version Control
No silver bullet to keep code and database schema in sync
Strategies:
• All db changes done via script
• Scripts are numbered
• Database knows what numbers it already has
Tools:
• homespun scripts
• DbDeploy http://dbdeploy.com/
• Liquibase http://www.liquibase.org/
17
Task Tracking
Once called ’bug tracking’.
We can track what status everything is in.
Developers understand bug trackers, bug trackers understand your
workflow.
21
Workflow
Backlog Sprint
Active
Blocked Verify
Complete
22
Automated Testing Tools
• Selenium: browser-based record and play of tests
• Selenium IDE http://seleniumhq.org/projects/ide/
• Selenium RC
http://seleniumhq.org/projects/remote-control/
• PHPUnit: unit testing and automation
• http://phpunit.de
• Also generates code coverage graphs
28
My First Unit Test
require_once '../src/models/MathUtilModel.php';
class MathUtilModelTest extends PHPUnit_Framework_TestCase {
public function testAddNumbersWithNumbers() {
$util = new MathUtilModel();
$result = $util->addNumbers(3,5);
$this->assertEquals(8, $result);
}
}
29
Running One Test
To run our tests, from the tests directory do:
phpunit models/MathUtilModel
Output:
PHPUnit 3.5.13 by Sebastian Bergmann.
.
Time: 0 seconds, Memory: 3.00Mb
OK (1 test, 1 assertion)
30
Testable Code
• Testable code is clean and modular
• Need to be able to separate elements to test
• Each function does one thing
• Not too many paths through the code
• Dependencies are dangerous
31
Dependency Injection
Passing things in or looking them up.
function getData() {
$db = new MyDatabaseObject();
// sql and query
}
function getData($db) {
// sql and query
}
32
Code Coverage
What percentage of your code is tested?
• Summary view
• Drill in to see which lines are run by tests
• Beware: 100% code coverage does not mean fully tested
Use phpunit -coverage-html and specify where PHPUnit should
write the report files
Examples from http://jenkins.joind.in
33
API Documentation
Another form of static analysis is to generate documentation
• Commented documentation in each file, class, function
• Automatically generate into readable documents
• Tools:
• PHPDocumentor http://www.phpdoc.org/
• DocBlox http://www.docblox-project.org/
40
PHPCS Examples
Source code:
class recipe
{
protected $_id;
public $name;
public $prep_time;
function getIngredients() {
$ingredients = Ingredients::fetchAllById($this->_id);
return $ingredients;
}
}
43
PHPCS Examples
Sniffer output:
FILE: /home/lorna/phpcs/recipe.class.php
----------------------------------------------------------------------
FOUND 8 ERROR(S) AND 0 WARNING(S) AFFECTING 5 LINE(S)
----------------------------------------------------------------------
2 | ERROR | Missing file doc comment
3 | ERROR | Class name must begin with a capital letter
3 | ERROR | Missing class doc comment
6 | ERROR | Protected member variable "_id" must not be prefixed wit
| | underscore
12 | ERROR | Missing function doc comment
12 | ERROR | Opening brace should be on a new line
13 | ERROR | Line indented incorrectly; expected at least 8 spaces, f
13 | ERROR | Spaces must be used to indent lines; tabs are not allowe
----------------------------------------------------------------------
44
Automating Deployment: Why
• Minimise mistakes
• Save time on each deploy
• Better than documentation
• Reliable process - use for different platforms
• Scope for rollback
48
Automating Deployment: What
• Application code
• minimal downtime or time in an inconsistent state
• easy rollback
• additional setup steps (upload files, etc) also automated
• Database
• apply database patches
• include rollback patches
• Config changes
• useful for large or complex sites
• config deploys separately, can update quickly and easily
49
Code Deployment
• Get a clean copy of code
• Place in new directory on server
• Perform any other preparation tasks
• Change symlink in web directory to point to new version
• Tools: shell script or ant/phing
50
Config Deployment
• Exactly like code deployment
• Application needs to be designed with this in mind
• Default to live config
• Environment variables set in vhost
51
Continuous Integration
The glue that holds everything together!
• Source control commit triggers:
• Static analysis tools
• Automated tests
• Document generation
• CI system centralises:
• Deployment (to various platforms)
• Other tasks, cron jobs
• Centralised dashboard and reporting
55
The Main Ingredients for LAMP
Preparation time: some years
Ingredients:
• Source control
• Development platforms
• Task tracking
• Automated testing
• Static analysis
• Automated deployment
• Continuous integration
58