Skip to content
Closed
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 src/Helpers/DateHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Saloon\Helpers;

use DateTimeImmutable;

final class DateHelper
{
private static ?DateTimeImmutable $fixedTime = null;

public static function now(): DateTimeImmutable
{
return self::$fixedTime ?? new DateTimeImmutable();
}

/**
* This method is only for setting a fixed test time.
*/
public static function setFixedTime(?DateTimeImmutable $time): void
{
self::$fixedTime = $time;
}

public static function useSystemTime(): void
{
self::$fixedTime = null;
}
}
3 changes: 2 additions & 1 deletion src/Http/Auth/AccessTokenAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Saloon\Http\Auth;

use DateTimeImmutable;
use Saloon\Helpers\DateHelper;
use Saloon\Http\PendingRequest;
use Saloon\Contracts\OAuthAuthenticator;

Expand Down Expand Up @@ -38,7 +39,7 @@ public function hasExpired(): bool
return false;
}

return $this->expiresAt->getTimestamp() <= (new DateTimeImmutable)->getTimestamp();
return $this->expiresAt->getTimestamp() <= DateHelper::now()->getTimestamp();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Http/BaseResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class BaseResource
/**
* Constructor
*/
public function __construct(readonly protected Connector $connector)
public function __construct(protected readonly Connector $connector)
{
//
}
Expand Down
3 changes: 2 additions & 1 deletion src/Traits/OAuth2/AuthorizationCodeGrant.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Saloon\Http\Response;
use InvalidArgumentException;
use Saloon\Helpers\URLHelper;
use Saloon\Helpers\DateHelper;
use Saloon\Helpers\StringHelpers;
use Saloon\Helpers\OAuth2\OAuthConfig;
use Saloon\Http\OAuth2\GetUserRequest;
Expand Down Expand Up @@ -158,7 +159,7 @@ protected function createOAuthAuthenticatorFromResponse(Response $response, ?str
$expiresAt = null;

if (isset($responseData->expires_in) && is_numeric($responseData->expires_in)) {
$expiresAt = (new DateTimeImmutable)->add(
$expiresAt = DateHelper::now()->add(
DateInterval::createFromDateString((int)$responseData->expires_in . ' seconds')
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Traits/OAuth2/ClientCredentialsGrant.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use DateTimeImmutable;
use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\Helpers\DateHelper;
use Saloon\Helpers\OAuth2\OAuthConfig;
use Saloon\Contracts\OAuthAuthenticator;
use Saloon\Http\Auth\AccessTokenAuthenticator;
Expand Down Expand Up @@ -62,7 +63,7 @@ protected function createOAuthAuthenticatorFromResponse(Response $response): OAu
$expiresAt = null;

if (isset($responseData->expires_in) && is_numeric($responseData->expires_in)) {
$expiresAt = (new DateTimeImmutable)->add(
$expiresAt = DateHelper::now()->add(
DateInterval::createFromDateString((int)$responseData->expires_in . ' seconds')
);
}
Expand Down
14 changes: 7 additions & 7 deletions tests/Feature/Oauth2/AuthCodeFlowConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Saloon\Http\Request;
use Saloon\Http\Response;
use Saloon\Tests\Helpers\Date;
use Saloon\Helpers\DateHelper;
use Saloon\Http\Faking\MockClient;
use Saloon\Http\Faking\MockResponse;
use Saloon\Http\OAuth2\GetUserRequest;
Expand Down Expand Up @@ -165,7 +165,7 @@

$connector->withMockClient($mockClient);

$authenticator = new AccessTokenAuthenticator('access', 'refresh', Date::now()->addSeconds(3600)->toDateTime());
$authenticator = new AccessTokenAuthenticator('access', 'refresh', DateHelper::now()->add(new DateInterval('PT3600S')));

$newAuthenticator = $connector->refreshAccessToken($authenticator);

Expand All @@ -184,7 +184,7 @@

$connector->withMockClient($mockClient);

$authenticator = new AccessTokenAuthenticator('access', 'refresh', Date::now()->addSeconds(3600)->toDateTime());
$authenticator = new AccessTokenAuthenticator('access', 'refresh', DateHelper::now()->add(new DateInterval('PT3600S')));

$newAuthenticator = $connector->refreshAccessToken($authenticator, requestModifier: function (Request $request) {
$request->query()->add('yee', 'haw');
Expand All @@ -209,7 +209,7 @@

$connector->withMockClient($mockClient);

$authenticator = new AccessTokenAuthenticator('access', null, Date::now()->addSeconds(3600)->toDateTime());
$authenticator = new AccessTokenAuthenticator('access', null, DateHelper::now()->add(new DateInterval('PT3600S')));

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The provided OAuthAuthenticator does not contain a refresh token.');
Expand All @@ -226,7 +226,7 @@

$connector->withMockClient($mockClient);

$authenticator = new AccessTokenAuthenticator('access', 'refresh', Date::now()->addSeconds(3600)->toDateTime());
$authenticator = new AccessTokenAuthenticator('access', 'refresh', DateHelper::now()->add(new DateInterval('PT3600S')));

$response = $connector->refreshAccessToken($authenticator, true);

Expand All @@ -242,7 +242,7 @@
$connector = new OAuth2Connector;
$connector->withMockClient($mockClient);

$accessToken = new AccessTokenAuthenticator('access', 'refresh', Date::now()->addSeconds(3600)->toDateTime());
$accessToken = new AccessTokenAuthenticator('access', 'refresh', DateHelper::now()->add(new DateInterval('PT3600S')));

$response = $connector->getUser($accessToken);

Expand All @@ -265,7 +265,7 @@
$connector = new OAuth2Connector;
$connector->withMockClient($mockClient);

$accessToken = new AccessTokenAuthenticator('access', 'refresh', Date::now()->addSeconds(3600)->toDateTime());
$accessToken = new AccessTokenAuthenticator('access', 'refresh', DateHelper::now()->add(new DateInterval('PT3600S')));

$response = $connector->getUser($accessToken, function (Request $request) {
$request->query()->add('yee', 'haw');
Expand Down
8 changes: 4 additions & 4 deletions tests/Fixtures/Authenticators/CustomOAuthAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class CustomOAuthAuthenticator extends AccessTokenAuthenticator
* Constructor
*/
public function __construct(
readonly public string $accessToken,
readonly public string $greeting,
readonly public ?string $refreshToken = null,
readonly public ?DateTimeImmutable $expiresAt = null,
public readonly string $accessToken,
public readonly string $greeting,
public readonly ?string $refreshToken = null,
public readonly ?DateTimeImmutable $expiresAt = null,
) {
//
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/Requests/QueryParameterRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class QueryParameterRequest extends Request
/**
* Constructor
*/
public function __construct(readonly public string $endpoint = '/user')
public function __construct(public readonly string $endpoint = '/user')
{
//
}
Expand Down
64 changes: 0 additions & 64 deletions tests/Helpers/Date.php

This file was deleted.

16 changes: 16 additions & 0 deletions tests/Unit/DateHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

use Saloon\Helpers\DateHelper;

test('A fixed time can be set in the tests', function () {
$time = new \DateTimeImmutable('2021-01-01 12:00:00 +00:00');
DateHelper::setFixedTime($time);

expect(DateHelper::now())->toEqual($time);

DateHelper::useSystemTime();

expect(DateHelper::now())->not->toEqual($time);
});
16 changes: 12 additions & 4 deletions tests/Unit/Oauth2/AccessTokenAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@

declare(strict_types=1);

use Saloon\Tests\Helpers\Date;
use Saloon\Helpers\DateHelper;
use Saloon\Http\Auth\AccessTokenAuthenticator;

beforeEach(function () {
DateHelper::setFixedTime(new DateTimeImmutable('2025-01-01 00:00:00 +00:00'));
});

afterEach(function () {
DateHelper::useSystemTime();
});

it('can be serialized and unserialized', function () {
$accessToken = 'access';
$refreshToken = 'refresh';
$expiresAt = Date::now()->toDateTime();
$expiresAt = DateHelper::now();

$authenticator = new AccessTokenAuthenticator($accessToken, $refreshToken, $expiresAt);

Expand All @@ -28,7 +36,7 @@
it('can return if it has expired or not', function () {
$accessToken = 'access';
$refreshToken = 'refresh';
$expiresAt = Date::now()->subMinutes(5)->toDateTime();
$expiresAt = DateHelper::now()->sub(new DateInterval('PT5M'));

$authenticator = new AccessTokenAuthenticator($accessToken, $refreshToken, $expiresAt);

Expand All @@ -49,7 +57,7 @@
});

test('can be constructed with just an access token and expiry', function () {
$expiresAt = Date::now()->subMinutes(5)->toDateTime();
$expiresAt = DateHelper::now()->sub(new DateInterval('PT5M'));

$authenticator = new AccessTokenAuthenticator('access', null, $expiresAt);

Expand Down
4 changes: 2 additions & 2 deletions tests/Unit/Oauth2/AuthCodeFlowConnectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

use Saloon\Tests\Helpers\Date;
use Saloon\Helpers\DateHelper;
use Saloon\Http\Faking\MockClient;
use Saloon\Http\Faking\MockResponse;
use Saloon\Helpers\OAuth2\OAuthConfig;
Expand Down Expand Up @@ -51,7 +51,7 @@

$connector->withMockClient($mockClient);

$authenticator = new AccessTokenAuthenticator('access', 'refresh-old', Date::now()->addSeconds(3600)->toDateTime());
$authenticator = new AccessTokenAuthenticator('access', 'refresh-old', DateHelper::now()->add(new DateInterval('PT3600S')));

$newAuthenticator = $connector->refreshAccessToken($authenticator);

Expand Down