PHP code example of unit-testing / function-spy

1. Go to this page and download the library: Download unit-testing/function-spy 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/ */

    

unit-testing / function-spy example snippets


 namespace MyNamespace;

class TestCase extends \PHPUnit_Framework_TestCase {
	use \UnitTesting\FunctionSpy\SpyTrait;

	function test_functionOrMethod_CanBeSpied()
	{
		function_under_test('foo');
		$this->assertFunctionLastCalledWith('function_under_test', array('foo'));

		$instance = new FakeInstanceThatHasMethodWhichCallsFunctionUnderTest();
		$instance->methodWhichCallsFunctionUnderTest('bar');
		$this->assertFunctionLastCalledWith('function_under_test', array('bar'));
	}
}

function function_under_test()
{
	\UnitTesting\FunctionSpy\Spy::function_under_test();
}

class FakeInstanceThatHasMethodWhichCallsFunctionUnderTest {

	function methodWhichCallsFunctionUnderTest($param)
	{
		function_under_test($param);
	}
}

 namespace MyNamespace;
class MyClass {
	public function doFoo($param1, $param2)
	{
		if ($param1 == 'bar')
		{
			return doSomething($param1, $params);
		}
	}
}