import controller test for quickbooks

This commit is contained in:
karneaud 2024-07-30 14:19:46 -04:00
parent 6d40891c74
commit 404933625f

View File

@ -2,6 +2,9 @@
namespace Tests\Feature\Http\Controllers;
use App\Services\Import\Quickbooks\Contracts\SdkInterface as QuickbooksInterface;
use App\Services\Import\Quickbooks\Service as QuickbooksService;
use App\Services\Import\Quickbooks\SdkWrapper as QuickbooksSDK;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
@ -10,6 +13,7 @@ use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Bus;
use GuzzleHttp\Psr7\Message;
use Illuminate\Support\Arr;
use Tests\MockAccountData;
use Tests\TestCase;
use Mockery;
@ -24,6 +28,7 @@ class ImportQuickbooksControllerTest extends TestCase
parent::setUp();
$this->makeTestData();
Session::start();
}
@ -31,42 +36,27 @@ class ImportQuickbooksControllerTest extends TestCase
{
Cache::spy();
$data = ($this->setUpTestResponseData('200-cutomer-response.txt'))['QueryResponse']['Customer'];
// Create a mock of the UserController
$controllerMock = Mockery::mock('App\Http\Controllers\ImportQuickbooksController[getData]')->shouldAllowMockingProtectedMethods();
// Define what the mocked getData method should return
$controllerMock->shouldReceive('getData')
->once()
->andReturn( $data, true);
// Bind the mock to the Laravel container
$this->app->instance('App\Http\Controllers\ImportQuickbooksController', $controllerMock);
$data = $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);
Cache::shouldHaveReceived('put')->once()->with("{$response->hash}-client", base64_encode(json_encode($data)),600);
$this->assertNotNull($response->hash);
Cache::shouldHaveReceived('put')->once();
}
public function testImportQuickbooksCustomers(): void
{
Cache::spy();
Bus::fake();
$data = ($this->setUpTestResponseData('200-cutomer-response.txt'))['QueryResponse']['Customer'];
// Create a mock of the UserController
$controllerMock = Mockery::mock('App\Http\Controllers\ImportQuickbooksController[getData]')->shouldAllowMockingProtectedMethods();
// Define what the mocked getData method should return
$controllerMock->shouldReceive('getData')
->once()
->andReturn( $data, true);
// Bind the mock to the Laravel container
$this->app->instance('App\Http\Controllers\ImportQuickbooksController', $controllerMock);
$this->setUpTestData('customers');
// Perform the test
$response = $this->withHeaders([
'X-API-TOKEN' => $this->token,
@ -76,24 +66,34 @@ class ImportQuickbooksControllerTest extends TestCase
$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 setUpTestResponseData($file) {
$fullResponse = file_get_contents( base_path("tests/Mock/Quickbooks/Http/Response/$file") );
// Parse the full response using Guzzle
$response = Message::parseResponse($fullResponse);
// Extract the JSON body
$jsonBody = (string) $response->getBody();
// Decode the JSON body to an array
return json_decode($jsonBody, true);
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;
}
}