PHP code example of mock-server / mockserver-testcontainers

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

    

mock-server / mockserver-testcontainers example snippets


use MockServer\Testcontainers\MockServerContainer;
use MockServer\HttpRequest;
use MockServer\HttpResponse;

$container = (new MockServerContainer())->start();

// A ready-wired MockServer client pointed at the mapped host/port.
// (getMockServerClient() is the PHP analogue of Java's getClient() — the
// inherited getClient() name is reserved by Testcontainers for the Docker client.)
$client = $container->getMockServerClient();
$client
    ->when(HttpRequest::request()->method('GET')->path('/hello'))
    ->respond(HttpResponse::response()->statusCode(200)->body('world'));

// Point the system under test at the container endpoint
$container->getEndpoint();        // "http://localhost:49152"
$container->getSecureEndpoint();  // "https://localhost:49152" (HTTP/HTTPS share the port)

$container->stop();

new MockServerContainer('mockserver/mockserver:mockserver-9.0.0');