mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
update quickbooks controller tests
This commit is contained in:
parent
ab5e05485e
commit
32aad06308
@ -14,6 +14,8 @@ use Illuminate\Support\Facades\Http;
|
|||||||
use Illuminate\Support\Facades\Bus;
|
use Illuminate\Support\Facades\Bus;
|
||||||
use GuzzleHttp\Psr7\Message;
|
use GuzzleHttp\Psr7\Message;
|
||||||
use Illuminate\Support\Arr;
|
use Illuminate\Support\Arr;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
use Mockery\MockInterface;
|
||||||
use Tests\MockAccountData;
|
use Tests\MockAccountData;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
use Mockery;
|
use Mockery;
|
||||||
@ -22,78 +24,112 @@ class ImportQuickbooksControllerTest extends TestCase
|
|||||||
{
|
{
|
||||||
use MockAccountData;
|
use MockAccountData;
|
||||||
use DatabaseTransactions;
|
use DatabaseTransactions;
|
||||||
|
|
||||||
|
private $mock;
|
||||||
|
private $state;
|
||||||
|
|
||||||
protected function setUp(): void {
|
protected function setUp(): void {
|
||||||
|
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
|
$this->state = Str::random(4);
|
||||||
|
$this->mock = Mockery::mock(stdClass::class);
|
||||||
$this->makeTestData();
|
$this->makeTestData();
|
||||||
|
|
||||||
Session::start();
|
Session::start();
|
||||||
|
|
||||||
|
app()->singleton(QuickbooksInterface::class, fn() => new QuickbooksSDK($this->mock));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testPreImportQuickbooksController(): void
|
public function testAuthorize(): void
|
||||||
{
|
{
|
||||||
Cache::spy();
|
|
||||||
|
$this->mock->shouldReceive('getState')->andReturn($this->state);
|
||||||
|
$this->mock->shouldReceive('getAuthorizationCodeURL')->andReturn('https://example.com');
|
||||||
|
$this->mock->shouldReceive("getOAuth2LoginHelper")->andReturn($this->mock);
|
||||||
|
|
||||||
$data = $this->setUpTestData('customers');
|
Cache::spy();
|
||||||
|
Cache::shouldReceive('get')
|
||||||
|
->with($token = $this->company->company_key)
|
||||||
|
->andReturn( ['company_key' => $token, 'id' => $this->company->id]);
|
||||||
|
Cache::shouldReceive('has')
|
||||||
|
->andReturn(true);
|
||||||
// Perform the test
|
// Perform the test
|
||||||
$response = $this->withHeaders([
|
$response = $this->get(route('authorize.quickbooks', ['token' => $token]));
|
||||||
'X-API-TOKEN' => $this->token,
|
$response->assertStatus(302);
|
||||||
])->post('/api/v1/import/quickbooks/preimport',[
|
|
||||||
'import_type' => 'client'
|
Cache::shouldHaveReceived('put')->once()->with($this->state, $token, 90);
|
||||||
]);
|
}
|
||||||
|
|
||||||
|
public function testOnAuthorized(): void
|
||||||
|
{
|
||||||
|
$token = ['company_key' => $this->company->company_key, 'id' => $this->company->id] ;
|
||||||
|
|
||||||
|
$this->mock->shouldReceive('getAccessToken')->andReturn(Mockery::mock(stdClass::class,function(MockInterface $mock){
|
||||||
|
$mock->shouldReceive('getAccessToken')->andReturn('abcdefg');
|
||||||
|
$mock->shouldReceive('getRefreshToken')->andReturn('abcdefghi');
|
||||||
|
$mock->shouldReceive('getAccessTokenExpiresAt')->andReturn(3600);
|
||||||
|
$mock->shouldReceive('getRefreshTokenExpiresAt')->andReturn(8726400);
|
||||||
|
}));
|
||||||
|
$this->mock->shouldReceive("getOAuth2LoginHelper")->andReturn($this->mock);
|
||||||
|
$this->mock->shouldReceive('exchangeAuthorizationCodeForToken')->once();
|
||||||
|
|
||||||
$response->assertStatus(200);
|
|
||||||
$response = json_decode( $response->getContent());
|
|
||||||
|
|
||||||
$this->assertNotNull($response->hash);
|
|
||||||
Cache::shouldHaveReceived('put')->once();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testImportQuickbooksCustomers(): void
|
|
||||||
{
|
|
||||||
Cache::spy();
|
Cache::spy();
|
||||||
Bus::fake();
|
Cache::shouldReceive('has')
|
||||||
|
->andReturn(true);
|
||||||
$this->setUpTestData('customers');
|
Cache::shouldReceive('get')->andReturn($token);
|
||||||
|
Cache::shouldReceive('pull')->andReturn($token['company_key']);
|
||||||
// Perform the test
|
// Perform the test
|
||||||
$response = $this->withHeaders([
|
$response = $this->get("/quickbooks/authorized/?code=123456&state={$this->state}&realmId=12345678");
|
||||||
'X-API-TOKEN' => $this->token,
|
|
||||||
])->post('/api/v1/import/quickbooks/preimport',[
|
|
||||||
'import_type' => 'client'
|
|
||||||
]);
|
|
||||||
$response->assertStatus(200);
|
$response->assertStatus(200);
|
||||||
$response = json_decode( $response->getContent());
|
|
||||||
$this->assertNotNull($response->hash);
|
|
||||||
$hash = $response->hash;
|
|
||||||
$response = $this->withHeaders([
|
|
||||||
'X-API-TOKEN' => $this->token,
|
|
||||||
])->post('/api/v1/import/quickbooks',[
|
|
||||||
'import_type' => 'client',
|
|
||||||
'hash' => $response->hash
|
|
||||||
]);
|
|
||||||
|
|
||||||
$response->assertStatus(200);
|
Cache::shouldHaveReceived('put')->once()->with($token['company_key'], 'abcdefg', 3600);
|
||||||
Cache::shouldHaveReceived('has')->once()->with("{$hash}-client");
|
|
||||||
Bus::assertDispatched(\App\Jobs\Import\QuickbooksIngest::class);
|
$this->mock->shouldHaveReceived('exchangeAuthorizationCodeForToken')->once()->with(123456,12345678);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// public function testImport(): void
|
||||||
|
// {
|
||||||
|
// Cache::spy();
|
||||||
|
// Bus::fake();
|
||||||
|
// $this->mock->shouldReceive('Query')->andReturnUsing(
|
||||||
|
// function($val, $s = 1, $max = 1000) use ($count, $data) {
|
||||||
|
// if(stristr($val, 'count')) {
|
||||||
|
// return $count;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return Arr::take($data,$max);
|
||||||
|
// }
|
||||||
|
// );
|
||||||
|
// $this->setUpTestData('customers');
|
||||||
|
// // Perform the test
|
||||||
|
// $response = $this->withHeaders([
|
||||||
|
// 'X-API-TOKEN' => $this->token,
|
||||||
|
// ])->post('/api/v1/import/quickbooks/preimport',[
|
||||||
|
// 'import_type' => 'client'
|
||||||
|
// ]);
|
||||||
|
// $response->assertStatus(200);
|
||||||
|
// $response = json_decode( $response->getContent());
|
||||||
|
// $this->assertNotNull($response->hash);
|
||||||
|
// $hash = $response->hash;
|
||||||
|
// $response = $this->withHeaders([
|
||||||
|
// 'X-API-TOKEN' => $this->token,
|
||||||
|
// ])->post('/api/v1/import/quickbooks',[
|
||||||
|
// 'import_type' => 'client',
|
||||||
|
// 'hash' => $response->hash
|
||||||
|
// ]);
|
||||||
|
// $response->assertStatus(200);
|
||||||
|
|
||||||
|
// Cache::shouldHaveReceived('has')->once()->with("{$hash}-client");
|
||||||
|
// Bus::assertDispatched(\App\Jobs\Import\QuickbooksIngest::class);
|
||||||
|
// }
|
||||||
|
|
||||||
protected function setUpTestData($file) {
|
protected function setUpTestData($file) {
|
||||||
$data = json_decode(
|
$data = json_decode(
|
||||||
file_get_contents(base_path("tests/Mock/Quickbooks/Data/$file.json")),true
|
file_get_contents(base_path("tests/Mock/Quickbooks/Data/$file.json")),true
|
||||||
);
|
);
|
||||||
$count = count($data);
|
$count = count($data);
|
||||||
$sdkMock = Mockery::mock(sdtClass::class);
|
|
||||||
$sdkMock->shouldReceive('Query')->andReturnUsing(function($val, $s = 1, $max = 1000) use ($count, $data) {
|
|
||||||
if(stristr($val, 'count')) {
|
|
||||||
return $count;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Arr::take($data,$max);
|
|
||||||
});
|
|
||||||
app()->singleton(QuickbooksInterface::class, fn() => new QuickbooksSDK($sdkMock));
|
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user