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 testAuthorize(): void { $this->mock->shouldReceive('getState')->andReturn($this->state); $this->mock->shouldReceive('getAuthorizationCodeURL')->andReturn('https://example.com'); $this->mock->shouldReceive("getOAuth2LoginHelper")->andReturn($this->mock); 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->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(); Cache::spy(); Cache::shouldReceive('has') ->andReturn(true); Cache::shouldReceive('get')->andReturn($token); Cache::shouldReceive('pull')->andReturn($token['company_key']); // Perform the test $response = $this->get("/quickbooks/authorized/?code=123456&state={$this->state}&realmId=12345678"); $response->assertStatus(200); 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); return $data; } }