aliG's avatar
Level 1

Need to test whether methods have been called on a specific interface

I have got a class, which accepts multiple contracts i.e. interfaces in its constructor.


    class Chapter
    {

      public function __construct(
        XPath $xpath,
        MetaData $metaData,
        BinaryReference $binaryReference,
        Section $section,
        Keyword $keyword,
        AbstractContent $abstract,
        Affiliation $affiliation,
        Contributor $contributor
    )
    {
        parent::__construct();
        $this->xpath = $xpath;
        $this->metaData = $metaData;
        $this->section = $section;
        $this->keyword = $keyword;
        $this->abstract = $abstract;
        $this->affiliation = $affiliation;
        $this->contributor = $contributor;
        $this->binaryReference = $binaryReference;
    }

I am intentionally omitting the rest of the class. Now I have got unit tests which separately test each contract. However, I want to test the chapter class and just want to make sure that required methods on interfaces were triggered. Now I tried testing with prophecy in PHPUnit as follow,

    /** @test */
    public function calling_registeredElementQueries_method_triggers_XPath_contract_to_build_queries()
    {
        $xpathContract = $this->prophesize(XPath::class);

        $chapter = $this->app->make(Chapter::class);
        $chapter->registeredElementQueries();
        $xpathContract->reveal()->from('rootXPath', $this->testCrawler);

        $xpathContract->from('rootXPath', $this->testCrawler)->shouldHaveBeenCalled();
        $xpathContract->buildQueries()->shouldHaveBeenCalled();


    }

Now from and buildQueries are methods on XPath Interface that I need to make sure are triggered when i call $chapter->registeredElementQueries(). This test is passing but off course its a false positive. Any ideas on how can I achieve that ???? Thanks in anticipation

0 likes
0 replies

Please or to participate in this conversation.