6

我在 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
4

2 回答 2

7

我有同样的问题 - 结果mockery/mockery0.9在我的composer.json. 升级mockery/mockery到版本1.3为我解决了这个问题。

相关composer.json片段:

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

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

于 2020-02-02T09:53:32.783 回答
1

setUp可能的罪魁祸首是由于对and的新语法要求tearDown来源

这可能是由于缺少返回类型(即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(),所以我描述的问题与原始问题不同,但我的回答将是相关的。

于 2020-06-03T05:11:11.830 回答