【问题标题】:Object of class App\Billing\FakePaymentGateway could not be converted to stringApp\Billing\FakePaymentGateway 类的对象无法转换为字符串
【发布时间】:2020-01-08 06:10:18
【问题描述】:

有 1 个错误:

1) 查看ConcertListingTest::customer_can_purchase_concert_tickets ErrorException: 类 App\Billing\FakePaymentGateway 的对象无法转换为字符串

正在做测试,但不知道它在抱怨什么......

<?php

namespace App\Billing;

class FakePaymentGateway implements PaymentGateway
{

    private $charges;

    public function __construct()
    {
        $this->charges = collect();
    }


    public function getValidTestToken()
    {
        return "valid-token";
    }



    public function charge($amount, $token)
    {
        $this->charges[] = $amount;
    }

    public function totalCharges()
    {
        return $this->charges->sum();
    }

}

这是它的另一部分 购买TiketTest.php

<?php
    class ViewConcertListingTest extends TestCase
    {
    use DatabaseMigrations;
    /**
    * @test
    *
    * @return void
    */
    public function customer_can_purchase_concert_tickets()
    {
    $paymentGateway = new FakePaymentGateway;

    $this->app->instance(PaymentGateway::class, $paymentGateway);
    //dd($paymentGateway);

    // Arrange

    //Create a concert
    $concert = factory(Concert::class)->create(['ticket_price' => 3250 ]);


    // Act

    // View the concert listing

    $response = $this->json('POST', "/concerts/{$concert->id}/orders", [
    'email' => 'john@example.com',
    'ticket_quantity' => 3,
    'payment_token' => $paymentGateway->getValidTestToken(),
    ] );

    $response->assertStatus(201);


    $this->assertEquals( 9750 , $paymentGateway->totalCharges() );

    $order = $concert->orders()->where('email')->first();

    $this->$this->assertNotNull($order);

    $this->assertEquals( 3, $order->tickets->count() );
    }


    }

第 18 行带有注释的订单控制器

class ConcertsOrdersController extends Controller
{

    private $paymentGateway;

    public function __construct(PaymentGateway $paymentGateway)
    {
        $this->$paymentGateway = $paymentGateway; // Line 18
    }

    //
    public function store($concertId)
    {
        $concert = Concert::find($concertId);

        $ticketQuantity = request('ticket_quantity');

        $amount = $ticketQuantity * $concert->ticket_price;

        $token = request('payment_token');

        $this->paymentGateway->charge($amount, $token);


$concert->orders()->create(['email' => request('email')]);


        return response()->json([], 201);
    }
}

预计通过结果

【问题讨论】:

  • 1.你的测试文件在哪里。 2.发布完整的错误消息。它包含发生错误的行,它不是随机数,它意味着什么。
  • 看到错误堆栈跟踪的第一行指向您的控制器吗?发布您的控制器(请指出第 18 行)
  • 感谢您的时间,因为我很困惑,这方面的教程视频似乎较旧,并且与它们的外观不符......
  • ``` interface PaymentGateway { public function charge($amount, $token); } ```

标签: php laravel object payment-gateway


【解决方案1】:

您的错误是ConcertsOrdersController 的第 18 行上您的属性名称之前的一个简单的附加(不需要的)$

    $this->$paymentGateway = $paymentGateway; // Line 18

当你这样做时,你告诉 PHP 以字符串形式获取 $paymentGateway 的值,并设置一个使用该字符串值调用的方法。

例如:

$paymentGateway = 'test';
$this->$paymentGateway = 123;
echo $this->$paymentGateway; // 123
echo $this->test; // 123

发生错误是因为您的 $paymentGateway 变量是对象而不是字符串。

实际上,您在第 18 行添加了一个额外的 $。它应该如下所示:

    $this->paymentGateway = $paymentGateway; // Line 18

【讨论】:

  • 你的明星,我太烦了,没注意到非常感谢
猜你喜欢
  • 2021-06-16
  • 2023-01-05
  • 1970-01-01
  • 2022-01-05
  • 2015-07-27
  • 2012-04-29
  • 2011-04-06
  • 2011-11-30
相关资源
最近更新 更多