Php 类的对象无法转换为int

Php 类的对象无法转换为int,php,laravel,unit-testing,phpunit,Php,Laravel,Unit Testing,Phpunit,我想在我的存储库中为save方法编写phpunit测试。我的回购代码是: public function saveCustomer(Custom $custom) { try { $custom->save(); return array( 'status' => true, 'customerId' => $custom->getId() ); }

我想在我的存储库中为save方法编写phpunit测试。我的回购代码是:

public function saveCustomer(Custom $custom)
{
    try
    {
        $custom->save();

        return array(
            'status' => true,
            'customerId' => $custom->getId()
        );
    }
    catch(\Exception $e)
    {
        return array(
            'status' => false,
            'customerId' => 0
        );
    }
 }
我写了这个测试:

public function testSaveNewUye()
{
    $request = array(
        'email' => 'www@www.com',
        'phone' => '555 555 555',
        'password' => '34636'
    );
    $repo = new CustomerRepository();
    $result_actual = $this->$repo->saveCustomer($request);
    $result_expected = array(
        'status' => true,
        'customerId' => \DB::table('custom')->select('id')->orderBy('id', 'DESC')->first() + 1
    );
    self::assertEquals($result_expected, $result_actual);
}
我得到了下面给出的错误:

ErrorException:类App\CustomerRepository的对象无法转换为int

你能帮我吗?

问题在这里:

$repo = new CustomerRepository();
$result_actual = $this->$repo->saveCustomer($request);
分配和使用的变量不同

试着这样做:

$this->repo = new CustomerRepository();
//     ^------- assign to `$this`
$result_actual = $this->repo->saveCustomer($request);
//                      ^------- remove `$`
当执行
$this->$repo->
时,PHP尝试将(对象)
$repo
转换为字符串
$this->(对象)->
,但该字符串不起作用

然后,这里出现第二个错误:

\DB::table('custom')->select('id')->orderBy('id','DESC')->first()+1

从数据库中可以得到一个对象(instanceof
stdClass
),您不能简单地
+1

整个事情大概是这样的

\DB::table('custom')->select('id')->orderBy('id', 'DESC')->first()->id + 1

(从返回的对象中,您需要属性
id

挑剔:这不是一个单元测试,而是一个集成测试,因为您不模拟数据库内容;类stdClass的对象无法转换为int