1. Go to this page and download the library: Download ernestmarcinko/mockutils 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/ */
ernestmarcinko / mockutils example snippets
use ErnestMarcinko\MockUtils\MockUtils;
use PHPUnit\Framework\TestCase;
class MyTest extends TestCase {
use MockUtils;
...
}
use ErnestMarcinko\MockUtils\MockUtilsTestCase;
class MyTest extends MockUtilsTestCase {
...
}
protected function setGlobalMocks(array $global_mocks, ?string $namespace): void
namespace MyNamespace\MySubNamespace;
class MyClass {
public function handler() {
//....
$response = curl_exec($curl); // you want to mock this
//You do something with $response below
}
}
namespace MyTestNamespace;
class TestMyClass {
public function testHandler() {
$this->setGlobalMocks(
[
'curl_exec' => 'response'
],
'MyNamespace\\MySubNamespace'
)
$o = new MyClass();
$this->assertSame( 'expected output', $o->handler() );
}
}
namespace MyTestNamespace;
class TestMyClass {
public function testHandler() {
$this->setGlobalMocks(
[
'curl_exec' => function($curl) {
return 'response';
}
],
'MyNamespace\\MySubNamespace'
)
$o = new MyClass();
$this->assertSame( 'expected output', $o->handler() );
}
}
protected function unsetGlobalMocks(?array $global_mocks=null): void
namespace MyTestNamespace;
class TestMyClass {
public function testHandler() {
$this->setGlobalMocks(
[
'time' => fn()=>time()-3600,
'json_decode' => array(),
'strval' => fn($v)=>$v,
],
'MyNamespace\\MySubNamespace'
)
$o = new MyClass();
$this->assertSame( 'expected output 1', $o->handler() );
$this->unsetGlobalMocks(array('time')); // unset just the time mock
$this->assertSame( 'expected output 2', $o->handler() );
$this->unsetGlobalMocks(); // unset all remaining mocks
$this->assertSame( 'expected output 3', $o->handler() );
}
}