Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests with Pest PHP #2

Merged
merged 7 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"laravel/sail": "^1.0.1",
"mockery/mockery": "^1.4.4",
"nunomaduro/collision": "^5.10",
"pestphp/pest": "^1.21",
"pestphp/pest-plugin-laravel": "^1.2",
"phpunit/phpunit": "^9.5.10"
},
"autoload": {
Expand All @@ -37,6 +39,7 @@
}
},
"scripts": {
"test": "./vendor/bin/pest",
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi"
Expand All @@ -59,7 +62,10 @@
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"minimum-stability": "dev",
"prefer-stable": true
Expand Down
2 changes: 2 additions & 0 deletions tests/Feature/Auth/RegistrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public function test_registration_screen_can_be_rendered()

public function test_new_users_can_register()
{
$this->markTestSkipped('Register route not implemented');

$response = $this->post('/register', [
'name' => 'Test User',
'email' => 'test@example.com',
Expand Down
21 changes: 0 additions & 21 deletions tests/Feature/ExampleTest.php

This file was deleted.

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

use App\Models\Post;
use App\Models\User;
use App\Models\Category;

it('can create a post', function () {
// Create an user for this test
$user = User::factory()->create();

// Create a category
$category = Category::factory()->create();

// We should have no Posts
expect(Post::all())->toHaveCount(0);

Post::create([
'user_id' => $user->id,
'category_id' => $category->id,
'title' => 'This is a post',
'slug' => 'this-is-a-post',
'excerpt' => 'My post is...',
'body' => 'My post is very cool.',
]);

//We should have one post
$posts = Post::all();

expect($posts)->toHaveCount(1);

//The first post should match what we have just created
expect($posts->first())
->title->toBe('This is a post');
});

test('a post can be accessed by URL', function () {
// Create an user for this test
$post = Post::factory()->create();

$response = $this->get('/posts/'.$post->slug);

//Page loads
$response->assertStatus(200);

//Make sure it is the correct view
$response->assertViewIs('posts.show');
});
41 changes: 41 additions & 0 deletions tests/Feature/RoutesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use App\Models\User;

test('Home page loads', function () {
//Requests the page
$response = $this->get(route('home'));

//Page could be loaded
$response->assertStatus(200);
});

test('Dashboard: cannot be accessed by guests', function () {
$response = $this->get(route('dashboard'));

//Redirect to login page
$response->assertStatus(302);
});

test('Dashboard: Random user can NOT access dashboard', function () {

// Create an user for this test
$user = User::factory()->create(['username' => 'random1234']);

$response = $this->actingAs($user)
->get(route('dashboard'));

//403 Forbidden access
$response->assertStatus(403);
});

test('Dashboard: MooseS94 can access dashboard', function () {

// Create MooseS94 for this test
$user = User::factory()->create(['username' => 'MooseS94']);

$response = $this->actingAs($user)
->get(route('dashboard'));

$response->assertStatus(200);
});
45 changes: 45 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "uses()" function to bind a different classes or traits.
|
*/

uses(Tests\TestCase::class)->in('Feature');

/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/

expect()->extend('toBeOne', function () {
return $this->toBe(1);
});

/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/

function something()
{
// ..
}
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

namespace Tests;

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

abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
use LazilyRefreshDatabase;
}