Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempting to mock a method with static return type in PHP 8 results in ParseError #1155

Closed
ramsey opened this issue Jan 9, 2022 · 1 comment · Fixed by #1157
Closed

Comments

@ramsey
Copy link

ramsey commented Jan 9, 2022

PHP 8.0 introduced the static return type.

When using Mockery to mock a method with a return type hint of static, PHP generates a ParseError, with a message similar to the following:

ParseError : syntax error, unexpected token "static", expecting identifier /private/var/folders/jt/dm973jds5b33ffvl0x2b8f940000gn/T/MockeryYyJCuE:1

The generated file contains the following:

<?php class static {} 

#1149 describes a similar issue with the mixed return type.

To Reproduce

💡 NOTE: To help, I've created a repository so you can quickly reproduce the issue: https://github.com/ramsey/mockery-static-return-type-bug

Create a class with a method on it that has a static return type hint:

<?php

namespace Foo;

class Foo
{
    public function setFoo(string $value): static
    {
        return $this;
    }
}

Create a PHPUnit test that attempts to create a Mockery mock of the class and method:

<?php

namespace Foo\Tests;

use Foo\Foo;
use Mockery;
use PHPUnit\Framework\TestCase;

class FooTest extends TestCase
{
    public function testFoo(): void
    {
        $fooMock = Mockery::mock(Foo::class);
        $fooMock->allows('setFoo');

        $fooMock->setFoo('some value');

        $this->assertTrue(true);
    }
}

Run the test on PHP 8.0 or 8.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants