Skip to content

Commit 63a1e35

Browse files
committed
- fixes the wrong unit tests for PhpClient
1 parent 36c2747 commit 63a1e35

File tree

3 files changed

+108
-53
lines changed

3 files changed

+108
-53
lines changed

Tests/Client/ClientTestCaseTrait.php

Lines changed: 0 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -49,59 +49,6 @@ public function test_should_exit_on_bad_request()
4949
(string)$badResponse->getBody());
5050
}
5151

52-
public function test_when_curl_returns_error()
53-
{
54-
$SUT = new class('get', 'http://example.com') extends CurlClient
55-
{
56-
protected function hasError($resource): bool
57-
{
58-
return true;
59-
}
60-
};
61-
62-
$response = $SUT->read();
63-
64-
$this->assertInstanceOf(ServerResponse::class, $response);
65-
$this->assertSame($response->getHeaderLine('Content-type'), 'application/problem+json');
66-
$this->assertSame(StatusCode::FAILED_DEPENDENCY, $response->getStatusCode(),
67-
(string)$response->getBody());
68-
}
69-
70-
public function test_when_creating_resource_fails()
71-
{
72-
$SUT = new class('get', 'http://example.com') extends CurlClient
73-
{
74-
protected function createResource(): \CurlHandle|bool
75-
{
76-
return false;
77-
}
78-
};
79-
80-
$response = $SUT->read();
81-
82-
$this->assertInstanceOf(ServerResponse::class, $response);
83-
$this->assertSame($response->getHeaderLine('Content-type'), 'application/problem+json');
84-
$this->assertSame(StatusCode::FAILED_DEPENDENCY, $response->getStatusCode());
85-
$this->assertStringContainsString('The HTTP client is not created therefore cannot read anything',
86-
(string)$response->getBody());
87-
}
88-
89-
public function test_on_exception()
90-
{
91-
$SUT = new class('get', 'http://example.com') extends CurlClient
92-
{
93-
protected function createResource(): \CurlHandle|bool
94-
{
95-
throw new \Exception('Exception message');
96-
}
97-
};
98-
$response = $SUT->read();
99-
100-
$this->assertSame($response->getHeaderLine('Content-type'), 'application/problem+json');
101-
$this->assertSame(StatusCode::INTERNAL_SERVER_ERROR, $response->getStatusCode());
102-
$this->assertStringContainsString('Exception message', (string)$response->getBody());
103-
}
104-
10552
protected function tearDown(): void
10653
{
10754
$this->SUT = null;

Tests/Client/CurlClientTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
namespace Tests\Koded\Http\Client;
44

55
use Koded\Http\Client\ClientFactory;
6+
use Koded\Http\Client\CurlClient;
67
use Koded\Http\Interfaces\HttpRequestClient;
8+
use Koded\Http\ServerResponse;
9+
use Koded\Http\StatusCode;
710
use PHPUnit\Framework\TestCase;
811
use Tests\Koded\Http\AssertionTestSupportTrait;
912

@@ -70,6 +73,57 @@ public function test_protocol_version()
7073
$this->assertSame(CURL_HTTP_VERSION_1_0, $options[CURLOPT_HTTP_VERSION]);
7174
}
7275

