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

[1.x] Adds --pest option #99

Merged
merged 3 commits into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 10 additions & 1 deletion src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class InstallCommand extends Command
*/
protected $signature = 'breeze:install {stack=blade : The development stack that should be installed (blade,react,vue)}
{--inertia : Indicate that the Vue Inertia stack should be installed (Deprecated)}
{--pest : Indicate that Pest should be installed }
{--composer=global : Absolute path to the Composer binary which should be used to install packages}';

/**
Expand Down Expand Up @@ -76,7 +77,15 @@ public function handle()
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/default/App/View/Components', app_path('View/Components'));

// Tests...
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/default/tests/Feature', base_path('tests/Feature'));
if ($this->option('pest')) {
$this->requireComposerPackages('pestphp/pest:^1.16', 'pestphp/pest-plugin-laravel:^1.1');

(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/default/pest-tests/Feature', base_path('tests/Feature'));
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/default/pest-tests/Unit', base_path('tests/Unit'));
(new Filesystem)->copy(__DIR__.'/../../stubs/default/pest-tests/Pest.php', base_path('tests/Pest.php'));
} else {
(new Filesystem)->copyDirectory(__DIR__.'/../../stubs/default/tests/Feature', base_path('tests/Feature'));
}

// Routes...
copy(__DIR__.'/../../stubs/default/routes/web.php', base_path('routes/web.php'));
Expand Down
33 changes: 33 additions & 0 deletions stubs/default/pest-tests/Feature/AuthenticationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use App\Models\User;
use App\Providers\RouteServiceProvider;

test('login screen can be rendered', function () {
$response = $this->get('/login');

$response->assertStatus(200);
});

test('users can authenticate using the login screen', function () {
$user = User::factory()->create();

$response = $this->post('/login', [
'email' => $user->email,
'password' => 'password',
]);

$this->assertAuthenticated();
$response->assertRedirect(RouteServiceProvider::HOME);
});

test('users can not authenticate with invalid password', function () {
$user = User::factory()->create();

$this->post('/login', [
'email' => $user->email,
'password' => 'wrong-password',
]);

$this->assertGuest();
});
55 changes: 55 additions & 0 deletions stubs/default/pest-tests/Feature/EmailVerificationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Tests\Feature;
nunomaduro marked this conversation as resolved.
Show resolved Hide resolved

use App\Models\User;
use App\Providers\RouteServiceProvider;
use Illuminate\Auth\Events\Verified;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\URL;

test('email verification screen can be rendered', function () {
$user = User::factory()->create([
'email_verified_at' => null,
]);

$response = $this->actingAs($user)->get('/verify-email');

$response->assertStatus(200);
});

test('email can be verified', function () {
$user = User::factory()->create([
'email_verified_at' => null,
]);

Event::fake();

$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => $user->id, 'hash' => sha1($user->email)]
);

$response = $this->actingAs($user)->get($verificationUrl);

Event::assertDispatched(Verified::class);
expect($user->fresh()->hasVerifiedEmail())->toBeTrue();
$response->assertRedirect(RouteServiceProvider::HOME.'?verified=1');
});

test('email is not verified with invalid hash', function () {
$user = User::factory()->create([
'email_verified_at' => null,
]);

$verificationUrl = URL::temporarySignedRoute(
'verification.verify',
now()->addMinutes(60),
['id' => $user->id, 'hash' => sha1('wrong-email')]
);

$this->actingAs($user)->get($verificationUrl);

expect($user->fresh()->hasVerifiedEmail())->toBeFalse();
});
7 changes: 7 additions & 0 deletions stubs/default/pest-tests/Feature/ExampleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

test('example', function () {
$response = $this->get('/');

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

use App\Models\User;

test('confirm password screen can be rendered', function () {
$user = User::factory()->create();

$response = $this->actingAs($user)->get('/confirm-password');

$response->assertStatus(200);
});

test('password can be confirmed', function () {
$user = User::factory()->create();

$response = $this->actingAs($user)->post('/confirm-password', [
'password' => 'password',
]);

$response->assertRedirect();
$response->assertSessionHasNoErrors();
});

test('password is not confirmed with invalid password', function () {
$user = User::factory()->create();

$response = $this->actingAs($user)->post('/confirm-password', [
'password' => 'wrong-password',
]);

$response->assertSessionHasErrors();
});
58 changes: 58 additions & 0 deletions stubs/default/pest-tests/Feature/PasswordResetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

use App\Models\User;
use Illuminate\Auth\Notifications\ResetPassword;
use Illuminate\Support\Facades\Notification;

test('reset password link screen can be rendered', function () {
$response = $this->get('/forgot-password');

$response->assertStatus(200);
});

test('reset password link can be requested', function () {
Notification::fake();

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

$this->post('/forgot-password', ['email' => $user->email]);

Notification::assertSentTo($user, ResetPassword::class);
});

test('reset password screen can be rendered', function () {
Notification::fake();

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

$this->post('/forgot-password', ['email' => $user->email]);

Notification::assertSentTo($user, ResetPassword::class, function ($notification) {
$response = $this->get('/reset-password/'.$notification->token);

$response->assertStatus(200);

return true;
});
});

test('password can be reset with valid token', function () {
Notification::fake();

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

$this->post('/forgot-password', ['email' => $user->email]);

Notification::assertSentTo($user, ResetPassword::class, function ($notification) use ($user) {
$response = $this->post('/reset-password', [
'token' => $notification->token,
'email' => $user->email,
'password' => 'password',
'password_confirmation' => 'password',
]);

$response->assertSessionHasNoErrors();

return true;
});
});
21 changes: 21 additions & 0 deletions stubs/default/pest-tests/Feature/RegistrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use App\Providers\RouteServiceProvider;

test('registration screen can be rendered', function () {
$response = $this->get('/register');

$response->assertStatus(200);
});

test('new users can register', function () {
$response = $this->post('/register', [
'name' => 'Test User',
'email' => '[email protected]',
'password' => 'password',
'password_confirmation' => 'password',
]);

$this->assertAuthenticated();
$response->assertRedirect(RouteServiceProvider::HOME);
});
48 changes: 48 additions & 0 deletions stubs/default/pest-tests/Pest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

/*
|--------------------------------------------------------------------------
| 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(TestCase::class, RefreshDatabase::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()
{
// ..
}
5 changes: 5 additions & 0 deletions stubs/default/pest-tests/Unit/ExampleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

test('example', function () {
expect(true)->toBeTrue();
});