PHP code example of nivseb / php-mock-server-connector
1. Go to this page and download the library: Download nivseb/php-mock-server-connector 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/ */
nivseb / php-mock-server-connector example snippets
use Nivseb\PhpMockServerConnector\Server;
MockServer::init('https://your_mock_server.localhost');
use Nivseb\PhpMockServerConnector\Server;
MockServer::close();
use Nivseb\PhpMockServerConnector\PhpUnit\UseMockServer;
use PHPUnit\Framework\TestCase;
class YourTest extends TestCase {
use UseMockServer;
protected function setUp(): void
{
parent::setUp();
$this->initMockServer('https://your_mock_server.localhost');
}
}
use Nivseb\PhpMockServerConnector\PhpUnit\MockServerEndpoint;
$mockServer = new MockServerEndpoint('/rootPath');
use Nivseb\PhpMockServerConnector\PhpUnit\UseMockServer;
use Nivseb\PhpMockServerConnector\PhpUnit\MockServerEndpoint;
use PHPUnit\Framework\TestCase;
use GuzzleHttp\Client;
$mockServer = new MockServerEndpoint('/rootPath');
$mockServer->allows('GET', '/firstPath')->andReturn(200, ['data' => 'This is a JSON test content.']);
// OR
$myRequest = $mockServer->allows('GET', '/secondPath');
$myRequest->andReturn(200, ['data' => 'This is a JSON test content.']);
$myRequest->run();
use Nivseb\PhpMockServerConnector\PhpUnit\UseMockServer;
use Nivseb\PhpMockServerConnector\PhpUnit\MockServerEndpoint;
use PHPUnit\Framework\TestCase;
use GuzzleHttp\Client;
// Expected: /test/myExpectedPath?myQueryParameter=myExpectedValue
$mockServer = new MockServerEndpoint('/test');
$mockServer
->allows('GET', '/{myPathParameter}')
->withPathParameters(['myPathParameter' => 'myExpectedPath'])
->withQueryParameters(['myQueryParameter' => 'myExpectedValue']);
use Nivseb\PhpMockServerConnector\PhpUnit\UseMockServer;
use Nivseb\PhpMockServerConnector\PhpUnit\MockServerEndpoint;
use PHPUnit\Framework\TestCase;
use GuzzleHttp\Client;
$mockServer = new MockServerEndpoint('/test');
$mockServer->allows('GET', '/')->withHeaders(['myHeader' => 'myExpectedValue']);
use Nivseb\PhpMockServerConnector\PhpUnit\UseMockServer;
use Nivseb\PhpMockServerConnector\PhpUnit\MockServerEndpoint;
use PHPUnit\Framework\TestCase;
use GuzzleHttp\Client;
$mockServer = new MockServerEndpoint('/test');
$mockServer->allows('POST', '/')->withBody(['data' => 'This is a JSON test content.']);
use Nivseb\PhpMockServerConnector\PhpUnit\UseMockServer;
use Nivseb\PhpMockServerConnector\PhpUnit\MockServerEndpoint;
use PHPUnit\Framework\TestCase;
use GuzzleHttp\Client;
$mockServer = new MockServerEndpoint('/test');
$mockServer->allows('GET', '/')->andReturn(200, ['data' => 'This is a JSON test content.']);
use Nivseb\PhpMockServerConnector\PhpUnit\UseMockServer;
use Nivseb\PhpMockServerConnector\PhpUnit\MockServerEndpoint;
use PHPUnit\Framework\TestCase;
use GuzzleHttp\Client;
$mockServer = new MockServerEndpoint('/');
$mockServer->allows('GET', '/');
use Nivseb\PhpMockServerConnector\PhpUnit\UseMockServer;
use Nivseb\PhpMockServerConnector\PhpUnit\MockServerEndpoint;
use PHPUnit\Framework\TestCase;
use GuzzleHttp\Client;
class ExampleTest extends TestCase {
use UseMockServer;
protected function setUp(): void
{
parent::setUp();
$this->initMockServer('https://your_mock_server.localhost');
}
public function testMyRequest() : void {
$mockServer = new MockServerEndpoint('/rootPath');
$mockServer->allows('GET', '/mySubPath')->andReturn(200, ['data' => 'This is a JSON test content.'])
$client = new Client(['base_uri' => 'https://your_mock_server.localhost/rootPath'])
$response = $this->client->get('/mySubPath');
self::assertEquals(200, $response->getStatusCode());
self::assertEquals('{"data":"This is a JSON test content."}',$response->getBody()->getContents());
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.