Skip to content

Commit

Permalink
Merge pull request #243 from bowphp/5.x-refactoring-for-perf
Browse files Browse the repository at this point in the history
[5.x] Fixes testcase service errors
  • Loading branch information
papac authored May 24, 2023
2 parents 5cb3710 + 7f6f223 commit 7488f49
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 8 deletions.
20 changes: 14 additions & 6 deletions src/Http/Client/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ class HttpClient
*/
private $attach = [];

/**
* The headers collection
*
* @var array
*/
private $headers = [];

/**
* The curl instance
*
Expand Down Expand Up @@ -146,6 +153,7 @@ public function delete(string $url, array $data = []): Response
$this->applyCommonOptions();

curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, "DELETE");

$content = $this->execute();

return new Response($this->ch, $content);
Expand All @@ -170,14 +178,10 @@ public function addAttach(string|array $attach): array
*/
public function addHeaders(array $headers): HttpClient
{
$data = [];

foreach ($headers as $key => $value) {
$data[] = $key . ': ' . $value;
$this->headers[] = $key . ': ' . $value;
}

curl_setopt($this->ch, CURLOPT_HTTPHEADER, $data);

return $this;
}

Expand All @@ -189,7 +193,7 @@ public function addHeaders(array $headers): HttpClient
*/
private function init(string $url): void
{
if (is_null($this->base_url)) {
if (!is_null($this->base_url)) {
$url = $this->base_url . "/" . trim($url, "/");
}

Expand Down Expand Up @@ -227,6 +231,10 @@ private function close(): void
*/
private function execute(): string
{
if ($this->headers) {
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $this->headers);
}

$content = curl_exec($this->ch);
$errno = curl_errno($this->ch);

Expand Down
17 changes: 15 additions & 2 deletions src/Testing/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,31 @@ public function attach(array $attach): TestCase
}

/**
* Specify the additionnal who are use in the request
* Specify the additionnal headers
*
* @param array $headers
* @return TestCase
*/
public function withHeader(array $headers): TestCase
public function withHeaders(array $headers): TestCase
{
$this->headers = $headers;

return $this;
}

/**
* Specify the additionnal header
*
* @param array $headers
* @return TestCase
*/
public function withHeader(string $key, string $value): TestCase
{
$this->headers[$key] = $value;

return $this;
}

/**
* Get request
*
Expand Down
38 changes: 38 additions & 0 deletions tests/Support/HttpClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Bow\Tests\Support;

use Bow\Http\Client\HttpClient;
use PHPUnit\Framework\TestCase;

class HttpClientTest extends TestCase
{
public function test_get_method()
{
$http = new HttpClient();

$response = $http->get("https://google.com");

$this->assertEquals($response->statusCode(), 200);
}

public function test_get_method_with_custom_headers()
{
$http = new HttpClient();

$http->addHeaders(["X-Api-Key" => "Fake-Key"]);
$response = $http->get("https://google.com");

$this->assertEquals($response->statusCode(), 200);
}

public function test_should_be_fail_with_get_method()
{
$http = new HttpClient("https://google.com");

$http->addHeaders(["X-Api-Key" => "Fake-Key"]);
$response = $http->get("/the-fake-url");

$this->assertEquals($response->statusCode(), 404);
}
}
40 changes: 40 additions & 0 deletions tests/Support/TestingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Bow\Tests\Support;

use Bow\Testing\TestCase;

class TestingTest extends TestCase
{
/**
* The base url
*
* @var string
*/
protected ?string $url = "https://google.com";

public function test_get_method()
{
$response = $this->get("/");

$response->assertStatus(200);
}

public function test_get_method_with_custom_headers()
{
$this->withHeaders(["X-Api-Key" => "Fake-Key"]);

$response = $this->get("/");

$response->assertStatus(200);
}

public function test_should_be_fail_with_get_method()
{
$this->withHeaders(["X-Api-Key" => "Fake-Key"]);

$response = $this->get("/the-fake-url-for-my-testing-please-do-not-block-this");

$response->assertStatus(404);
}
}

0 comments on commit 7488f49

Please sign in to comment.