Skip to content
This repository was archived by the owner on Feb 20, 2023. It is now read-only.

No longer possible to configure methods for the mock a class that does not exist #322

Closed
greg0ire opened this issue Aug 24, 2016 · 13 comments
Labels

Comments

@greg0ire
Copy link

Since 3.2.4, using getMock on a class that does not exist no longer allows specifying methods that should be recognized. Calling a method on such a mock results in an undefined index warning.

This can be reproduced thanks to @nubs by following the steps described here : https://gist.github.com/nubs/ddb081e1d1b09541d7f7a1d93d009533

@sebastianbergmann
Copy link
Owner

Classes that do not exist cannot be doubled using the new createMock() API:

<?php
class Test extends PHPUnit\Framework\TestCase
{
    public function testClassThatDoesNotExistCanBeDoubledUsingMockBuilder()
    {
        $o = $this->createMock('DoesNotExist');

        $this->assertInstanceOf('DoesNotExist', $o);
    }
}
$ phpunit Test
PHPUnit 5.5.2-9-g88f407a by Sebastian Bergmann and contributors.

W                                                                   1 / 1 (100%)

Time: 19 ms, Memory: 4.00MB

There was 1 warning:

1) Test::testClassThatDoesNotExistCanBeDoubledUsingMockBuilder
Cannot stub or mock class or interface "DoesNotExist" which does not exist

WARNINGS!
Tests: 1, Assertions: 0, Warnings: 1.

Classes that do not exist can still be doubled using the Mock Builder API:

<?php
class Test extends PHPUnit\Framework\TestCase
{
    public function testClassThatDoesNotExistCanBeDoubledUsingMockBuilder()
    {
        $o = $this->getMockBuilder('DoesNotExist')->getMock();

        $this->assertInstanceOf('DoesNotExist', $o);
    }
}
$ phpunit Test
PHPUnit 5.5.2-9-g88f407a by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 21 ms, Memory: 4.00MB

OK (1 test, 1 assertion)

What is currently broken, and what I think this ticket is about, is configuring methods for test doubles of classes that do not exist:

<?php
class Test extends PHPUnit\Framework\TestCase
{
    public function testClassThatDoesNotExistCanBeDoubledUsingMockBuilder()
    {
        $o = $this->getMockBuilder('DoesNotExist')
                  ->setMethods(['someMethod'])
                  ->getMock();
    }
}
PHPUnit 5.5.2-9-g88f407a by Sebastian Bergmann and contributors.

E                                                                   1 / 1 (100%)

Time: 19 ms, Memory: 4.00MB

There was 1 error:

1) Test::testClassThatDoesNotExistCanBeDoubledUsingMockBuilder
Undefined index: someMethod

/home/sb/Test.php:8

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

This is a bug that I will fix ASAP.

@sebastianbergmann
Copy link
Owner

Seems to be related to this change a8c81f2#diff-64dbbc1c21f9be2d92e2b715617ffe34R839 by @sawmurai for #319.

@greg0ire
Copy link
Author

Sorry, I failed to express it clearly but this is indeed the issue I wanted to report

@sebastianbergmann
Copy link
Owner

No worries, glad we figured it out now :-)

sebastianbergmann added a commit that referenced this issue Aug 24, 2016
@sebastianbergmann
Copy link
Owner

@sawmurai Can you have a look at this, please? Thanks!

greg0ire pushed a commit to greg0ire/dbal that referenced this issue Aug 24, 2016
It has a bug that makes it incompatible with the doctrine dbal test
suite, see
sebastianbergmann/phpunit-mock-objects#322
greg0ire pushed a commit to greg0ire/dbal that referenced this issue Aug 24, 2016
It has a bug that makes it incompatible with the doctrine dbal test
suite, see
sebastianbergmann/phpunit-mock-objects#322
Fixes doctrine#2475
@greg0ire greg0ire changed the title No longer possible to mock a class that does not exist No longer possible to configure methods for the mock a class that does not exist Aug 24, 2016
greg0ire pushed a commit to greg0ire/dbal that referenced this issue Aug 24, 2016
It has a bug that makes it incompatible with the doctrine dbal test
suite, see
sebastianbergmann/phpunit-mock-objects#322
Fixes doctrine#2475
@sawmurai
Copy link
Contributor

Sorry guys I was busy the entire day. Is there anything left to do? What was/is the outcome of this one?

@greg0ire
Copy link
Author

greg0ire commented Aug 24, 2016

I think the todo list is the following:

  • put the above failing test case under version control
  • make it pass
  • optionally, maybe in the same PR maybe not, and if it is not already done, document the fact that you can ask PHPUnit to create a mock from a class that does not exist

@sebastianbergmann
Copy link
Owner

As of db55f4b that test is already in master.

@sawmurai
Copy link
Contributor

#323

Here you go guys. Tests now work for me.

@greg0ire
Copy link
Author

Apparently this test was not enough, see #324

@sebastianbergmann
Copy link
Owner

@sawmurai Can you have another look, please? Thanks!

@sawmurai
Copy link
Contributor

sawmurai commented Aug 26, 2016

This should help: #325

I hope that now we covered everything :)

@sebastianbergmann
Copy link
Owner

Thanks!

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

No branches or pull requests

3 participants