Skip to content

Commit

Permalink
Merge pull request #347 from saloonphp/feature/throw-an-exception-whe…
Browse files Browse the repository at this point in the history
…n-missing-method

Feature | Throw an exception when missing HTTP method
  • Loading branch information
Sammyjo20 committed Dec 7, 2023
2 parents d6f38f3 + fdae11f commit bf41570
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Saloon\Http;

use LogicException;
use Saloon\Enums\Method;
use Saloon\Traits\Bootable;
use Saloon\Traits\Makeable;
Expand Down Expand Up @@ -43,6 +44,10 @@ abstract class Request
*/
public function getMethod(): Method
{
if (! isset($this->method)) {
throw new LogicException('Your request is missing a HTTP method. You must add a method property like [protected Method $method = Method::GET]');
}

return $this->method;
}

Expand Down
18 changes: 18 additions & 0 deletions tests/Fixtures/Requests/MissingMethodRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Saloon\Tests\Fixtures\Requests;

use Saloon\Http\Request;

class MissingMethodRequest extends Request
{
/**
* Define the endpoint for the request.
*/
public function resolveEndpoint(): string
{
return '/user';
}
}
8 changes: 8 additions & 0 deletions tests/Unit/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Saloon\Tests\Fixtures\Responses\CustomResponse;
use Saloon\Exceptions\InvalidResponseClassException;
use Saloon\Tests\Fixtures\Requests\InvalidResponseClass;
use Saloon\Tests\Fixtures\Requests\MissingMethodRequest;
use Saloon\Tests\Fixtures\Requests\CustomEndpointRequest;
use Saloon\Tests\Fixtures\Requests\DefaultEndpointRequest;
use Saloon\Tests\Fixtures\Connectors\CustomBaseUrlConnector;
Expand Down Expand Up @@ -117,3 +118,10 @@
['', 'google.com/search', '/google.com/search'],
['https://google.com', 'https://api.google.com/search', 'https://api.google.com/search'],
]);

test('it throws an exception if you forget to add a method', function () {
$connector = new TestConnector;
$request = new MissingMethodRequest;

$connector->send($request);
})->throws(LogicException::class, 'Your request is missing a HTTP method. You must add a method property like [protected Method $method = Method::GET]');

0 comments on commit bf41570

Please sign in to comment.