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

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

    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

    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

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

  • U04FXCTCWPP
    U04FXCTCWPP Posts: 91 πŸ§‘πŸ»β€πŸš€ - Cadet

    Yep, that makes sense. πŸ‘

  • U04FXCTCWPP
    U04FXCTCWPP Posts: 91 πŸ§‘πŸ»β€πŸš€ - Cadet
    edited December 2022

    @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

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

  • U04FXCTCWPP
    U04FXCTCWPP Posts: 91 πŸ§‘πŸ»β€πŸš€ - Cadet

    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

    @U04FXCTCWPP thanks. will do πŸ™‚

  • U04FXCTCWPP
    U04FXCTCWPP Posts: 91 πŸ§‘πŸ»β€πŸš€ - Cadet

    You're welcome. πŸ™‚