Dr.Knowitall
Dr.Knowitall

Reputation: 10488

Why is symfony2 functional test client->request() not getting successful response()?

So I've decided to delve into phpunit testing and I've shamefully written out my php code before writing my test. Anyway, I'm just writing a very simple test that tells me if I actually found the correct web page. Unfortunately my one assertion test keeps failing. I know my route "/login" is correct because when I navigate to localhost/index.php/login (where index.php is a link to app_dev.php), the page comes up correctly. Bellow is my routing.php file:

caremonk_mainsite_login:
    pattern:  /login
    defaults: { _controller: CaremonkMainSiteBundle:Security:login }
    requirements:
        _method: POST|GET

caremonk_mainsite_login_check:
    pattern:  /login_check
    requirements:
        _method: POST|GET

caremonk_mainsite_signup:
    pattern:  /signup
    defaults: { _controller: CaremonkMainSiteBundle:CreateUser:signup }
    requirements:
        _method: POST|GET

caremonk_mainsite_logout:
    pattern:  /logout
    defaults: { _controller: CaremonkMainSiteBundle:Security:logout}
    requirements:
        _method: POST|GET

caremonk_mainsite_post_blog:
    pattern:  /post_blog
    defaults: { _controller: CaremonkMainSiteBundle:UserEvents:post }
    requirements:
        _method: POST|GET

caremonk_mainsite_my_profile:
    pattern:  /my_profile_edit
    defaults: { _controller: CaremonkMainSiteBundle:UserEvents:editProfile }
    requirements:
        _method: POST|GET

caremonk_mainsite_activate:
    pattern:  /activate/{username}/{token}
    defaults: { _controller: CaremonkMainSiteBundle:CreateUser:activateAccount }
    requirements:
        _methods: GET

caremonk_mainsite_password_reset_request:
    pattern:  /reset_password/
    defaults: { _controller: CaremonkMainSiteBundle:Security:passwordResetRequest }
    requirements:
        _methods: GET | POST

caremonk_mainsite_reset_password_email:
    pattern: /reset_password_email/{username}/{resetPasswordToken}
    defaults: { _controller: CaremonkMainSiteBundle:Security:sendNewPassword }
    requirements:
        _methods: GET

caremonk_mainsite_change_password:
    pattern: /change_password
    defaults: { _controller: CaremonkMainSiteBundle:Security:changePassword }
    requirements:
        _methods: GET | POST

caremonk_mainsite_home:
    pattern:  /
    defaults: { _controller: CaremonkMainSiteBundle:Home:index }
    requirements:
        _methods: GET

Anyway bellow is the test code that keeps failing:

<?php

namespace Caremonk\MainSiteBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class SecurityControllerFunctionalTest extends WebTestCase
{
    public function testIndex()
    {
        $client = static::createClient();

        // I've done many tests
        // I've tried the following request with all failed results
        // $crawler = $client->request('GET', 'index.php/login');
        // $crawler = $client->request('GET', 'http://localhost/indpex.php/login');
        // $crawler = $client->request('GET', 'localhost/index.php/login');
        // You get the idea 

        $crawler = $client->request('GET', '/login');
        $this->assertTrue($client->getResponse()->isSuccessful());
    }
}

My routing.yml and routing_dev.yml files are shown bellow

#routing_dev.yml
_wdt:
    resource: "@WebProfilerBundle/Resources/config/routing/wdt.xml"
    prefix:   /_wdt

_profiler:
    resource: "@WebProfilerBundle/Resources/config/routing/profiler.xml"
    prefix:   /_profiler

_configurator:
    resource: "@SensioDistributionBundle/Resources/config/routing/webconfigurator.xml"
    prefix:   /_configurator

_main:
    resource: routing.yml

#routing.yml
caremonk_main_site:
    resource: "@CaremonkMainSiteBundle/Resources/config/routing.yml"
    prefix:   /

Upvotes: 0

Views: 3232

Answers (1)

John Cartwright
John Cartwright

Reputation: 5084

You prefixed your imported routes with a "/", and your routes path start with a "/".

Normally I would prefix my routes with something more meaningful (and does not end with a "/") or remove the "/" from your imported routes.

Running the following command should give you insight to how your routes are registered.

app/console debug:router

Upvotes: 3

Related Questions