phpunit icon indicating copy to clipboard operation
phpunit copied to clipboard

error on mock object on phar

Open franz-deleon opened this issue 12 years ago • 4 comments

I am using a zf2 testcase(AbstractHttpControllerTestCase) that extends a PHPUnit_Framework_TestCase and i get this error when i use the phpunit phar

Fatal error: Call to a member function getMock() on a non-object in phar:///vagrant/www/fdl-oauth2-provider/tests/phpunit.phar/phpunit/Framework/TestCase.php on line 1301

franz-deleon avatar Mar 11 '14 16:03 franz-deleon

Please provide a minimal, self-contained, reproducing test case. Otherwise we cannot reproduce, debug, and fix the issue. Thanks!

sebastianbergmann avatar Mar 11 '14 16:03 sebastianbergmann

The test runs fine when I used the PEAR installed version of phpunit 3.7.2 The phpunit.phar version is 4.0.6

it errors on line 78

$this->getMockBuilder('OAuth2Provider\Server')
            ->setMethods(array('handleTokenRequest'))
            ->getMock();

testcase:

<?php
namespace OAuth2ProviderTests;

use OAuth2Provider\Controller\UserCredentialsController;
use OAuth2Provider\Version;

use OAuth2\Response as OAuth2Response;

use Zend\Http\Headers;
use Zend\Json\Json;
use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase;
use Zend\Stdlib\ArrayUtils;

/**
 * UserCredentialsController test case.
 */
class UserCredentialsControllerTest extends AbstractHttpControllerTestCase
{
    /**
     * @var UserCredentialsController
     */
    private $UserCredentialsController;

    /**
     * Prepares the environment before running a test.
     */
    protected function setUp()
    {
        parent::setUp();
        $this->setApplicationConfig(Bootstrap::getServiceManager()->get('ApplicationConfig'));
    }

    /**
     * Cleans up the environment after running a test.
     */
    protected function tearDown()
    {
        $this->UserCredentialsController = null;
        parent::tearDown();
    }

    /**
     * Constructs the test case.
     */
    public function __construct()
    {
    }

    /**
     * Tests UserCredentialsController->authorizeAction()
     * @group test1
     */
    public function testAuthorizeAction()
    {
        $this->dispatch('/oauth2/' . Version::API_VERSION . '/authorize', 'POST');

        $r = Json::decode($this->getResponse()->getContent(), Json::TYPE_ARRAY);
        $this->assertNotEmpty($r['error']);
    }

    /**
     * Tests UserCredentialsController->requestAction()
     * @group test2
     */
    public function testRequestAction()
    {
        $result = array(
            "access_token" => "b43fde87a6e2cdbd001b09c27f68f2b60a201a06",
            "token_type" => "bearer",
            "scope" => "get",
            "access_expires_in" => 3600,
            "refresh_token" => "f934c943a5f5d5a3f2db8889b2d734fd7ca4aa64",
            "refresh_expires_in" => "952659",
        );

        $serverMock = $this->getMockBuilder('OAuth2Provider\Server')
            ->setMethods(array('handleTokenRequest'))
            ->getMock();
        $serverMock->expects($this->once())
            ->method('handleTokenRequest')
            ->will($this->returnValue(new OAuth2Response($result)));

        $mainSm = $this->getApplicationServiceLocator()->setAllowOverride(true);
        // having set to server.default provides some sort if integration test
        $mainSm->setService('oauth2provider.server.default', $serverMock);

        $this->dispatch('/oauth2/' . Version::API_VERSION . '/request', 'POST');

        $r = Json::decode($this->getResponse()->getContent(), Json::TYPE_ARRAY);
        $this->assertSame($result, $r);
    }
}

here is the stack trace:

Fatal error: Call to a member function getMock() on a non-object in phar:///vagrant/www/zf2skeleton/vendor/franz-deleon/fdl-oauth2-provider/tests/phpunit.phar/phpunit/Framework/TestCase.php on line 1301

