Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cretueusebiu committed Jul 18, 2017
1 parent 9ca060a commit d342a09
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 23 deletions.
23 changes: 0 additions & 23 deletions tests/Feature/ExampleTest.php

This file was deleted.

55 changes: 55 additions & 0 deletions tests/Feature/LoginTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Tests\Feature;

use App\User;
use Tests\TestCase;

class LoginTest extends TestCase
{
/** @var \App\User */
protected $user;

public function setUp()
{
parent::setUp();

$this->user = factory(User::class)->create();
}

/** @test */
public function can_authenticate()
{
$this->postJson('/api/login', [
'email' => $this->user->email,
'password' => 'secret',
])
->assertSuccessful()
->assertJsonStructure(['token', 'expires_in'])
->assertJson(['token_type' => 'bearer']);
}

/** @test */
public function can_fetch_the_current_user()
{
$this->actingAs($this->user)
->getJson('/api/user')
->assertSuccessful()
->assertJsonStructure(['id', 'name', 'email']);
}

/** @test */
public function can_log_out()
{
$token = $this->postJson('/api/login', [
'email' => $this->user->email,
'password' => 'secret',
])->json()['token'];

$this->json('POST', "/api/logout?token=$token")
->assertSuccessful();

$this->getJson("/api/user?token=$token")
->assertStatus(401);
}
}
21 changes: 21 additions & 0 deletions tests/Feature/RegisterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Tests\Feature;

use Tests\TestCase;

class RegisterTest extends TestCase
{
/** @test */
public function can_register()
{
$this->postJson('/api/register', [
'name' => 'Test User',
'email' => '[email protected]',
'password' => 'secret',
'password_confirmation' => 'secret',
])
->assertSuccessful()
->assertJsonStructure(['id', 'name', 'email']);
}
}
14 changes: 14 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,23 @@

namespace Tests;

use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
use DatabaseTransactions;

/**
* Setup the test environment.
*
* @return void
*/
protected function setUp()
{
parent::setUp();

$this->artisan('migrate');
}
}

0 comments on commit d342a09

Please sign in to comment.