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

Trigger an error when final or private method is configured #296

Closed
sebastianbergmann opened this issue Mar 3, 2016 · 3 comments
Closed

Comments

@sebastianbergmann
Copy link
Owner

final and private methods cannot be stubbed or mocked. They are ignored and retain their original behavior.

It would be nice if PHPUnit could emit a warning when test code tries to configure a final or private method.

Example

<?php
class C
{
    private function m1()
    {
    }

    final function m2()
    {
    }
}

class Test extends PHPUnit_Framework_TestCase
{
    public function testOne()
    {
        $c = $this->getMock(C::class);

        $c->method('m1')->willReturn(null);
        $c->method('m2')->willReturn(null);
    }
}

Current Result

phpunit Test
PHPUnit 5.2.10 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)

Time: 15 ms, Memory: 4.00Mb

OK (1 test, 0 assertions)

Desired Result

PHPUnit 5.2.10 by Sebastian Bergmann and contributors.

W                                                                   1 / 1 (100%)

Time: 16 ms, Memory: 4.00Mb

There was 1 warning:

1) Test::testOne
Trying to configure final or private method on test double

WARNINGS!
Tests: 1, Assertions: 0, Warnings: 1.
@adamcameron
Copy link

Thanks for raising this on my behave, Sebastian.

@sebastianbergmann
Copy link
Owner Author

Thank me after I have actually fixed this, @adamcameron :-)

I have started to work on this on the https://github.com/sebastianbergmann/phpunit-mock-objects/tree/issue-296/trigger-error-when-configuring-final-private-static branch. No promises on when I'll be able to finish this, though.

@yapro
Copy link

yapro commented Mar 23, 2017

Is it not ready yet?

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

No branches or pull requests

3 participants