📜 ⬆️ ⬇️

Installing and Configuring Functional Testing in Symfony2 with Behat and Mink

The idea that web applications written in PHP need to be tested is not new and is gradually entering the daily practice of developers. PHPUnit has become the standard for testing PHP applications, including the new Symfony2 framework. In the installation of the symfony-standard in AcmeDemoBundle, it is he who is used to test the controller 1 . I want to talk about an alternative way of testing functionality, using Behat and Mink, and describe the details of the installation and testing process.

Installing the framework


First we need a working bundle Apache + PHP 5.3 , with support for local hosts , intl , git . I will not dwell on the process of installing and configuring them - there is enough information in the network on how to do this for each individual system, and the developer has probably already installed all this (I personally believe that development should be done on a local or test machine, but not on a working site)
Next, we need to download and configure our project from GitHub. Open the terminal, go to the folder where the local sites are located.

$ cd ~/Sites
$ git clone github.com/symfony/symfony-standard.git
$ cd symfony-standard


Check whether your Apache + PHP is suitable for work in Symfony2, for this we use the script supplied with the framework:
$ php app/check.php
If you need any settings or modules - install and configure.

We download the framework itself (it is located by default in the vendors folder). To do this, we type in the terminal:
$ php bin/vendors install
After that, you need to customize the project display in the browser, for which I make changes to two files (you may have different paths, I work on Mac Os 10.6 with standardly supplied apache):
- I add a line to / etc / hosts indicating that I have a local project, and there’s nothing to look for on the network:
...
127.0.0.1 symfony-standard

- in /etc/apache2/extra/httpd-vhosts.conf
')
 <VirtualHost *:80> DocumentRoot "/Users/Standart-User/Sites/symfony-standard/web" ServerName t0002.loc <Directory "/Users/Standart-User/Sites/symfony-standard/web"> Options Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews AllowOverride All </Directory> </VirtualHost> 

Restart Apache, and check the result of the work in the browser by opening the symfony-standard / app_dev.php page . If everything is done correctly, we get the following image:
Symfony-standard start page

And when you type in the terminal in the root folder of the project
$ php app/console
get the list of framework commands. We note for ourselves that in this list there is no behat command after listing Options in the Available commands block

At this stage, we can assume that we managed to install Symfony2.

Installing Mink and Behat for testing


Since all the projects I work on are in my ~ / Sites folder and I don’t want to install bundles for testing for each Symfony2 framework installation - I did it once in the ~ / Sites / testsuite.behat folder , and then I connected its contents in their projects.

For ease of installation, I created a project on GitHub, from there you can install it:

 $ cd ~/Sites $ git clone https://github.com/livsi/testsuite.behat.git $ cd testsuite.behat 


In fact, it contains a set of submodules and instructions on how to tie it all up to your working draft on Symfony2. After cloning the repository, we need to download submodules. To do this, execute the command:
 $ git submodule update --init 


Everything with the installation of the test kit is almost finished. Since the active branches in Behat and Mink are the develop, not the master branches, you need to switch to them and switch manually.
 $ cd ~/Sites/testsuite.behat/vendor/Behat/Behat/ $ git checkout --track -b develop origin/develop $ cd ~/Sites/testsuite.behat/vendor/Behat/Mink/ $ git checkout --track -b develop origin/develop 


We connect testsuite.behat to the symfony-standart project


This is the fastest stage, since we do not need to get anything from the network, just edit the configuration files in the symfony-standart project. Instructions are attached to the code downloaded earlier, are in the README.md file

Open the file ~ / Sites / symfony-standart / app / autoload.php and add the following lines (you need to correct the paths according to your own) to load the test bundles into the symfony2 project:
 // app/autoload.php $loader->registerNamespaces(array( // ... 'Behat\BehatBundle' => '/Users/Standart-User/Sites/testsuite.behat/vendor', 'Behat\Behat' => '/Users/Standart-User/Sites/testsuite.behat/vendor/Behat/Behat/src', 'Behat\Gherkin' => '/Users/Standart-User/Sites/testsuite.behat/vendor/Behat/Gherkin/src', 'Behat\Mink' => '/Users/Standart-User/Sites/testsuite.behat/vendor/Behat/Mink/src', 'Behat\MinkBundle' => '/Users/Standart-User/Sites/testsuite.behat/vendor', 'Goutte' => '/Users/Standart-User/Sites/testsuite.behat/vendor/Goutte/src', 'Zend' => '/Users/Standart-User/Sites/testsuite.behat/vendor/Zend/library', 'Behat\SahiClient' => '/Users/Standart-User/Sites/testsuite.behat/vendor/Behat/SahiClient/src', 'Buzz' => '/Users/Standart-User/Sites/testsuite.behat/vendor/Buzz/lib', // ... 

In the file ~ / Sites / symfony-standart / app / AppKernel.php we include two main bundles - BehatBundle and BehatMinkBundle :
 // app/AppKernel.php if (in_array($this->getEnvironment(), array('dev', 'test'))) { // ... $bundles[] = new Behat\BehatBundle\BehatBundle(); $bundles[] = new Behat\MinkBundle\BehatMinkBundle(); // ... } 

We set the required parameters in ~ / Sites / symfony-standart / app / config / config_dev.yml :
 # app/config/config_dev.yml framework: test: ~ # ... behat: ~ behat_mink: base_url: http://symfony-standart/app_test.php/ goutte: ~ # enable both Goutte sahi: ~ # and Sahi session 

We check that everything is installed and configured correctly. To do this, from the root of the symfony-standart project in the terminal, enter:
$ php app/console
get the list of framework commands, which should now include the behat command. This means that everything works and the necessary bundles are connected.

Actually testing



To create a testing framework for a bundle, execute the command in the console from the project root:
$ php app/console behat:bundle --init Acme\\DemoBundle
It creates the Features / directory in the src / Acme / DemoBundle folder.

Add a file to it, which we call feature1.feature with the following content:

 # language: ru :    AcmeDemoBundle       :     dev           "/app_dev.php"       200     "Congratulations! You have successfully installed a new Symfony application."     "Welcome!" :         "Run The Demo"     "/app_dev.php"      "Run The Demo"     "/app_dev.php/demo/"       200     "Available demos" 


These are the very functional tests. To run a test, enter the following command in the console:
$ php app/console behat --no-paths @AcmeDemoBundle and see about the following result:
Test result AcmeDemoBundle symfony-standard

You can see from the screenshot that the call syntax is simplified: first from behat: test: bundle to behat: bundle , and now to just behat .

Help on the necessary keys of the console command can be obtained by typing
app/console help behat

Having spent relatively little time on connecting Behat and Mink, we get a significant profit in the form of simple and readable, visual tests that are written in an understandable language. They are quickly written and quickly executed.

Acknowledgments


Special thanks to Konstantin Kudryashov (everzet habrauzer ) for creating Behat and Mink , advising them on using KnpLabs for sponsorship and assistance to their author, for supporting the project and further improving it.
Also, many thanks to the developers of Symfony2 for a powerful and convenient framework, and detailed documentation that helps to quickly master it.

Used materials and repositories:






An example of controller testing using PHPUnit (used by default in symfony2):

 namespace Acme\DemoBundle\Tests\Controller; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class DemoControllerTest extends WebTestCase { public function testIndex() { $client = $this->createClient(); $crawler = $client->request('GET', '/demo/hello/Fabien'); $this->assertTrue($crawler->filter('html:contains("Hello Fabien")')->count() > 0); } } 


Same thing with behat
: , PHPUnit
"/app_dev.php/demo/hello/Fabien"
"Hello Fabien"


Isn't it true - the test on Behat is more visual?

Source: https://habr.com/ru/post/124100/


All Articles