Coder Social home page Coder Social logo

Cannot mock Matcher about php-matcher HOT 8 CLOSED

coduo avatar coduo commented on July 30, 2024
Cannot mock Matcher

from php-matcher.

Comments (8)

teklakct avatar teklakct commented on July 30, 2024

Matcher class is marked as final for a reason. It is so simple that you should use it. I suggest you to check your code and ensure is everything is ok. In most cases your design is broken.

Can you provide example why you want to mock Matcher?

from php-matcher.

Canadadry avatar Canadadry commented on July 30, 2024

Because I am building code arround the matcher and I need to test what happend when matcher failed or succeded. I could write some fake data but that sould not concerne my test. And your code is used as a service and therefore could be mock.
Anyway, I have forked it because @uuid@ is missing some implementation. So I also use it to allow mocking.

from php-matcher.

norberttech avatar norberttech commented on July 30, 2024

I think in this case we can remove final statement from Matcher

from php-matcher.

teklakct avatar teklakct commented on July 30, 2024

Still not sure is that a good way to use it. I use Matcher like if construct in order to check some more complex conditions.
IMO you don't need to mock Matcher but create some abstraction for it. You can create your own Matcher interface and mock it. Next create class which implements your interface and extends a coduo Matcher

BTW. Adding uuid pattern for RegexConverter looks interesting.

from php-matcher.

Canadadry avatar Canadadry commented on July 30, 2024

Yes it work too. It seems to me like adding another layer of abstraction only for mocking. Not very fan of this solution, but it does not require to fork the all project, which is a plus.
I might go fir this solution if you add uuid pattern to RegexConverter.

I use this pattern in unit test of api with hateos implementation. Wich has a lot a autoreferencing link, with obviously a lot of uuid.

from php-matcher.

teklakct avatar teklakct commented on July 30, 2024

@norzechowicz would you remove final statement from Matcher?

from php-matcher.

norberttech avatar norberttech commented on July 30, 2024

Iā€™m not sure, this final statement is so meaningless and creatig abstraction just to mock Matcher, sounds a bit like overengeneerig something simple. I would say, lets remove it

from php-matcher.

Canadadry avatar Canadadry commented on July 30, 2024

Before you do anything, removing the final statement is not enough to allow mocking, I had to do some other minor fix. You can have a lot at what I have on my fork. (From memory I have also hadded an interface and make the factory return the interface)

For the exact use case here how Create my mock.

 $simpleFactory = $this->getMockBuilder('Coduo\PHPMatcher\Factory\SimpleFactory')
            ->setMethods(['createMatcher'])
            ->getMock();
 $matcher = $this->getMockBuilder('Coduo\PHPMatcher\MatcherInterface')
            ->setMethods(['match','getError'])
            ->getMock();
 $simpleFactory->method('createMatcher')->willReturn($matcher);
 $matcher->method('match')->willReturn($matcherReturn);
 $matcher->method('getError')->willReturn('mock error');

Also one last thing, I have written the first version of this mock of your service montg ago, the issue only occured recently when you changed the interface.

edit : after reflexion, removing the final keywork is not necessary if you had an interface, because I mock the interface and not the matcher itself, wich is way cleaner.

from php-matcher.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.