diff --git a/tests/Feature/Http/Controllers/ImportQuickbooksControllerTest.php b/tests/Feature/Http/Controllers/ImportQuickbooksControllerTest.php index 6a7094c9922c..92d6f58f9148 100644 --- a/tests/Feature/Http/Controllers/ImportQuickbooksControllerTest.php +++ b/tests/Feature/Http/Controllers/ImportQuickbooksControllerTest.php @@ -14,6 +14,8 @@ use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Bus; use GuzzleHttp\Psr7\Message; use Illuminate\Support\Arr; +use Illuminate\Support\Str; +use Mockery\MockInterface; use Tests\MockAccountData; use Tests\TestCase; use Mockery; @@ -22,78 +24,112 @@ class ImportQuickbooksControllerTest extends TestCase { use MockAccountData; use DatabaseTransactions; + + private $mock; + private $state; protected function setUp(): void { parent::setUp(); + $this->state = Str::random(4); + $this->mock = Mockery::mock(stdClass::class); $this->makeTestData(); - + 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 - $response = $this->withHeaders([ - 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/import/quickbooks/preimport',[ - 'import_type' => 'client' - ]); + $response = $this->get(route('authorize.quickbooks', ['token' => $token])); + $response->assertStatus(302); + + 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(); - Bus::fake(); - - $this->setUpTestData('customers'); + Cache::shouldReceive('has') + ->andReturn(true); + Cache::shouldReceive('get')->andReturn($token); + Cache::shouldReceive('pull')->andReturn($token['company_key']); // Perform the test - $response = $this->withHeaders([ - 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/import/quickbooks/preimport',[ - 'import_type' => 'client' - ]); + $response = $this->get("/quickbooks/authorized/?code=123456&state={$this->state}&realmId=12345678"); $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); + Cache::shouldHaveReceived('put')->once()->with($token['company_key'], 'abcdefg', 3600); + + $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) { $data = json_decode( file_get_contents(base_path("tests/Mock/Quickbooks/Data/$file.json")),true ); $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; } }