Code Monkey home page Code Monkey logo

Comments (4)

markrogoyski avatar markrogoyski commented on May 14, 2024

You can't mock static functions in PHPUnit; only object methods.

If static method a uses static method b, you'd want to have unit tests on method b on its own. Then the tests on static method a are testing just a, since you know your tests on b test that that works properly.

Now, if method b is a random number generator, then it is going to be impossible to test specific output since the output will depend on the rng. The best you could do is test that it is a number, that it is within the range of possible values based on the range of the rng, etc.

This doesn't necessarily sound like an issue of not being able to mock static methods, but that a rng is being used. Even if there was just one static method and it used a PHP rand function, your output is dependent on that, and you can't know exactly what the output will be.

from math-php.

markrogoyski avatar markrogoyski commented on May 14, 2024

Thinking about this a little more, a possibly elegant solution is to make your static method a higher-order function that accepts as an additional parameter a function (PHP callable closure) that will act as the random number generator. Then for unit tests, you can pass in your testRNG function that returns non-random numbers so you can test the expected output of your method knowing what the inputs will be because you seeded the RNG with your own values.

Then, from a user interface perspective, you might want to wrap that higher-order function with a convenience wrapper method that will supply the standard RNG, but still give the user the option to directly call it with their own RNG if they want.

Just an idea.

from math-php.

Beakerboy avatar Beakerboy commented on May 14, 2024

I think I have a better solution. If I add the Dirac Delta as a distribution, I could add it's inverse function to the DiracDetla class, which would override the parent inverse function. The inverse of the Dirac Delta is Zero for all values of p.

from math-php.

Beakerboy avatar Beakerboy commented on May 14, 2024

This is in a pull request.

from math-php.

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.