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; 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\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker; use Illuminate\Foundation\Testing\WithFaker;
@ -10,6 +13,7 @@ use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http; 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 Tests\MockAccountData; use Tests\MockAccountData;
use Tests\TestCase; use Tests\TestCase;
use Mockery; use Mockery;
@ -24,6 +28,7 @@ class ImportQuickbooksControllerTest extends TestCase
parent::setUp(); parent::setUp();
$this->makeTestData(); $this->makeTestData();
Session::start(); Session::start();
} }
@ -31,42 +36,27 @@ class ImportQuickbooksControllerTest extends TestCase
{ {
Cache::spy(); Cache::spy();
$data = ($this->setUpTestResponseData('200-cutomer-response.txt'))['QueryResponse']['Customer']; $data = $this->setUpTestData('customers');
// 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);
// Perform the test // Perform the test
$response = $this->withHeaders([ $response = $this->withHeaders([
'X-API-TOKEN' => $this->token, 'X-API-TOKEN' => $this->token,
])->post('/api/v1/import/quickbooks/preimport',[ ])->post('/api/v1/import/quickbooks/preimport',[
'import_type' => 'client' 'import_type' => 'client'
]); ]);
$response->assertStatus(200); $response->assertStatus(200);
$response = json_decode( $response->getContent()); $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 public function testImportQuickbooksCustomers(): void
{ {
Cache::spy();
Bus::fake(); Bus::fake();
$data = ($this->setUpTestResponseData('200-cutomer-response.txt'))['QueryResponse']['Customer']; $this->setUpTestData('customers');
// 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);
// Perform the test // Perform the test
$response = $this->withHeaders([ $response = $this->withHeaders([
'X-API-TOKEN' => $this->token, 'X-API-TOKEN' => $this->token,
@ -76,24 +66,34 @@ class ImportQuickbooksControllerTest extends TestCase
$response->assertStatus(200); $response->assertStatus(200);
$response = json_decode( $response->getContent()); $response = json_decode( $response->getContent());
$this->assertNotNull($response->hash); $this->assertNotNull($response->hash);
$hash = $response->hash;
$response = $this->withHeaders([ $response = $this->withHeaders([
'X-API-TOKEN' => $this->token, 'X-API-TOKEN' => $this->token,
])->post('/api/v1/import/quickbooks',[ ])->post('/api/v1/import/quickbooks',[
'import_type' => 'client', 'import_type' => 'client',
'hash' => $response->hash 'hash' => $response->hash
]); ]);
$response->assertStatus(200);
$response->assertStatus(200);
Cache::shouldHaveReceived('has')->once()->with("{$hash}-client");
Bus::assertDispatched(\App\Jobs\Import\QuickbooksIngest::class); Bus::assertDispatched(\App\Jobs\Import\QuickbooksIngest::class);
} }
protected function setUpTestResponseData($file) { protected function setUpTestData($file) {
$fullResponse = file_get_contents( base_path("tests/Mock/Quickbooks/Http/Response/$file") ); $data = json_decode(
// Parse the full response using Guzzle file_get_contents(base_path("tests/Mock/Quickbooks/Data/$file.json")),true
$response = Message::parseResponse($fullResponse); );
// Extract the JSON body $count = count($data);
$jsonBody = (string) $response->getBody(); $sdkMock = Mockery::mock(sdtClass::class);
// Decode the JSON body to an array $sdkMock->shouldReceive('Query')->andReturnUsing(function($val, $s = 1, $max = 1000) use ($count, $data) {
return json_decode($jsonBody, true); if(stristr($val, 'count')) {
return $count;
}
return Arr::take($data,$max);
});
app()->singleton(QuickbooksInterface::class, fn() => new QuickbooksSDK($sdkMock));
return $data;
} }
} }