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

Mock WordPress functions with the same object of the provider #6

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

antonioeatgoat
Copy link

@antonioeatgoat antonioeatgoat commented Jul 19, 2021

At the current moment, a mocked object is returned by the provider, when you call it. However, a new mocked object is generated for each stubbed WordPress function. This doesn't allow to rely on the parameters checking, while testing expectations.

I've fixed it for the User provider only for now, if the solution makes sense I can implement the solution for the other providers as well.

Watch out, this PR also include the commit already present in this other one, otherwise I'd not be able to run tests.

This is the code with which you can reproduce the problem:

<?php

use Brain\Monkey;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\TestCase as FrameworkTestCase;

class UserParser {
    public function parse(\WP_User $wpUser): string {
        return 'pointless-value';
    }
}

class PostAuthorFetcher {

    /**
     * @var UserParser
     */
    private $userParser;

    public function __construct(UserParser $userParser) {

        $this->userParser = $userParser;
    }

    public function fetchAuthor(\WP_Post $wpPost) {
        return $this->userParser->parse(get_userdata($wpPost->post_author));
    }
}

class FakerTest extends FrameworkTestCase {

    /**
     * @var \Brain\Faker\Providers
     */
    protected $wpFaker;

    /**
     * @return void
     */
    protected function setUp(): void
    {
        parent::setUp();
        Monkey\setUp();

        $this->wpFaker = \Brain\faker()->wp();
    }

    /**
     * @return void
     */
    protected function tearDown(): void
    {
        Monkey\tearDown();
        parent::tearDown();
    }

    public function testFakerStub()
    {
        $mockPost = $this->wpFaker->post(['post_author' => 10]);

        $mockUser = $this->wpFaker->user(['ID' => 10]);

        $mockUserParser = \Mockery::mock(UserParser::class);
        $mockUserParser->expects('parse')
            ->with($mockUser)
            ->andReturn('pointless-value');

        $postAuthorFetcher = new PostAuthorFetcher($mockUserParser);

        $postAuthorFetcher->fetchAuthor($mockPost);
    }
}

antonioeatgoat and others added 2 commits July 9, 2021 17:59
The method `expectExceptionMessageRegExp` is deprecated and has been replaced with `expectExceptionMessageMatches` from version 8.4. It will be removed from version 9.

See https://github.com/sebastianbergmann/phpunit/blob/8.4.0/src/Framework/TestCase.php
At the current moment, a mocked object is returned by the provider, when you call it. However, a new mocked object is generated for each stubbed WordPress function. This doesn't allow to rely on the parameters checking, while testing expectations.
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 this pull request may close these issues.

1 participant