PHP code example of creativestyle / app-http-server-mock
1. Go to this page and download the library: Download creativestyle/app-http-server-mock 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/ */
creativestyle / app-http-server-mock example snippets
namespace YourNamespace;
use Creativestyle\AppHttpServerMock\Server;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class YourTestServer extends Server
{
protected function registerRequestHandlers()
{
$this->registerRequestHandler('GET', '/', function(Request $request) {
return new Response('Hello');
});
$this->registerRequestHandler(['PUT', 'POST'], '/number/(?<num>\d+)/test', function(Request $request, array $args) {
return [
'arrays' => [
'are',
'transformed',
'into',
'json' => ['how' => 'automatically']
],
'your_method' => $request->getMethod(),
'your_number' => $args['num']
];
});
}
}
namespace YouNamespace\Tests;
use YourNamespace\YourTestServer;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ConnectException;
use PHPUnit\Framework\TestCase;
class YourTest extends TestCase
{
/**
* @var YourTestServer
*/
private static $testServer;
public static function setUpBeforeClass()
{
self::$testServer = new YourTestServer();
self::$testServer->start();
}
public static function tearDownAfterClass()
{
self::$testServer->stop();
}
private function getClient()
{
return new Client([
'base_uri' => self::$testServer->getBaseUrl(),
'http_errors' => false
]);
}
public function testSomething()
{
$response = $this->getClient()->get('/');
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals('Hello', $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.