Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature | V3 - Deprecated MockClient, use Global Mock Client #55

Merged
merged 1 commit into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: PHPStan

on:
push:
branches:
- 'v1'
- 'v2'
- 'v3'
pull_request:
branches:
- '*'

jobs:
phpstan:
name: phpstan
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.3'
coverage: none

- name: Install composer dependencies
uses: ramsey/composer-install@v2

- name: Run PHPStan
run: ./vendor/bin/phpstan analyse --error-format=github
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
fail-fast: true
matrix:
os: [ubuntu-latest, windows-latest]
php: [8.1, 8.2]
php: [8.1, 8.2, 8.3]
laravel: [10.*]
stability: [prefer-lowest, prefer-stable]
include:
Expand Down
15 changes: 7 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "saloonphp/laravel-plugin",
"description": "Laravel plugin for Saloon",
"description": "The official Laravel plugin for Saloon",
"license": "MIT",
"type": "library",
"keywords": [
"sammyjo20",
"saloonphp",
"saloon",
"sdk",
"api",
Expand All @@ -21,15 +21,14 @@
"require": {
"php": "^8.1",
"illuminate/console": "^10.0",
"illuminate/http": "^10.0",
"illuminate/support": "^10.0",
"saloonphp/saloon": "^3.0"
"saloonphp/saloon": "^3.5"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.5",
"orchestra/testbench": "^v7.30 || ^v8.10",
"pestphp/pest": "^v1.23",
"phpstan/phpstan": "^1.9"
"friendsofphp/php-cs-fixer": "^3.48",
"orchestra/testbench": "^8.21",
"pestphp/pest": "^2.32",
"phpstan/phpstan": "^1.10.56"
},
"minimum-stability": "stable",
"autoload": {
Expand Down
4 changes: 1 addition & 3 deletions src/Casts/EncryptedOAuthAuthenticatorCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ public function get($model, string $key, $value, array $attributes): ?OAuthAuthe

/**
* Prepare the given value for storage.
*
* @return mixed|void
*/
public function set($model, string $key, $value, array $attributes)
public function set($model, string $key, $value, array $attributes): ?string
{
if (is_null($value)) {
return null;
Expand Down
4 changes: 1 addition & 3 deletions src/Casts/OAuthAuthenticatorCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ public function get($model, string $key, $value, array $attributes): ?OAuthAuthe

/**
* Prepare the given value for storage.
*
* @return mixed|void
*/
public function set($model, string $key, $value, array $attributes)
public function set($model, string $key, $value, array $attributes): ?string
{
if (is_null($value)) {
return null;
Expand Down
112 changes: 88 additions & 24 deletions src/Console/Commands/ListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ListCommand extends Command
/**
* Execute the console command.
*/
public function handle()
public function handle(): int
{
$this->newLine();

Expand Down Expand Up @@ -63,43 +63,84 @@ public function handle()
$this->newLine();
}

return Command::SUCCESS;
return self::SUCCESS;
}


/**
* @return array<int, string>
*/
protected function getIntegrations(): array
{
return glob(config('saloon.integrations_path').'/*') ?? [];
if (! $files = glob(config('saloon.integrations_path').'/*')) {
return [];
}

return $files;
}

/**
* @return array<int, string>
*/
protected function getIntegrationConnectors(string $integration): array
{
return glob($integration . '/*Connector.php') ?? [];
if (! $files = glob($integration . '/*Connector.php')) {
return [];
}

return $files;
}

/**
* @return array<int, string>
*/
protected function getIntegrationRequests(string $integration): array
{
return glob($integration . '/Requests/*') ?? [];
if (! $files = glob($integration . '/Requests/*')) {
return [];
}

return $files;
}

/**
* @return array<int, string>
*/
protected function getIntegrationPlugins(string $integration): array
{
return glob($integration . '/Plugins/*') ?? [];
if (! $files = glob($integration . '/Plugins/*')) {
return [];
}

return $files;
}

/**
* @return array<int, string>
*/
protected function getIntegrationResponses(string $integration): array
{
return glob($integration . '/Responses/*') ?? [];
if (! $files = glob($integration . '/Responses/*')) {
return [];
}

return $files;
}

/**
* @return array<int, string>
*/
protected function getIntegrationAuthenticators(string $integration): array
{
return glob($integration . '/Auth/*') ?? [];
if (! $files = glob($integration . '/Auth/*')) {
return [];
}

return $files;
}

protected function getIntegrationOutput(string $integration)
protected function getIntegrationOutput(string $integration): void
{
return $this->components->twoColumnDetail(
$this->components->twoColumnDetail(
'<fg=green;options=bold>' . Str::afterLast($integration, '/') . '</>',
sprintf(
'<fg=white>Authenticators: %s / Connectors: %s / Requests: %s / Plugins: %s / Responses: %s</>',
Expand All @@ -112,22 +153,22 @@ protected function getIntegrationOutput(string $integration)
);
}

protected function getIntegrationAuthenticatorOutput(string $authenticator)
protected function getIntegrationAuthenticatorOutput(string $authenticator): void
{
return $this->components->twoColumnDetail(
$this->components->twoColumnDetail(
'<fg=red>Authenticator</> <fg=gray>...</> ' . Str::afterLast($authenticator, '/')
);
}

protected function getIntegrationConnectorOutput(string $connector)
protected function getIntegrationConnectorOutput(string $connector): void
{
return $this->components->twoColumnDetail(
$this->components->twoColumnDetail(
'<fg=blue>Connector</> <fg=gray>.......</> ' . Str::afterLast($connector, '/'),
'<fg=gray>' . $this->getIntegrationConnectorBaseUrl($connector) . '</>'
);
}

protected function getIntegrationRequestOutput(string $request)
protected function getIntegrationRequestOutput(string $request): void
{
$requestMethod = Str::afterLast($this->getIntegrationRequestMethod($request), ':');

Expand All @@ -138,7 +179,7 @@ protected function getIntegrationRequestOutput(string $request)
default => 'magenta'
};

return $this->components->twoColumnDetail(
$this->components->twoColumnDetail(
'<fg=magenta>Request</> <fg=gray>.........</> ' .
Str::afterLast($request, '/'),
' <fg=gray>' . $this->getIntegrationRequestEndpoint($request) . '</>' .
Expand All @@ -147,29 +188,45 @@ protected function getIntegrationRequestOutput(string $request)
);
}

protected function getIntegrationPluginOutput(string $plugin)

protected function getIntegrationPluginOutput(string $plugin): void
{
return $this->components->twoColumnDetail(
$this->components->twoColumnDetail(
'<fg=cyan>Plugin</> <fg=gray>..........</> ' . Str::afterLast($plugin, '/')
);
}

protected function getIntegrationResponseOutput(string $response)

protected function getIntegrationResponseOutput(string $response): void
{
return $this->components->twoColumnDetail(
$this->components->twoColumnDetail(
'<fg=yellow>Response</> <fg=gray>........</> ' . Str::afterLast($response, '/')
);
}

protected function getIntegrationRequestMethod(string $request): string
{
return Str::match('/\$method\s*=\s*(.*?);/', file_get_contents($request));
$contents = file_get_contents($request);

if ($contents === false) {
$contents = '';
}

return Str::match('/\$method\s*=\s*(.*?);/', $contents);
}


protected function getIntegrationRequestEndpoint(string $request): string
{
$contents = file_get_contents($request);

if ($contents === false) {
$contents = '';
}

$regex = '/public\s+function\s+resolveEndpoint\(\):\s+string\s*\{\s*return\s+(.*?);/s';
$match = Str::match($regex, file_get_contents($request));

$match = Str::match($regex, $contents);
$matchSegments = explode('/', $match);

foreach ($matchSegments as $key => $matchSegment) {
Expand All @@ -184,10 +241,17 @@ protected function getIntegrationRequestEndpoint(string $request): string
return str_replace('\'', '', implode('/', $matchSegments));
}


protected function getIntegrationConnectorBaseUrl(string $connector): string
{
$contents = file_get_contents($connector);

if ($contents === false) {
$contents = '';
}

$regex = '/public\s+function\s+resolveBaseUrl\(\):\s+string\s*\{\s*return\s+\'(.*?)\';\s*/s';
$matches = Str::match($regex, file_get_contents($connector));
$matches = Str::match($regex, $contents);

return Str::after($matches, '://');
}
Expand Down
7 changes: 6 additions & 1 deletion src/Console/Commands/MakeConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ protected function resolveStubName(): string
: 'saloon.connector.stub';
}

protected function getOptions()
/**
* Get the options for making a connector
*
* @return array<int, array<mixed>>
*/
protected function getOptions(): array
{
return [
['oauth', null, InputOption::VALUE_NONE, 'Whether the connector should include the OAuth boilerplate'],
Expand Down
14 changes: 13 additions & 1 deletion src/Console/Commands/MakeRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Saloon\Enums\Method;
use Illuminate\Support\Arr;
use InvalidArgumentException;
use function Laravel\Prompts\select;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
Expand Down Expand Up @@ -49,6 +50,11 @@ class MakeRequest extends MakeCommand
*/
protected $stub = 'saloon.request.stub';

/**
* Get the options for making a request
*
* @return array<int, array<mixed>>
*/
protected function getOptions(): array
{
return [
Expand Down Expand Up @@ -82,8 +88,14 @@ protected function afterPromptingForMissingArguments(InputInterface $input, Outp
*/
protected function buildClass($name): MakeRequest|string
{
$method = $this->option('method') ?? 'GET';

if (! is_string($method)) {
throw new InvalidArgumentException('The method option must be a string.');
}

$stub = $this->files->get($this->getStub());
$stub = $this->replaceMethod($stub, $this->option('method') ?? 'GET');
$stub = $this->replaceMethod($stub, $method);

return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
}
Expand Down
6 changes: 4 additions & 2 deletions src/Facades/Saloon.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
namespace Saloon\Laravel\Facades;

use Saloon\Http\Response;
use Illuminate\Support\Facades\Facade;
use Saloon\Laravel\Http\Faking\MockClient;
use Illuminate\Support\Facades\Facade as BaseFacade;

/**
* @deprecated You should use MockClient::global() instead. This facade will be removed in Saloon v4.
*
* @see \Saloon\Laravel\Saloon
*
* @method static MockClient fake(array $responses)
Expand All @@ -25,7 +27,7 @@
* @method static \Saloon\Http\Response[] getRecordedResponses()
* @method static \Saloon\Http\Response getLastRecordedResponse()
*/
class Saloon extends BaseFacade
class Saloon extends Facade
{
/**
* Register the Saloon facade
Expand Down
Loading
Loading