Skip to content
This repository

Arguments that are passed by reference are not handled properly #81

Closed
sebastianbergmann opened this Issue · 4 comments

5 participants

Sebastian Bergmann Benjamin Eberlei Davide Principi Marco Pivetta Jeff Welch
Sebastian Bergmann
➜  ~  cat Test.php 
<?php
class Foo {
    public function bar($a, &$b, $c) {
        Legacy::bar($a, $b, $c);
    }
}

class Test extends PHPUnit_Framework_TestCase {
    public function testCallback() {
        $foo = $this->getMockBuilder('Foo')
                    ->disableOriginalConstructor()
                    ->disableArgumentCloning()
                    ->getMock();

        $foo->expects($this->any())
            ->method('bar')
            ->will($this->returnCallback(array($this, 'callback')));

        $a = $b = $c = 0;

        $foo->bar($a, $b, $c);

        $this->assertEquals(1, $b);
    }

    public function testCallback2() {
        $foo = $this->getMockBuilder('Foo')
                    ->disableOriginalConstructor()
                    ->disableArgumentCloning()
                    ->getMock();

        $foo->expects($this->any())
            ->method('bar')
            ->will($this->returnCallback(
            function ($a, &$b, $c) {
                $b = 1;
            }
            ));

        $a = $b = $c = 0;

        $foo->bar($a, $b, $c);

        $this->assertEquals(1, $b);
    }

    public function callback($a, &$b, $c) {
        $b = 1;
    }
}


➜  ~  phpunit Test     
PHPUnit @package_version@ by Sebastian Bergmann.

EE

Time: 0 seconds, Memory: 4.50Mb

There were 2 errors:

1) Test::testCallback
Parameter 2 to Test::callback() expected to be a reference, value given

/home/sb/Test.php:21

2) Test::testCallback2
Parameter 2 to Test::{closure}() expected to be a reference, value given

/home/sb/Test.php:42

FAILURES!
Tests: 2, Assertions: 0, Errors: 2.
Benjamin Eberlei

Hit that problem yesterday.

Davide Principi

Me too..

Marco Pivetta

Same here while mocking Zend\Cache...

Jeff Welch whatthejeff referenced this issue from a commit in whatthejeff/phpunit-mock-objects
Jeff Welch Added tests for #81. Fix is in the phpunit project. 72d15ce
Jeff Welch whatthejeff referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
Jeff Welch whatthejeff referenced this issue from a commit in whatthejeff/phpunit-mock-objects
Jeff Welch Refs #81
 * Use a better test name.
c0c0b2c
Jeff Welch
Collaborator

@sebastianbergmann, you can probably close this now.

Sebastian Bergmann sebastianbergmann closed this
Christoph xrstf referenced this issue in composer/composer
Open

(possibly) update RootPackageLoaderTest #1508

Greg Lamb greglamb referenced this issue from a commit in greglamb/phpunit
Jeff Welch Fixes sebastianbergmann/phpunit-mock-objects#81.
 * Ensure that mock methods preserve pass-by-reference.
b3481d7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.