Last active
March 28, 2020 22:15
-
-
Save simoebenhida/9fa814836ad04c2cf3fb4c7606bb8369 to your computer and use it in GitHub Desktop.
Live about testing with PHPunit/Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```php | |
<?php | |
namespace Tests\Feature; | |
use App\Post; | |
use Illuminate\Foundation\Testing\RefreshDatabase; | |
use Tests\TestCase; | |
class PostControllerTest extends TestCase | |
{ | |
use RefreshDatabase; | |
/** @test */ | |
public function user_can_create_a_post() | |
{ | |
$this->withoutExceptionHandling(); | |
$post = factory(Post::class)->make()->toArray(); | |
$this->authenticate()->json('POST', '/posts', $post)->assertStatus(201); | |
$this->assertDatabaseHas('posts', $post); | |
} | |
} | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```php | |
<?php | |
namespace Tests\Unit; | |
use App\Post; | |
use App\User; | |
use Illuminate\Foundation\Testing\DatabaseTransactions; | |
use Illuminate\Foundation\Testing\RefreshDatabase; | |
use Tests\TestCase; | |
class PostTest extends TestCase | |
{ | |
use RefreshDatabase; | |
/** @test */ | |
public function should_get_only_published_posts() | |
{ | |
// create 2 unplished posts | |
factory(Post::class, 2)->create([ | |
'published_at' => null | |
]); | |
// create 2 published posts | |
factory(Post::class, 2)->create([ | |
'published_at' => now() | |
]); | |
$posts = Post::published()->get(); | |
$this->assertCount(2, $posts->toArray()); | |
} | |
/** @test */ | |
public function can_publish_a_post() | |
{ | |
$post = factory(Post::class)->create([ | |
'published_at' => null | |
]); | |
$post->publish(); | |
$this->assertNotNull($post->fresh()->published_at); | |
} | |
} | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```php | |
<?php | |
namespace Tests; | |
use App\User; | |
use Illuminate\Foundation\Testing\TestCase as BaseTestCase; | |
abstract class TestCase extends BaseTestCase | |
{ | |
use CreatesApplication; | |
public function authenticate($user = null) | |
{ | |
if (is_null($user)) { | |
$user = factory(User::class)->create(); | |
} | |
$this->actingAs($user); | |
return $this; | |
} | |
} | |
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
```php | |
<?php | |
namespace Tests\Feature; | |
use App\User; | |
use Illuminate\Foundation\Testing\DatabaseTransactions; | |
use Illuminate\Foundation\Testing\RefreshDatabase; | |
use Illuminate\Support\Arr; | |
use Tests\TestCase; | |
class UserControllerTest extends TestCase | |
{ | |
use RefreshDatabase; | |
/** @test */ | |
public function it_can_add_a_new_user() | |
{ | |
$data = [ | |
'name' => 'John Doe', | |
'email' => 'john@email.com' | |
]; | |
// endpoint POST api/users | |
$this->authenticate()->json('POST', '/users', $data)->assertSuccessful(); | |
$this->assertCount(2, User::all()->toArray()); | |
// database has that specific user | |
$this->assertDatabaseHas('users', $data); | |
} | |
/** @test */ | |
public function unauthenticated_user_cannot_create_another_user() | |
{ | |
$data = [ | |
'name' => 'John Doe', | |
'email' => 'john@email.com' | |
]; | |
// endpoint POST api/users | |
$this->json('POST', '/users', $data)->assertUnauthorized(); | |
$this->assertCount(0, User::all()->toArray()); | |
} | |
/** @test */ | |
public function name_is_required_when_creating_a_new_user() | |
{ | |
$response = $this->authenticate()->json('POST', '/users', [ | |
'email' => 'john@email.com' | |
])->assertStatus(422); | |
$response = json_decode($response->getContent(), true); | |
$this->assertTrue(array_key_exists('name', $response['errors'])); | |
} | |
/** @test */ | |
public function email_is_required_when_creating_a_new_user() | |
{ | |
$response = $this->authenticate()->json('POST', '/users', [ | |
'name' => 'Example' | |
])->assertStatus(422); | |
$response = json_decode($response->getContent(), true); | |
$this->assertTrue(array_key_exists('email', $response['errors'])); | |
} | |
/** @test */ | |
public function it_should_be_an_email_when_creating_an_user() | |
{ | |
$response = $this->authenticate()->json('POST', '/users', [ | |
'name' => 'Example', | |
'email' => 'Example' | |
])->assertStatus(422); | |
$response = json_decode($response->getContent(), true); | |
$this->assertTrue(array_key_exists('email', $response['errors'])); | |
} | |
} | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment