我在 PHP 7.4 上使用 PHPUnit 6.5.13 和 Laravel 5.5。我最近从 PHP 7.2 升级到 7.4。似乎触发了错误。
在我的测试中,我使用$this->expectsEvents
它来测试事件是否被触发。测试类看起来有点像这样:
namespace Tests\Feature;
use Tests\TestCase;
use App\Events\OrderReSent;
class MyEventTest extends TestCase {
/** @test */
public function authenticated_client_can_resend()
{
$this->expectsEvents(OrderReSent::class); // there is some more code but this is the line that returns the error
}
}
OrderReSent 看起来像这样(我尝试注释掉 broadcastOn 并删除 InteractsWithSockets 使用,结果没有变化):
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class OrderReSent
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $invoiceId;
public function __construct($invoiceId)
{
$this->invoiceId = $invoiceId;
}
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
我看到调用 parent::__construct 的唯一地方是 in Illuminate\Broadcasting\PrivateChannel
,它扩展了 Illuminate\Broadcasting\Channel (它是一个子类,所以我不明白为什么它会抛出这个错误):
namespace Illuminate\Broadcasting;
class PrivateChannel extends Channel
{
/**
* Create a new channel instance.
*
* @param string $name
* @return void
*/
public function __construct($name)
{
parent::__construct('private-'.$name);
}
}
stacktrace 看起来像这样,让我相信 Mockery 是罪魁祸首:
1) Tests\Feature\MyEventTest::authenticated_client_can_resend
ErrorException: Cannot use "parent" when current class scope has no parent
/project-root/vendor/mockery/mockery/library/Mockery/Loader/EvalLoader.php:16
/project-root/vendor/mockery/mockery/library/Mockery/Loader/EvalLoader.php:16
/project-root/vendor/mockery/mockery/library/Mockery/Container.php:219
/project-root/vendor/mockery/mockery/library/Mockery.php:89
/project-root/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MocksApplicationServices.php:99
/project-root/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/MocksApplicationServices.php:54
/project-root/tests/Feature/MyEventTest.php:29