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

Unable to test method which use func_get_args() in mocked class #50

Closed
marmotz opened this issue Jan 26, 2012 · 2 comments
Closed

Unable to test method which use func_get_args() in mocked class #50

marmotz opened this issue Jan 26, 2012 · 2 comments

Comments

@marmotz
Copy link
Contributor

marmotz commented Jan 26, 2012

I have a class with a method without parameter in declaration but which use func_get_args():

class MyClass {
    public function myMethod() {
        $args = func_get_args();
        // ...
    }
}

In my tests, i create a mock of this class and want to test this method. But the generate code is:

class MyClass extends atoum\test {
    public function testMyMethod() {
        $this->mockTestedClass();
        $myClass = new \mock\MyClass;

        $this->assert()
            ->string($myClass->myMethod('foo'))
                ->isEqualTo('bar')
    }
}

This test fail because myMethod never receive parameter.

In mageekguy\atoum\mock\generator::getMockedClassCode(), here is the generated code:

// ...snip...
public function myMethod()
    {
        if (isset($this->getMockController()->myMethod) === true)
        {
            return $this->mockController->invoke('myMethod', array());
        }
        else
        {
            $this->getMockController()->addCall('myMethod', array());
            return parent::myMethod();
        }
    }
// ...snip...
mageekguy added a commit that referenced this issue Jan 26, 2012
@jubianchi
Copy link
Member

There is still an issue when mocking a method wich accepts a variable number of arguments but has one or more mandatory arguments (like the sprintf function : http://fr.php.net/manual/en/function.sprintf.php).

See here for a case where the mocking engine fails : https://gist.github.com/1712910
And here for a workaround involving changes in the tested code : https://gist.github.com/1712935

@jubianchi
Copy link
Member

Ok, works for me : both of the two test cases (gist #1712910 and gist #1712935) are now working fine : no more
exceptions and mocks are behaving correctly...

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

3 participants