PHP code example of tomkyle / mock-psr

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

    

tomkyle / mock-psr example snippets



use tomkyle\MockPsr\MockPsr11ContainerTrait;
use tomkyle\MockPsr\MockPsr6CacheTrait;
use tomkyle\MockPsr\MockPsr7MessagesTrait;
use tomkyle\MockPsr\MockPsr15RequestHandlerTrait;
use tomkyle\MockPsr\MockPsr18ClientTrait;  

# Bonus
use tomkyle\MockPsr\MockPdoTrait;


use tomkyle\MockPsr\MockPsr7MessagesTrait;

class SomeUnitTest extends \PHPUnit\Framework\TestCase
{
	use MockPsr7MessagesTrait;

	public function testSomething() 
	{
		// Psr\Http\Message\ServerRequestInterface
		$server_request = $this->mockServerRequest();
		$attributes = array();
		$headers = array();
		$server_request = $this->mockServerRequest($attributes, $headers);

		// Psr\Http\Message\UriInterface
		$uri = $this->mockUri("https://test.com");

		// Psr\Http\Message\RequestInterface
		$request = $this->mockRequest("GET", $uri);
		$request = $this->mockRequest("GET", "/home");

		// Psr\Http\Message\StreamInterface
		$stream = $this->mockStream("body string");

		// Psr\Http\Message\ResponseInterface
		$response = $this->mockResponse(200, $stream);
		$response = $this->mockResponse(404, "body string");
	}
}


use tomkyle\MockPsr\MockPsr11ContainerTrait;

class SomeUnitTest extends \PHPUnit\Framework\TestCase
{
	use MockPsr11ContainerTrait;

	public function testSomething() 
	{
		// Psr\Container\ContainerInterface
		$container = $this->mockContainer();
		$container = $this->mockContainer([
			'foo' => 'bar',
			'qux' => 'baz'        
		]);

		$container->has("foo"); // true
		$container->has("hello"); // false
		$container->get("hello"); // throws 'NotFoundExceptionInterface'
	}
}


use tomkyle\MockPsr\MockPsr15RequestHandlerTrait;

class SomeUnitTest extends \PHPUnit\Framework\TestCase
{
	use MockPsr15RequestHandlerTrait;

	public function testSomething() 
	{
		// Psr\Http\Server\RequestHandlerInterface
		$request_handler = $this->mockRequestHandler();

		$response = $this->mockResponse(404, "body string");
		$request_handler = $this->mockRequestHandler( $response );
	}
}


use tomkyle\MockPsr\MockPsr17FactoriesTrait;

class SomeUnitTest extends \PHPUnit\Framework\TestCase
{
	use MockPsr17FactoriesTrait;

	public function testSomething() 
	{
		// Psr\Http\Message\RequestFactoryInterface
		$request_factory = $this->mockRequestFactory();

		$request = $this->mockRequest();
		$request_factory = $this->mockRequestFactory( $request );


		// Psr\Http\Message\ResponseFactoryInterface
		$response_factory = $this->mockResponseFactory();

		$response = $this->mockResponse(404, "body string");
		$response_factory = $this->mockResponseFactory( $response );
	}
}


use tomkyle\MockPsr\MockPsr18ClientTrait;

class SomeUnitTest extends \PHPUnit\Framework\TestCase
{
	use MockPsr18ClientTrait;

	public function testSomething() 
	{
		// Psr\Http\Client\ClientInterface
		$client = $this->mockClient();

		$response = $this->mockResponse(404, "body string");
		$client = $this->mockClient( $response );
	}
}


use tomkyle\MockPsr\MockPdoTrait;

class SomeUnitTest extends \PHPUnit\Framework\TestCase
{
	use MockPdoTrait;

	public function testSomething() 
	{
		// \PDOStatement
		$execution_result = true;
    $stmt = $this->mockPdoStatement($execution_result);
    $stmt = $this->mockPdoStatement(true, array("foo" => "bar"));    

    // \PDO
    $pdo = $this->mockPdo();
    $pdo = $this->mockPdo($stmt);   
    
    $stmt_2 = $pdo->prepare("SELECT");
    $stmt_2 == $stmt
	}
}
bash
$ PHP_CS_FIXER_IGNORE_ENV=1 composer phpcs