phpunit icon indicating copy to clipboard operation
phpunit copied to clipboard

"Class does not exist" warning when MockBuilder::addMethods() used

Open daveh opened this issue 4 years ago • 3 comments

Q A
PHPUnit version 8.5.0
PHP version 7.2.24
Installation Method Composer

This issue is related to the MockBuilder::setMethods() method being deprecated, and replaced with addMethods() and onlyMethods() (detailed in issue 3911).

I currently have this code for creating a mock "PaymentGateway" object with a stubbed "charge" method, and it works:

$gateway = $this->getMockBuilder('PaymentGateway')
                ->setMethods(['charge'])
                ->getMock();

However, with setMethods being deprecated, I've tried to replace it with addMethods:

$gateway = $this->getMockBuilder('PaymentGateway')
                ->addMethods(['charge'])
                ->getMock();

However this gives me a Class PaymentGateway does not exist warning.

Does the class actually need to exist before we can mock it? Or am I misunderstanding how addMethods works?

daveh avatar Dec 10 '19 17:12 daveh

Thank you for your report.

Please provide a minimal, self-contained, reproducing test case that shows the problem you are reporting.

Without such a minimal, self-contained, reproducing test case I will not be able to investigate this issue.

sebastianbergmann avatar Dec 11 '19 05:12 sebastianbergmann

<?php

class ExampleTest extends PHPUnit\Framework\TestCase
{
    public function testFails()
    {   
        $mock = $this->getMockBuilder('Example')
                     ->addMethods(['test'])
                     ->getMock();

        $mock->method('test')
             ->willReturn(true);

        $this->assertTrue($mock->test());
    }   
}

This results in the following:

1) ExampleTest::testFails Class Example does not exist

It works if setMethods() is used instead of addMethods().

daveh avatar Dec 11 '19 11:12 daveh

I would love to see a fix for this.

utietze avatar Mar 24 '22 19:03 utietze

Something new to this (happens to me too under PHP v8.1.13 and PHPUnit v9.5.26) ?

And ...happy holidays everyone :-)

ZYZ64738 avatar Dec 04 '22 09:12 ZYZ64738

Any updates on this? Getting the same warning using PHP 8.0.23 and PHPUnit 8.5.28.

Cyclonecode avatar Jan 19 '23 08:01 Cyclonecode

getMockBuilder() will be soft-deprecated (@deprecated annotation) in PHPUnit 10.1, deprecated (triggering a deprecation warning) in PHPUnit 11, and removed in PHPUnit 12. Therefore no changes should be made to it anymore.

sebastianbergmann avatar Mar 06 '23 06:03 sebastianbergmann

Encountered the same issue using PHPUnit 10.0.15. Is there any way to create a mock object for the class that does not exist yet? Or should we just wait for PHPUnit 10.1 release? What will be a replacement for the getMockBuilder() then?

aleksanderbabayev avatar Mar 23 '23 11:03 aleksanderbabayev

getMockBuilder() will be soft-deprecated (@deprecated annotation) in PHPUnit 10.1, deprecated (triggering a deprecation warning) in PHPUnit 11, and removed in PHPUnit 12. Therefore no changes should be made to it anymore.

Screenshot 2023-11-30 at 14 54 27

I use PHPUnit 10.4.2 and I've got an error: Class "PaymentGateway" does not exist. So, if any way to mock the class that doesn't exist yet?

dimablack avatar Nov 30 '23 12:11 dimablack

I know this is a old thread, but with the latest version of PHPUnit (11), it seems there is still no way to mock a class that doesn't exist yet. Is this just not possible?

daveh avatar Feb 09 '24 06:02 daveh