我正在使用PHP 7.4上的PHPUnit 6.5.13和Laravel 5.5。我最近从PHP 7.2升级到了7.4,似乎这触发了错误。
在我的测试中,我使用
OrderReSent看起来像这样(我尝试注释掉broadcastOn和remove InteractsWithSockets的使用,但结果没有改变):
堆栈跟踪看起来像这样,让我相信 Mockery 是罪魁祸首:
在我的测试中,我使用
$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和remove 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');
}
}
我只在Illuminate\Broadcasting\PrivateChannel
中看到了parent::__construct
的调用,这个类继承自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);
}
}
堆栈跟踪看起来像这样,让我相信 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