Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/API/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,19 @@ public function getAccessToken(): mixed
'Content-Type' => 'application/x-www-form-urlencoded',
];

// Make the call
$response = $this->call(
url : $url,
args : $args,
headers: $headers,
method : 'POST',
asForm : true
);
try {
// Make the call
$response = $this->call(
url : $url,
args : $args,
headers: $headers,
method : 'POST',
asForm : true
);
} catch (\Exception $e) {
// If the call fails, throw a SigmaAuthenticationException instead of a generic Exception
throw new SigmaAuthenticationException('Authentication failed: ' . $e->getMessage(), $e->getCode(), $e);
}

// Transform response to an array
/** @var array<string, mixed> $responseArr */
Expand Down
40 changes: 40 additions & 0 deletions tests/Mocks/SigmaRESTMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,44 @@ public static function mockWithCall(mixed $expectedResponse, string $url): mixed

return $mock;
}

/**
* Mock the SigmaREST class call function to simulate a failed call.
*
* @param int $status
* @param string $body
* @param string $url
*
* @return mixed[]|Response
*/
public static function mockWithFailedCall(int $status, string $body, string $url): mixed
{
// Mock the SigmaREST class, specifically to check if _authenticate is called
$mock = Mockery::mock(SigmaREST::class)->makePartial()->shouldAllowMockingProtectedMethods();

// Ensure the _authenticate method is called in the constructor
$mock->shouldReceive('_authenticate')
->once()
->andReturnNull();

// Instantiate the class to trigger the constructor
$mock->__construct();

// Create a mock for the Illuminate\Http\Client\Response class that simulates a failed response
$responseMock = Mockery::mock(Response::class);
$responseMock->shouldReceive('body')->andReturn($body);
$responseMock->shouldReceive('json')->andReturn(json_decode($body, true));
$responseMock->shouldReceive('status')->andReturn($status);
$responseMock->shouldReceive('failed')->andReturnTrue();
$responseMock->shouldReceive('successful')->andReturnFalse();

// Mock the call method to return the response mock
$mock->shouldReceive('call')
->withArgs(function ($passedUrl) use ($url) {
return $passedUrl === $url;
})
->andReturn($responseMock);

return $mock;
}
}
16 changes: 16 additions & 0 deletions tests/Unit/AuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,19 @@
// Clean up Mockery expectations
Mockery::close();
});

test('throws SigmaAuthenticationException when authentication fails', function () {
// Create a mock for the class that will simulate a failed authentication
$mock = SigmaRESTMock::mockWithFailedCall(
status: 401,
body: '{"error": "invalid_credentials"}',
url: 'auth/token'
);

// Expect the specific exception to be thrown
expect(fn() => $mock->getAccessToken())
->toThrow(\InterWorks\SigmaREST\Exceptions\SigmaAuthenticationException::class);

// Clean up Mockery expectations
Mockery::close();
});