class BuggyTest extends TestCase
{
public function testSuccessfulAuthentication()
{
$sessionId = 'SX9taRonx8yUsKgt6KaR';
$this->mock(NumbersService::class, function ($mock){ });
$response = $this->withoutMiddleware()->get('/api/authenticate', [
'username' => 'test_username',
'password' => 'test_password'
]);
$response->assertStatus(200);
$response->assertJson([
'status' => 200,
'message' => 'OK',
'data' => [
'generated_session_id' => $sessionId
]
]);
}
public function testNotSuccessfulAuthentication()
{
$this->mock(SomeProxyService::class, function ($mock) {
$mock->shouldReceive('authenticate')->andReturn(false);
});
$response = $this->withoutMiddleware()->get('/api/authenticate', []);
$response->assertStatus(401);
$response->assertJson([
'status' => 401,
'message' => ' authentication failed'
]);
}
}
This is the test
class SomeProxyService extends BaseProxy
{
public function __construct()
{
$this->initialize(config('api_url'));
}
public function initialize($baseUrl)
{
$this->client = new Client([
'base_uri' => $baseUrl,
'HTTP_Accept' => 'application/json',
'verify' => Config::get('ssl_verify'),
'cookies' => true
]);
}
public function authenticate($username, $password)
{
$authentication = $this->performRequest('/login', 'GET', [
'query' => [
'username' => $username,
'password' => $password,
'login' => true,
'jsonlogin' => true
]
]);
if($authentication->responsecode !== 200) {
return false;
}
return $authentication->cookies->JSESSIONID ?? false;
}
}
And this is the class with the 'authenticate' method I'm trying to mock. As I mentioned I tried it with different classes, but no luck. I think the issue is with the test.