Behat2.4 : Painless installation with Composer

31 Aug

Behat is a BDD framework for PHP application. Behat has becoming so popular in recent days. Behat has drastic changes in recent days in order to add some cool features into it.

One of the amazing feature is, behat installation with use of Composer. Composer is dependency manager for PHP application which helps us to manage all the third party dependencies at one place and use them properly. Another great feature behat added is ‘Behat Extensions’. You can extend behat by adding extensions of your choice.

In this tutorial, we will see how to upgrade Behat vesion with use of composer . Most of us have been installed Behat with ‘pear’ packages. behat version installed with pear has no longer supported. Read Behat installation guide for further information.

Now, get ready to uninstall old version of behat and install new one. Open your terminal and type in following comands.

Check your existing installation,

Shashi-MacBook-Pro:~ user$ which behat

You will see something like this, if you have installed Behat with pear

Shashi-MacBook-Pro:~ user$ which behat
/usr/local/pear/bin/behat

Remove this Behat vesion by typing

Shashi-MacBook-Pro:~ user$ sudo pear uninstall behat/behat
Password:
uninstall ok: channel://pear.behat.org/behat-2.3.5

Remove ‘Gherkin’ package

Shashi-MacBook-Pro:~ user$ sudo pear uninstall behat/gherkin
uninstall ok: channel://pear.behat.org/gherkin-2.1.1

Remove ‘Mink‘ as we are going to use ‘MinkExtension’


Shashi-MacBook-Pro:~ user$ sudo pear uninstall behat/mink
uninstall ok: channel://pear.behat.org/mink-1.3.3

MinkExtension creates Mink instance in each sub-context or it could be even used as subcontext on its own.
You can read more about it on MinkExtension documentation.

Behat 2.4 installation

As we have just removed old version of behat, so we won’t find anything in binary when you execute ‘behat’ command

Shashi-MacBook-Pro:~ user$ behat
-bash: /usr/local/pear/bin/behat: No such file or directory

Now create new Behat installation Direcory, I have created in /opt/behat like this

Shashi-MacBook-Pro:~ user$ sudo mkdir /opt/behat

Shashi-MacBook-Pro:~ user$ cd /opt/behat/
Shashi-MacBook-Pro:behat user$ sudo chmod 777 /opt/behat/

Create ‘composer.json’ file with all the required dependencies like this

Shashi-MacBook-Pro:behat user$ sudo vi composer.json

Insert following dependencies in the json file, you can add more

{
"require": {
"behat/behat": "2.4.*@stable",
"behat/mink": "1.4@stable",
"behat/mink-goutte-driver": "*",
"behat/mink-selenium-driver": "*",
"behat/mink-selenium2-driver": "*",
"behat/mink-extension": "*",
"behat/mink-sahi-driver": "*"

},
"minimum-stability": "dev",
"config": {
"bin-dir": "bin/"
}
}

Note that we have included ‘Mink Extension’ as well in the dependencies. Now download the composer and intstall dependencies.

Shashi-MacBook-Pro:behat user$ ls
composer.json

 

Shashi-MacBook-Pro:behat user$ curl http://getcomposer.org/installer | php
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
Dload  Upload   Total   Spent    Left  Speed
100 11038    0 11038    0     0  88692      0 --:--:-- --:--:-- --:--:--  307k
#!/usr/bin/env php
All settings correct for using Composer
Downloading...

Composer successfully installed to: /opt/behat/composer.phar
Use it: php composer.phar

This will download ‘composer.phar’ file. Check it

Shashi-MacBook-Pro:behat user$ ls
composer.json    composer.phar

Now install it using


php composer.phar install

This will download all the dependencies. You can see it in the ‘Behat’ directory.

Shashi-MacBook-Pro:behat user$ ls
bin        composer.json    composer.lock    composer.phar    vendor

Now, you are ready to run Behat using ‘bin/behat’ command.

Shashi-MacBook-Pro:behat user$ ./bin/behat

You will see something like this in the output. This means you have installed behat successfully !!

Shashi-MacBook-Pro:behat user$ ./bin/behat

[RuntimeException]
Context class not found.
Maybe you have provided wrong or no `bootstrap` path in your behat.yml:

http://docs.behat.org/guides/7.config.html#paths

behat [--init] [-f|--format="..."] [--out="..."] [--lang="..."] [--[no-]ansi] [--[no-]time] [--[no-]paths] [--[no-]snippets] [--[no-]snippets-paths] [--[no-]multiline] [--[no-]expand] [--story-syntax] [-d|--definitions="..."] [--name="..."] [--tags="..."] [--cache="..."] [--strict] [--dry-run] [--rerun="..."] [--append-snippets] [features]

Check Behat version


Shashi-MacBook-Pro:behat user$ ./bin/behat --version
Behat version DEV

Make Behat Global

Now Create a global symlink to access behat from anywhere

Shashi-MacBook-Pro:behat user$ sudo ln -s /opt/behat/bin/behat /usr/local/bin/behat

You can run behat from anywhere. It completes Behat2.4 installation process.

Note: You may need to restart your terminal session but not nessessarily.

Shashi-MacBook-Pro:bin user$ which behat
/usr/local/bin/behat

Enjoy New Behat !! 

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: