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

Support for ProphecySubjectInterface::getProphecy() #228

Open
weeman1337 opened this issue Oct 5, 2020 · 5 comments · May be fixed by #281
Open

Support for ProphecySubjectInterface::getProphecy() #228

weeman1337 opened this issue Oct 5, 2020 · 5 comments · May be fixed by #281

Comments

@weeman1337
Copy link

There should be support for ProphecySubjectInterface::getProphecy().
It should be handled the same way as prophesize().

@weeman1337
Copy link
Author

Example:

TestInterface

<?php

declare(strict_types=1);

namespace Test;

interface TestInterface
{
    public function func(): string;
}

ExampleTest:

<?php

declare(strict_types=1);

namespace Test;

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

class ExampleTest extends TestCase
{
    use ProphecyTrait;

    /**
     * @var ProphecySubjectInterface|TestInterface
     */
    private $test;

    public function setUp(): void
    {
        parent::setUp();

        $this->test = $this->prophesize(TestInterface::class)
            ->reveal();
    }

    public function testFuncHello(): void
    {
        $this->test->getProphecy()
            ->func()
            ->willReturn('hello');

        self::assertSame('hello', $this->test->func());
    }
}

PHPStan output:

 Line   test/ExampleTest.php                                                                                                            
 ------ -------------------------------------------------------------------------------------------------------------------------------- 
  30     Call to an undefined method Prophecy\Prophecy\ProphecyInterface::func().                                                        
  30     Call to an undefined method Prophecy\Prophecy\ProphecySubjectInterface|Test\TestInterface::getProphecy().  
  34     Call to an undefined method Prophecy\Prophecy\ProphecySubjectInterface|Test\TestInterface::func().         

@xrogers xrogers linked a pull request Feb 12, 2022 that will close this issue
@stof
Copy link

stof commented Mar 13, 2024

This @var ProphecySubjectInterface|TestInterface should actually be @var ProphecySubjectInterface&TestInterface

@alexander-schranz
Copy link
Collaborator

alexander-schranz commented Mar 27, 2024

Shouldnt that be:

    /**
     * @var ObjectProphecy<TestInterface>
     */
    private $test;

But looks like getProphecy is returning still a Prophecy\Prophecy\MethodProphecy (my issue forgot the reveal call), still you currently not require to do reveal in the setUp and can so do:

        $this->test
-           ->getProphecy()
            ->func()
            ->willReturn('hello');

As I'm not sure if there is any easy way to fix the getProphecy

@stof
Copy link

stof commented Mar 27, 2024

My own usage is indeed to store the ObjectProphecy instead of the revealed double, which makes it easier.

@alexander-schranz
Copy link
Collaborator

alexander-schranz commented Mar 27, 2024

@stof Same for me make it a lot easier.

I'm thinking of that reveal returns ProphecySubjectInterface<T>:

/**
 * @template T of object
 * @template-implements ProphecyInterface<T>
 */
class ObjectProphecy implements ProphecyInterface
{
    /**
     * @return ProphecySubjectInterface<T>
     */
    public function reveal()
    {
    }
}

And ProphecySubjectInterface has extends to T:

/**
 * @template T of object
 * @extends T
 */
interface ProphecySubjectInterface
{
    /**
     * @return ObjectProphecy<T>
     */
    public function getProphecy()
    {
    }
}

But not sure and did not yet test it.

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

Successfully merging a pull request may close this issue.

3 participants