Skip to content

Commit

Permalink
fix: testing feature (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
perisicnikola37 authored Jan 23, 2025
1 parent 21490f9 commit 788937b
Show file tree
Hide file tree
Showing 26 changed files with 80 additions and 41 deletions.
14 changes: 9 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
APP_NAME=LaravelInertiaVueSpa
APP_ENV=local
APP_ENV=testing ## or 'testing'
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost
Expand All @@ -8,13 +8,17 @@ LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

# use this for docker
DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=LaravelInertiaVueSpa.Database_2024!
DB_USERNAME=root
DB_USERNAME=root
DB_PASSWORD=LaravelInertiaVueSpa.Password_2024!

# modify this for sqlite
# DB_DATABASE=/home/${yourusername}/${path}/database/database.sqlite

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
Expand Down Expand Up @@ -55,4 +59,4 @@ VITE_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}"
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
4 changes: 3 additions & 1 deletion .github/workflows/docker_action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ on:
paths:
- 'Dockerfile'
- 'docker-compose.yml'
pull_request:
branches: [ master ]

jobs:
build:
Expand All @@ -18,4 +20,4 @@ jobs:

- name: Build and run Docker Compose
run: |
docker-compose up --build -d
docker compose up --build -d
25 changes: 13 additions & 12 deletions .github/workflows/phpunit_action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,44 @@ jobs:
project-tests:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2

- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"

- name: Update Dependencies (if needed)
run: composer update --no-ansi --no-interaction --no-scripts --no-progress

- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress

- name: Generate key
run: php artisan key:generate

- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache

- name: Create Database
run: |
mkdir -p database
touch database/database.sqlite
[ -f database/database.sqlite ] || touch database/database.sqlite
- name: Build assets for development
env:
LARAVEL_BYPASS_ENV_CHECK: 1
run: npm install && npm run dev &

- name: Wait for server to start
run: sleep 10s
run: sleep 10s

- name: Execute tests (Unit and Feature tests) via PHPUnit
env:
DB_CONNECTION: sqlite
DB_DATABASE: database/database.sqlite
CACHE_DRIVER: array
SESSION_DRIVER: array
QUEUE_DRIVER: sync
APP_ENV: testing
run: vendor/bin/phpunit
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ RUN composer install --no-scripts --no-autoloader

COPY . .

COPY .env.example .env

RUN npm install
RUN npm run build

Expand Down
Empty file modified bootstrap/cache/.gitignore
100644 → 100755
Empty file.
4 changes: 2 additions & 2 deletions config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
|
*/

'default' => env('DB_CONNECTION', 'mysql'),
'default' => env('DB_CONNECTION', 'sqlite'),

/*
|--------------------------------------------------------------------------
Expand All @@ -38,7 +38,7 @@
'sqlite' => [
'driver' => 'sqlite',
'url' => env('DATABASE_URL'),
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'database' => dirname(__DIR__).'/database/database.sqlite',
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],
Expand Down
2 changes: 1 addition & 1 deletion config/queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@

'failed' => [
'driver' => env('QUEUE_FAILED_DRIVER', 'database-uuids'),
'database' => env('DB_CONNECTION', 'mysql'),
'database' => env('DB_CONNECTION', 'sqlite'),
'table' => 'failed_jobs',
],

Expand Down
2 changes: 1 addition & 1 deletion database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function definition(): array
'name' => $this->faker->name(),
'email' => $this->faker->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'password' => $this->faker->password(8),
'two_factor_secret' => null,
'two_factor_recovery_codes' => null,
'remember_token' => Str::random(10),
Expand Down
Empty file modified storage/app/.gitignore
100644 → 100755
Empty file.
Empty file modified storage/app/public/.gitignore
100644 → 100755
Empty file.
Empty file modified storage/framework/.gitignore
100644 → 100755
Empty file.
Empty file modified storage/framework/cache/.gitignore
100644 → 100755
Empty file.
Empty file modified storage/framework/cache/data/.gitignore
100644 → 100755
Empty file.
Empty file modified storage/framework/sessions/.gitignore
100644 → 100755
Empty file.
Empty file modified storage/framework/testing/.gitignore
100644 → 100755
Empty file.
Empty file modified storage/framework/views/.gitignore
100644 → 100755
Empty file.
Empty file modified storage/logs/.gitignore
100644 → 100755
Empty file.
8 changes: 6 additions & 2 deletions tests/Feature/AuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,21 @@ public function test_login_screen_can_be_rendered(): void

public function test_users_can_authenticate_using_the_login_screen(): void
{
$user = User::factory()->create();
$password = 'password123';
$user = User::factory()->create([
'password' => bcrypt($password),
]);

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

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


public function test_users_can_not_authenticate_with_invalid_password(): void
{
$user = User::factory()->create();
Expand Down
6 changes: 5 additions & 1 deletion tests/Feature/BrowserSessionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ class BrowserSessionsTest extends TestCase

public function test_other_browser_sessions_can_be_logged_out(): void
{
$this->actingAs($user = User::factory()->create());
$user = User::factory()->create([
'password' => bcrypt('password'),
]);

$this->actingAs($user);

$response = $this->delete('/user/other-browser-sessions', [
'password' => 'password',
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/DeleteApiTokenTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function test_api_tokens_can_be_deleted(): void
'abilities' => ['create', 'read'],
]);

$response = $this->delete('/user/api-tokens/'.$token->id);
$response = $this->delete('/user/api-tokens/' . $token->id);

$this->assertCount(0, $user->fresh()->tokens);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/EmailVerificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function test_email_can_be_verified(): void
Event::assertDispatched(Verified::class);

$this->assertTrue($user->fresh()->hasVerifiedEmail());
$response->assertRedirect(RouteServiceProvider::HOME.'?verified=1');
$response->assertRedirect(RouteServiceProvider::HOME . '?verified=1');
}

public function test_email_can_not_verified_with_invalid_hash(): void
Expand Down
10 changes: 8 additions & 2 deletions tests/Feature/PasswordConfirmationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,22 @@ public function test_confirm_password_screen_can_be_rendered(): void

public function test_password_can_be_confirmed(): void
{
$user = User::factory()->create();
$user = User::factory()->create([
'password' => bcrypt('password'),
]);

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

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

$response->assertRedirect();

$response->assertSessionHasNoErrors();
}


public function test_password_is_not_confirmed_with_invalid_password(): void
{
$user = User::factory()->create();
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/PasswordResetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function test_reset_password_screen_can_be_rendered(): void
]);

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

$response->assertStatus(200);

Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/RoleManagementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public function test_role_model_update_feature()
$role = Role::factory()->create();

$response = $this->put('/roles/' . $role->id, [
'name' => 'Updated role name']);
'name' => 'Updated role name'
]);

$this->assertDatabaseHas('roles', [
'id' => $role->id,
Expand Down Expand Up @@ -90,5 +91,4 @@ public function test_role_user_count_is_shown_with_one_user()

$response->assertSee(1);
}

}
18 changes: 15 additions & 3 deletions tests/Feature/UpdatePasswordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ class UpdatePasswordTest extends TestCase

public function test_password_can_be_updated(): void
{
$this->actingAs($user = User::factory()->create());
$user = User::factory()->create([
'password' => bcrypt('password'),
]);

$this->actingAs(user: $user);

$response = $this->put('/user/password', [
'current_password' => 'password',
Expand All @@ -26,7 +30,11 @@ public function test_password_can_be_updated(): void

public function test_current_password_must_be_correct(): void
{
$this->actingAs($user = User::factory()->create());
$user = User::factory()->create([
'password' => bcrypt('password'),
]);

$this->actingAs($user);

$response = $this->put('/user/password', [
'current_password' => 'wrong-password',
Expand All @@ -41,7 +49,11 @@ public function test_current_password_must_be_correct(): void

public function test_new_passwords_must_match(): void
{
$this->actingAs($user = User::factory()->create());
$user = User::factory()->create([
'password' => bcrypt('password'),
]);

$this->actingAs($user);

$response = $this->put('/user/password', [
'current_password' => 'password',
Expand Down
16 changes: 10 additions & 6 deletions tests/Feature/UserManagementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ public function test_user_model_update_feature()

$response = $this->put('/users/' . $user->id, [
'name' => 'John Doe',
'email' => '[email protected]']);

$this->assertDatabaseHas('users', [
'id' => $user->id,
'name' => 'John Doe',
'email' => '[email protected]']
'email' => '[email protected]'
]);

$this->assertDatabaseHas(
'users',
[
'id' => $user->id,
'name' => 'John Doe',
'email' => '[email protected]'
]
);

$response->assertStatus(302);
Expand Down

0 comments on commit 788937b

Please sign in to comment.