I
I
itcoder2015-03-23 19:02:13
PHP
itcoder, 2015-03-23 19:02:13

How do you test static methods?

I came across an article on testing static methods via Mock or Stub
https://sebastian-bergmann.de/archives/883-Stubbin... but it deals with the case for PhpUnit 3.5. Accordingly, it does not work in 4.5.
I would like to hear how you test static methods?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2015-03-23
@itcoder

How do you test static methods?

No way. If you have something inside a static method that needs to be covered by tests, then something went wrong. Usually there is extremely primitive logic, call the constructor with the necessary parameters at the level, etc.
In your own question, you are interested in how to mock static methods. And here the answer is no way:
There is really all sorts of mokeri, etc.

H
He11ion, 2015-03-24
@He11ion

Depends on your architecture, I use a "wrapper" class with an overload of the desired method (I use the same trick to change the call of the functions of the parent class)
Something like this:

class A { // "родной" класс
  static function static_call ()
  {
    echo 'static_call a';
  }
}

class B extends A{ // "обертка" для тестирования
  static function static_call ()
  {
    echo 'static_call b';
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question