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

Object passed by reference are not handled properly #116

Closed
jakzal opened this issue Jan 26, 2013 · 5 comments
Closed

Object passed by reference are not handled properly #116

jakzal opened this issue Jan 26, 2013 · 5 comments

Comments

@jakzal
Copy link

jakzal commented Jan 26, 2013

Following test was passing before fixing the #81 in de6d32a8474704e71ce099d1762c69a76eca4b30

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

        $a = new \stdClass();
        $b = $c = 0;

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

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

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

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

Notice that first argument to the callback is an object and it's passed by reference. Test fails with:

1) Test::testCallback
Expectation failed for method name is equal to <string:bar> when invoked zero or more times
Parameter 0 for invocation Foo::bar(Array (), 0, 0) does not match expected value.
Array () does not match expected type "object".

Test.php:23

I realize objects are passed by reference anyway and defining references in the method signature is not needed. However, this is a valid scenario if a method accepts either object or array.

@whatthejeff
Copy link
Collaborator

Thanks for the detailed bug report. This seems related to sebastianbergmann/phpunit#796, but I can't reproduce it yet. Can you provide the following information:

  • PHP version (php -v)
  • PHPUnit version (phpunit --version)
  • PHPUnit install method (composer, pear, etc.)

@jakzal
Copy link
Author

jakzal commented Jan 29, 2013

I installed PHPUnit with composer.

$ php -v
PHP 5.3.15 with Suhosin-Patch (cli) (built: Aug 24 2012 17:45:44) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
$ ./vendor/bin/phpunit --version
PHPUnit 3.7.13 by Sebastian Bergmann.

You can see failures here:

One of the failing tests:

Related code:

To reproduce, use the following composer.json file to install Symfony's form component:

{
    "require": {
        "symfony/form": "2.1.*@dev",
        "phpunit/phpunit": "3.7.13"
    },
    "config": {
        "bin-dir": "bin/"
    }
}

and run the following test:

./bin/phpunit vendor/symfony/form/Symfony/Component/Form/Tests/CompoundFormTest.php

It passes with PHPUnit 3.7.10 but fails with 3.7.13.

@whatthejeff
Copy link
Collaborator

Thanks, @jakzal. I'll look into this ASAP.

@whatthejeff
Copy link
Collaborator

Alright, it seems to work in PHP 5.4, but fail in PHP 5.3. I'll have a fix for this shortly.

whatthejeff added a commit to whatthejeff/phpunit-mock-objects that referenced this issue Jan 30, 2013
@whatthejeff
Copy link
Collaborator

@sebastianbergmann, I believe this can be closed now.

sebastianbergmann pushed a commit to sebastianbergmann/phpunit that referenced this issue Feb 6, 2013
 * Fixed a call-time pass-by-reference issue exposed by de6d32a.
 * Cleaned up some slightly obfuscated code.
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