76+
public function test_when_curl_returns_error()
77+
{
78+
$SUT = new class('get', 'http://example.com') extends CurlClient
79+
{
80+
protected function hasError($resource): bool
81+
{
82+
return true;
83+
}
84+
};
85+
$response = $SUT->read();
86+
87+
$this->assertInstanceOf(ServerResponse::class, $response);
88+
$this->assertSame($response->getHeaderLine('Content-type'), 'application/problem+json');
89+
$this->assertSame(StatusCode::FAILED_DEPENDENCY, $response->getStatusCode(),
90+
(string)$response->getBody());
91+
}
92+
93+
public function test_when_creating_resource_fails()
94+
{
95+
$SUT = new class('get', 'http://example.com') extends CurlClient
96+
{
97+
protected function createResource(): \CurlHandle|bool
98+
{
99+
return false;
100+
}
101+
};
102+
$response = $SUT->read();
103+
104+
$this->assertInstanceOf(ServerResponse::class, $response);
105+
$this->assertSame($response->getHeaderLine('Content-type'), 'application/problem+json');
106+
$this->assertSame(StatusCode::FAILED_DEPENDENCY, $response->getStatusCode());
107+
$this->assertStringContainsString('The HTTP client is not created therefore cannot read anything',
108+
(string)$response->getBody());
109+
}
110+
111+
public function test_on_exception()
112+
{
113+
$SUT = new class('get', 'http://example.com') extends CurlClient
114+
{
115+
protected function createResource(): \CurlHandle|bool
116+
{
117+
throw new \Exception('Exception message');
118+
}
119+
};
120+
$response = $SUT->read();
121+
122+
$this->assertSame($response->getHeaderLine('Content-type'), 'application/problem+json');
123+
$this->assertSame(StatusCode::INTERNAL_SERVER_ERROR, $response->getStatusCode());
124+
$this->assertStringContainsString('Exception message', (string)$response->getBody());
125+
}
126+
73127
/**
74128
* @group internet
75129
*/

Tests/Client/PhpClientTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
namespace Tests\Koded\Http\Client;
44

55
use Koded\Http\Client\ClientFactory;
6+
use Koded\Http\Client\PhpClient;
67
use Koded\Http\Interfaces\HttpRequestClient;
8+
use Koded\Http\ServerResponse;
9+
use Koded\Http\StatusCode;
710
use PHPUnit\Framework\TestCase;
811
use Tests\Koded\Http\AssertionTestSupportTrait;
912

@@ -58,6 +61,57 @@ public function test_setting_the_client_with_methods()
5861
$this->assertSame(false, $options['ssl']['verify_peer']);
5962
}
6063

64+
public function test_when_curl_returns_error()
65+
{
66+
$SUT = new class('get', 'http://example.com') extends PhpClient
67+
{
68+
protected function hasError($resource): bool
69+
{
70+
return true;
71+
}
72+
};
73+
$response = $SUT->read();
74+
75+
$this->assertInstanceOf(ServerResponse::class, $response);
76+
$this->assertSame($response->getHeaderLine('Content-type'), 'application/problem+json');
77+
$this->assertSame(StatusCode::FAILED_DEPENDENCY, $response->getStatusCode(),
78+
(string)$response->getBody());
79+
}
80+
81+
public function test_when_creating_resource_fails()
82+
{
83+
$SUT = new class('get', 'http://example.com') extends PhpClient
84+
{
85+
protected function createResource($resource)
86+
{
87+
return false;
88+
}
89+
};
90+
$response = $SUT->read();
91+
92+
$this->assertInstanceOf(ServerResponse::class, $response);
93+
$this->assertSame($response->getHeaderLine('Content-type'), 'application/problem+json');
94+
$this->assertSame(StatusCode::FAILED_DEPENDENCY, $response->getStatusCode());
95+
$this->assertStringContainsString('The HTTP client is not created therefore cannot read anything',
96+
(string)$response->getBody());
97+
}
98+
99+
public function test_on_exception()
100+
{
101+
$SUT = new class('get', 'http://example.com') extends PhpClient
102+
{
103+
protected function createResource($resource)
104+
{
105+
throw new \Exception('Exception message');
106+
}
107+
};
108+
$response = $SUT->read();
109+
110+
$this->assertSame($response->getHeaderLine('Content-type'), 'application/problem+json');
111+
$this->assertSame(StatusCode::INTERNAL_SERVER_ERROR, $response->getStatusCode());
112+
$this->assertStringContainsString('Exception message', (string)$response->getBody());
113+
}
114+
61115
protected function setUp(): void
62116
{
63117
$this->SUT = (new ClientFactory(ClientFactory::PHP))

0 commit comments

Comments
 (0)