PHP code example of badoo / soft-mocks

1. Go to this page and download the library: Download badoo/soft-mocks library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

badoo / soft-mocks example snippets


  class A {const NAME = 'A';}
  class B {}
  echo A::NAME . "\n"; // A
  echo B::NAME . "\n"; // A
  \Badoo\SoftMocks::redefineConstant(A::class . '::NAME', 'B');
  echo A::NAME . "\n"; // B
  echo B::NAME . "\n"; // B
  

  class A {const NAME = 'A';}
  class B {}
  class C {}
  echo A::NAME . "\n"; // A
  echo B::NAME . "\n"; // A
  echo C::NAME . "\n"; // A
  \Badoo\SoftMocks::redefineConstant(B::class . '::NAME', 'B');
  echo A::NAME . "\n"; // A
  echo B::NAME . "\n"; // B
  echo C::NAME . "\n"; // B
  

  class A {const NAME = 'A';}
  class B {}
  echo A::NAME . "\n"; // Undefined class constant 'NAME'
  echo B::NAME . "\n"; // Undefined class constant 'NAME'
  \Badoo\SoftMocks::redefineConstant(A::class . '::NAME', 'A');
  echo A::NAME . "\n"; // A
  echo B::NAME . "\n"; // A
  

  class A {const NAME = 'A';}
  class B {const NAME = 'B';}
  class C {}
  echo A::NAME . "\n"; // A
  echo B::NAME . "\n"; // B
  echo C::NAME . "\n"; // B
  \Badoo\SoftMocks::removeConstant(B::class . '::NAME');
  echo A::NAME . "\n"; // A
  echo B::NAME . "\n"; // A
  echo C::NAME . "\n"; // A
  

composer.phar mougrim/php-xdebug-proxy/config xdebug-proxy-config


use Mougrim\XdebugProxy\Factory\SoftMocksFactory;

return new SoftMocksFactory();

\Badoo\SoftMocks::redefineFunction('call_user_func_array', '', 'return 20;');
\Badoo\SoftMocks::redefineFunction('strlen', '', 'return 5;');
...
strlen('test'); // will return 5
call_user_func_array('strlen', ['test']); // will return 20
call_user_func('strlen', 'test'); // will return 5



$ php example/run_me.php
Result before applying SoftMocks = array (
  'TEST_CONSTANT_WITH_VALUE_42' => 42,
  'someFunc(2)' => 84,
  'Example::doSmthStatic()' => 42,
  'Example->doSmthDynamic()' => 84,
  'Example::STATIC_DO_SMTH_RESULT' => 42,
)
Result after applying SoftMocks = array (
  'TEST_CONSTANT_WITH_VALUE_42' => 43,
  'someFunc(2)' => 57,
  'Example::doSmthStatic()' => 'Example::doSmthStatic() redefined',
  'Example->doSmthDynamic()' => 'Example->doSmthDynamic() redefined',
  'Example::STATIC_DO_SMTH_RESULT' => 'Example::STATIC_DO_SMTH_RESULT value changed',
)
Result after reverting SoftMocks = array (
  'TEST_CONSTANT_WITH_VALUE_42' => 42,
  'someFunc(2)' => 84,
  'Example::doSmthStatic()' => 42,
  'Example->doSmthDynamic()' => 84,
  'Example::STATIC_DO_SMTH_RESULT' => 42,
)

\Badoo\SoftMocks::redefineFunction(
    'strlen',
    '$a',
    'return \\Badoo\\SoftMocks::callOriginal("strlen", [trim($a)]));'
);

var_dump(strlen("  a  ")); // int(1)