MinkBundle is Symfony2 bundle (plugin) created in order to give ability to write functional tests for Symfony2 applications.
Add next lines to your composer.json
file:
"require-dev": {
...
"behat/mink-bundle": "dev-master"
}
By default, MinkBundle
will use custom SymfonyDriver
. But if you want
to use GoutteDriver
, you'll need to also add:
"require-dev": {
...
"behat/mink-bundle": "dev-master",
"behat/mink-goutte-driver": "1.0.*"
}
And if you want to use SahiDriver
, you'll need to add another line:
"require-dev": {
...
"behat/mink-bundle": "dev-master",
"behat/mink-sahi-driver": "1.0.*"
}
Now run:
composer.phar install --dev
in order to install all parts.
Now it's time to install and setup MinkBundle
itself.
And to app/AppKernel.php
:
if ('test' === $this->getEnvironment()) { $bundles[] = new Behat\MinkBundle\MinkBundle(); }
Now, as you've setted up the bundle, you should configure it:
# app/config/config_test.yml
mink:
base_url: http://your_app.dev/app_test.php
By default, MinkBundle uses only SymfonyDriver
session. If you want to use
GoutteDriver
, SahiDriver
or ZombieDriver
sessions - you should
specify them in config explicitly:
# app/config/config_test.yml
mink:
base_url: http://your_app.dev/app_test.php
goutte: ~
sahi: ~
zombie: ~
Out of the box, Mink will use SymfonyDriver
session as default one. This
means that any session call without argument:
$this->getMink()->getSession()->...;
will be done against default Symfony2 test.client
library. If you want to
change this, use default_session
configuration option:
# app/config/config_test.yml
mink:
base_url: http://your_app.dev/app_test.php
default_session: goutte
goutte: ~
Note
Note, that we do our configuration in config_test.yml
. It's convenient
way to configure MinkBundle, because test
environment has all the
needed requirements for Mink and default SymfonyDriver
enabled out
of the box.
MinkBundle provides bunch of useful options for you to configure Mink's behavior. You can use them to make your testing experience even more smooth:
base_url
- most important one. Defines base url for your application. Used heavily inside BehatBundle and can be used inside your test cases to be able to use relative paths in your web test cases.default_session
- defines session name, which will be used by default. It'ssymfony
out of the box.browser_name
- specifies browser to be used withSahi
,Selenium
orSelenium2
sessions.
Now, as you've configured MinkBundle
, you can use the special MinkTestCase
,
provided with it as a base class for your tests:
<?php
namespace Acme\AcmeBundle\Tests;
use Behat\MinkBundle\Test\MinkTestCase;
class AcmeWebTestCase extends MinkTestCase
{
protected $base;
protected function setUp()
{
$this->base = $this->getKernel()
->getContainer()
->getParameter('mink.base_url');
}
// write functional tests
}
Base Behat\MinkBundle\Test\MinkTestCase
class provides an easy way to get
$mink
and specific session instances in your tests:
symfony
session will be used by default, sogetSession()
without parameters will returntest.client
enabled session for you:$session = $this->getMink()->getSession(); // or you can use the more verbose call: $session = $this->getMink()->getSession('symfony');
If you want to test your application with real HTTP requests, you should use
goutte
session instead (should be enabled inconfig_test.yml
first):$session = $this->getMink()->getSession('goutte');
If you want to test your app running in real browser - use
sahi
session (should be enabled inconfig_test.yml
first):$session = $this->getMink()->getSession('sahi');
If you want to test your app running in zombie.js browser - use
zombie
session (should be enabled inconfig_test.yml
first):$session = $this->getMink()->getSession('zombie');
After you've choosen needed session - use it to perform actions on your Symfony2 app:
$session
->visit($this->base.'/_behat/tests/page/page1');
$this->assertTrue(
$session->getPage()->hasContent('Page N1')
);
$session->getPage()->clickLink('p10');
For example, form specification with symfony
session will look like that:
public function testForms()
{
$session = $this->getMink()->getSession();
$session->visit($this->base.'/_behat/tests/form');
$page = $session->getPage();
// 3. FILL FORMS:
$page->fillField('name', 'ever');
$page->fillField('age', '23');
$page->selectFieldOption('speciality', 'programmer');
$page->pressButton('Send spec info');
// 4. ASSERT RESPONSE:
$this->assertTrue(
$page->hasContent('POST recieved')
);
$this->assertTrue(
$page->hasContent('ever is 23 years old programmer')
);
}