Skip to content

Commit

Permalink
- Fixed the tests & nullable fields
Browse files Browse the repository at this point in the history
- Removed the Test Command for simplicity
  • Loading branch information
PavlosIsaris committed Dec 11, 2024
1 parent 107cfcc commit 2f5a4fe
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 228 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/laravel-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,11 @@ jobs:
- name: Cache
run: php artisan config:cache --env=testing

- name: Run the migrations in the testing environment
run: php artisan migrate --env=testing --database=sqlite_testing

- name: Run the seeders in the testing environment
run: php artisan db:seed --env=testing --database=sqlite_testing

- name: Execute tests (Unit and Feature tests) via PHPUnit
run: php artisan app:test --migrate --seed
run: php artisan test --env=testing
60 changes: 6 additions & 54 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,72 +440,24 @@ php artisan cache:clear
php artisan config:clear
```

### Method 1: PHPUnit with the Laravel Custom Command

There is a custom Laravel Command that runs the test suite for the application, which can be run by executing the
following command:

```bash
php artisan app:test
```

This command is located in the `app/Console/Commands/TestCommand.php` file.

This command takes the following options:

- `--coverage`: Run the tests with coverage.
- `--filter`: Filter the tests to run only a specific test class or method.
- `--migrate`: Run the migrations before running the tests.
- `--seed`: Seed the database before running the tests.

Every time you run this command, the test database will be created (or used) at `storage/database_testing.sqlite`, the
suite
will be executed, and the results will be displayed in the console.

If you want to only test a specific test class or method, you can run the following command:

```bash
php artisan app:test --filter={METHOD OR CLASS NAME}
```

Note that unlike the PHPUnit command, you need to use the `--filter` flag with the `=` sign.

Example:

```bash
php artisan app:test --filter=CrowdSourcingProjectControllerTest
```

Example of running the tests with migrations and seeding:

```bash
php artisan app:test --migrate --seed
```

### Run the tests with coverage

In order to run the tests with coverage, you can use the `--coverage` flag, like so:

```bash
php artisan app:test --coverage
```

### Method 2: PHPUnit with the `php artisan test` command

You can also run the tests using the `php artisan test` command, which is a built-in Laravel command.
Then, make sure that you have set up the test database:

First you need to run the migrations for the test database:

```bash
php artisan migrate --env=testing --database=sqlite_testing
```

Then run the seeders:
Then run the seeders for the test database:

```bash
php artisan db:seed --env=testing --database=sqlite_testing
```

### PHPUnit with the `php artisan test` command

You can run the tests using the `php artisan test` command, which is a built-in Laravel command.

And finally, run the tests:

```bash
Expand Down
92 changes: 0 additions & 92 deletions app/Console/Commands/TestCommand.php

This file was deleted.

