-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
908a2ff
commit cd1cd7a
Showing
5 changed files
with
156 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Xray\Tests\Fakes; | ||
|
||
use Closure; | ||
use GuzzleHttp\ClientInterface; | ||
use GuzzleHttp\Promise\{Promise, PromiseInterface}; | ||
use GuzzleHttp\Psr7\Response; | ||
use PHPUnit\Framework\Assert; | ||
use Psr\Http\Message\{RequestInterface, ResponseInterface}; | ||
|
||
class ClientFake implements ClientInterface | ||
{ | ||
/** @var array<string, array{uri: string, options: array<string, scalar>}> */ | ||
protected array $requests = []; | ||
|
||
protected int $status = 200; | ||
|
||
protected array $headers = []; | ||
|
||
protected ?string $body = null; | ||
|
||
public function withResponseFake(?string $body = null, array $headers = [], int $status = 200): self | ||
{ | ||
$this->status = $status; | ||
$this->headers = $headers; | ||
$this->body = $body; | ||
|
||
return $this; | ||
} | ||
|
||
/** @param array<string, scalar> $options */ | ||
public function send(RequestInterface $request, array $options = []): ResponseInterface | ||
{ | ||
return new Response($this->status, $this->headers, $this->body); | ||
} | ||
|
||
/** @param array<string, scalar> $options */ | ||
public function sendAsync(RequestInterface $request, array $options = []): PromiseInterface | ||
{ | ||
return new Promise(); | ||
} | ||
|
||
/** @param array<string, scalar> $options */ | ||
public function request(string $method, mixed $uri, array $options = []): ResponseInterface | ||
{ | ||
/** @phpstan-ignore-next-line */ | ||
$this->requests[$method] = [ | ||
'uri' => $uri, | ||
'options' => $options, | ||
]; | ||
|
||
return new Response($this->status, $this->headers, $this->body); | ||
} | ||
|
||
/** @param array<string, scalar> $options */ | ||
public function requestAsync(string $method, mixed $uri, array $options = []): PromiseInterface | ||
{ | ||
return new Promise(); | ||
} | ||
|
||
public function getConfig(?string $option = null): mixed | ||
{ | ||
return []; | ||
} | ||
|
||
public function assertRequestSent(string $method, string $uri, ?Closure $options = null): void | ||
{ | ||
Assert::assertArrayHasKey($method, $this->requests, 'Request not sent'); | ||
Assert::assertSame($uri, $this->requests[$method]['uri'], 'Invalid URI'); | ||
|
||
if (!is_null($options)) { | ||
Assert::assertTrue($options($this->requests[$method]['options']), 'Invalid options'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Xray\AzureStoragePhpSdk\Authentication\MicrosoftEntraId; | ||
use Xray\AzureStoragePhpSdk\BlobStorage\Enums\HttpVerb; | ||
use Xray\AzureStoragePhpSdk\Contracts\Authentication\Auth; | ||
use Xray\AzureStoragePhpSdk\Tests\Http\RequestFake; | ||
use Xray\Tests\Fakes\ClientFake; | ||
|
||
uses()->group('authentications'); | ||
|
||
it('should implements Auth interface', function () { | ||
expect(MicrosoftEntraId::class) | ||
->toImplement(Auth::class); | ||
}); | ||
|
||
it('should get date formatted correctly', function () { | ||
$auth = new MicrosoftEntraId('account', 'directory', 'application', 'secret'); | ||
|
||
expect($auth->getDate()) | ||
->toBe(gmdate('D, d M Y H:i:s T')); | ||
}); | ||
|
||
it('should get the authentication account', function () { | ||
$auth = new MicrosoftEntraId('account', 'directory', 'application', 'secret'); | ||
|
||
expect($auth->getAccount()) | ||
->toBe('account'); | ||
}); | ||
|
||
it('should get correctly the authentication signature from a login request', function () { | ||
$client = (new ClientFake()) | ||
->withResponseFake(json_encode([ | ||
'token_type' => $tokeType = 'Bearer', | ||
'access_token' => $token = 'token', | ||
'expires_in' => 3600, | ||
])); | ||
|
||
$auth = (new MicrosoftEntraId('account', 'directory', $application = 'application', $secret = 'secret')) | ||
->withRequestClient($client); | ||
|
||
expect($auth->getAuthentication(new RequestFake())) | ||
->toBe("{$tokeType} {$token}"); | ||
|
||
expect($auth->getAuthentication(new RequestFake())) | ||
->toBe("{$tokeType} {$token}"); | ||
|
||
$client->assertRequestSent(HttpVerb::POST->value, 'https://login.microsoftonline.com/directory/oauth2/v2.0/token', fn (array $options): bool => $options === [ | ||
'form_params' => [ | ||
'grant_type' => 'client_credentials', | ||
'client_id' => $application, | ||
'client_secret' => $secret, | ||
'scope' => 'https://storage.azure.com/.default', | ||
], | ||
]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters