Giter VIP home page Giter VIP logo

Comments (4)

malkusch avatar malkusch commented on June 12, 2024

The hole point of declaring the class final and setting the property private is, I didn't design the class for extension. Your use case is very unique, as normal clients would not have to mock that class. Couldn't you design your test to use the actual implementation?

If you really need a mock, you could refactor your code to not use PHPProphet directly, but through a proxy. Then you have full control over the design of that proxy.

Please point me to your test, so that I can have a look on my own of the situation.

from php-mock-prophecy.

cyruscollier avatar cyruscollier commented on June 12, 2024

I wasn't planning on subclassing PHPProphet, but needing to mock PHPProphet as a dependency in another library hardly seems like a unique use case. In my opinion, the only classes that could be final are those that have no dependencies or side effects of their own and do not need to be mocked, like domain entities or value objects, for example.

from php-mock-prophecy.

malkusch avatar malkusch commented on June 12, 2024

needing to mock PHPProphet as a dependency in another library hardly seems like a unique use case

Mocking a mocking library is a unique use case.

In my opinion, the only classes that could be final are those that have no dependencies or side effects of their own

Thank you for sharing your opinion on the final construct. I follow Josh Bloch's approach and protect the clients to mess around with sub classing.

But I see, you don't want to mess around, you just need a mock. As you pointed already out your familiarity with domain driven design, then I suggest using an anti corruption layer which you can mock:

interface MockablePHPProphet {
    public function prophesize($namespace);
}

final class PHPProphetProxy implements MockablePHPProphet {
    private $phpProphet;
    public function prophesize($namespace)
    {
        return $this->phpProphet->prophesize($namespace);
    }
}

from php-mock-prophecy.

cyruscollier avatar cyruscollier commented on June 12, 2024

I was actually considering doing something similar to what you suggest. However, I think I'm satisfied for now about the test coverage of my classes, given the early stage of the project. If things grow in complexity, I'll probably need a mockable interface for both PHPProphet and the special collaborator class for phpspec, since that has been difficult to mock as well (a collaborator of a collaborator!). Thanks.

from php-mock-prophecy.

Related Issues (4)

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.