mockery icon indicating copy to clipboard operation
mockery copied to clipboard

Mocking pass by reference do not work in protected methods, while in public it does

Open psychob opened this issue 6 years ago • 1 comments

<?php

use PHPUnit\Framework\TestCase;

require_once './vendor/autoload.php';

class Bug
{
    public function doWork(&$test)
    {
        return true;
    }

    protected function doNotWork(&$test)
    {
        return true;
    }
}

class BugTest extends TestCase
{
    public function testWorking()
    {
        $mock = \Mockery::mock(Bug::class);
        $mock->makePartial()->shouldAllowMockingProtectedMethods();

        $test = [];

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

    public function testNotWorking()
    {
        $mock = \Mockery::mock(Bug::class);
        $mock->makePartial()->shouldAllowMockingProtectedMethods();

        $test = [];

        $this->assertTrue($mock->doNotWork($test));
    }
}
PHPUnit 8.0.4 by Sebastian Bergmann and contributors.

.E                                                                  2 / 2 (100%)

Time: 42 ms, Memory: 4.00 MB

There was 1 error:

1) BugTest::testNotWorking
Parameter 1 to Bug::doNotWork() expected to be a reference, value given

/var/workspace/tests/Test.php:40

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

psychob avatar Mar 12 '19 07:03 psychob

The error is rather strange, but shouldn't you get an error for calling protected method from outer scope instead anyway?

slepic avatar Sep 05 '19 12:09 slepic