3

I have following class with one method:

class A 
{
    public function my( $myParam )
    {       
        \modelClass::truncateTable('table_name');

        return $myParam * 4;            
    }   
}

Is it possible to mock static method "truncateTable"? I want to make sure it was called once in "my" method. PHPUnit version 4.5, so "staticExpects" is no longer available in this version (depending this post).

Community
  • 1
  • 1
Bounce
  • 2,066
  • 6
  • 34
  • 65
  • possible duplicate of [PHPUnit Mock Objects and Static Methods](http://stackoverflow.com/questions/2357001/phpunit-mock-objects-and-static-methods) – Schleis Mar 18 '15 at 17:31
  • 1
    Can't be done in your case. You could only mock static methods within the same class and that is not available in your version of PHPUnit – Schleis Mar 18 '15 at 17:32

1 Answers1

-1

You can use Proxy class that will wrap your static calls.

class ProxyModel{
   public function truncateTable($tableName){
      \modelClass::truncateTable($tableName);
   }
}

After that in the Class that you are using the static call use your proxy class method instead.

class A{
   private $model;

   public function __construct(ProxyModel $model){
      $this->model = $model;
   }

   public function my(){
      $this->model->truncateModel("table_name");
   }

}

Now you can easily mock the proxy class and pass it as dependency to your class.

petkopara
  • 189
  • 3
  • 9