mirror of
https://github.com/wearepixel/laravel-quickbooks.git
synced 2026-11-14 14:01:46 +00:00
- Widen illuminate/* deps to ^10.0|^11.0|^12.0, require PHP ^8.3 - Upgrade PHPUnit to ^10.5|^11.0, Mockery to ^1.6 - Rewrite phpunit.xml.dist for PHPUnit 10+ format (source/include replaces filter/whitelist, removed deprecated attributes) - Replace $dates with $casts on Token model - Implement DeferrableProvider interface instead of $defer property on ClientServiceProvider - Use FQCN route definitions instead of string-based namespace() - Fix migration getColumnType() check for Laravel 11+ native types - Replace @test doc annotations with #[Test] attributes - Fix dynamic property deprecation in ControllerTest
190 lines
4.9 KiB
PHP
190 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace Wearepixel\QuickBooks;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
|
use Mockery;
|
|
use PHPUnit\Framework\Attributes\Test;
|
|
use QuickBooksOnline\API\Core\OAuth\OAuth2\OAuth2AccessToken;
|
|
use Wearepixel\QuickBooks\Stubs\TokenStub;
|
|
use Wearepixel\QuickBooks\Stubs\User;
|
|
|
|
/**
|
|
* Class TokenTest
|
|
*/
|
|
class TokenTest extends TestCase
|
|
{
|
|
/**
|
|
* @var Token
|
|
*/
|
|
protected $token;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->token = new TokenStub;
|
|
}
|
|
|
|
#[Test]
|
|
public function it_can_be_constructed()
|
|
{
|
|
$this->assertInstanceOf(Token::class, $this->token);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_has_accessor_for_valid_access_token()
|
|
{
|
|
$this->assertNotNull($this->token->getHasValidAccessTokenAttribute());
|
|
}
|
|
|
|
#[Test]
|
|
public function it_has_accessor_for_valid_refresh_token()
|
|
{
|
|
$this->assertNotNull($this->token->getHasValidRefreshTokenAttribute());
|
|
}
|
|
|
|
#[Test]
|
|
public function it_knows_that_the_access_token_expires_at_has_to_be_valid()
|
|
{
|
|
$this->assertFalse($this->token->getHasValidAccessTokenAttribute());
|
|
}
|
|
|
|
#[Test]
|
|
public function it_knows_that_the_refresh_token_expires_at_has_to_be_valid()
|
|
{
|
|
$this->assertFalse($this->token->getHasValidRefreshTokenAttribute());
|
|
}
|
|
|
|
#[Test]
|
|
public function it_know_if_access_token_expires_at_is_less_than_now_it_is_not_expired()
|
|
{
|
|
$now = Carbon::now();
|
|
Carbon::setTestNow($now);
|
|
|
|
$this->token->access_token_expires_at = Carbon::now()->addSecond();
|
|
|
|
$this->assertTrue($this->token->getHasValidAccessTokenAttribute());
|
|
}
|
|
|
|
#[Test]
|
|
public function it_know_if_refresh_token_expires_at_is_less_than_now_it_is_not_expired()
|
|
{
|
|
$now = Carbon::now();
|
|
Carbon::setTestNow($now);
|
|
|
|
$this->token->refresh_token_expires_at = Carbon::now()->addSecond();
|
|
|
|
$this->assertTrue($this->token->getHasValidRefreshTokenAttribute());
|
|
}
|
|
|
|
#[Test]
|
|
public function it_know_if_access_token_expires_at_is_greater_than_or_equal_to_now_it_is_expired()
|
|
{
|
|
$now = Carbon::now();
|
|
Carbon::setTestNow($now);
|
|
|
|
$this->token->access_token_expires_at = Carbon::now();
|
|
|
|
$this->assertFalse($this->token->getHasValidAccessTokenAttribute());
|
|
|
|
$this->token->access_token_expires_at->subSecond();
|
|
|
|
$this->assertFalse($this->token->getHasValidAccessTokenAttribute());
|
|
}
|
|
|
|
#[Test]
|
|
public function it_know_if_refresh_token_expires_at_is_greater_than_or_equal_to_now_it_is_expired()
|
|
{
|
|
$now = Carbon::now();
|
|
Carbon::setTestNow($now);
|
|
|
|
$this->token->refresh_token_expires_at = Carbon::now();
|
|
|
|
$this->assertFalse($this->token->getHasValidRefreshTokenAttribute());
|
|
|
|
$this->token->refresh_token_expires_at->subSecond();
|
|
|
|
$this->assertFalse($this->token->getHasValidRefreshTokenAttribute());
|
|
}
|
|
|
|
#[Test]
|
|
public function it_stores_the_oauth_token_parts_in_expected_properties()
|
|
{
|
|
$oauth_token_mock = Mockery::mock(OAuth2AccessToken::class);
|
|
|
|
$oauth_token_mock
|
|
->shouldReceive('getAccessToken')
|
|
->once()
|
|
->withNoArgs()
|
|
->andReturn('access_token');
|
|
|
|
$oauth_token_mock
|
|
->shouldReceive('getAccessTokenExpiresAt')
|
|
->once()
|
|
->withNoArgs()
|
|
->andReturn('now');
|
|
|
|
$oauth_token_mock
|
|
->shouldReceive('getRealmID')
|
|
->once()
|
|
->withNoArgs()
|
|
->andReturn('realm_id');
|
|
|
|
$oauth_token_mock
|
|
->shouldReceive('getRefreshToken')
|
|
->once()
|
|
->withNoArgs()
|
|
->andReturn('refresh_token');
|
|
|
|
$oauth_token_mock
|
|
->shouldReceive('getRefreshTokenExpiresAt')
|
|
->once()
|
|
->withNoArgs()
|
|
->andReturn('now');
|
|
|
|
$this->token->parseOauthToken($oauth_token_mock);
|
|
}
|
|
|
|
#[Test]
|
|
public function it_allows_itself_to_be_deleted_and_returns_new_token()
|
|
{
|
|
$this->token->user = Mockery::mock(User::class);
|
|
|
|
$has_one_mock = Mockery::mock(HasOne::class);
|
|
$token_mock = Mockery::mock(Token::class);
|
|
|
|
$has_one_mock
|
|
->shouldReceive('make')
|
|
->once()
|
|
->withNoArgs()
|
|
->andReturn($token_mock);
|
|
|
|
$this->token->user
|
|
->shouldReceive('quickBooksToken')
|
|
->once()
|
|
->withNoArgs()
|
|
->andReturn($has_one_mock);
|
|
|
|
$this->token->user->id = 1;
|
|
|
|
$this->assertEquals($token_mock, $this->token->remove());
|
|
}
|
|
|
|
#[Test]
|
|
public function it_get_related_user_model_from_configuration()
|
|
{
|
|
$this->assertInstanceOf(User::class, $this->token->user()->getModel());
|
|
}
|
|
}
|
|
|
|
function config($key)
|
|
{
|
|
return [
|
|
'keys' => [
|
|
'foreign' => 'user_id',
|
|
'owner' => 'id',
|
|
],
|
|
'model' => User::class,
|
|
];
|
|
}
|