1. Go to this page and download the library: Download qoliber/servermock 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/ */
qoliber / servermock example snippets
use Qoliber\ServerMock\Mock;
use Qoliber\ServerMock\MockServer;
// Start a mock server on a random port
$server = MockServer::start();
// Define a stub
Mock::get('/api/users/1')
->respondJson(['id' => 1, 'name' => 'John Doe'])
->mount($server);
// Make requests against it
$response = file_get_contents($server->url('/api/users/1'));
// Returns: {"id":1,"name":"John Doe"}
// Clean up
$server->shutdown();
use Qoliber\ServerMock\ServerMockTestCase;
use Qoliber\ServerMock\Mock;
class MyIntegrationTest extends ServerMockTestCase
{
public function testPaymentGateway(): void
{
Mock::post('/v1/charges')
->respondJson(['id' => 'ch_123', 'status' => 'succeeded'], 201)
->mount($this->mockServer);
$paymentService = new PaymentService($this->getMockServerUri());
$result = $paymentService->charge(1000, 'usd');
$this->assertTrue($result->isSuccessful());
}
}
use PHPUnit\Framework\TestCase;
use Qoliber\ServerMock\ServerMockTrait;
use Qoliber\ServerMock\Mock;
class MyTest extends TestCase
{
use ServerMockTrait;
protected function setUp(): void
{
parent::setUp();
$this->setUpServerMock();
}
protected function tearDown(): void
{
$this->tearDownServerMock();
parent::tearDown();
}
}
$stub = Mock::get('/tracked')->respondOk()->mount($server);
$client->get($server->url('/tracked'));
$client->get($server->url('/tracked'));
$requests = $server->getRecordedRequests();
$count = $server->getRequestCount(); // 2
$server->assertCalled($stub); // At least once
$server->assertNotCalled($stub); // Never called
$server->verify($stub, 2); // Exactly 2 times
use Qoliber\ServerMock\State\StateMachine;
$scenario = StateMachine::create('order-flow')
->initialState('empty')
->when('empty')
->on(Mock::post('/cart/add'))
->respondJson(['success' => true, 'itemCount' => 1])
->transitionTo('has-items')
->when('has-items')
->on(Mock::get('/cart'))
->respondJson(['items' => ['SKU-001']])
->stay() // Stay in current state
->when('has-items')
->on(Mock::post('/checkout'))
->respondJson(['orderId' => 'ORD-123'])
->transitionTo('ordered')
->when('ordered')
->on(Mock::get('/order/ORD-123'))
->respondJson(['status' => 'confirmed'])
->getMachine();
$server->addStateMachine($scenario);
// Now requests trigger state transitions
$client->post('/cart/add'); // empty -> has-items
$client->get('/cart'); // stays in has-items
$client->post('/checkout'); // has-items -> ordered
// Inspect and control state
$state = $server->getScenarioState('order-flow'); // 'ordered'
$server->setScenarioState('order-flow', 'empty'); // Reset manually
$server->resetScenario('order-flow'); // Reset to initial
$server->resetAllScenarios(); // Reset all
$server->enableProxy('https://api.example.com', record: true);
// Requests that don't match any stub are forwarded
// With record: true, responses are recorded for later inspection
$recordings = $server->getProxyRecordings();
$server->disableProxy();
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.