Slide 1

Slide 1 text

Drupal 7 Continuous Integration and Testing Smashing Phing and Behat for fun & profit

Slide 2

Slide 2 text

Heya! \|°▿▿▿▿°|/ • Claudio Beatrice • @omissis • 7+ years experience in PHP • Founder of the Drupal Italia Association • Worked in Italy, France and Germany

Slide 3

Slide 3 text

So: D7, CI and Testing Yeah! Nowadays it’s possible to leverage the power of a tool or two to get a seamless continuous integration process not much different to the one we’re used to when developing with lower-level frameworks.

Slide 4

Slide 4 text

Continuous Integration Continuous integration (CI) is the practice, in software engineering, of merging all developer workspaces with a shared mainline several times a day. - Wikipedia

Slide 5

Slide 5 text

Testing History tldr BDD Combining TDD with Domain-Driven Design Unit testing Testing code automatically TDD Writing tests first

Slide 6

Slide 6 text

Agile Testing Matrix by Briack Marick Acceptance Testing Usability Testing Unit Testing Performance Testing Technology-facing Business-facing Critique product Support programming

Slide 7

Slide 7 text

What is that about? Basically, a collection of softwares sporting the ugliest set of icons in the known universe!

Slide 8

Slide 8 text

Jenkins Jenkins is a Java application that monitors executions of repeated jobs, such as building a software project or jobs run by cron.

Slide 9

Slide 9 text

Jenkins In a nutshell, Jenkins provides an easy-to-use so-called continuous integration system, making it easier for developers to integrate changes to the project, and making it easier for users to obtain a fresh build. The automated, continuous build increases the productivity.

Slide 10

Slide 10 text

Phing A cross-platform build tool for PHP based on Apache Ant It offers a standard, declarative way(XML) of writing scripts to handle operations such as packaging, deploying and testing web application. It features a rich set of tasks for dealing with file system operations, SQL executions, code analysis and linting, documentation generation and much, much more.

Slide 11

Slide 11 text

Phing

Slide 12

Slide 12 text

Drush Drush is a command line shell and scripting interface for Drupal, a veritable Swiss Army knife designed to make life easier for those of us who spend some of our working hours hacking away at the command prompt.

Slide 13

Slide 13 text

PHPUnit It’s the de-facto standard unit testing framework for PHP and part of the larger xUnit family. Its primary goal is to take the smallest piece of testable software in the application, isolate it from the rest of the code, and determine whether it behaves exactly as you expect.

Slide 14

Slide 14 text

PHPUnit It provides a very useful set of tools for testing code units such as: • Assertions • Fixtures • Database testing • Mock Objects • Code coverage analysis • Several kind of logging formats

Slide 15

Slide 15 text

Behat Behat is a tool for writing human-readable tests in the form of a story, using a language called Gherkin, that describe the behavior of your application. By writing stories, it is possible to define acceptance criteria that can be actually executed for testing the behavior of a web application.

Slide 16

Slide 16 text

Ambiguity and miscommunication between developers, business owners and testers can be greatly reduced. Behat One vocabulary to rule them all

Slide 17

Slide 17 text

Behat Feature: front page In order to access the website As an anonymous user I need to be able to see the login form Every feature comes with a name, followed by three lines describing the benefit, the role and the feature itself. This section is required but not important to Behat in terms of execution, it’s just there for reading purposes.

Slide 18

Slide 18 text

Behat Scenario: Login page shows the correct buttons Given I am on “/” Then I should not see the link “register” And I should see the link “login” And I should see the link “forgot password” Every scenario describes how a feature acts under different conditions and it’s composed by three parts: the context, the event and the outcome This is the part that will be transformed into a test.

Slide 19

Slide 19 text

Behat /** * @Then /^I should not see the link "(?P[^"]*)"$/ */ public function iShouldNotSeeTheLink($link) { $element = $this->getSession()->getPage(); $result = $element->findLink($link); if ($result) { throw new \Exception(sprintf( "The link '%s' was present on the page %s and was not supposed to be", $link, $this->getSession()->getCurrentUrl() )); } }

Slide 20

Slide 20 text

A few more little pieces... • Git, for versioning code • Drush Phing Task, for integrating Drush commands into Phing targets • Drupal Behat extension, needed to leverage the power of Drush into our Behat features • Migrate Module, for importing fixtures and migrating data whenever needed • Features Module, for exporting configuration into code

Slide 21

Slide 21 text

...and files! • .gitignore, for not tracking a good bunch of files we don’t need • drush make, for keeping track of the modules, themes and libraries to ues • build.xml, for describing all the actions Phing will have to take to build the website • build.ENV.properties, for storing environments variables used during the build process • behat.yml.dist, for storing Behat’s configurations

Slide 22

Slide 22 text

Save the environment(s)! • local, the developer’s machine, where everyday work is done. • dev, a server where the CI actually happens. It gets rebuilt from scratch and throughly tested every time(ideally after every push on the repository) • stage, a server used to deploy “stable” dev snapshots at a given point in time, usually for letting the client tests on a more stable environment

Slide 23

Slide 23 text

Save the environment(s)! • debug, a copy of the production environment (as faithful as possible), used for easily reproducing bugs happening on production and also to test deploys. • prod, the environment that hosts the final product, tuned and optimized for best performances.

Slide 24

Slide 24 text

Let’s sum up a second Jenkins takes those logs and publishes the results Jenkins clones the repository and invokes Phing Phing performs the build of the website, upload the files and the database and finally launches the tests PHPUnit and Behat run the tests and log the results in JUnit format

Slide 25

Slide 25 text

Time for some action! Go grab the code at http://github.com/agavee/drupal7-base

Slide 26

Slide 26 text

You deserve some moar • http://www.slideshare.net/everzet/bdd-in-symfony2/ • http://www.slideshare.net/headrevision/web- acceptance-testing-with-behat/ • http://www.exampler.com/old-blog/2003/08/21/ • http://www.phpunit.de/manual/current/en/index.html • http://dannorth.net/whats-in-a-story/

Slide 27

Slide 27 text

Thank you! http://silent-voice.org/ http://agavee.com/ @omissis