1. Go to this page and download the library: Download box/shmock 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/ */
box / shmock example snippets
namespace Foo;
/**
* Here's a class we're trying to test yay.
*/
class Foo
{
private $foo = 0;
private $incrementing_service = null;
public function __construct(Incrementing_Service $incrementing_service)
{
$this->incrementing_service = $incrementing_service;
}
public function next_foo()
{
$this->foo = $this->incrementing_service->increment($this->foo);
return $this->foo;
}
}
/**
* Our test case runs the same test case twice - once with the original PHPUnit mocking
* syntax and a second time with Shmock syntax.
*/
class Foo_Test extends PHPUnit_Framework_TestCase
{
use \Shmock\Shmockers; // This enables the use of the Shmock helper methods (replicated below)
public function test_phpunit_original_mocking_syntax()
{
// this is the original PHPUnit mock syntax
$incrementing_service_mock = $this->getMock('\Foo\Incrementing_Service', array('increment'));
$incrementing_service_mock->expects($this->once())
->method('increment')
->with($this->equalTo(0))
->will($this->returnValue(1));
$foo = new Foo($incrementing_service_mock);
$this->assertEquals(1, $foo->next_foo(0));
}
/**
* Create a shmock representation for $class_name and configure expected
* mock interaction with $conf_closure
* @return Shmock A fully configured mock object
* @note You do not need this protected method if you use the Shmockers trait, shown above
*/
protected function shmock($class_name, $conf_closure)
{
return \Shmock\Shmock::create_class($this, $class_name, $conf_closure);
}
public function test_shmock_syntax()
{
// here's shmock. Neat huh?
$incrementing_service_mock = $this->shmock('\Foo\Incrementing_Service', function($shmock)
{
$shmock->increment(0)->return_value(1);
});
$foo = new Foo($incrementing_service_mock);
$this->assertEquals(1, $foo->next_foo(0));
}
}
// This code could conceptually be part of a test method from the above Foo_Test class
$inc_service = $this->shmock('\Foo\Incrementing_Service', function($my_class_shmock) // [1]
{
$my_class_shmock->no_args_method(); // [2]
$my_class_shmock->two_arg_method('param1', 'param2'); // [3]
$my_class_shmock->method_that_returns_a_number()->return_value(100); // [4]
$my_class_shmock->method_that_gets_run_twice()->times(2); // [5]
$my_class_shmock->method_that_gets_run_any_times()->any(); // [6]
$my_class_shmock->method_puts_it_all_together('with', 'args')->times(2)->return_value(false);
$my_class_shmock->method_returns_another_mock()->return_shmock('\Another_Namespace\Another_Class', function($another_class) // [7]
{
$another_class->order_matters(); // [8]
$another_class->disable_original_constructor(); // [9a]
$another_class->set_constructor_arguments(1, 'Foo'); // [9b]
$another_class->method_dies_horribly()->throw_exception(new InvalidArgumentException()); // [10]
$another_class->method_gets_stubbed(1,2)->will(function(PHPUnit_Framework_MockObject_Invocation $invocation)
{
$a = $invocation->parameters[0];
$b = $invocation->parameters[1];
return $a + $b; // [11]
});
});
$my_class_shmock->shmock_class(function($Inc_Service)
{
$Inc_Service->my_static_method()->any()->return_value('This was returned inside the mock instance using the static:: prefix'); // [12]
});
})
$another_class = $this->shmock_class('\Another_Namespace\Another_Class', function($Another_Class) // [13]
{
$Another_Class->a_static_method()->return_value(1);
});
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.