0

I've been trying to write simple login test and this is my action ( the data should log me in ):

 $client = self::createClient();

 $crawler = $client->request('GET', '/login');

 $form = $crawler->selectButton('Submit')->form();

 $form['_username'] = 'admin';
 $form['_password'] = 'adminpass';

 $client->submit($form);
 //$crawler = $client->followRedirect();

 $this->assertEquals('Portal\FrontendBundle\Controller\ProfileController::indexAction', $client->getRequest()->attributes->get('_controller'));

And it stops on checkAction:

Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'Portal\FrontendBundle\Controller\ProfileController::indexAction'
+'FOS\UserBundle\Controller\SecurityController::checkAction'
C:\xampp\htdocs\portal\src\Portal\FrontendBundle\Tests\Controller\DefaultControllerTest.php:24

If I'm following redirect than it comes back to loginAction.

I'm new to Unit tests actually and I don't really know how I'm supposed to write it properly.

Nicolai Fröhlich
  • 51,330
  • 11
  • 126
  • 130
mmmm
  • 3,768
  • 9
  • 36
  • 63
  • You're acutally writing a *functional* test and not a unit test there :) – Nicolai Fröhlich Feb 01 '14 at 14:42
  • oops, well I guess it's easy to notice that I'm not familiar with tests :) do You know maybe what am I doing wrong? – mmmm Feb 01 '14 at 14:48
  • is your login-form working regularly? if **[`SecurityController::checkAction`](https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Controller/SecurityController.php#L70)** is invoked - you usually have a misconfigured firewall. That action only throws the exception **You must configure the check path to be handled by the firewall using form_login in your security firewall configuration** . – Nicolai Fröhlich Feb 01 '14 at 15:06
  • btw. you can have a look at **[my answer here](http://stackoverflow.com/a/17406584/1832745)** for an explanation of the `check_path` configuration. That will probably guide you into the right direction. – Nicolai Fröhlich Feb 01 '14 at 15:10
  • it is configured, also - I can log in via the website, so I think something is wrong with the test? Or with my understanding how it works. – mmmm Feb 01 '14 at 15:14
  • The test is usually executed in the `test` environment. Check your configuration - mainly that `config_test.yml` actually exists and imports `config.yml` which in turn imports `security.yml`. This means a working form in the prod/dev environment doesn't necessarily mean your test environment has the same configuration. – Nicolai Fröhlich Feb 01 '14 at 15:16
  • it does exist and also import config_dev. Btw my check path has value "fos_user_security_check" - I don't know if that changes anything? – mmmm Feb 01 '14 at 15:26
  • did you maybe import FOSUserbundle's routing in `routing_dev.yml` instead of `routing.yml` ? try `app/console router:debug --env=test` – Nicolai Fröhlich Feb 01 '14 at 15:44
  • nope.. it is in routing.yml. Also Your command shows it is there. – mmmm Feb 01 '14 at 17:11

1 Answers1

0

are you sure that you are testing it in the correct environment? is your user "admin" stored in the database? you should follow the redirect I think ... maybe the login just fails because of bad credentials... try to validate it with:

$this->assertTrue($crawler->filter('html:contains("bad credentials")')->count() > 0);

or something similar...

Andrej Sramko
  • 833
  • 7
  • 18