2 changes: 1 addition & 1 deletion app/Repository/LanguageRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ public function getLanguageByCode(string $code): Language {

public function getDefaultLanguage(): Language {
// english is the default language
return Language::find(6);
return Language::findOrFail(6);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void {
Schema::table('crowd_sourcing_project_translations', function (Blueprint $table) {
$table->mediumText('footer')->nullable()->change();
$table->text('sm_description')->nullable()->change();
$table->text('sm_keywords')->nullable()->change();
});
}

/**
* Reverse the migrations.
*/
public function down(): void {
Schema::table('crowd_sourcing_project_translations', function (Blueprint $table) {
//
});
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public function guestCannotAccessCreatePage() {

// 302 is the status code for a redirect (to the login page)
$response->assertStatus(302);
$response->assertRedirect(route('login', ['locale' => 'en']));
$response->assertRedirectContains(route('login', ['locale' => 'en']));
}

/**
Expand Down Expand Up @@ -202,7 +202,7 @@ public function guestCannotAccessIndexPage() {
$response = $this->get(route('projects.index', ['locale' => 'en']));

$response->assertStatus(302);
$response->assertRedirect(route('login', ['locale' => 'en']));
$response->assertRedirectContains(route('login', ['locale' => 'en']));
}

/**
Expand Down Expand Up @@ -288,7 +288,7 @@ public function guestCannotStoreProject() {
]);

$response->assertStatus(302);
$response->assertRedirect(route('login', ['locale' => 'en']));
$response->assertRedirectContains(route('login', ['locale' => 'en']));
}

/**
Expand Down Expand Up @@ -410,7 +410,7 @@ public function guestCannotUpdateProject() {
]);

$response->assertStatus(302);
$response->assertRedirect(route('login', ['locale' => 'en']));
$response->assertRedirectContains(route('login', ['locale' => 'en']));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function guestCannotAccessProblemCreatePage() {

// 302 is the status code for a redirect (to the login page)
$response->assertStatus(302);
$response->assertRedirect(route('login', ['locale' => 'en']));
$response->assertRedirectContains(route('login', ['locale' => 'en']));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function guestCannotSaveQuestionnaireStatus() {
]);

$response->assertStatus(302);
$response->assertRedirect(route('login', ['locale' => 'en']));
$response->assertRedirectContains(route('login', ['locale' => 'en']));
}

/**
Expand Down Expand Up @@ -210,7 +210,7 @@ public function guestCannotUpdateQuestionnaire() {
]);

$response->assertStatus(302);
$response->assertRedirect(route('login', ['locale' => 'en']));
$response->assertRedirectContains(route('login', ['locale' => 'en']));
}

/**
Expand Down Expand Up @@ -302,7 +302,7 @@ public function guestCannotTranslateQuestionnaire() {
]);

$response->assertStatus(302);
$response->assertRedirect(route('login', ['locale' => 'en']));
$response->assertRedirectContains(route('login', ['locale' => 'en']));
}

/**
Expand Down
26 changes: 26 additions & 0 deletions tests/Feature/Controllers/Solution/SolutionControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Feature\Controllers\Solution;

use Tests\TestCase;

class SolutionControllerTest extends TestCase {
/**
* @test
*
* @group solution-controller-test
*
* Test Scenario 1:
* GIVEN that a user is not authenticated,
*
* AND they try to access the solution propose page,
* THEN they should be redirected to the login page.
*
* @return void
*/
public function test_user_proposal_create_redirects_to_login_page_when_user_is_not_authenticated() {
$response = $this->get('/en/project-slug/problems/problem-slug/solutions/propose');

$response->assertRedirectContains(route('login', ['locale' => 'en']));
}
}
10 changes: 5 additions & 5 deletions tests/Feature/Controllers/UserControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function myDashboardRedirectsToLoginForUnauthenticatedUser() {
$response = $this->get(route('my-dashboard', ['locale' => 'en']));

$response->assertStatus(302);
$response->assertRedirect(route('login', ['locale' => 'en']));
$response->assertRedirectContains(route('login', ['locale' => 'en']));
}

/** @test */
Expand All @@ -61,7 +61,7 @@ public function myAccountRedirectsToLoginForUnauthenticatedUser() {
$response = $this->get(route('my-account', ['locale' => 'en']));

$response->assertStatus(302);
$response->assertRedirect(route('login', ['locale' => 'en']));
$response->assertRedirectContains(route('login', ['locale' => 'en']));
}

/** @test */
Expand Down Expand Up @@ -224,7 +224,7 @@ public function patchRedirectsToLoginForUnauthenticatedUser() {
]);

$response->assertStatus(302);
$response->assertRedirect(route('login', ['locale' => 'en']));
$response->assertRedirectContains(route('login', ['locale' => 'en']));
}

/** @test */
Expand Down Expand Up @@ -267,7 +267,7 @@ public function deleteRedirectsToLoginForUnauthenticatedUser() {
->post(route('deleteUser'), ['id' => $user->id]);

$response->assertStatus(302);
$response->assertRedirect(route('login', ['locale' => 'en']));
$response->assertRedirectContains(route('login', ['locale' => 'en']));
}

/** @test */
Expand Down Expand Up @@ -302,7 +302,7 @@ public function showUsersByCriteriaRedirectsToLoginForUnauthenticatedUser() {
$response = $this->get(route('api.users.get-filtered', ['name' => 'John']));

$response->assertStatus(302);
$response->assertRedirect(route('login', ['locale' => 'en']));
$response->assertRedirectContains(route('login', ['locale' => 'en']));
}

/** @test */
Expand Down
Loading

0 comments on commit 2f5a4fe

Please sign in to comment.