Laravel 在codeception验收测试中提交表格时,授权中断
我正在laravel 4.2中编写codecept验收测试,我对以下代码有问题:Laravel 在codeception验收测试中提交表格时,授权中断,laravel,codeception,Laravel,Codeception,我正在laravel 4.2中编写codecept验收测试,我对以下代码有问题: $I->authenticateUserByEmail($I, 'pera@lozac.com'); $I->amOnPage('/admin/offer/create-specijalna-ponuda'); $I->fillField('input[name=offer_name]', 'My Offer'); $I->attachFile('input[name=offer_image
$I->authenticateUserByEmail($I, 'pera@lozac.com');
$I->amOnPage('/admin/offer/create-specijalna-ponuda');
$I->fillField('input[name=offer_name]', 'My Offer');
$I->attachFile('input[name=offer_image]', $I->getSpecPonudaImageSport1());
$I->selectOption('form select[name=offer_brand]', 1);
$I->click('Submit');
//$I->see('Success: Offer My Offer created.');
authenticateUserByMail()
正在执行以下操作:
$user = \User::where('email','=',$email)->first();
$I->amLoggedAs($user);
单击“提交”按钮之前,所有操作都正常。当该行出现时,我在处理此post方法的控制器内收到以下错误:
PHP Fatal error: Call to a member function hasSupplier() on a non-object
这是发生错误的一行:
$this->isUserSupplier = Auth::user()->hasSupplier();
如果我马上把这句话放在前面:
dd(Auth::user()->hasSupplier());
这是控制台中的printend:
bool(false)
无论如何,当这一行删除错误时,“PHP致命错误:对非对象调用成员函数hasSupplier()”
”仍然存在
我正在使用基于角色的权限的包。这是我的用户类:
use Zizaco\Confide\ConfideUser;
use Zizaco\Confide\ConfideUserInterface;
use Zizaco\Entrust\HasRole;
class User extends Eloquent implements ConfideUserInterface
{
use ConfideUser;
use HasRole;
public function supplier(){
return $this->hasOne('Supplier', 'id', 'supplier_id');
}
public function hasSupplier(){
$supplier = $this->supplier;
return !is_null($supplier);
}
}
如果我在控制器中的错误行之前放置以下行以检查用户:
dd(Auth::user());
这是控制台中的printend:
object(User)#13077 (21) {
["connection":protected]=>
NULL
["table":protected]=>
NULL
["primaryKey":protected]=>
string(2) "id"
["perPage":protected]=>
int(15)
["incrementing"]=>
bool(true)
["timestamps"]=>
bool(true)
["attributes":protected]=>
array(18) {
["id"]=>
int(2)
["username"]=>
string(6) "pera"
["email"]=>
string(19) "pera@lozac.com"
["password"]=>
string(60) "$2y$10$zAOANTPOjW7BBA010i3TrexBe23nhEuQnlK0h/Y1m1XVD8kCaEIEC"
["first_name"]=>
string(6) "Pera"
["last_name"]=>
string(9) "Lozac"
["phone"]=>
NULL
["address"]=>
NULL
["age"]=>
NULL
["active"]=>
int(1)
["weight"]=>
int(1000)
["notify"]=>
int(1)
["remember_token"]=>
string(60) "g6S5hjtz7TbEB96DBmBZjaBNTZfmtKaV4zObY3TssDSFxaHnL98myqzfebmU"
["confirmation_code"]=>
string(32) "fd5a00cd497c2bf5be6f4df48fb49719"
["confirmed"]=>
int(0)
["created_at"]=>
string(19) "2016-01-30 13:31:46"
["updated_at"]=>
string(19) "2016-01-30 22:37:19"
["supplier_id"]=>
NULL
}
["original":protected]=>
array(18) {
["id"]=>
int(2)
["username"]=>
string(6) "pera"
["email"]=>
string(19) "pera@lozac.com"
["password"]=>
string(60) "$2y$10$zAOANTPOjW7BBA010i3TrexBe23nhEuQnlK0h/Y1m1XVD8kCaEIEC"
["first_name"]=>
string(6) "Pera"
["last_name"]=>
string(9) "Lozac"
["phone"]=>
NULL
["address"]=>
NULL
["age"]=>
NULL
["active"]=>
int(1)
["weight"]=>
int(1000)
["notify"]=>
int(1)
["remember_token"]=>
string(60) "g6S5hjtz7TbEB96DBmBZjaBNTZfmtKaV4zObY3TssDSFxaHnL98myqzfebmU"
["confirmation_code"]=>
string(32) "fd5a00cd497c2bf5be6f4df48fb49719"
["confirmed"]=>
int(0)
["created_at"]=>
string(19) "2016-01-30 13:31:46"
["updated_at"]=>
string(19) "2016-01-30 22:37:19"
["supplier_id"]=>
NULL
}
["relations":protected]=>
array(0) {
}
["hidden":protected]=>
array(0) {
}
["visible":protected]=>
array(0) {
}
["appends":protected]=>
array(0) {
}
["fillable":protected]=>
array(0) {
}
["guarded":protected]=>
array(1) {
[0]=>
string(1) "*"
}
["dates":protected]=>
array(0) {
}
["touches":protected]=>
array(0) {
}
["observables":protected]=>
array(0) {
}
["with":protected]=>
array(0) {
}
["morphClass":protected]=>
NULL
["exists"]=>
bool(true)
["errors"]=>
NULL
}
提交表格会破坏某些东西。知道如何解决问题吗?在调用
hasSupplier()
之前,不能假定用户已登录,请确保Auth::user()
没有返回NULL/false
而不是
$this->isUserSupplier=Auth::user()->hasSupplier()代码>
做
$this->isUserSupplier=Auth::check()?Auth::user()->hasSupplier():false代码>
您确定您检查的电子邮件存在于数据库中吗?
你能不能dd($user)
?我已经用所需的信息编辑了这个问题。用户存在,但连接为空,表为空。如果执行dd(Auth::User())
操作,您会得到什么?刚才注意到,即使删除调用,错误仍然存在,这意味着错误不是来自此行,在代码的其他地方,您对非对象变量调用了hasSupplier
。从浏览器运行时,此代码正常工作。在CodException上下文中,auth和所有其他测试在提交表单之前都有效。然后休息。似乎提交表单以某种方式注销了用户。如果您在提交后立即再次添加对authenticateUserByMail
的调用,是否可以正常工作?