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

Mock classes with new in methods on PHP 8.1 #1300

Closed
jesperbeisner opened this issue Jul 17, 2023 · 2 comments · Fixed by #1301
Closed

Mock classes with new in methods on PHP 8.1 #1300

jesperbeisner opened this issue Jul 17, 2023 · 2 comments · Fixed by #1301
Assignees
Labels
Bug An error or unexpected behavior. Changelog what's changed
Milestone

Comments

@jesperbeisner
Copy link

Mockery Version

1.6.2

PHP Version

PHP 8.2

Issue Description

Hey,

I'm trying to mock a class method which has a new DateTime as default argument but it's not working. I get: Error: Undefined constant "DateTime".

Steps to Reproduce

<?php

declare(strict_types=1);

namespace Tests;

use DateTime;
use Mockery;
use PHPUnit\Framework\TestCase;

class HandlerClassTest extends TestCase
{
    public function testItDoesNotWork(): void
    {
        $class = Mockery::mock(MockClass::class)->shouldReceive('test')->with('test')->andReturn('yay')->getMock();

        self::assertSame('yay', (new HandlerClass())->doStuff($class));
    }
}

class MockClass
{
    public function test(string $a, DateTime $b = new DateTime()): string
    {
        return $a . ' - ' . $b->format('Y-m-d');
    }
}

class HandlerClass
{
    public function doStuff(MockClass $mockClass): string
    {
        return $mockClass->test('test');
    }
}

Expected Behavior

No response

Actual Behavior

No response

Exception or Error

docker compose exec php vendor/bin/phpunit tests/HandlerClassTest.php

PHPUnit 10.1.3 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.2.7
Configuration: /var/www/html/phpunit.xml

E                                                                   1 / 1 (100%)

Time: 00:00.006, Memory: 12.00 MB

There was 1 error:

1) Tests\HandlerClassTest::testItDoesNotWork
Error: Undefined constant "DateTime"

/var/www/html/tests/HandlerClassTest.php:33
/var/www/html/tests/HandlerClassTest.php:17

--

There was 1 risky test:

1) Tests\HandlerClassTest::testItDoesNotWork
This test did not perform any assertions

/var/www/html/tests/HandlerClassTest.php:13

Additional Information

I found this PR #1160 but I think this did only work by accident? Or am I missing something? 😄

@jesperbeisner jesperbeisner added the triage needs to be triaged label Jul 17, 2023
@ghostwriter ghostwriter self-assigned this Jul 17, 2023
@ghostwriter ghostwriter added this to the 1.6.3 milestone Jul 17, 2023
@ghostwriter ghostwriter added Bug An error or unexpected behavior. and removed Bug An error or unexpected behavior. triage needs to be triaged labels Jul 17, 2023
@ghostwriter
Copy link
Member

@jesperbeisner Thanks for reporting this issue.

I've identified the cause of this issue and am working on a patch now.

@ghostwriter
Copy link
Member

Mockery is currently generating the wrong code.

- public function test(string $a, \DateTime $b = DateTime): string {} // wrong
+ public function test(string $a, \DateTime $b = new DateTime): string {} // correct

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug An error or unexpected behavior. Changelog what's changed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants