Set Up Zend Framework 2 With Behat And Twig

I'll be documenting an interesting setup of a robust ZendFramework 2 based web application, integrating some cool helper libraries and discussing best practices!

Installing the Skeleton App

To start with, you would want to clone this Skeleton app repository, and then make modifications as you see fit.

I updated the composer.json file to have my own settings, and also changed the framework version explicitly to 2.0.2:

"zendframework/zendframework": "2.0.2"

Remove the composer.phar file, assuming you already have it installed (which you should).

Then create the 'ole virtual host file:

<VirtualHost *:80>
    DocumentRoot "/Users/jason/Development/test-app/public"

    SetEnv APPLICATION_ENV development

   <Directory "/Users/jason/Development/test-app/public">
       DirectoryIndex index.php index.html
       Options Indexes MultiViews FollowSymLinks
       AllowOverride All
       Order allow,deny
       Allow from all

And I added a record to my /etc/hosts file:

And OMG it works!

Installing Behat

Now how the hell am I supposed to mess around with this sample app without first having some TESTS?!

Let's start by installing the mega pimp behavior driven framework: behat.

You'll want to add this section to your composer.json:


And also these to the require block:

"behat/behat": "2.4.*@stable",
"behat/mink": "1.4@stable",
"behat/mink-extension": "*",

I like to put my behat tests in a tests/functional directory, and also have the config in some sensible place so do this:

mkdir -pv tests/functional/config
cd tests/functional
../../bin/behat --init

And that will initialize your behat features and bootstrap file. Now, if you run:

bin/behat tests/functional/

It should indicate that you have no scenarios…so let's create one to verify that we are testing the default view of this Skeleton app. First we need to create a basic configuration file in the tests/functional/config/ folder called behat.yml:

# behat.yml
    features: '%behat.paths.base%/../features'
    bootstrap:  '%behat.paths.features%/bootstrap'
      goutte: ~
      browser: firefox

And then we can add this basic feature description to the tests/functional/features/ folder and call it something like skeleton.feature:

Feature: ZendFramework 2 Default Page
    In order ensure my sample app works
    As a developer
    I need to access the default landing page

    Scenario: Accessing the landing page
        Given I am on homepage
        Then I should see "Welcome to Zend Framework 2"
        And I should see "Congratulations! You have successfully installed the ZF2 Skeleton Application. You are currently running Zend Framework version 2.0.2. This skeleton can serve as a simple starting point for you to begin building your application on ZF2."

Finally, you must modify your FeatureContext.php file, adding an additional required class:

use Behat\MinkExtension\Context\MinkContext;

Also change the context so that it extends MinkContext:

class FeatureContext extends MinkContext

Now, when you run this:

bin/behat --config=tests/functional/config/behat.yml

you should notice that all your basic tests have passed!

Later, we can add this directive to a build script to make things quite fast.

Installing Twig

Now we all know that Twig is the bomb, especially when it can integrate with ZendFramework 2 as this article indicates.

Here is the package we will be using to integrate the two: It even has example template files for the Skeleton application, which means we can use the test we just created to verify correct installation. Neat!

Add this to your composer.json file:

"zf-commons/zfc-twig": "dev-master"

And update!

Following the instructions from ZfcTwig: Add ZfcTwig to your config/application.config.php file under the modules key. Mine looks like this:

'modules' => array(

Testing at this point revealed that I had a "permissions issue," and had to make the daemon owner of the cache folder:

sudo chown daemon data/cache/

At this point you can use the files that are in the vendor/zf-commons/zfc-twig/examples/ to replace the same ones in your Skeleton app. But, if you run your tests at this point, you will get all sorts of horrible errors. The last thing we need to do is update the module/Application/config/module.config.php to point to the new twig files. Mine looks something like this in the relevant section:

'layout/layout'           => __DIR__ . '/../view/layout/layout.twig',
'application/index/index' => __DIR__ . '/../view/application/index/index.twig',
'error/404'               => __DIR__ . '/../view/error/404.twig',
'error/index'             => __DIR__ . '/../view/error/index.twig',

I also noticed that the files in the Twig library were a little out of date and that my tests were failing. I pulled out the "version" call from the template, back into the controller:

return array('version'=> \Zend\Version\Version::VERSION);

Then referenced it inside the template:

…Zend Framework version {{ version }}. This skeleton ca…

At this point, the original tests should pass indicating we have successfully integrated Twig with ZendFramework!

Up Next!

The plan is to integrate the assetic asset management library using this nifty integration. I expect some challenges getting it to play nicely with Twig, but we shall see!

If you enjoyed reading this or learned something, please consider sharing via , , or . Thanks!

If you enjoyed this article, you might like others related to the Testing interest.