Calling Developers!
We are reenergizing our code contribution process! Learn More

What are the Slack Archives?

It’s a history of our time together in the Slack Community! There’s a ton of knowledge in here, so feel free to search through the archives for a possible answer to your question.

Because this space is not active, you won’t be able to create a new post or comment here. If you have a question or want to start a discussion about something, head over to our categories and pick one to post in! You can always refer back to a post from Slack Archives if needed; just copy the link to use it as a reference..

I am getting the following error with the code sniffer when sniffing my UnitTests class `Return type

Options
U048WDEP3R7
U048WDEP3R7 Posts: 217 🧑🏻‍🚀 - Cadet

I am getting the following error with the code sniffer when sniffing my UnitTests class
Return type annotation is missing the class that is mocked

This is the function where the sniffer gives the error:

 /**
     * @return \PHPUnit\Framework\MockObject\MockObject
     */
    public function getMockSpyGlossaryTranslationQuery()
    {
        return $this->getMockBuilder(SpyGlossaryTranslationQuery::class)
            ->getMock();
    }

Can you please tell me what is missing in the docs?

Comments

  • U04FXCTCWPP
    U04FXCTCWPP Posts: 91 🧑🏻‍🚀 - Cadet
    Options

    I think it's not the docs, but the
    public function getMockSpyGlossaryTranslationQuery()
    line. It probably should have something like
    : MockObject
    at the end. Like this:
    public function getMockSpyGlossaryTranslationQuery(): MockObject

  • U04FXCTCWPP
    U04FXCTCWPP Posts: 91 🧑🏻‍🚀 - Cadet
    Options

    Or, reading that error message again, more likely, it would need some annotation referring to SpyGlossaryTranslationQuery::class, as that is the class you're mocking.

  • U048WDEP3R7
    U048WDEP3R7 Posts: 217 🧑🏻‍🚀 - Cadet
    Options

    @U04FXCTCWPP Thanks for the hint. It turns out I need to write @return SpyGlossaryTranslationQuery instead in the docs

  • U04FXCTCWPP
    U04FXCTCWPP Posts: 91 🧑🏻‍🚀 - Cadet
    Options

    Yep, that makes sense. 👍

  • U04FXCTCWPP
    U04FXCTCWPP Posts: 91 🧑🏻‍🚀 - Cadet
    edited December 2022
    Options

    @U048WDEP3R7 Also, an alternative approach that you might want to consider (I've been using this for quite a long time now):

    /**
         * @return \Orm\Zed\Glossary\Persistence\SpyGlossaryTranslationQuery
         */
        public function createSpyGlossaryTranslationQueryMock(): SpyGlossaryTranslationQuery
        {
            /** @var \Orm\Zed\Glossary\Persistence\SpyGlossaryTranslationQuery $queryMock */
            $queryMock = $this->make(SpyGlossaryTranslationQuery::class);
    
            return $queryMock;
        }
    
  • U048WDEP3R7
    U048WDEP3R7 Posts: 217 🧑🏻‍🚀 - Cadet
    Options

    Thanks @U04FXCTCWPP. This works the same as the code I wrote right? Looks better and cleaner.

  • U04FXCTCWPP
    U04FXCTCWPP Posts: 91 🧑🏻‍🚀 - Cadet
    Options

    I would assume, yes, but I can't say for certain as I haven't used your version yet. Maybe just try it and see if it works for you. 🙂

  • U048WDEP3R7
    U048WDEP3R7 Posts: 217 🧑🏻‍🚀 - Cadet
    Options

    @U04FXCTCWPP thanks. will do 🙂

  • U04FXCTCWPP
    U04FXCTCWPP Posts: 91 🧑🏻‍🚀 - Cadet
    Options

    You're welcome. 🙂