Skip to content

ByDefault doesn't appear to allow overwrites #159

@Rayrn

Description

@Rayrn

Hi. As per the title, I can't seem to make byDefault work. Setup:

ExampleClass class:

<?php

declare(strict_types=1);

namespace Example\Plugin\DataProvider;

class ExampleClass
{
    public function getExampleOption(): string
    {
        return get_option('example', null);
    }
}

Base TestCase class:

<?php

declare(strict_types=1);

namespace Example\Package\TestConfig\PhpUnit;

use Brain\Monkey;
use Brain\Monkey\Expectation\Expectation;
use Brain\Monkey\Functions;
use Mockery;
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
use PHPUnit\Framework\TestCase as Base;

abstract class TestCase extends Base
{
    use MockeryPHPUnitIntegration;

    protected function setUp(): void
    {
        parent::setUp();
        Monkey\setUp();

        Functions\stubEscapeFunctions();
        Functions\stubTranslationFunctions();
    }

    protected function tearDown(): void
    {
        parent::tearDown();
        Monkey\tearDown();
        Mockery::close();
    }

    protected function userFunction(string $functionName): Expectation
    {
        return Functions\expect($functionName);
    }
}

And finally, an example test case

<?php

declare(strict_types=1);

namespace Example\Plugin\Tests\DataProvider;

use Example\Plugin\DataProvider\ExampleClass;
use Example\Package\TestConfig\PhpUnit\TestCase;

class ExampleClassTest extends TestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        $this->userFunction('get_option')
            ->with('example', null)
            ->andReturn('I was set in setUp')
            ->byDefault();
    }

    public function testReturnOption(): void
    {
        $example = new ExampleClass();
        $this->assertSame('I was set in setUp', $example->getExampleOption());

        $this->userFunction('get_option')
            ->with('example', null)
            ->andReturn('I was set in testReturnOption');

        $this->assertSame('I was set in testReturnOption', $example->getExampleOption());
    }
}

What I'd expect to see is the assertions varying. What I instead see if the first scenario pass and the second fail as the text hasn't changed.

Is there something I'm doing wrong and if not, is this something we think could be supported as it's currently causing major headaches when shifting to this from WP Mock (as we want to use newer PHPUnit versions!)

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions