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

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

Closed
daveh opened this issue Dec 10, 2019 · 9 comments
Closed

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

daveh opened this issue Dec 10, 2019 · 9 comments
Labels
feature/test-doubles Stubs and Mock Objects type/bug Something is broken

Comments

@daveh
Copy link

daveh commented Dec 10, 2019

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 daveh added the support label Dec 10, 2019
@sebastianbergmann
Copy link
Owner

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.

@daveh
Copy link
Author

daveh commented Dec 11, 2019

<?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().

@sebastianbergmann sebastianbergmann added feature/test-doubles Stubs and Mock Objects type/bug Something is broken and removed support labels Feb 11, 2020
@utietze
Copy link

utietze commented Mar 24, 2022

I would love to see a fix for this.

@ZYZ64738
Copy link

ZYZ64738 commented Dec 4, 2022

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

And ...happy holidays everyone :-)

@Cyclonecode
Copy link

Cyclonecode commented Jan 19, 2023

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

@sebastianbergmann
Copy link
Owner

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.

@aleksanderbabayev
Copy link

aleksanderbabayev commented Mar 23, 2023

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?

@dimablack
Copy link

dimablack commented Nov 30, 2023

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?

@daveh
Copy link
Author

daveh commented Feb 9, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/test-doubles Stubs and Mock Objects type/bug Something is broken
Projects
None yet
Development

No branches or pull requests

7 participants