|
| 1 | +MinkBundle for Symfony2 |
| 2 | +======================= |
| 3 | + |
| 4 | +MinkBundle is Symfony2 bundle (plugin) created in order to give ability to |
| 5 | +write functional tests for Symfony2 applications. |
| 6 | + |
| 7 | +Mink Installation |
| 8 | +----------------- |
| 9 | + |
| 10 | +In order to be able to use MinkBundle, you need to install Mink first. |
| 11 | + |
| 12 | +Method #1 (PEAR) |
| 13 | +~~~~~~~~~~~~~~~~ |
| 14 | + |
| 15 | +The simplest way to install Mink is through PEAR: |
| 16 | + |
| 17 | +.. code-block:: bash |
| 18 | +
|
| 19 | + $ pear channel-discover pear.behat.org |
| 20 | + $ pear install behat/mink |
| 21 | +
|
| 22 | +Now you should include Mink's autoloader in your ``AppKernel::registerBundles()`` |
| 23 | +method (for ``test`` environment only): |
| 24 | + |
| 25 | +.. code-block:: php |
| 26 | +
|
| 27 | + <?php # app/AppKernel.php |
| 28 | + |
| 29 | + //... |
| 30 | +
|
| 31 | + if ('test' === $this->getEnvironment()) { |
| 32 | + // don't autoload Symfony2 classes, as they are |
| 33 | + // already loaded by the Symfony2 itself |
| 34 | + if (!defined('BEHAT_AUTOLOAD_SF2')) define('BEHAT_AUTOLOAD_SF2', false); |
| 35 | + require_once 'mink/autoload.php'; |
| 36 | + } |
| 37 | +
|
| 38 | + //... |
| 39 | +
|
| 40 | +Method #2 (Git) |
| 41 | +~~~~~~~~~~~~~~~ |
| 42 | + |
| 43 | +Add next lines to your ``deps`` file: |
| 44 | + |
| 45 | +.. code-block:: ini |
| 46 | +
|
| 47 | + [mink] |
| 48 | + git=https://github.com/Behat/Mink.git |
| 49 | + target=/behat/mink |
| 50 | +
|
| 51 | +By default, ``MinkBundle`` will use custom ``SymfonyDriver``. But if you want |
| 52 | +to use ``GoutteDriver``, you'll need to also add: |
| 53 | + |
| 54 | +.. code-block:: ini |
| 55 | +
|
| 56 | + [goutte] |
| 57 | + git=https://github.com/fabpot/Goutte.git |
| 58 | + target=/goutte |
| 59 | + [zend] |
| 60 | + git=https://github.com/zendframework/zf2.git |
| 61 | + target=/zend |
| 62 | +
|
| 63 | +And if you want to use ``SahiDriver``, you'll need to add another 2 lines: |
| 64 | + |
| 65 | +.. code-block:: ini |
| 66 | +
|
| 67 | + [buzz] |
| 68 | + git=https://github.com/kriswallsmith/Buzz.git |
| 69 | + target=/buzz |
| 70 | + [SahiClient] |
| 71 | + git=https://github.com/Behat/SahiClient |
| 72 | + target=/behat/sahi |
| 73 | +
|
| 74 | +Now run: |
| 75 | + |
| 76 | +.. code-block:: bash |
| 77 | +
|
| 78 | + bin/vendors install |
| 79 | +
|
| 80 | +in order to install all missing parts. |
| 81 | + |
| 82 | +It's time to setup your ``app/autoload.php``: |
| 83 | + |
| 84 | +.. code-block:: php |
| 85 | +
|
| 86 | + $loader->registerNamespaces(array( |
| 87 | + //... |
| 88 | + 'Behat\Mink' => __DIR__.'/../vendor/behat/mink/src', |
| 89 | +
|
| 90 | + // if you want to use GoutteDriver |
| 91 | + 'Goutte' => __DIR__.'/../vendor/goutte/src', |
| 92 | + 'Zend' => __DIR__.'/../vendor/zend/library', |
| 93 | +
|
| 94 | + // if you want to use SahiDriver |
| 95 | + 'Behat\SahiClient' => __DIR__.'/../vendor/behat/sahi/src', |
| 96 | + 'Buzz' => __DIR__.'/../vendor/buzz/lib', |
| 97 | + //... |
| 98 | + )); |
| 99 | +
|
| 100 | +Bundle Installation & Setup |
| 101 | +--------------------------- |
| 102 | + |
| 103 | +Now it's time to install and setup ``MinkBundle`` itself. |
| 104 | + |
| 105 | +1. Add ``MinkBundle`` repository address to your ``deps`` file: |
| 106 | + |
| 107 | + .. code-block:: ini |
| 108 | +
|
| 109 | + [MinkBundle] |
| 110 | + git=https://github.com/Behat/MinkBundle.git |
| 111 | + target=/bundles/Behat/MinkBundle |
| 112 | +
|
| 113 | +2. Add it to ``app/autoload.php``: |
| 114 | + |
| 115 | + .. code-block:: php |
| 116 | +
|
| 117 | + $loader->registerNamespaces(array( |
| 118 | + //... |
| 119 | + 'Behat\MinkBundle' => __DIR__.'/../vendor/bundles', |
| 120 | + //... |
| 121 | + )); |
| 122 | +
|
| 123 | +3. And to ``app/AppKernel.php``: |
| 124 | + |
| 125 | + .. code-block:: php |
| 126 | +
|
| 127 | + if ('test' === $this->getEnvironment()) { |
| 128 | + $bundles[] = new Behat\MinkBundle\MinkBundle(); |
| 129 | + } |
| 130 | +
|
| 131 | +4. Run ``bin/vendors install`` |
| 132 | + |
| 133 | +Bundle Configuration |
| 134 | +~~~~~~~~~~~~~~~~~~~~ |
| 135 | + |
| 136 | +Now, as you've setted up the bundle, you should configure it: |
| 137 | + |
| 138 | +.. code-block:: yaml |
| 139 | +
|
| 140 | + # app/config/config_test.yml |
| 141 | + mink: |
| 142 | + base_url: http://your_app.dev/app_test.php |
| 143 | +
|
| 144 | +By default, MinkBundle uses only ``SymfonyDriver`` session. If you want to use |
| 145 | +``GoutteDriver``, ``SahiDriver`` or ``ZombieDriver`` sessions - you should |
| 146 | +specify them in config explicitly: |
| 147 | + |
| 148 | +.. code-block:: yaml |
| 149 | +
|
| 150 | + # app/config/config_test.yml |
| 151 | + mink: |
| 152 | + base_url: http://your_app.dev/app_test.php |
| 153 | + goutte: ~ |
| 154 | + sahi: ~ |
| 155 | + zombie: ~ |
| 156 | +
|
| 157 | +Out of the box, Mink will use ``SymfonyDriver`` session as default one. This |
| 158 | +means that any session call without argument: |
| 159 | + |
| 160 | +.. code-block:: php |
| 161 | +
|
| 162 | + $this->getMink()->getSession()->...; |
| 163 | +
|
| 164 | +will be done against default Symfony2 ``test.client`` library. If you want to |
| 165 | +change this, use ``default_session`` configuration option: |
| 166 | + |
| 167 | +.. code-block:: yaml |
| 168 | +
|
| 169 | + # app/config/config_test.yml |
| 170 | + mink: |
| 171 | + base_url: http://your_app.dev/app_test.php |
| 172 | + default_session: goutte |
| 173 | + goutte: ~ |
| 174 | +
|
| 175 | +.. note:: |
| 176 | + |
| 177 | + Note, that we do our configuration in ``config_test.yml``. It's convenient |
| 178 | + way to configure MinkBundle, because ``test`` environment has all the |
| 179 | + needed requirements for Mink and default ``SymfonyDriver`` enabled out |
| 180 | + of the box. |
| 181 | + |
| 182 | +Available Options |
| 183 | +~~~~~~~~~~~~~~~~~ |
| 184 | + |
| 185 | +MinkBundle provides bunch of useful options for you to configure Mink's |
| 186 | +behavior. You can use them to make your testing experience even more |
| 187 | +smooth: |
| 188 | + |
| 189 | +* ``base_url`` - most important one. Defines base url for your application. |
| 190 | + Used heavily inside BehatBundle and can be used inside your test cases to |
| 191 | + be able to use relative paths in your web test cases. |
| 192 | + |
| 193 | +* ``default_session`` - defines session name, which will be used by default. It's |
| 194 | + ``symfony`` out of the box. |
| 195 | + |
| 196 | +* ``javascript_session`` - defines session name, which will be used for ``@javascript`` |
| 197 | + tagged Behat scenarios. |
| 198 | + |
| 199 | +* ``browser_name`` - specifies browser to be used with ``sahi`` session. |
| 200 | + |
| 201 | +* ``show_cmd`` - specified console command to run on "show" step in BehatBundle. |
| 202 | + For Mac OS, it could be something like ``open %s``. |
| 203 | + |
| 204 | +Writing your first test |
| 205 | +----------------------- |
| 206 | + |
| 207 | +Now, as you've configured ``MinkBundle``, you can use the special ``MinkTestCase``, |
| 208 | +provided with it as a base class for your tests: |
| 209 | + |
| 210 | +.. code-block:: php |
| 211 | +
|
| 212 | + <?php |
| 213 | +
|
| 214 | + namespace Acme\AcmeBundle\Tests; |
| 215 | +
|
| 216 | + use Behat\MinkBundle\Test\MinkTestCase; |
| 217 | +
|
| 218 | + class AcmeWebTestCase extends MinkTestCase |
| 219 | + { |
| 220 | + protected $base; |
| 221 | +
|
| 222 | + protected function setUp() |
| 223 | + { |
| 224 | + $this->base = $this->getKernel() |
| 225 | + ->getContainer() |
| 226 | + ->getParameter('behat.mink.base_url'); |
| 227 | + } |
| 228 | +
|
| 229 | + // write functional tests |
| 230 | + } |
| 231 | +
|
| 232 | +Base ``Behat\MinkBundle\Test\MinkTestCase`` class provides an easy way to get |
| 233 | +``$mink`` and specific session instances in your tests: |
| 234 | + |
| 235 | +1. ``symfony`` session will be used by default, so ``getSession()`` without |
| 236 | + parameters will return ``test.client`` enabled session for you: |
| 237 | + |
| 238 | + .. code-block:: php |
| 239 | +
|
| 240 | + $session = $this->getSession(); |
| 241 | + // or you can use the more verbose call: |
| 242 | + $session = $this->getSession('symfony'); |
| 243 | +
|
| 244 | +2. If you want to test your application with **real** HTTP requests, you should |
| 245 | + use ``goutte`` session instead (should be enabled in ``config_test.yml`` |
| 246 | + first): |
| 247 | + |
| 248 | + .. code-block:: php |
| 249 | +
|
| 250 | + $session = $this->getSession('goutte'); |
| 251 | +
|
| 252 | +3. If you want to test your app running in real browser - use ``sahi`` |
| 253 | + session (should be enabled in ``config_test.yml`` first): |
| 254 | + |
| 255 | + .. code-block:: php |
| 256 | +
|
| 257 | + $session = $this->getSession('sahi'); |
| 258 | +
|
| 259 | +3. If you want to test your app running in zombie.js browser - use ``zombie`` |
| 260 | + session (should be enabled in ``config_test.yml`` first): |
| 261 | + |
| 262 | + .. code-block:: php |
| 263 | +
|
| 264 | + $session = $this->getSession('zombie'); |
| 265 | +
|
| 266 | +After you've choosen needed session - use it to perform actions on your |
| 267 | +Symfony2 app: |
| 268 | + |
| 269 | +.. code-block:: php |
| 270 | +
|
| 271 | + $session |
| 272 | + ->visit($this->base.'/_behat/tests/page/page1'); |
| 273 | + $this->assertTrue( |
| 274 | + $session->getPage()->hasContent('Page N1') |
| 275 | + ); |
| 276 | +
|
| 277 | + $session->getPage()->clickLink('p10'); |
| 278 | +
|
| 279 | +For example, form specification with ``symfony`` session will look like that: |
| 280 | + |
| 281 | +.. code-block:: php |
| 282 | +
|
| 283 | + public function testForms() |
| 284 | + { |
| 285 | + $session = $this->getSession(); |
| 286 | +
|
| 287 | + $session->visit($this->base.'/_behat/tests/form'); |
| 288 | + $page = $session->getPage(); |
| 289 | +
|
| 290 | + // 3. FILL FORMS: |
| 291 | +
|
| 292 | + $page->fillField('name', 'ever'); |
| 293 | + $page->fillField('age', '23'); |
| 294 | + $page->selectFieldOption('speciality', 'programmer'); |
| 295 | + $page->pressButton('Send spec info'); |
| 296 | +
|
| 297 | + // 4. ASSERT RESPONSE: |
| 298 | +
|
| 299 | + $this->assertTrue( |
| 300 | + $page->hasContent('POST recieved') |
| 301 | + ); |
| 302 | + $this->assertTrue( |
| 303 | + $page->hasContent('ever is 23 years old programmer') |
| 304 | + ); |
| 305 | + } |
0 commit comments