phpunit,laravel:当前类范围中没有父级时,不能使用“父级”

问题描述 投票:2回答:1

我在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
php laravel-5 phpunit mockery php-7.4
1个回答
0
投票

我有同样的问题-事实证明,我的mockery/mockery中将0.9设置为版本composer.jsonmockery/mockery升级到1.3版本为我解决了这个问题。

相关的composer.json片段:

        "mockery/mockery": "~1.3.0",
        "phpunit/phpunit": "~8.0",

尝试设置相同的版本并运行composer update

© www.soinside.com 2019 - 2024. All rights reserved.