【问题标题】:phpunit, laravel: Cannot use "parent" when current class scope has no parentphpunit,laravel:当当前类范围没有父级时不能使用“父级”
【发布时间】:2020-04-09 00:10:19
【问题描述】:

我在 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);
    }
}

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

【问题讨论】:

    标签: php laravel-5 phpunit mockery php-7.4


    【解决方案1】:

    我遇到了同样的问题 - 原来 mockery/mockery 在我的 composer.json 中设置为版本 0.9。将mockery/mockery 升级到版本1.3 为我解决了这个问题。

    相关composer.json片段:

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

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

    【讨论】:

    • 我在想这个,但我们目前正在使用几个不同的 PHP 版本,即 7.0、7.2 和一些 7.3。我希望让我们升级到 7.4,但有点担心向后兼容性,因为我们还没有准备好升级我们的 7.0 站。我会试着用结果回复你:p
    • 测试在 PHP 7.2、7.3 和 7.4 上运行良好。这似乎已经解决了。非常感谢!
    【解决方案2】:

    可能的罪魁祸首是setUptearDown 的新语法要求。 Source

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-13
      • 1970-01-01
      相关资源
      最近更新 更多