the php way of tdd - think first, code later
DESCRIPTION
A presentation about test driven development in php worldTRANSCRIPT
TEST DRIVEN DEVELOPMENTThink first, code later, php way
Schema
3 Factors1. Test2. Code3. Refactor
Result:1. Readable code2. Managed code
First Implement?
Second Implement?
Are you sure?
Source Code Tested?
How?User Stories:
As a guest, i want to go to homepage and want to see “hello World” message
Test Code<?php //cakephp way
$msg = $this->testAction(‘/’, array(‘return’ => ‘contents’));$this->assertEquals(‘hello World’, $msg);========================================================<?php //laravel way
$msg = $this->call(‘GET’, ‘/’);$this->assertEquals(‘hello World’, $msg);
Result?
remember: source code not exists yet! so it must be fail!
Implement Feature ~ CakePHP<?php //file: app/Controller/HomeController.php
class HomeController extends AppController { public function index() { $this->autoRender = false; echo ‘hello world’; }}
Implement Feature ~ Laravel<?php //file: app/routes.php
Route::get(‘/’, function() { return ‘hello world’;});
Result?
Programmer
Why?
‘hello world’ != ‘hello World’
Fix it!
Replace : ‘hello world’ with ‘hello World’
Test it!
Programmer
It’s just a simple test!
Simple doesn’t mean not to fail!
Complex? (example 1)
Complex ? (example 2)
Then?
Your boss?
the question is...
Good QualityGood Quality == Good BusinessGood Quality == More Happy CustomersGood Quality == More Happy Programmers
Happy Programmers == Readable Source CodeReadable Source Code == Good Quality
Time Management
CODE FIRST, TEST LATER == TIME LOSSTEST FIRST, CODE LATER == TIME INVESTMENT
Time Loss1. We don’t know bug2. We don’t know errors3. We don’t know fail validations!
Question: If you don’t know anything, are you sleeping or coding?
4. Test features? just open the browser!It’s 2014 now!!
Time Investment1. More fast we detect a bug, we can fix it
faster!2. More fast we know an error, we can fix it
faster!3. We control the code, not controlled by code!4. Test all features? One single hit => Enter!
Test All Features (cli)$ phpunit
Test Sub Features (cli)$ phpunit --testsuite FeatureA$ phpunit --testsuite FeatureB
ToolsPHPUnithttp://phpunit.de
PEARhttp://pear.php.net
MVC Framework Unit Test1. Controller2. Model3. View4. Library / Vendor /Package5. Helper
Forbidden Framework1. Developed not by test2. Not integrated with unit test3. Cannot test their own self!
Suggested Framework1. Laravel >= 4.12. CakePHP >= 2.4.5
Unit Test Environment1. OS: Linux Base (Ubuntu)2. PHP Error Level: error_reporting(-1) *all
Refactor
Programmer Assumptions
The real problem is not whether machines think but whether men do.
END - Happy Hunting