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

Testing, Build Automation & Auto Deployment

Testing, Build Automation & Auto Deployment

An indepth journey into software testing principles, build automation with jenkins and auto deployment with capistrano. All with a focus on PHP.

Paul Dragoonis

November 21, 2014
Tweet

Other Decks in Programming

Transcript

  1. About Me Paul Dragoonis - @dr4goonis - [email protected] Work on

    PHP core/extensions/documentation Make php.net pretty & modern PHPFIG maintainer / member PPI Framework Project Lead CTO / Lead Developer at Opinurate
  2. PPI Framework Project Lead PPI is the PHP Interoperability Framework.

    It provides an equal and open platform allowing PHP developers to pick the best tools from the best PHP frameworks PPI bootstraps framework components for you from the top frameworks such as ZendFramework2, Symfony2, Laravel4, Doctrine2
  3. Outline Testing Benefits, Guidelines, Mocking, Practical Examples Setting up Build

    Automation Benefits, Jenkins, Ant Tasks, Jenkins Plugins, PHP QA Tools Auto Deployment to any remote server
  4. Unit Testing Tests a single unit of code (otherwise it’s

    integration testing) A unit is the smallest testable part of an application, i.e: as class or a method Tests contain assertions that are either true or false. The tests pass when the behaviour of the code is correct.
  5. Reasons Testing Matters Even the best programmers can and do

    make mistakes! Allows a project to move in baby-steps from one completely tested working version to the next with complete confidence. The amount of confidence is proportional to the quality of the automated testing.
  6. Some Testing Fears Won’t writing tests slow me down? A

    project using TDD can take up to 30% longer to build, but results in up to 90% fewer bugs in production Bugs are test cases you have not written yet.
  7. Tips For Writing Testable Code Test one class at a

    time Make each test independant Mock external state and services
  8. ROI Benefits of Unit Testing > Identifies tight code coupling

    and behavioural changes > Facilitate refactoring (check if you broke something?) > Find problems early > Encourages use of Dependency Injection > Encourages Single Responsibility Principle (S of SOLID) > Increases the quality of OOD/OOP > Changes how you think when developing
  9. Dependency Injection Providing the objects that an object needs (its

    dependencies) instead of having it construct them itself. It's a very useful technique for testing, since it allows dependencies to be mocked.
  10. Things to mock Objects that you’re not directly testing, isolate

    all the things! External services > database calls > api calls > filesystem calls > environment state
  11. Things that are hard to mock Global variables statics variables

    and methods singletons private methods Magic methods (__call, __get, __set)
  12. Integration Testing Check out dbUnit XML files which become SQL

    inserts so you can run database queries and check the results. Good for SQL-level logic and checking that your queries or stored procedures work properly.
  13. Acceptance Testing No time today, but look at the following

    technologies: Selenium, Behat, phpspec, Mink, PhantomJS BDD Test cases: Useful customer communication because they’re human readable. Focuses on business requirements and their behaviour
  14. Build Automation What is a build? A build acts as

    the process for putting source code together and verifying that the source works as a cohesive unit -- “Paul M. Duvall” What is build automation? “Build automation is the act of scripting a wide variety of tasks that developers do in their day-to-day activities. Such as: Compiling, Running Tests, Deployment to Production -- Wikipedia
  15. What’s in a build? Code generation > Autoloader optimisations >

    Object-Relational Mapper (php or sql code) > Framework Scaffolding (cache warming)
  16. Getting the most out of CI Keep changesets as small

    as possible. Merge early, merge often > less conflicts the better Write tests that run fast > don’t want to be waiting 10 minutes for a hotfix to build Automate the entire QA process (one click action)
  17. Jenkins Extensible with lots of plugins and integrates with PHP

    QA Tools Tasks using Apache Ant Automated Test suite Code Analysis / Coding Standards Documentation Generation Software metrics in one central place Packaging / Deployment
  18. QA Tools For PHP PHP_CodeSniffer (PSR Compliance) > Checks for

    coding style violations > Best Practises: Comes with PSR-1 and PSR-2 PHP-CPD (Copy Paste Detector) PHPMD (Mess detector) > Warns you on complex code Clover PHP (code coverage output)
  19. Auto Deployment - Benefits One click deploys, no manual intervention

    Anyone on your team can deploy You can deploy from Jenkins or IRC ! Release Management, you can roll back!
  20. Capistrano - How does it work ? Pulls down your

    VCS (git) contents Performs tasks on the codebase > db migrations > asset minification 1) SSH into your target server 2) rsync’s the code to the target dir 3) updates the ‘current’ symlink to the latest release 4) if errors, runs cap:rollback
  21. Using Capistrano - Configuration Capfile Default file outlining basic setup

    and including a deploy.rb file config/deploy.rb This file contains your application’s deployment config config/deploy/environment_name.rb Specific configurations for the specific environment.
  22. Capifony - Capistrano Ext for Symfony2 When not using a

    Symfony2 - I’ve been using PPI Framework 2.1