From 1d1a01fcd1b302b63dfc8d05484ffca1d29490c5 Mon Sep 17 00:00:00 2001 From: karneaud Date: Mon, 22 Jul 2024 20:04:02 -0400 Subject: [PATCH] add quickbooks imports controller test --- .../ImportQuickbooksControllerTest.php | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 tests/Feature/Http/Controllers/ImportQuickbooksControllerTest.php diff --git a/tests/Feature/Http/Controllers/ImportQuickbooksControllerTest.php b/tests/Feature/Http/Controllers/ImportQuickbooksControllerTest.php new file mode 100644 index 000000000000..8534e33ed681 --- /dev/null +++ b/tests/Feature/Http/Controllers/ImportQuickbooksControllerTest.php @@ -0,0 +1,99 @@ +makeTestData(); + Session::start(); + } + + public function testPreImportQuickbooksController(): void + { + 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); + // 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); + } + + public function testImportQuickbooksCustomers(): void + { + + 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); + // 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); + $response = $this->withHeaders([ + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/import/quickbooks',[ + 'import_type' => 'client', + 'hash' => $response->hash + ]); + $response->assertStatus(200); + + 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); + } +}