phpunit icon indicating copy to clipboard operation
phpunit copied to clipboard

Proxied method calls are not counted when called on $this

Open maryo opened this issue 4 years ago • 0 comments

Q A
PHPUnit version 9.5.10
PHP version 7.4.19
Installation Method Composer

Summary

When a method on proxy calls a different method on $this, that invocation is not counted properly. Also reported here https://github.com/sebastianbergmann/phpunit-mock-objects/issues/204 few years ago. I guess this should be possible since both methods are proxied but I may be wrong.

Current behavior

1) SampleTest::test_proxy_calling_method_on_this
Expectation failed for method name is "doSomethingElse" when invoked 1 time(s).
Method was expected to be called 1 times, actually called 0 times.

FAILURES!
Tests: 2, Assertions: 4, Failures: 1.

How to reproduce

<?php

use PHPUnit\Framework\TestCase;

class Foo
{
    public function doSomethingOnThis()
    {
        return $this->doSomethingElse();
    }

    public function doSomethingOnThat(self $self)
    {
        return $self->doSomethingElse();
    }

    public function doSomethingElse()
    {
        return 'result';
    }
}


class SampleTest extends TestCase
{
    function test_proxy_calling_method_on_same_instance_passed_as_argument() // passes
    {
        $proxy = $this->createTestProxy(Foo::class);
        $proxy->expects($this->once())->method('doSomethingElse');
        $this->assertEquals('result', $proxy->doSomethingOnThat($proxy));		
    }

    function test_proxy_calling_method_on_this() // fails
    {
        $proxy = $this->createTestProxy(Foo::class);
        $proxy->expects($this->once())->method('doSomethingElse');
        $this->assertEquals('result', $proxy->doSomethingOnThis($proxy));		
    }
}

Expected behavior

Both tests pass.

maryo avatar Nov 09 '21 20:11 maryo