测试过滤器在laravel 4 [英] testing filters in laravel 4

查看:61
本文介绍了测试过滤器在laravel 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是laravel的新手.我正在尝试测试身份验证适用于我的网站,并且我想在一个测试案例中测试身份验证过程.我创建了一个内存中的sqlite数据库,我创建了一个新的User并使用->save()雄辩的方法将其存储在数据库中.我已经设置了一个身份验证过滤器,该过滤器检查数据库中的用户名,并取决于它允许用户登录或返回无效的凭据"

I'm new to laravel. I am trying to test that authentication works for my website and I want to test the authentication process in a test case. I create a in-memory sqlite database, I create a new User and use ->save() method of eloquent to store it in the database. I have setup an authentication filter which checks for the username in the database and depending on that it either allows the user to login or returns "invalid credentials"

//我的UserTest.php文件:

// my UserTest.php file :

class UserTest extends TestCase {

public function testUsernameIsNotRequired()
{
    // Create a new User
    $user = new User;
    $user->username = "phil@ipbrown.com";
    $user->password = "123456";
    //$user->password_confirmation = "password";

    // User should save
    $this->assertTrue($user->save());

    // Save the errors
    $password = $user->getAuthPassword();

    // There should be 0 error
    $this->assertEquals("123456",$password);
    $this->seed();

    $this->be($user);

    $this->assertTrue(Redirect::route('authFilter'));
}
}

只是为了让您知道测试完成后内存数据库已丢失,因为与数据库的所有连接都已丢失,因此我想检查是否正确插入了保存到数据库中的用户,并且我想检查第二个数据库.检查我是否可以使用该新用户的信息登录到我的网站.

just to let you know that the in-memory db is lost once the test is complete as all the connections to it are lost so I want to check that the user that I saved to my db is properly inserted and second I want to check if I can login to my website using the information of this new user.

//我的filters.php文件:

// my filters.php file :

Route::filter('auth', function()
{
    if (Auth::guest()) return Redirect::guest('login');
});


Route::filter('auth.basic', function()
{
    return Auth::basic('username');
});

Route::filter('guest', function()
{
    if (Auth::check()) return Redirect::to('/');
});

Route::filter('csrf', function()
{
    if (Session::token() != Input::get('_token'))
    {
        throw new Illuminate\Session\TokenMismatchException;
    }
});

我正在尝试将过滤器附加到路由,以便可以在测试期间重定向到该路由,并调用auth.basic过滤器,以便可以测试过滤器,我知道我做错了很多事,所以请随时纠正您遇到的任何错误

I was trying to attach a filter to a route so that I can redirect to the route during my test and which calls the auth.basic filter so I can test my filter, I know Im doing a lot of things wrong so please feel free to correct any mistakes that you come accross

我的routes.php文件:>

my routes.php file :>

Route::get('/', function()
{
    return View::make('hello');
});

Route::get('/authtest', array('as'=>'/authtest','before' => 'auth.basic', function()
{
     return View::make('hello');
}));

Route::group(array('prefix' => 'api/v1', 'before' => 'auth.basic'), function()
{
    Route::resource('url', 'UrlController');
});

Route::get('authFilter', array('as'=>'authFilter','before' => 'auth.basic', function()
{
    return Auth::basic('username');
}));

我还有一个uri控制器,其中包含我网站的所有页面

I also have a uri controller which has all the pages for my website

这是我目前创建uri控制器所遵循的

this is what I followed to create my uri controller for the moment

我需要一个创建用户的测试用例,将其存储到内存数据库中,然后使用该用户信息进行身份验证.如果有人知道laravel进行过滤器测试,请告诉我,我查阅了测试过滤器的文档,但我想它没有很好地记录在案.

I need a test case that creates a user stores it into the in-memory database and then authenticates using that users information. If any one knows laravel testing for filters please let me know I looked up the documentation for testing filters but I guess it is not well documented.

谢谢

推荐答案

在Laravel 4的测试中禁用了过滤器.

Filters are disabled in tests on Laravel 4.

您可以在测试中使用Route::enableFilters()强制打开过滤器.

You can use Route::enableFilters() in your test to force the filters on.

您可以阅读有关为什么/为什么不测试过滤器的各种讨论;

You can read up on the various discussions about why/why not to test filters;

  • https://github.com/laravel/framework/issues/344
  • https://github.com/laravel/framework/issues/766
  • http://forums.laravel.io/viewtopic.php?id=7404
  • https://github.com/laravel/framework/issues/682

这篇关于测试过滤器在laravel 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