PHP code example of danrevah / shortifypunit

1. Go to this page and download the library: Download danrevah/shortifypunit 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/ */

    

danrevah / shortifypunit example snippets


// Creating a new mock for SimpleClassForMocking
$mock = ShortifyPunit::mock('SimpleClassForMocking');

// Returns NULL, was not stubbed yet
$mock->first_method();

// Creating a new mock for SimpleClassForMocking
$mock = ShortifyPunit::mock('SimpleClassForMocking');

// Stubbing first_method() function without arguments
ShortifyPunit::when($mock)->first_method()->returns(1);
echo $mock->first_method(); // prints '1'

// Stubbing first_method() function with arguments
ShortifyPunit::when($mock)->first_method(1,2)->returns(2);
echo $mock->first_method(); // still prints '1'
echo $mock->first_method(1,2); // prints '2'

// Stubbing callback
ShortifyPunit::when($mock)->first_method()->callback(function() { echo 'Foo Bar'; });
echo $mock->first_method(); // prints 'Foo Bar'

// Stubbing throws exception
ShortifyPunit::when($mock)->second_method()->throws(new Exception());
$mock->second_method(); // throws Exception

class Foo {
  function bar() { return 'bar'; }
}

$mock = ShortifyPunit::mock('Foo');
$partialMock = ShortifyPunit::partialMock('Foo');

$mock->bar(); // returns NULL
echo $partialMock->bar(); // prints 'bar'

ShortifyPunit::when($partialMock)->bar()->returns('foo'); // stubbing partialMock
echo $partialMock->bar(); // prints 'foo'

 // Creating a new mock for SimpleClassForMocking
 $mock = ShortifyPunit::mock('SimpleClassForMocking');

  ShortifyPunit::when($mock)->first_method()->second_method(1)->returns(1);
  ShortifyPunit::when($mock)->first_method()->second_method(2)->returns(2);
  ShortifyPunit::when($mock)->first_method(1)->second_method(1)->returns(3);
  ShortifyPunit::when($mock)->first_method(2)->second_method(2)->third_method()->returns(4);
  
  echo $mock->first_method()->second_method(1); // prints '1'
  echo $mock->first_method()->second_method(2); // prints '2'
  echo $mock->first_method(1)->second_method(1); // prints '3'
  echo $mock->first_method(2)->second_method(2)->third_method(); // prints '4'

    $mock = ShortifyPunit::mock('SimpleClassForMocking');

    ShortifyPunit::when($mock)->first_method()->returns(1);
    echo $mock->first_method(); // method called once

    ShortifyPunit::verify($mock)->first_method()->neverCalled(); // returns FALSE
    ShortifyPunit::verify($mock)->first_method()->atLeast(2); // returns FALSE
    ShortifyPunit::verify($mock)->first_method()->calledTimes(1); // returns TRUE

    echo $mock->first_method(); // method has been called twice

    ShortifyPunit::verify($mock)->first_method()->neverCalled(); // returns FALSE
    ShortifyPunit::verify($mock)->first_method()->atLeast(2); // returns TRUE
    ShortifyPunit::verify($mock)->first_method()->calledTimes(2); // returns TRUE

class Foo
{
  function bar($arg){}
}

$stub = ShortifyPunit::mock('Foo');
ShortifyPunit::when($stub)->bar(anything())->return('FooBar');