Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/2.x' into 2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkGhostHunter committed Mar 14, 2024
2 parents 4e57516 + 722ba5e commit 4d12373
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 31 deletions.
15 changes: 8 additions & 7 deletions src/ApiRequestProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\ForwardsCalls;
use Laragear\ApiManager\Attributes\UseResponse;
use LogicException;
use ReflectionMethod;
use ReflectionProperty;

use function array_merge;
use function array_splice;
use function class_basename;
Expand Down Expand Up @@ -72,19 +72,19 @@ protected function createRequest(): PendingRequest
->when(
$this->api->authBasic(),
static function (PendingRequest $request, array $auth): PendingRequest {
return $request->withBasicAuth(... $auth);
return $request->withBasicAuth(...$auth);
}
)
->when(
$this->api->authDigest(),
static function (PendingRequest $request, array $auth): PendingRequest {
return $request->withDigestAuth(... $auth);
return $request->withDigestAuth(...$auth);
}
)
->when(
$this->api->authToken(),
static function (PendingRequest $request, array|string $auth): PendingRequest {
return $request->withToken(... (array) $auth);
return $request->withToken(...(array) $auth);
}
);

Expand All @@ -99,7 +99,7 @@ static function (PendingRequest $request, array|string $auth): PendingRequest {
public function on(Pool $pool, string $as = null): static
{
// We will have to retrieve by force the pool values.
$handler = tap((new ReflectionProperty($pool, 'handler')))->setAccessible(true)->getValue($pool);
$handler = tap(new ReflectionProperty($pool, 'handler'))->setAccessible(true)->getValue($pool);
$requests = (new ReflectionProperty($pool, 'pool'));

$request = $this->getApiRequest()->setHandler($handler)->async();
Expand Down Expand Up @@ -163,8 +163,9 @@ protected function executeApiAction(string $name, string $action, array $paramet
/**
* Wrap the response or promise into a custom response if found.
*
* @param mixed $response
* @param string $name
* @param mixed $response
* @param string $name
*
* @return mixed
*/
protected function wrapResponse(mixed $response, string $name): mixed
Expand Down
10 changes: 7 additions & 3 deletions src/ApiServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Traits\Conditionable;
use Illuminate\Support\Traits\Tappable;

use function app;

abstract class ApiServer
Expand Down Expand Up @@ -50,7 +51,8 @@ abstract public function getBaseUrl();
/**
* Modify a pristine new Pending Request.
*
* @param \Illuminate\Http\Client\PendingRequest $request
* @param \Illuminate\Http\Client\PendingRequest $request
*
* @return \Illuminate\Http\Client\PendingRequest|null|void
*/
public function beforeBuild(PendingRequest $request)
Expand All @@ -61,7 +63,8 @@ public function beforeBuild(PendingRequest $request)
/**
* Modify Pending Request after its bootstrapped.
*
* @param \Illuminate\Http\Client\PendingRequest $request
* @param \Illuminate\Http\Client\PendingRequest $request
*
* @return \Illuminate\Http\Client\PendingRequest|null|void
*/
public function afterBuild(PendingRequest $request)
Expand All @@ -74,7 +77,8 @@ public function afterBuild(PendingRequest $request)
*
* @deprecated Use `afterBuild()` instead.
*
* @param \Illuminate\Http\Client\PendingRequest $request
* @param \Illuminate\Http\Client\PendingRequest $request
*
* @return \Illuminate\Http\Client\PendingRequest|null|void
*/
public function build(PendingRequest $request)
Expand Down
8 changes: 5 additions & 3 deletions src/Console/Commands/MakeApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Console\GeneratorCommand;
use Illuminate\Support\Str;
use Symfony\Component\Console\Attribute\AsCommand;

use function class_basename;
use function file_exists;
use function str_replace;
Expand Down Expand Up @@ -40,7 +41,8 @@ class MakeApi extends GeneratorCommand
/**
* Build the class with the given name.
*
* @param string $name
* @param string $name
*
* @return string
*/
protected function buildClass($name)
Expand All @@ -55,7 +57,7 @@ protected function buildClass($name)
*/
protected function rootNamespace()
{
return $this->laravel->getNamespace() . 'Http\Apis\\';
return $this->laravel->getNamespace().'Http\Apis\\';
}

/**
Expand All @@ -67,7 +69,7 @@ protected function getPath($name)
{
$name = Str::replaceFirst($this->laravel->getNamespace(), '', $name);

return $this->laravel['path'].'/'. str_replace('\\', '/', $name).'.php';
return $this->laravel['path'].'/'.str_replace('\\', '/', $name).'.php';
}

/**
Expand Down
11 changes: 6 additions & 5 deletions src/Console/Commands/MakeApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Str;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Input\InputArgument;

use function file_exists;

/**
Expand Down Expand Up @@ -38,9 +39,9 @@ class MakeApiResponse extends GeneratorCommand
/**
* Execute the console command.
*
* @return bool|null
*
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*
* @return bool|null
*/
public function handle()
{
Expand All @@ -59,7 +60,7 @@ protected function apiDoesntExists(): bool
{
$api = Str::ucfirst($this->argument('api'));

return ! file_exists($this->getPath($this->laravel->getNamespace() . "Http\Apis\\$api"));
return !file_exists($this->getPath($this->laravel->getNamespace()."Http\Apis\\$api"));
}

/**
Expand Down Expand Up @@ -89,7 +90,7 @@ protected function rootNamespace()
{
$api = Str::studly($this->argument('api'));

return $this->laravel->getNamespace() . "Http\Apis\\$api\\Responses\\";
return $this->laravel->getNamespace()."Http\Apis\\$api\\Responses\\";
}

/**
Expand Down Expand Up @@ -131,7 +132,7 @@ protected function getArguments()
{
return [
['api', InputArgument::REQUIRED, 'The name of the API for the namespace of the custom response'],
['name', InputArgument::REQUIRED, 'The name of the '.strtolower($this->type)]
['name', InputArgument::REQUIRED, 'The name of the '.strtolower($this->type)],
];
}

Expand Down
27 changes: 18 additions & 9 deletions tests/ApiRequestProxyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Laragear\ApiManager\ApiServer;
use LogicException;
use PHPUnit\Framework\Attributes\Test;

use function func_get_args;

class ApiRequestProxyTest extends TestCase
Expand Down Expand Up @@ -86,6 +87,7 @@ public function builds_on_inline_action_get(): void
Http::assertSent(static function (Request $request): bool {
static::assertSame('GET', $request->method());
static::assertSame('https://www.test.com/foo/action', $request->url());

return true;
});
}
Expand All @@ -100,6 +102,7 @@ public function builds_on_inline_action_with_get_verb(): void
Http::assertSent(static function (Request $request): bool {
static::assertSame('GET', $request->method());
static::assertSame('https://www.test.com/bar/action', $request->url());

return true;
});
}
Expand All @@ -113,6 +116,7 @@ public function builds_on_inline_action_using_camel_case(): void

Http::assertSent(static function (Request $request): bool {
static::assertSame('https://www.test.com/baz/quz', $request->url());

return true;
});
}
Expand All @@ -127,6 +131,7 @@ public function builds_on_inline_action_using_http_verb(): void
Http::assertSent(static function (Request $request): bool {
static::assertSame('POST', $request->method());
static::assertSame('https://www.test.com/baz/quz', $request->url());

return true;
});
}
Expand All @@ -141,6 +146,7 @@ public function builds_on_inline_action_with_url_parameters(): void
Http::assertSent(static function (Request $request): bool {
static::assertSame('POST', $request->method());
static::assertSame('https://www.test.com/baz/quz/10', $request->url());

return true;
});
}
Expand All @@ -157,6 +163,7 @@ public function builds_on_inline_action_with_hacky_verb_and_path(): void
Http::assertSent(static function (Request $request): bool {
static::assertSame('GET', $request->method());
static::assertSame('www.google.com/test', $request->url());

return true;
});
}
Expand Down Expand Up @@ -223,6 +230,7 @@ public function forwards_calls_to_the_request(): void
Http::assertSent(static function (Request $request): bool {
static::assertSame('GET', $request->method());
static::assertSame('www.google.com/test', $request->url());

return true;
});
}
Expand Down Expand Up @@ -256,6 +264,7 @@ public function forwards_properties_to_api_server_inline_action(): void
Http::assertSent(static function (Request $request): bool {
static::assertSame('GET', $request->method());
static::assertSame('https://www.test.com/foo/action', $request->url());

return true;
});
}
Expand Down Expand Up @@ -456,14 +465,14 @@ public function getBaseUrl(): string
}

public $actions = [
'foo' => 'foo/action',
'bar' => 'get:bar/action',
'baz quz' => 'post:baz/quz',
'parameter' => 'post:baz/quz/{id}',
'invalid' => 'invalid:/something',
'hacky' => 'baseUrl:www.google.com',
'override' => 'get:/not-overridden',
'as property' => 'get:/not-overridden-property'
'foo' => 'foo/action',
'bar' => 'get:bar/action',
'baz quz' => 'post:baz/quz',
'parameter' => 'post:baz/quz/{id}',
'invalid' => 'invalid:/something',
'hacky' => 'baseUrl:www.google.com',
'override' => 'get:/not-overridden',
'as property' => 'get:/not-overridden-property',
];

public function override(PendingRequest $request, string $message)
Expand Down Expand Up @@ -565,7 +574,7 @@ public function example(PendingRequest $request)
class TestAuthWrapRequestServerAction extends ApiServer
{
public $actions = [
'example' => '200'
'example' => '200',
];

public $responses = [
Expand Down
1 change: 0 additions & 1 deletion tests/ApiServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public function builds_itself(): void
{
static::assertInstanceOf(TestSelfRegistrableApiServer::class, TestSelfRegistrableApiServer::api()->api);
}

}

class TestSelfRegistrableApiServer extends ApiServer
Expand Down
1 change: 0 additions & 1 deletion tests/Fixtures/CustomResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,4 @@

class CustomResponse extends Response
{

}
4 changes: 2 additions & 2 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
namespace Tests;

use Laragear\ApiManager\ApiManagerServiceProvider;
use Laragear\ApiManager\Facades\Api;
use Orchestra\Testbench\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
/**
* Get package providers.
*
* @param \Illuminate\Foundation\Application $app
* @param \Illuminate\Foundation\Application $app
*
* @return array
*/
protected function getPackageProviders($app)
Expand Down

0 comments on commit 4d12373

Please sign in to comment.