Skip to content

Instantly share code, notes, and snippets.

@devmsh
Last active February 16, 2020 19:36
Show Gist options
  • Select an option

  • Save devmsh/77d64c4f84d7a569bd3d82cc6b5401cf to your computer and use it in GitHub Desktop.

Select an option

Save devmsh/77d64c4f84d7a569bd3d82cc6b5401cf to your computer and use it in GitHub Desktop.
Write better Laravel tests 2
<?php
class LoanTest extends TestCase
{
use DatabaseMigrations;
public function test_can_log_a_loan()
{
$user = factory(User::class)->create();
$wallet = factory(Wallet::class)->create([
'user_id' => $user->id
]);
$this->passportAs($user)
->post('api/loans', [
'total' => 1000,
'payoff_at' => Carbon::today()->addYear(),
'wallet_id' => $wallet->id,
])
->assertSuccessful()
->assertJsonStructure([
'id',
'total',
'payoff_at',
]);
$loan = Loan::find(1);
$this->assertEquals(1000, $loan->total);
$this->assertEquals($user->id, $loan->user_id);
$this->assertEquals(Carbon::today()->addYear(), $loan->payoff_at);
/** @var Transaction $transaction */
$transaction = $loan->transaction;
$this->assertInstanceOf(Transaction::class, $transaction);
$this->assertEquals($loan->id, $transaction->causedby->id);
$this->assertEquals($loan->total, $transaction->amount);
$this->assertEquals($user->id, $transaction->user_id);
$this->assertEquals(1000, $wallet->balance());
/** @var Goal $goal */
$goal = $loan->goal;
$this->assertInstanceOf(Goal::class, $goal);
$this->assertEquals($goal->total, $loan->total);
$this->assertEquals($goal->user_id, $loan->user_id);
$this->assertEquals($goal->due_date, $loan->payoff_at);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment