phpunit,laravel:当当前 class 作用域没有 parent 时,无法使用 "parent"
phpunit, laravel: Cannot use "parent" when current class scope has no parent
我在 PHP7.4 上使用 PHP6.5.13 单元和 Laravel 5.5。我最近从 PHP 7.2 升级到 7.4。似乎这触发了错误。
在我的测试中,我使用 $this->expectsEvents
来测试事件是否被触发。测试 class 看起来有点像这样:
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(它是 child class,所以我不明白为什么会抛出这个错误):
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
我有同样的问题 - 结果 mockery/mockery
在我的 composer.json
中设置为版本 0.9
。将 mockery/mockery
升级到版本 1.3
解决了我的问题。
相关composer.json
片段:
"mockery/mockery": "~1.3.0",
"phpunit/phpunit": "~8.0",
尝试设置相同的版本 运行 composer update
可能的罪魁祸首是 setUp
和 tearDown
的新语法要求。
Source
可能是缺少 return 类型造成的,即 void
。
例如,更改为:
public function setUp()
{
parent::setUp();
}
public function tearDown()
{
parent::tearDown();
}
对此:
public function setUp() : void
{
parent::setUp();
}
public function tearDown() : void
{
parent::tearDown();
}
注意:我发现这个问题是在单元测试中查找错误消息,奇怪的是它与 m::close()
有关,所以我描述的问题与原始问题不同,但我的回答将是相关的。
我在 PHP7.4 上使用 PHP6.5.13 单元和 Laravel 5.5。我最近从 PHP 7.2 升级到 7.4。似乎这触发了错误。
在我的测试中,我使用 $this->expectsEvents
来测试事件是否被触发。测试 class 看起来有点像这样:
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(它是 child class,所以我不明白为什么会抛出这个错误):
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
我有同样的问题 - 结果 mockery/mockery
在我的 composer.json
中设置为版本 0.9
。将 mockery/mockery
升级到版本 1.3
解决了我的问题。
相关composer.json
片段:
"mockery/mockery": "~1.3.0",
"phpunit/phpunit": "~8.0",
尝试设置相同的版本 运行 composer update
可能的罪魁祸首是 setUp
和 tearDown
的新语法要求。
Source
可能是缺少 return 类型造成的,即 void
。
例如,更改为:
public function setUp()
{
parent::setUp();
}
public function tearDown()
{
parent::tearDown();
}
对此:
public function setUp() : void
{
parent::setUp();
}
public function tearDown() : void
{
parent::tearDown();
}
注意:我发现这个问题是在单元测试中查找错误消息,奇怪的是它与 m::close()
有关,所以我描述的问题与原始问题不同,但我的回答将是相关的。