PHP code example of sirbrillig / spies
1. Go to this page and download the library: Download sirbrillig/spies 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/ */
sirbrillig / spies example snippets
$spy = new \Spies\Spy();
$spy( 'hello', 'world' );
$spy->was_called(); // Returns true
$spy->was_called_times( 1 ); // Returns true
$spy->was_called_times( 2 ); // Returns false
$spy->get_times_called(); // Returns 1
$spy->was_called_with( 'hello', 'world' ); // Returns true
$spy->was_called_with( 'goodbye', 'world' ); // Returns false
\Spies\stub_function( 'add_one' )->when_called->with( 5 )->will_return( 6 );
\Spies\stub_function( 'add_one' )->when_called->with( 1 )->will_return( 2 );
add_one( 5 ); // Returns 6
add_one( 1 ); // Returns 2
class Greeter {
public function say_hello() {
return 'hello';
}
public function say_goodbye() {
return 'goodbye';
}
}
function test_greeter() {
$mock = \Spies\mock_object_of( 'Greeter' );
$mock->add_method( 'say_hello' )->that_returns( 'greetings' );
$greet = $mock->spy_on_method( 'greet' );
$this->assertEquals( 'greetings', $mock->say_hello() );
$this->assertEquals( null, $mock->say_goodbye() );
$mock->greet();
$this->assertTrue( $greet->was_called() );
}
function test_spy_is_called_correctly() {
$spy = \Spies\make_spy();
$spy( 'hello', 'world', 7 );
$spy( 'hello', 'world', 8 );
$expectation = \Spies\expect_spy( $spy )->to_have_been_called->with( 'hello', 'world', \Spies\any() )->twice();
$expectation->verify();
}
function test_calculation() {
$add_one = \Spies\get_spy_for( 'add_together' );
add_together( 2, 3 );
$expectation = \Spies\expect_spy( $add_one )->to_have_been_called->with( 2, 3 ); // Passes
$expectation->verify();
}
function test_calculation() {
$add_one = \Spies\get_spy_for( '\Calculator\add_together' );
\Calculator\add_together( 2, 3 );
$expectation = \Spies\expect_spy( $add_one )->to_have_been_called->with( 2, 3 ); // Passes
$expectation->verify();
}
\Spies\stub_function( 'get_color' )->and_return( 'green' );
get_color(); // Returns 'green'
\Spies\stub_function( 'add_one' )->when_called->with( 5 )->will_return( 6 );
\Spies\stub_function( 'add_one' )->when_called->with( 1 )->will_return( 2 );
add_one( 5 ); // Returns 6
add_one( 1 ); // Returns 2
\Spies\stub_function( 'get_first' )->when_called->will_return( \Spies\passed_arg( 0 ) );
get_first( 5, 6, 7 ); // Returns 5
get_first( 1, 2, 3 ); // Returns 1
\Spies\stub_function( 'add_one' )->and_return( function( $a ) {
return $a + 1;
} );
add_one( 5 ); // Returns 6
add_one( 1 ); // Returns 2
function test_calculation() {
$adder = \Spies\mock_object();
$adder->add_method( 'add_one' )->when_called->with( 6 )->will_return( 7 );
$add_one = $adder->spy_on_method( 'add_one' );
$calculator = new Calculator( $adder );
$calculator->add_one( 4 ); // Returns null
$calculator->add_one( 6 ); // Returns 7
\Spies\expect_spy( $add_one )->to_have_been_called(); // Passes
\Spies\expect_spy( $add_one )->to_have_been_called->with( 2 ); // Fails
\Spies\finish_spying(); // Verifies all Expectations
}
class Greeter {
public function say_hello() {
return 'hello';
}
public function say_goodbye() {
return 'goodbye';
}
}
function test_greeter() {
$mock = \Spies\mock_object_of( 'Greeter' );
$mock->add_method( 'say_hello' )->that_returns( 'greetings' );
$this->assertEquals( 'greetings', $mock->say_hello() );
$this->assertEquals( null, $mock->say_goodbye() );
}
function test_greeter() {
$mock = \Spies\mock_object()->and_ignore_missing();
$this->assertEquals( null, $mock->say_goodbye() );
}
class Greeter {
public function say_hello() {
return 'hello';
}
public function say_goodbye() {
return 'goodbye';
}
}
function test_greeter() {
$mock = \Spies\mock_object( new Greeter() );
$say_goodbye = $mock->spy_on_method( 'say_goodbye' );
$mock->add_method( 'say_hello' )->that_returns( 'greetings' );
$this->assertEquals( 'greetings', $mock->say_hello() );
$this->assertEquals( 'goodbye', $mock->say_goodbye() );
$this->assertSpyWasCalled( $say_goodbye );
}
function test_spy_is_called() {
$spy = \Spies\make_spy();
$spy();
$this->assertTrue( $spy->was_called() );
}
function test_spy_is_called() {
$spy = \Spies\make_spy();
$spy();
$expectation = \Spies\expect_spy( $spy )->to_have_been_called();
$expectation->verify();
}
function test_spy_is_called_correctly() {
$spy = \Spies\make_spy();
$spy( 'hello', 'world', 7 );
$spy( 'hello', 'world', 8 );
$this->assertTrue( $spy->was_called_with( 'hello', 'world', \Spies\any() ) );
$this->assertTrue( $spy->was_called_times( 2 ) );
}
function test_spy_is_called_correctly() {
$spy = \Spies\make_spy();
$spy( 'hello', 'world', 7 );
$spy( 'hello', 'world', 8 );
$expectation = \Spies\expect_spy( $spy )->to_have_been_called->with( 'hello', 'world', \Spies\any() )->twice();
$expectation->verify();
}
function tearDown() {
\Spies\finish_spying();
}
function test_calculation() {
$add_one = \Spies\get_spy_for( 'add_together' );
add_together( 2, 3 );
\Spies\expect_spy( $add_one )->to_have_been_called->with( 2, 3 ); // Passes
}
function tearDown() {
\Spies\finish_spying();
}
function test_calculation() {
$add_one = \Spies\get_spy_for( 'add_together' );
\Spies\expect_spy( $add_one )->to_be_called->with( 2, 3 ); // Passes
add_together( 2, 3 );
}
function tearDown() {
\Spies\finish_spying();
}
function test_calculation() {
$add_one = \Spies\get_spy_for( 'add_together' );
\Spies\expect_spy( $add_one )->to_be_called->with( \Spies\Expectation::any(), \Spies\Expectation::any() ); // Passes
add_together( 2, 3 );
}
$spy = \Spies\get_spy_for( 'run_experiment' );
run_experiment( 'slartibartfast' );
\Spies\expect_spy( $spy )->to_have_been_called->with( \Spies\match_pattern( '/bart/' ) );
\Spies\finish_spying();
function tearDown() {
\Spies\finish_spying();
}
function test_name() {
$say_hello = \Spies\get_spy_for( 'say_hello' );
\Spies\expect_spy( $say_hello )->to_be_called->with( \Spies\match_array( [ 'name' => 'Raistlin' ] ) ) ); // Passes
say_hello( [ 'name' => 'Raistlin', 'job' => 'wizard', 'robes' => 'black' ] );
}
class MyTest extends \Spies\TestCase {
function test_spy_is_called_correctly() {
$spy = \Spies\make_spy();
$spy( 'hello', 'world', 7 );
$spy( 'hello', 'world', 8 );
$this->assertSpyWasCalledWith( $spy, [ 'hello', 'world', \Spies\any() ] );
}
}
$array = [ 'baz' => 'boo', 'foo' => 'bar' ];
$this->assertTrue( \Spies\do_arrays_match( $array, \Spies\match_array( [ 'foo' => 'bar' ] ) ) );
$autoload = 'vendor/autoload.php';
$patchwork = 'vendor/antecedent/patchwork/Patchwork.php';
# uire_once $autoload;
}
<phpunit bootstrap="vendor/autoload.php">
...
<phpunit bootstrap="tests/bootstrap.php">
...
php
function sayHello() {
return 'hello';
}
//...
\Spies\mock_function( 'sayHello' )->and_return( 'bye' );
$this->assertEquals( 'bye', sayHello() );
php
function sayHello() {
return 'hello';
}
//...
$spy = \Spies\get_spy_for( 'sayHello' );
sayHello();
$this->assertTrue( $spy->was_called() );