Skip to content

Commit

Permalink
Merge pull request #18 from xray-labs/enhancement/update-pest-to-v3
Browse files Browse the repository at this point in the history
[Enhancement] update pest to v3
  • Loading branch information
sjspereira authored Sep 14, 2024
2 parents c871322 + 6a396c9 commit 10d60e7
Show file tree
Hide file tree
Showing 75 changed files with 844 additions and 516 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ jobs:
run: ./vendor/bin/phpstan analyse

- name: Run Pest
run: ./vendor/bin/pest --coverage --min=100 --parallel
run: ./vendor/bin/pest --coverage --min=76 --parallel
2 changes: 1 addition & 1 deletion captainhook.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"action": "./vendor/bin/phpstan analyse"
},
{
"action": "./vendor/bin/pest --coverage --min=100 --parallel"
"action": "./vendor/bin/pest --coverage --min=76 --parallel"
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"require-dev": {
"laravel/pint": "^1.16",
"phpstan/phpstan": "^1.11",
"pestphp/pest": "^2.34",
"pestphp/pest": "^3.0",
"symfony/var-dumper": "^7.0",
"phpmd/phpmd": "^2.15",
"squizlabs/php_codesniffer": "^3.10",
Expand Down
931 changes: 532 additions & 399 deletions composer.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ parameters:
ignoreErrors:
-
messages:
- '#Call to an undefined method Pest\\PendingCalls\\TestCall\:\:expect\(\)#'
- '#Call to an undefined method Pest\\PendingCalls\\TestCall\:\:(expect|preset)\(\)#'
- '#Undefined variable\: \$this#'
- '#Call to an undefined method Pest(\\Mixins)?\\Expectation\<.+?\>\:\:[a-zA-Z]+?\(\)#'
- '#Access to an undefined property Pest(\\Mixins)?\\Expectation\<.+?\>\:\:\$[a-zA-Z]+?#'
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</include>

<exclude>
<directory suffix=".php">./src/Tests</directory>
<directory suffix=".php">./src/Fakes</directory>
</exclude>
</source>
</phpunit>
6 changes: 5 additions & 1 deletion src/BlobStorage/BlobStorageClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@

namespace Xray\AzureStoragePhpSdk\BlobStorage;

use Xray\AzureStoragePhpSdk\BlobStorage\Concerns\HasFakeRequest;
use Xray\AzureStoragePhpSdk\BlobStorage\Managers\Blob\BlobManager;
use Xray\AzureStoragePhpSdk\BlobStorage\Managers\{AccountManager, ContainerManager};
use Xray\AzureStoragePhpSdk\Contracts\Authentication\Auth;
use Xray\AzureStoragePhpSdk\Contracts\Http\Request as RequestContract;
use Xray\AzureStoragePhpSdk\Contracts\{Converter, Parser};
use Xray\AzureStoragePhpSdk\Http\Request;

final class BlobStorageClient
class BlobStorageClient
{
use HasFakeRequest;

public function __construct(protected RequestContract $request)
{
azure_app()->instance(RequestContract::class, $this->request);
Expand All @@ -22,6 +25,7 @@ public function __construct(protected RequestContract $request)
/** @param array{version?: string, parser?: Parser, converter?: Converter} $config */
public static function create(Auth $auth, array $config = []): static
{
/** @phpstan-ignore-next-line */
return new static(new Request($auth, new Config($config)));
}

Expand Down
20 changes: 20 additions & 0 deletions src/BlobStorage/Concerns/HasFakeRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Xray\AzureStoragePhpSdk\BlobStorage\Concerns;

use Xray\AzureStoragePhpSdk\BlobStorage\Config;
use Xray\AzureStoragePhpSdk\Contracts\Authentication\Auth;
use Xray\AzureStoragePhpSdk\Contracts\{Converter, Parser};
use Xray\AzureStoragePhpSdk\Fakes\Http\RequestFake;

trait HasFakeRequest
{
/** @param array{version?: string, parser?: Parser, converter?: Converter} $config */
public static function fake(?Auth $auth = null, array $config = []): static
{
/** @phpstan-ignore-next-line */
return new static(new RequestFake($auth, new Config($config)));
}
}
2 changes: 1 addition & 1 deletion src/BlobStorage/Concerns/ValidateContainerName.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ protected function validateContainerName(string $name): void
{
$replaced = preg_replace('/[^a-z0-9-]/', '', $name);

if ($replaced !== $name) {
if (trim($name) === '' || $replaced !== $name) {
throw InvalidArgumentException::create("Invalid container name: {$name}");
}
}
Expand Down
33 changes: 26 additions & 7 deletions src/BlobStorage/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,42 @@ final class Resource

public static function canonicalize(string $uri): string
{
/** @var array<string, string> */
$parsed = parse_url($uri);
$parsed = static::parseUrl($uri);

parse_str($parsed['query'] ?? '', $queryParams);

ksort($queryParams);
ksort($parsed['query']);

$result = '';

/**
* @var string $value
* @var string $key
*/
foreach ($queryParams as $key => $value) {
foreach ($parsed['query'] as $key => $value) {
$result .= mb_convert_case($key, MB_CASE_LOWER, 'UTF-8') . ':' . $value . "\n";
}

return $parsed['path'] . "\n" . rtrim($result, "\n");
return "{$parsed['path']}\n" . rtrim($result, "\n");
}

/** @return array{path: string, query: array<string, string>} */
protected static function parseUrl(string $uri): array
{
/** @var array<string, string> */
$parsed = parse_url($uri);

/** @var string $path */
$path = $parsed['path'] ?? '';

$queryParams = trim($parsed['query'] ?? '') === ''
? []
: array_reduce(explode('&', $parsed['query']), function (array $carry, string $query): array {
$parts = explode('=', $query);

$carry[$parts[0]] = $parts[1];

return $carry;
}, []);

return ['path' => $path, 'query' => $queryParams];
}
}
2 changes: 1 addition & 1 deletion src/Exceptions/CouldNotCreateTempFileException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Exception;

class CouldNotCreateTempFileException extends Exception
final class CouldNotCreateTempFileException extends Exception
{
protected function __construct(string $message)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidResourceTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Exception;

class InvalidResourceTypeException extends Exception
final class InvalidResourceTypeException extends Exception
{
public static function create(string $message): self
{
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/ManagerNotSetException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Exception;

class ManagerNotSetException extends Exception
final class ManagerNotSetException extends Exception
{
protected function __construct(string $message)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Xray\AzureStoragePhpSdk\Exceptions\RequestException;

class FailedAuthenticationException extends RequestException
final class FailedAuthenticationException extends RequestException
{
//
}
2 changes: 1 addition & 1 deletion src/Exceptions/UnableToConvertException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Exception;

class UnableToConvertException extends Exception
final class UnableToConvertException extends Exception
{
protected function __construct(string $message)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/UnableToParseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Exception;

class UnableToParseException extends Exception
final class UnableToParseException extends Exception
{
protected function __construct(string $message)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Xray\AzureStoragePhpSdk\Tests\Http\Concerns;
namespace Xray\AzureStoragePhpSdk\Fakes\Http\Concerns;

use PHPUnit\Framework\Assert;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Xray\AzureStoragePhpSdk\Tests\Http\Concerns;
namespace Xray\AzureStoragePhpSdk\Fakes\Http\Concerns;

use PHPUnit\Framework\Assert;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Xray\AzureStoragePhpSdk\Tests\Http\Concerns;
namespace Xray\AzureStoragePhpSdk\Fakes\Http\Concerns;

use Xray\AzureStoragePhpSdk\BlobStorage\Enums\HttpVerb;
use Xray\AzureStoragePhpSdk\Http\Headers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

declare(strict_types=1);

namespace Xray\AzureStoragePhpSdk\Tests\Http;
namespace Xray\AzureStoragePhpSdk\Fakes\Http;

use Closure;
use Xray\AzureStoragePhpSdk\Authentication\SharedKeyAuth;
use Xray\AzureStoragePhpSdk\BlobStorage\Config;
use Xray\AzureStoragePhpSdk\BlobStorage\Enums\HttpVerb;
use Xray\AzureStoragePhpSdk\Contracts\Authentication\Auth;
use Xray\AzureStoragePhpSdk\Contracts\Http\{Request, Response};
use Xray\AzureStoragePhpSdk\Fakes\Http\Concerns\{HasAuthAssertions, HasHttpAssertions, HasSharableHttp};
use Xray\AzureStoragePhpSdk\Http\Headers;
use Xray\AzureStoragePhpSdk\Tests\Http\Concerns\{HasAuthAssertions, HasHttpAssertions, HasSharableHttp};

/**
* @phpstan-type Method array{endpoint: string, body?: string}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Xray\AzureStoragePhpSdk\Tests\Http;
namespace Xray\AzureStoragePhpSdk\Fakes\Http;

use Xray\AzureStoragePhpSdk\Contracts\Http\Response;
use Xray\AzureStoragePhpSdk\Http\Response as BaseResponse;
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Xray\AzureStoragePhpSdk\Contracts\Http\{Request as RequestContract, Response as ResponseContract};

/** @suppressWarnings(PHPMD.ExcessiveParameterList) */
class Request implements RequestContract
final class Request implements RequestContract
{
use HasAuthenticatedRequest;
use HasSharingMethods;
Expand Down
3 changes: 2 additions & 1 deletion tests/Feature/Application/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use Xray\AzureStoragePhpSdk\Application\Application;
use Xray\AzureStoragePhpSdk\Exceptions\InvalidArgumentException;

uses()->group('applications');
pest()->group('applications');
covers(Application::class);

afterEach(fn () => Application::getInstance()->flush());

Expand Down
5 changes: 3 additions & 2 deletions tests/Feature/Authentication/MicrosoftEntraIdTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
use Xray\AzureStoragePhpSdk\BlobStorage\Enums\HttpVerb;
use Xray\AzureStoragePhpSdk\Contracts\Authentication\Auth;
use Xray\AzureStoragePhpSdk\Exceptions\RequiredFieldException;
use Xray\AzureStoragePhpSdk\Tests\Http\RequestFake;
use Xray\AzureStoragePhpSdk\Fakes\Http\RequestFake;
use Xray\Tests\Fakes\ClientFake;

uses()->group('authentications');
pest()->group('authentications');
covers(MicrosoftEntraId::class);

it('should implements Auth interface', function () {
expect(MicrosoftEntraId::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
use Xray\AzureStoragePhpSdk\Contracts\Authentication\SharedAccessSignature;
use Xray\AzureStoragePhpSdk\Exceptions\Authentication\InvalidAuthenticationMethodException;
use Xray\AzureStoragePhpSdk\Exceptions\InvalidResourceTypeException;
use Xray\AzureStoragePhpSdk\Tests\Http\{RequestFake, ResponseFake};
use Xray\AzureStoragePhpSdk\Fakes\Http\{RequestFake, ResponseFake};

uses()->group('authentications', 'shared-access-signatures');
pest()->group('authentications', 'shared-access-signatures');
covers(UserDelegationSas::class);

it('should implements SharedAccessSignature interface', function () {
expect(UserDelegationSas::class)
Expand Down
5 changes: 3 additions & 2 deletions tests/Feature/Authentication/SharedKeyAuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
use Xray\AzureStoragePhpSdk\BlobStorage\Resource;
use Xray\AzureStoragePhpSdk\Contracts\Authentication\Auth;
use Xray\AzureStoragePhpSdk\Exceptions\RequiredFieldException;
use Xray\AzureStoragePhpSdk\Fakes\Http\RequestFake;
use Xray\AzureStoragePhpSdk\Http\Headers;
use Xray\AzureStoragePhpSdk\Tests\Http\RequestFake;

uses()->group('authentications');
pest()->group('authentications');
covers(SharedKeyAuth::class);

it('should implements Auth interface', function () {
expect(SharedKeyAuth::class)
Expand Down
5 changes: 3 additions & 2 deletions tests/Feature/BlobStorage/BlobStorageClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
use Xray\AzureStoragePhpSdk\BlobStorage\Managers\Blob\BlobManager;
use Xray\AzureStoragePhpSdk\BlobStorage\Managers\{AccountManager, ContainerManager};
use Xray\AzureStoragePhpSdk\BlobStorage\{BlobStorageClient, Config};
use Xray\AzureStoragePhpSdk\Tests\Http\RequestFake;
use Xray\AzureStoragePhpSdk\Fakes\Http\RequestFake;

uses()->group('blob-storage');
pest()->group('blob-storage');
covers(BlobStorageClient::class);

it('should be able to get blob storage managers', function (string $method, string $class, array $parameters = []) {
$request = new RequestFake();
Expand Down
3 changes: 2 additions & 1 deletion tests/Feature/BlobStorage/BlobStorageConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
use Xray\AzureStoragePhpSdk\Contracts\Converter;
use Xray\AzureStoragePhpSdk\Parsers\XmlParser;

uses()->group('blob-storage');
pest()->group('blob-storage');
covers(Config::class);

it('should set default config value if none of the optional ones are provided', function () {
expect(new Config())
Expand Down
14 changes: 9 additions & 5 deletions tests/Feature/BlobStorage/BlobStorateResouceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

use Xray\AzureStoragePhpSdk\BlobStorage\Resource;

uses()->group('blob-storage');
pest()->group('blob-storage');
covers(Resource::class);

it('should canonicalize resource correctly', function () {
$uri = 'https://account.blob.core.windows.net/container/?RestType=service&Comp=properties';
it('should canonicalize resource correctly', function (string $query, string $expected) {
$uri = "https://account.blob.core.windows.net/container/?{$query}";

expect(Resource::canonicalize($uri))
->toBe("/container/\ncomp:properties\nresttype:service");
});
->toBe("/container/\n{$expected}");
})->with([
'With Query Params' => ['RestType=service&Comp=properties', "comp:properties\nresttype:service"],
'Without Query Params' => ['', ''],
]);
26 changes: 26 additions & 0 deletions tests/Feature/BlobStorage/Concerns/HasFakeRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Xray\AzureStoragePhpSdk\BlobStorage\Concerns\HasFakeRequest;
use Xray\AzureStoragePhpSdk\Contracts\Http\Request as RequestContract;
use Xray\AzureStoragePhpSdk\Fakes\Http\RequestFake;

pest()->group('blob-storage', 'concerns');
covers(HasFakeRequest::class);

it('should create a fake instance', function () {
$class = ClientToHasFakeRequestTest::fake();

expect($class)
->toBeInstanceOf(ClientToHasFakeRequestTest::class)
->request->toBeInstanceOf(RequestFake::class);
});

readonly class ClientToHasFakeRequestTest
{
use HasFakeRequest;

public function __construct(public RequestContract $request)
{
//
}
}
Loading

0 comments on commit 10d60e7

Please sign in to comment.