How to Use Codeship with Symfony2, Phpspec and Behat
How to use Codeship with Symfony2, phpspec and Behat

		

		
			
			
		
		My coworkers and I at waarneembemiddeling.nl are really fond of phpspec and Behat. Yes, we must confess: we didn’t test much since a couple of months ago. We skipped the phpunit age and started right away with phpspec and Behat. We also like services, so instead of setting up (and maintain) our own CI server, we use Codeship. To be honest we fell in love with Travis, but that was a little bit to expensive for us. And so our search ended at Codeship.
There is some documentation on how to use it with php, but its not that in depth about phpspec and friends. Let’s start with phpspec, as this is pretty easy. I’m assuming you install phpspec and Behat as dev dependencies using Composer:


		
				
					
				
					
				
						{    "require": {    },    "require-dev": {        "sensio/generator-bundle":           "~2.3",        "phpspec/phpspec":                   "~2.0",        "behat/behat":                       "~3.0",        "behat/symfony2-extension":          "~2.0@dev",        "behat/mink-extension":              "~2.0@dev",        "behat/mink-browserkit-driver":      "~1.2@dev",        "behat/mink-selenium2-driver":       "@dev",        "behat/mink":                        "~1.6@dev"    }}
					
				
			


phpspec
Now head over to codeship.com and edit your projects configuration. Pick “PHP” as your technology (didn’t see that one coming). In the “setup commands” field we first select the desired php version:


		


Next install deps (I believe this line is placed there by default by the codeship guys):


		
				
					
				
					
				
						composer install --prefer-source --no-interaction
					
				
			


Then add phpsec to the “test commands” field:

		
				
					
				
					
				
						bin/phpspec run --format=pretty --no-code-generation
					
				
			


Et voila, phpspec should now be functioning. 🙂
Behat
Behat is a little bit more difficult. The first problem you need to solve is to get the MySQL credentials into your Symfony2 application. These are provided trough environment vars, but differ from the naming convention in Symfony2. 
We start by changing our app/config/config_test.yml:


		
				
					
				
					123456789101112131415161718192021222324
				
						imports:    - { resource: config_dev.yml }doctrine:    dbal:        dbname:   "%test_database_name%"        driver:   pdo_mysql        user:     "%test_database_user%"        password: "%test_database_password%"framework:    test: ~    session:        storage_id: session.storage.mock_file    profiler:        collect: true        only_exceptions: falseweb_profiler:    toolbar: false    intercept_redirects: falseswiftmailer:    disable_delivery: true
					
				
			


Now to let Symfony2 pick up the environment vars we have to follow the convention I just mentioned. This means that an environment variable with the name SYMFONY__TEST_DATABASE_USER will be recognised when building the container. But let’s start by adding a bash script to ease the setup of the testing environment (locally and Codeship). Call it setup_test_env.sh and place it in the root of  your project:


		
				
					
				
					12345678910111213141516171819
				
						#!/usr/bin/env sh# we need to define our credentials here via environment vars as thats the way we get mysql# credentials in Codeship.if [ -z $SYMFONY__TEST_DATABASE_USER ]; then    export SYMFONY__TEST_DATABASE_USER=rootfiif [ -z $SYMFONY__TEST_DATABASE_PASSWORD ]; then    export SYMFONY__TEST_DATABASE_PASSWORD=yourdevweakdbpassfiif [ -z $SYMFONY__TEST_DATABASE_NAME ]; then    export SYMFONY__TEST_DATABASE_NAME=testfi# Setup DB and clear cachephp ./app/console --env=test doctrine:database:drop --forcephp ./app/console --env=test doctrine:database:createphp ./app/console --env=test doctrine:migrations:migrate --no-interactionphp ./app/console --env=test cache:clear
					
				
			


Then adjust your codeship setup commands and add:


		
				
					
				
					
				
						export SYMFONY__TEST_DATABASE_USER=$MYSQL_USERexport SYMFONY__TEST_DATABASE_PASSWORD=$MYSQL_PASSWORDexport SYMFONY__TEST_DATABASE_NAME=test./setup_test_env.sh
					
				
			


Last but not least add the behat command to the test commands:

		


Things should be working now. Quickly enough you will run into the infamous xdebug “Fatal error: Maximum function nesting level of ‘100’ reached” error. Let’s fix this right away and add this in your setup commands:


		
				
					
				
					
				
						echo "xdebug.max_nesting_level=200" >> /home/rof/.phpenv/versions/5.5/etc/php.ini
					
				
			


Summary
So the complete setup commands dialog for phpspec and Behat together looks like this:


		
				
					
				
					
				
						phpenv local 5.5# Install dependencies through Composercomposer install --prefer-source --no-interaction# Fix MySQL parametersexport SYMFONY__TEST_DATABASE_USER=$MYSQL_USERexport SYMFONY__TEST_DATABASE_PASSWORD=$MYSQL_PASSWORDexport SYMFONY__TEST_DATABASE_NAME=testecho "xdebug.max_nesting_level=200" >> /home/rof/.phpenv/versions/5.5/etc/php.ini./setup_test_env.sh
					
				
			


And the test commands like this:


		
				
					
				
					
				
						bin/phpspec run --format=pretty --no-code-generationbin/behat -n
					
				
			


Everything should be working fine now! To run your tests local don’t forget to first execute the bash script (notice the extra dot, it is required):


		


Happy testing! 😉
			

	


			
	
			
		
		
	