Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: userc865b
Added: Jan 29, 2018 9:53 AM
Modified: Jan 29, 2018 10:00 AM
Views: 20
Tags: phpunit
mocking time() funktion to test
  1. <?
  2.  
  3. //  composer require php-mock/php-mock    
  4. use phpmock\MockBuilder;
  5.  
  6.  $builder = new MockBuilder();
  7.        
  8.          $builder->setNamespace(__NAMESPACE__)
  9.             ->setName("time")
  10.             ->setFunction(
  11.                 function () {
  12.                     return 1417011228;
  13.                 }
  14.             );
  15.        /*  OR alternatively
  16.                  $builder->setNamespace(__NAMESPACE__)
  17.             ->setName("time")
  18.             ->setFunctionProvider(new FixedValueFunction(1417011228));
  19.        
  20.  
  21.  
  22.         */
  23.  
  24.  
  25.         $mock = $builder->build();
  26.         $mock->enable();
  27.         $this->assertEquals(1417011228,time());