Skip to content

Commit 53ccb48

Browse files
committed
refactor: enforces method enum in Request constructor
1 parent deafd0e commit 53ccb48

File tree

2 files changed

+9
-15
lines changed

2 files changed

+9
-15
lines changed

src/Providers/Http/DTO/Request.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,20 @@ class Request extends AbstractDataTransferObject
5757
*
5858
* @since n.e.x.t
5959
*
60-
* @param HttpMethodEnum|string $method The HTTP method.
60+
* @param HttpMethodEnum $method The HTTP method.
6161
* @param string $uri The request URI.
6262
* @param array<string, string|list<string>> $headers The request headers.
6363
* @param string|null $body The request body.
6464
*
65-
* @throws InvalidArgumentException If the URI is empty or method is invalid.
65+
* @throws InvalidArgumentException If the URI is empty.
6666
*/
67-
public function __construct($method, string $uri, array $headers = [], ?string $body = null)
67+
public function __construct(HttpMethodEnum $method, string $uri, array $headers = [], ?string $body = null)
6868
{
6969
if (empty($uri)) {
7070
throw new InvalidArgumentException('URI cannot be empty.');
7171
}
7272

73-
if (is_string($method)) {
74-
$this->method = HttpMethodEnum::from(strtoupper($method));
75-
} elseif ($method instanceof HttpMethodEnum) {
76-
$this->method = $method;
77-
} else {
78-
throw new InvalidArgumentException('Method must be a string or HttpMethodEnum instance.');
79-
}
80-
73+
$this->method = $method;
8174
$this->uri = $uri;
8275
$this->headers = $this->normalizeHeaders($headers);
8376
$this->body = $body;
@@ -283,7 +276,7 @@ public static function fromArray(array $array): self
283276
static::validateFromArrayData($array, [self::KEY_METHOD, self::KEY_URI, self::KEY_HEADERS]);
284277

285278
return new self(
286-
$array[self::KEY_METHOD],
279+
HttpMethodEnum::from($array[self::KEY_METHOD]),
287280
$array[self::KEY_URI],
288281
$array[self::KEY_HEADERS] ?? [],
289282
$array[self::KEY_BODY] ?? null

tests/unit/Providers/Http/HttpTransporterTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Psr\Http\Message\ResponseInterface;
1010
use WordPress\AiClient\Providers\Http\DTO\Request;
1111
use WordPress\AiClient\Providers\Http\DTO\Response;
12+
use WordPress\AiClient\Providers\Http\Enums\HttpMethodEnum;
1213
use WordPress\AiClient\Providers\Http\HttpTransporter;
1314
use GuzzleHttp\Psr7\Response as Psr7Response;
1415
use GuzzleHttp\Psr7\HttpFactory;
@@ -65,7 +66,7 @@ protected function setUp(): void
6566
public function testSendGetRequest(): void
6667
{
6768
// Arrange
68-
$request = new Request('GET', 'https://api.example.com/data');
69+
$request = new Request(HttpMethodEnum::GET(), 'https://api.example.com/data');
6970
$mockResponse = new Psr7Response(200, ['Content-Type' => 'application/json'], '{"success":true}');
7071
$this->mockClient->addResponse($mockResponse);
7172

@@ -97,7 +98,7 @@ public function testSendPostRequestWithBody(): void
9798
// Arrange
9899
$headers = ['Content-Type' => 'application/json'];
99100
$body = '{"name":"test"}';
100-
$request = new Request('POST', 'https://api.example.com/create', $headers, $body);
101+
$request = new Request(HttpMethodEnum::POST(), 'https://api.example.com/create', $headers, $body);
101102

102103
$mockResponse = new Psr7Response(201, ['Location' => '/resource/123'], '{"id":123}');
103104
$this->mockClient->addResponse($mockResponse);
@@ -136,7 +137,7 @@ public function testMultipleHeaderValues(): void
136137
'Accept' => ['application/json', 'application/xml'],
137138
'X-Custom' => 'single-value'
138139
];
139-
$request = new Request('GET', 'https://api.example.com', $headers);
140+
$request = new Request(HttpMethodEnum::GET(), 'https://api.example.com', $headers);
140141

141142
$mockResponse = new Psr7Response(200, ['Set-Cookie' => ['cookie1=value1', 'cookie2=value2']]);
142143
$this->mockClient->addResponse($mockResponse);

0 commit comments

Comments
 (0)