Call Stack:
    0.0400    1050272   1. {main}() /vagrant/www/zf2skeleton/vendor/franz-deleon/fdl-oauth2-provider/tests/phpunit.phar:0
    0.0417    1521456   2. PHPUnit_TextUI_Command::main() /vagrant/www/zf2skeleton/vendor/franz-deleon/fdl-oauth2-provider/tests/phpunit.phar:581
    0.0417    1522352   3. PHPUnit_TextUI_Command->run() phar:///vagrant/www/zf2skeleton/vendor/franz-deleon/fdl-oauth2-provider/tests/phpunit.phar/phpunit/TextUI/Command.php:132
    2.1036   19695488   4. PHPUnit_TextUI_TestRunner->doRun() phar:///vagrant/www/zf2skeleton/vendor/franz-deleon/fdl-oauth2-provider/tests/phpunit.phar/phpunit/TextUI/Command.php:179
    3.4615   20709872   5. PHPUnit_Framework_TestSuite->run() phar:///vagrant/www/zf2skeleton/vendor/franz-deleon/fdl-oauth2-provider/tests/phpunit.phar/phpunit/TextUI/TestRunner.php:419
    7.5041   31910432   6. PHPUnit_Framework_TestSuite->run() phar:///vagrant/www/zf2skeleton/vendor/franz-deleon/fdl-oauth2-provider/tests/phpunit.phar/phpunit/Framework/TestSuite.php:722
    8.7664   39408888   7. PHPUnit_Framework_TestCase->run() phar:///vagrant/www/zf2skeleton/vendor/franz-deleon/fdl-oauth2-provider/tests/phpunit.phar/phpunit/Framework/TestSuite.php:722
    8.7666   39408888   8. PHPUnit_Framework_TestResult->run() phar:///vagrant/www/zf2skeleton/vendor/franz-deleon/fdl-oauth2-provider/tests/phpunit.phar/phpunit/Framework/TestCase.php:764
    8.7669   39410888   9. PHPUnit_Framework_TestCase->runBare() phar:///vagrant/www/zf2skeleton/vendor/franz-deleon/fdl-oauth2-provider/tests/phpunit.phar/phpunit/Framework/TestResult.php:692
    8.7688   39470816  10. PHPUnit_Framework_TestCase->runTest() phar:///vagrant/www/zf2skeleton/vendor/franz-deleon/fdl-oauth2-provider/tests/phpunit.phar/phpunit/Framework/TestCase.php:824
    8.7689   39472960  11. ReflectionMethod->invokeArgs() phar:///vagrant/www/zf2skeleton/vendor/franz-deleon/fdl-oauth2-provider/tests/phpunit.phar/phpunit/Framework/TestCase.php:956
    8.7689   39473016  12. OAuth2ProviderTests\UserCredentialsControllerTest->testRequestAction() phar:///vagrant/www/zf2skeleton/vendor/franz-deleon/fdl-oauth2-provider/tests/phpunit.phar/phpunit/Framework/TestCase.php:956
    8.7707   39543944  13. PHPUnit_Framework_MockObject_MockBuilder->getMock() /vagrant/www/zf2skeleton/vendor/franz-deleon/fdl-oauth2-provider/tests/OAuth2ProviderTests/Controller/UserCredentialsControllerTest.php:78
    8.7707   39543944  14. PHPUnit_Framework_TestCase->getMock() phar:///vagrant/www/zf2skeleton/vendor/franz-deleon/fdl-oauth2-provider/tests/phpunit.phar/phpunit-mock-objects/Framework/MockObject/MockBuilder.php:142

franz-deleon avatar Mar 11 '14 16:03 franz-deleon

Sorry, but the test case you provide is not self-contained.

sebastianbergmann avatar Mar 11 '14 16:03 sebastianbergmann

It looks like i fixed the error by removing the constructor from the test case. although the test still works on the pear version with the constructor in it.

    /**
     * Constructs the test case.
     */
    public function __construct()
    {
    }

Thanks for the response but i hope you can give me a feedback if a constructor is even needed since my ide generates it automatocally. thanks for the replying!

franz-deleon avatar Mar 11 '14 17:03 franz-deleon