在代码中提交表单时,验证测试auth中断

时间:2016-01-30 21:07:23

标签: 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->getSpecPonudaImageSport1());
$I->selectOption('form select[name=offer_brand]', 1);
$I->click('Submit');
//$I->see('Success: Offer My Offer created.');

authenticateUserByEmail()正在做以下事情:

$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());

这是在控制台中打印:

bool(false)

无论如何,此行删除了错误" PHP Fatal error: Call to a member function hasSupplier() on a non-object"仍然存在。

我使用Zizaco Entrust包进行基于角色的权限。这是我的用户类:

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());

这是打印到控制台:

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
}

表单子目录会破坏某些东西。知道如何解决问题吗?

1 个答案:

答案 0 :(得分:0)

在致电hasSupplier()之前,您无法假设用户已登录,请确保Auth::user()未返回NULL / false

而不是

$this->isUserSupplier = Auth::user()->hasSupplier();

DO

$this->isUserSupplier = Auth::check() ? Auth::user()->hasSupplier() : false;

您确定要检查的电子邮件是否存在于数据库中? 你可以dd($user)吗?