我在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的唯一地方是在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);
}
}
堆栈跟踪看起来像这样,使我相信嘲笑是罪魁祸首:
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
我有同样的问题-事实证明,我的mockery/mockery
中将0.9
设置为版本composer.json
。 mockery/mockery
升级到1.3
版本为我解决了这个问题。
相关的composer.json
片段:
"mockery/mockery": "~1.3.0",
"phpunit/phpunit": "~8.0",
尝试设置相同的版本并运行composer update