/dev/null 2>&1 & echo $!;', str_replace('http://', '', self::URL), $fileToRun); self::$serverProcess = exec($command); if (empty(self::$serverProcess) || !is_numeric(self::$serverProcess)) { throw new Exception('Could not start test server'); } $this->log(sprintf('Test web server started on %s', self::URL)); sleep(1); } protected function tearDown(): void { if (is_numeric(self::$serverProcess)) { exec('kill ' . self::$serverProcess); } } protected function execute(): void { $this->testGetRequest(); $this->testGetWithQueryRequest(); $this->testPostJsonRequest(); $this->testPostFormRequest(); $this->testPostEmptyFormRequest(); $this->testWrongOptions(); $this->testWrongOptions2(); $this->testWrongOptions3(); $this->testWrongMethod(); $this->testWrongUrl(); } public function testGetRequest() { $response = http_client( ['base_url' => self::URL, 'headers' => ['Authorization' => 'Bearer secret_token']], function ($info) { $this->assertEquals( 'GET', $info['request']['method']); $this->assertEquals( 'Bearer secret_token', $info['request']['headers']['Authorization']); $this->assertEquals( '{"message":"GET request received"}', $info['response']['body']); } )->get('/api/data'); $this->assertEquals(200, $response->getStatusCode()); $this->assertNotEmpty($response->getBody()); } public function testGetWithQueryRequest() { $client = new HttpClient(['base_url' => self::URL, 'headers' => ['Authorization' => 'Bearer secret_token']]); $response = $client->get('/api/search', [ 'name' => 'foo', ]); $this->assertEquals(200, $response->getStatusCode()); $this->assertNotEmpty($response->getBody()); $data = $response->bodyToArray(); $this->assertEquals('foo', $data['name']); $this->assertEquals(1, $data['page']); $this->assertEquals(10, $data['limit']); $response = $client->get('/api/search', [ 'name' => 'foo', 'page' => 10, 'limit' => 100 ]); $this->assertEquals(200, $response->getStatusCode()); $this->assertNotEmpty($response->getBody()); $data = $response->bodyToArray(); $this->assertEquals('foo', $data['name']); $this->assertEquals(10, $data['page']); $this->assertEquals(100, $data['limit']); } public function testPostJsonRequest() { $dataToPost = [ 'title' => 'foo', 'body' => 'bar', 'userId' => 1 ]; $client = new HttpClient(['headers' => ['Authorization' => 'Bearer secret_token']]); $response = $client->post(self::URL . '/api/post/data', [ 'title' => 'foo', 'body' => 'bar', 'userId' => 1 ], true); $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals($dataToPost, $response->bodyToArray()); } public function testPostFormRequest() { $dataToPost = [ 'title' => 'foo', 'body' => 'bar', 'userId' => 1 ]; $client = new HttpClient(['headers' => ['Authorization' => 'Bearer secret_token']]); $response = $client->post(self::URL . '/api/post/data/form', $dataToPost); $this->assertEquals(200, $response->getStatusCode()); $this->assertEquals($dataToPost, $response->bodyToArray()); } public function testPostEmptyFormRequest() { $client = new HttpClient(['headers' => ['Authorization' => 'Bearer secret_token']]); $response = $client->post(self::URL . '/api/post/data/form', []); $this->assertEquals(400, $response->getStatusCode()); } public function testWrongOptions() { $this->expectException(LogicException::class, function () { new HttpClient(['headers' => 'string']); }); } public function testWrongOptions2() { $this->expectException(LogicException::class, function () { new HttpClient(['options_not_supported' => 'value']); }); } public function testWrongOptions3() { $this->expectException(LogicException::class, function () { new HttpClient(['timeout' => 'string']); }); } public function testWrongMethod() { $client = new HttpClient(['headers' => ['Authorization' => 'Bearer secret_token']]); $this->expectException(LogicException::class, function () use($client) { $client->fetch(self::URL . '/api/data', ['method' => 'WRONG']); }); } public function testWrongUrl() { $client = new HttpClient(['headers' => ['Authorization' => 'Bearer secret_token']]); $this->expectException(\InvalidArgumentException::class, function () use($client) { $client->fetch('WRONG_URL'); }); } }