Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClosureUsesThisRule produces false positive with Prophecy will() #142

Open
InvisibleSmiley opened this issue Nov 11, 2021 · 0 comments
Open

Comments

@InvisibleSmiley
Copy link

If you replace $that by $this in the closure of the following, the test will fail.

At runtime, the type of $this is \Prophecy\Prophecy\ObjectProphecy and the type of $that is MyTest.

<?php

declare(strict_types=1);

// Requires phpunit/phpunit, phpspec/prophecy and phpspec/prophecy-phpunit

namespace foo;

use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Prophecy\PhpUnit\ProphecyTrait;

class MyTarget
{
    public function run(string $input): string
    {
        return $input;
    }
}

class MyTest extends TestCase
{
    use ProphecyTrait;

    private array $stuff = ['foo' => 'bar'];

    public function testMyMock(): void
    {
        $prophecy = $this->prophesize(MyTarget::class);
        $that = $this;
        $prophecy->run(Argument::type('string'))->will(
            function (array $args) use ($that): string {
                return $that->stuff[$args[0]] ?? $args[0];
            }
        );
        $target = $prophecy->reveal();
        $result = $target->run('foo');
        self::assertSame('bar', $result);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant