PHP code example of lucatume / function-mocker-le

1. Go to this page and download the library: Download lucatume/function-mocker-le 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/ */

    

lucatume / function-mocker-le example snippets




use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Assert;
use function tad\FunctionMockerLe\define;
use function tad\FunctionMockerLe\defineWithMap;
use function tad\FunctionMockerLe\defineWithValueMap;
use function tad\FunctionMockerLe\defineAll;
use function tad\FunctionMockerLe\undefineAll;

class MyOrmTest extends TestCase {
		
	public function test_query_parameters_are_preserved(){
		// define a non existing function
		define('get_posts', function(array $args){
			Assert::assertEquals(['post_type' => 'book','paginated' => 2, 'posts_per_page' => 6], $args);
			
			return [];
		});
		
		$orm = new MyOrm('book');
		
		$orm->query()
			->page(2)
			->perPage(6)
			->fetch();
	}
	
	public function test_users_cannot_fetch_posts_they_cannot_read(){
		// define a number of functions using a <function> => <callback> map
		defineWithMap([
			'is_user_logged_in' =>  function(){
				return true;
			},
			'get_posts' => function(){
				return[
					['ID' => 1, 'post_title' => 'One'],
					['ID' => 2, 'post_title' => 'Two'],
					['ID' => 3, 'post_title' => 'Three'],
					['ID' => 4, 'post_title' => 'Four']
				];
			},
		'current_user_can' => function($cap, $id){
				// a post ID to 'can' map
				$map  = [
					1 => true,
					2 => true,
					3 => false,
					4 => true
				];
				
				return $map($id);
				}
		]);
		
		$orm = new MyOrm('book');
		
		$books = $orm->query()
			->fetch();
		
		$expected = [
				['ID' => 1, 'post_title' => 'One'],
				['ID' => 2, 'post_title' => 'Two'],
				['ID' => 4, 'post_title' => 'Four']
		];
		
		$this->assertEquals($expected, $books);
	}
	
	public function test_sticky_posts_are_not_prepended_if_not_home_archive_or_tag(){
		// define a number of functions all with the same callback...
		defineAll(['is_home', 'is_archive', 'is_tag'], function(){
			return false;
		});
		define('get_posts', function(array $args){
			Assert::arrayHasKey('ignore_sticky_posts,', $args);
			Assert::assertEquals('1', $args['ignore_sticky_posts']);
		});
		
		$orm = new MyOrm('book');
		$orm->query()->fetch();
		
		// ...and then redefine them
		defineWithValueMap([
			'is_home' => true,
			'is_archive' => false,
			'is_tag' => false
		]);
		
		// redefine as needed
		define('get_posts', function(array $args){
			Assert::arrayNotHasKey('ignore_sticky_posts,', $args);
		});
		
		$orm->query()->fetch();
	}
	
	public function tearDown()
    {
    	// undefine all the functions after each test to avoid test dependencies
    	undefineAll();
	}
}

public function test_current_user_can(){
	tad\FunctionMockerLe\define('current_user_can', function(){
		return true;
	});
	
	$this->assertTrue(current_user_can('read_posts'));
}

public function test_prophecy(){
	$pr = $this->prophesize(User::class);
	$pr->can('read_posts')->shouldBeCalled();
	$pr->can('edit_posts')->shouldNotBeCalled();
	
	tad\FunctionMockerLe\define('current_user_can', [$pr->reveal(), 'can']);
	
	current_user_can('read_posts');
	current_user_can('edit_posts'); // this will trigger a failure: not expected
	current_user_can('read_posts');
}

public function test_prophecy(){
	tad\FunctionMockerLe\define('current_user_can', function($cap){
		PHPUnit\Framework\Assert::assertNotEquals('edit_posts', $cap);
	});
	
	current_user_can('read_posts');
	current_user_can('edit_posts'); // this will trigger a failure: not expected
	current_user_can('read_posts');
}