Skip to content
This repository was archived by the owner on Feb 20, 2023. It is now read-only.

Mock methods are "undefined" #326

Closed
bensquire opened this issue Aug 26, 2016 · 3 comments
Closed

Mock methods are "undefined" #326

bensquire opened this issue Aug 26, 2016 · 3 comments

Comments

@bensquire
Copy link

Love the project! Use it extensively across multiple systems but within the last week I've had this problem appear across codebases since I upgrade to 5.5.2 and 5.5.3.

Sample code:

<?php

namespace Tests\Futureproofs\Service;


class ElasticSearch
{
    public $elasticSearchService;

    public function __construct(\ElasticSearch\Client $elasticSearch)
    {
        $this->elasticSearchService = $elasticSearch;
    }

    public function pushToElasticSearch(\FutureproofsLibrary\Document\ProofPage $proofPage, $content = '')
    {
        $docToIndex = [
            'index' => 'proof',
            'type' => 'proof_page',
            'id' => $proofPage->getId(),
            'body' => [
                'content' => $content,
                'page_number' => $proofPage->getNumber()
            ]
        ];

        return $this->elasticSearchService->index($docToIndex);
    }
}


class ElasticSearchTest extends \PHPUnit_Framework_TestCase
{
    public function testPushToElasticSearch()
    {
        $mockProofPage = new \FutureproofsLibrary\Document\ProofPage();
        $mockProofPage->setNumber(2)->setId('pageId');

        $elasticSearchMock = $this->getMockBuilder('\ElasticSearch\Client')->setMethods(['index'])->getMock();
        $elasticSearchMock->expects($this->at(0))->method('index')->willReturn(true);

        $object = new \Tests\Futureproofs\Service\ElasticSearch($elasticSearchMock);
        $this->assertTrue($object->pushToElasticSearch($mockProofPage, ''));
    }
}

Response:

Fatal error: Call to undefined method Mock_Client_aa099c83::index()

System:

PHPUnit 5.5.3 by Sebastian Bergmann and contributors.
Runtime:       PHP 5.6.24 with Xdebug 2.4.0 (OSX)
Configuration: /Users/squire/www/futureproofs/futureproofs-extraction/tests/phpunit.xml.dist

I can't see any reported breaking changes and I've had a hack around in PHPUnit and I can see the mock method being successfully set (InvocationMocker line 287), have I missed something?

Thanks

@sebastianbergmann
Copy link
Owner

Can you please try with the latest version?

@bensquire
Copy link
Author

That seemed to fix it, thanks! The release notes don't reveal what the issue might be?

@sebastianbergmann
Copy link
Owner

Duplicate of #322.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants