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

Need help to bypass Bug #68541 #3

Closed
fwolf opened this issue Feb 27, 2015 · 2 comments
Closed

Need help to bypass Bug #68541 #3

fwolf opened this issue Feb 27, 2015 · 2 comments

Comments

@fwolf
Copy link

fwolf commented Feb 27, 2015

This is a handy library, and i'm using it to replace a old test case, which originally with namespace hack like:

namespace Test;
class ATest {}

namespace App;
function extension_loaded() {}

The test purpose is to simulate PHP extension load status, and throw Exception when failed.

For using php-mock, i create a trait like this:

trait ExtensionLoadedMockTrait
{
    /** @type bool */
    public static $extensionLoaded = true;


    /**
     * @param   string  $namespace
     * @return  Mock
     */
    public function buildExtensionLoadedMock($namespace)
    {
        $function = 'extension_loaded';
        $mockContainer = MockContainer::getInstance();
        $mock = $mockContainer->getMock($namespace, $function);

        if (is_null($mock)) {
            $mock = (new MockBuilder())
                ->setNamespace($namespace)
                ->setName($function)
                ->setFunction(function($ext) {
                    return self::$extensionLoaded &&
                        \extension_loaded($ext);
                })
                ->build();

            $mock->define();

            $mockContainer->registerMock($namespace, $function, $mock);
        }

        return $mock;
    }
}

The source code of MockContainer is omit. In test case, use

$this->buildExtensionLoadedMock('Foo\Bar')->enable();

to enable or disable mock.

This test works fine when run by PHPUnit with single file given, and with directory given, but fails when run PHPUnit without any parameter:

// Ok
phpunit path/to/test/case.php
// Ok
phpunit path/to/tests/
// Fail
phpunit

Why ?

As you mentioned about Bug #68541, the mock need to define before native PHP functions. So I make a special test case name like 'Lib\Aaa\Aaa.php', and I'm sure it will run before all other test cases, but the mock mechanism still not work, the mocked function are not triggered at all.

Run single file are successful, i think the code is correct, but failed when batch run confuse me, do you have any tips ? Thanks.

PHP: 5.5.9
PHPUnit: 4.5.0
Os: Ubuntu 14.04 Trusty

@fwolf
Copy link
Author

fwolf commented Feb 27, 2015

A simplier version, still not work for phpunit batch test

trait ExtensionLoadedMockTrait
{
    /** @type bool */
    public static $extensionLoaded = true;

    /** @type Mock */
    private $extensionLoadedMock = null;

    /**
     * @param   string  $namespace
     * @return  Mock
     */
    public function buildExtensionLoadedMock($namespace)
    {
        $function = 'extension_loaded';

        if (is_null($this->extensionLoadedMock)) {
            $this->extensionLoadedMock = (new MockBuilder())
                ->setNamespace($namespace)
                ->setName($function)
                ->setFunction(function($ext) {
                    return self::$extensionLoaded &&
                        \extension_loaded($ext);
                })
                ->build();

            $this->extensionLoadedMock->define();
        }

        return $this->extensionLoadedMock;
    }
}

@fwolf
Copy link
Author

fwolf commented Feb 27, 2015

My bad, there is test case uses test object class, which runs before its own test.

@fwolf fwolf closed this as completed Feb 27, 2015
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