How to feature test more complicated cases on Laravel using PHPUnit

I’m using Laravel for my project and I’m new to unit/feature testing so I was wondering what is the best way to approach more complicated feature cases when writing tests?

Let’s take this test example:

  public function testSucceedIfConnectAuthorised()
{
$connection = factory(Connection::class)->make([
'sender_id' => 1,
'receiver_id' => 2,
'accepted' => false,
'connection_id' => 5,
]);

$user = factory(User::class)->make([
'id' => 1,
]);

$response = $this->actingAs($user)->post(
'/app/connection-request/accept',
[
'accept' => true,
'request_id' => $connection->id,
]
);

$response->assertLocation('/')->assertStatus(200);
}

So we’ve got this situation where we have some connection system between two users. There is a Connection entry in the DB created by one of the users. Now to make it a successful connection the second user has to approve it. The problem is within the UserController accepting this through connectionRequest:

  // TalentController
public function connectionRequest(Request $request)
{
// we check if the user isn't trying to accept the connection
// that he initiated himself
$connection = $this->repository->GetConnectionById($request->get('request_id'));
$receiver_id = $connection->receiver_id;
$current_user_id = auth()->user()->id;

if ($receiver_id !== $current_user_id) {
abort(403);
}

[...]
}


// UserRepository
public function GetConnectionById($id)
{
return Connection::where('id', $id)->first();
}

So we’ve got this fake (factory created) connection in the test function and then we unfortunately are using its fake id to run a check within the real DB among real connections, which is not what we want 🙁

Researching I found an idea of creating interfaces so then we can provide a different method bodies depending if we’re testing or not. Like here for GetConnectionById() making it easy to fake answers to for the testing case. And that seems OK, but:

  • for one it looks like a kind of overhead, besides writing tests I have to make the “real” code more complicated itself for the sole purpose of testing.
  • and second thing, I read all that Laravel documentation has to say about testing, and there is no one place where they mention using of interfaces, so that makes me wonder too if that’s the only way and the best way of solving this problem.

from Newest questions tagged laravel-5 – Stack Overflow https://ift.tt/3k3Qufx
via IFTTT

Related Posts

Codeigniter : Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE) in Laravel project

I’m getting following error: **Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE)** Note : It’s working in local but facing issue in production server. private…

Firebase receive notification while tab is active or on focus

What i want is to be able to perform an action when a user receives a notification while the browser is open and tab is active or…

Laravel’s alias loader does not find class

We have a legacy project that we cannot update and we need to make some changes in symfony’s Response.php in vendor. We have solved this by copying…

Laravel 5 – generic document management

I have a system where you can create different types of unique documents. For instance, one document is called Project Identified and this expects certain inputs. Originally,…

Laravel Nova limit the results in indexQuery

I ran intro a situation where I need to limit the results of a resource to only 3 results. To be more specific, based on the logged…

Auditoria en laravel 5.8 [closed]

Cómo puedo automatizar el registro de actividades de un usuario en laravel? Si un usuario ingresa a un app de laravel, debo guardar toda su actividas, a…

Leave a Reply

Your email address will not be published.