Testing 如何在Laravel中设置测试前cookie?
我需要基于cookie的存在来测试特定的行为,在发送请求(或访问页面)之前如何设置cookie?就在下面失败的那一刻,它的行为就像什么都没有设置一样Testing 如何在Laravel中设置测试前cookie?,testing,cookies,laravel-5,phpunit,Testing,Cookies,Laravel 5,Phpunit,我需要基于cookie的存在来测试特定的行为,在发送请求(或访问页面)之前如何设置cookie?就在下面失败的那一刻,它的行为就像什么都没有设置一样 $this->actingAs($user) ->withSession(['user' => $user, 'profile' => $profile]) ; @setcookie( 'locale_lc', "fr", time() + 60 * 60 * 24 * 900, '/', "domain.co
$this->actingAs($user)
->withSession(['user' => $user, 'profile' => $profile]) ;
@setcookie( 'locale_lc', "fr", time() + 60 * 60 * 24 * 900, '/', "domain.com", true, true) ;
$this->visit('/profile') ;
或
问题在于cookies的设置和读取。无论是
@setcookie
还是$\u COOKIE
都不能在测试上下文中工作。使用cookie数组的方法makeRequest
就是正确的方法
然而
读取脚本(控制器、中间件)必须使用Laravel的$request->cookie()
方法,而不是直接尝试使用$\u cookie
访问它。在我的例子中,cookie需要被我们域上的另一个应用程序读取,因此我还必须禁用该特定cookie的加密,这可以在EncryptCookies.php
加密cookies
<?php
protected $except = [
'locale_lc'
];
如果您没有禁用加密,这仍然有效吗?
<?php
protected $except = [
'locale_lc'
];
<?php
$cookie = ['locale_lc' => 'fr'] ;
$this->actingAs($user)
->withSession(['user' => $user, 'profile' => $profile])
->makeRequest('GET', '/profile', [], $cookie) ;
<?php
public function handle($request, Closure $next){
if($request->cookie('locale_lc')){...}
}