From b43f4296d67d176a6ada1301c370cb0928cadfbb Mon Sep 17 00:00:00 2001 From: Paul Isaris Date: Thu, 3 Oct 2024 15:57:06 +0300 Subject: [PATCH] Aligned tests and refactored testing strategy & setup --- .env.testing.example | 16 ++++---------- README.md | 10 +++++++-- app/Http/Controllers/UserController.php | 2 +- phpunit.xml | 18 +++++++--------- .../partials/navbar.blade.php | 21 ++++++++++++------- .../loggedin-environment/my-account.blade.php | 2 +- routes/web.php | 1 + .../CrowdSourcingProjectControllerTest.php | 1 + .../Controllers/FileControllerTest.php | 5 +++++ .../QuestionnaireControllerTest.php | 2 ++ .../QuestionnaireReportControllerTest.php | 13 ++++++++++-- .../QuestionnaireResponseControllerTest.php | 2 ++ .../QuestionnaireStatisticsControllerTest.php | 5 +++++ .../Controllers/UserControllerTest.php | 3 --- tests/TestCase.php | 6 +----- .../QuestionnaireAnswerVoteRepositoryTest.php | 5 +++++ 16 files changed, 67 insertions(+), 45 deletions(-) diff --git a/.env.testing.example b/.env.testing.example index f60a1dec..9c8529fb 100644 --- a/.env.testing.example +++ b/.env.testing.example @@ -5,15 +5,11 @@ APP_DEBUG=true DEBUGBAR_ENABLED=true APP_LOG_LEVEL=debug APP_URL=https://dev.crowdsourcing -APP_VERSION =v0.1 +APP_VERSION=v0.1 GOOGLE_MAPS_KEY= -DB_CONNECTION=mysql -DB_HOST=127.0.0.1 -DB_PORT=3306 -DB_DATABASE=crowdsourcing_testing -DB_USERNAME=root -DB_PASSWORD=root +DB_CONNECTION=sqlite_testing +DB_DATABASE=sqlite_testing BROADCAST_DRIVER=redis CACHE_DRIVER=array @@ -21,11 +17,7 @@ QUEUE_CONNECTION=sync SESSION_DRIVER=array SESSION_LIFETIME=120 -MAIL_MAILER=smtp -MAIL_HOST=smtp.mailtrap.io -MAIL_PORT=2525 -MAIL_USERNAME= -MAIL_PASSWORD= +MAIL_MAILER=array MAIL_FROM_ADDRESS=crowdsourcing@scify.org MAIL_FROM_NAME="Crowdsourcing Platform" diff --git a/README.md b/README.md index 949431f9..82083dc2 100644 --- a/README.md +++ b/README.md @@ -620,9 +620,15 @@ Then, run the `migrate` and `seed` commands for the testing Database: ```bash cp .env.testing.example .env.testing -php artisan migrate --env=testing +php artisan migrate --env=testing --database=sqlite_testing -php artisan db:seed --env=testing --class=DatabaseSeeder +php artisan db:seed --env=testing --database=sqlite_testing --class=DatabaseSeeder +``` + +Then, you can run the tests: + +```bash +php artisan test ``` ## How to debug diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 1dfbffa5..b16e2284 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -94,7 +94,7 @@ public function showUserHistory() { return view('loggedin-environment.my-history', ['responses' => $responses]); } - public function downloadUserData() { + public function downloadMyData() { $headers = [ 'Content-type' => 'text/csv', 'Content-Disposition' => 'attachment; filename=file' . time() . '.csv', diff --git a/phpunit.xml b/phpunit.xml index 85d7c295..e329ed21 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -3,6 +3,12 @@ colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false"> + + + + + + ./tests/Feature @@ -11,19 +17,9 @@ ./tests/Unit - - - - - - - - - - ./app - + \ No newline at end of file diff --git a/resources/views/crowdsourcing-project/partials/navbar.blade.php b/resources/views/crowdsourcing-project/partials/navbar.blade.php index 204bac3f..ebd7095e 100644 --- a/resources/views/crowdsourcing-project/partials/navbar.blade.php +++ b/resources/views/crowdsourcing-project/partials/navbar.blade.php @@ -17,13 +17,18 @@ - + @if(isset($viewModel->project)) + + @endif diff --git a/resources/views/loggedin-environment/my-account.blade.php b/resources/views/loggedin-environment/my-account.blade.php index 368c5633..982c8674 100644 --- a/resources/views/loggedin-environment/my-account.blade.php +++ b/resources/views/loggedin-environment/my-account.blade.php @@ -92,7 +92,7 @@ class="col-sm-4 control-label">{{ __("my-account.re_enter_password") }}
diff --git a/routes/web.php b/routes/web.php index 4727c057..886b5520 100644 --- a/routes/web.php +++ b/routes/web.php @@ -97,6 +97,7 @@ Route::post('/user/deactivate', [UserController::class, 'deactivateLoggedInUser'])->name('deactivateUser'); Route::post('questionnaire/delete-response', [QuestionnaireResponseController::class, 'destroy'])->name('questionnaire_response.destroy'); Route::get('/questionnaire/{questionnaire_id}/download-responses', [QuestionnaireResponseController::class, 'downloadQuestionnaireResponses'])->name('questionnaire.responses.download'); + Route::get('/user/my-data/download', [UserController::class, 'downloadMyData'])->name('my-data.download'); }); Route::group($localeInfo, function () { diff --git a/tests/Feature/Controllers/CrowdSourcingProjectControllerTest.php b/tests/Feature/Controllers/CrowdSourcingProjectControllerTest.php index 7027b43a..e466a4b0 100644 --- a/tests/Feature/Controllers/CrowdSourcingProjectControllerTest.php +++ b/tests/Feature/Controllers/CrowdSourcingProjectControllerTest.php @@ -16,6 +16,7 @@ class CrowdSourcingProjectControllerTest extends TestCase { use RefreshDatabase; + protected $seed = true; protected CrowdSourcingProjectRepository $crowdSourcingProjectRepository; protected function setUp(): void { diff --git a/tests/Feature/Controllers/FileControllerTest.php b/tests/Feature/Controllers/FileControllerTest.php index 8fc510d5..b70f5edf 100644 --- a/tests/Feature/Controllers/FileControllerTest.php +++ b/tests/Feature/Controllers/FileControllerTest.php @@ -2,11 +2,16 @@ namespace Tests\Feature\Controllers; +use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Http\UploadedFile; use Illuminate\Support\Facades\Storage; use Tests\TestCase; class FileControllerTest extends TestCase { + use RefreshDatabase; + + protected $seed = true; + /** @test */ public function uploadFilesSuccessfullyUploadsFiles() { Storage::fake('s3'); diff --git a/tests/Feature/Controllers/Questionnaire/QuestionnaireControllerTest.php b/tests/Feature/Controllers/Questionnaire/QuestionnaireControllerTest.php index 95daa887..1da12dc0 100644 --- a/tests/Feature/Controllers/Questionnaire/QuestionnaireControllerTest.php +++ b/tests/Feature/Controllers/Questionnaire/QuestionnaireControllerTest.php @@ -18,6 +18,8 @@ class QuestionnaireControllerTest extends TestCase { use RefreshDatabase; + protected $seed = true; + /** * @test */ diff --git a/tests/Feature/Controllers/Questionnaire/QuestionnaireReportControllerTest.php b/tests/Feature/Controllers/Questionnaire/QuestionnaireReportControllerTest.php index eb777224..5530fdc7 100644 --- a/tests/Feature/Controllers/Questionnaire/QuestionnaireReportControllerTest.php +++ b/tests/Feature/Controllers/Questionnaire/QuestionnaireReportControllerTest.php @@ -9,9 +9,14 @@ use App\Repository\Questionnaire\QuestionnaireRepository; use Exception; use Illuminate\Database\QueryException; +use Illuminate\Foundation\Testing\RefreshDatabase; use Tests\TestCase; class QuestionnaireReportControllerTest extends TestCase { + use RefreshDatabase; + + protected $seed = true; + /** @test */ public function viewReportsPageReturnsCorrectView() { $user = User::factory() @@ -44,7 +49,9 @@ public function getReportDataForQuestionnaireReturnsCorrectJsonResponse() { /** @test */ public function getReportDataForQuestionnaireHandlesQueryException() { - $user = User::factory()->create(); + $user = User::factory() + ->has(UserRole::factory()->state(['role_id' => UserRolesLkp::ADMIN])) + ->create(); $this->be($user); $this->mock(QuestionnaireRepository::class, function ($mock) { @@ -60,7 +67,9 @@ public function getReportDataForQuestionnaireHandlesQueryException() { /** @test */ public function getReportDataForQuestionnaireHandlesGeneralException() { - $user = User::factory()->create(); + $user = User::factory() + ->has(UserRole::factory()->state(['role_id' => UserRolesLkp::ADMIN])) + ->create(); $this->be($user); $this->mock(QuestionnaireRepository::class, function ($mock) { diff --git a/tests/Feature/Controllers/Questionnaire/QuestionnaireResponseControllerTest.php b/tests/Feature/Controllers/Questionnaire/QuestionnaireResponseControllerTest.php index 527b2f08..d0723174 100644 --- a/tests/Feature/Controllers/Questionnaire/QuestionnaireResponseControllerTest.php +++ b/tests/Feature/Controllers/Questionnaire/QuestionnaireResponseControllerTest.php @@ -13,6 +13,8 @@ class QuestionnaireResponseControllerTest extends TestCase { use RefreshDatabase; + protected $seed = true; + /** @test */ public function testStoreInvalidData() { $user = User::factory()->create(); diff --git a/tests/Feature/Controllers/Questionnaire/QuestionnaireStatisticsControllerTest.php b/tests/Feature/Controllers/Questionnaire/QuestionnaireStatisticsControllerTest.php index 870c4b99..23949ae3 100644 --- a/tests/Feature/Controllers/Questionnaire/QuestionnaireStatisticsControllerTest.php +++ b/tests/Feature/Controllers/Questionnaire/QuestionnaireStatisticsControllerTest.php @@ -10,9 +10,14 @@ use App\Models\Questionnaire\Questionnaire; use App\Models\User; use App\Models\UserRole; +use Illuminate\Foundation\Testing\RefreshDatabase; use Tests\TestCase; class QuestionnaireStatisticsControllerTest extends TestCase { + use RefreshDatabase; + + protected $seed = true; + /** @test */ public function showStatisticsPageForQuestionnaireReturnsCorrectView() { $user = User::factory()->create(); diff --git a/tests/Feature/Controllers/UserControllerTest.php b/tests/Feature/Controllers/UserControllerTest.php index e7fc7193..1c466c21 100644 --- a/tests/Feature/Controllers/UserControllerTest.php +++ b/tests/Feature/Controllers/UserControllerTest.php @@ -5,12 +5,9 @@ use App\BusinessLogicLayer\lkp\UserRolesLkp; use App\Models\User; use App\Models\UserRole; -use Illuminate\Foundation\Testing\RefreshDatabase; use Tests\TestCase; class UserControllerTest extends TestCase { - use RefreshDatabase; - /** @test */ public function myDashboardDisplaysDashboardForAuthenticated_user() { $user = User::factory()->create(); diff --git a/tests/TestCase.php b/tests/TestCase.php index 72d11d60..e301d09b 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,16 +2,12 @@ namespace Tests; -use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; abstract class TestCase extends BaseTestCase { - use CreatesApplication, RefreshDatabase; + use CreatesApplication; protected function setUp(): void { parent::setUp(); - - // Run the database seeds specific to testing - $this->artisan('db:seed', ['--class' => 'DatabaseSeeder']); } } diff --git a/tests/Unit/Repository/Questionnaire/QuestionnaireAnswerVoteRepositoryTest.php b/tests/Unit/Repository/Questionnaire/QuestionnaireAnswerVoteRepositoryTest.php index ba4fcbb5..df9819a9 100644 --- a/tests/Unit/Repository/Questionnaire/QuestionnaireAnswerVoteRepositoryTest.php +++ b/tests/Unit/Repository/Questionnaire/QuestionnaireAnswerVoteRepositoryTest.php @@ -7,9 +7,14 @@ use App\Models\User; use App\Repository\Questionnaire\Responses\QuestionnaireAnswerVoteRepository; use Illuminate\Contracts\Container\BindingResolutionException; +use Illuminate\Foundation\Testing\RefreshDatabase; use Tests\TestCase; class QuestionnaireAnswerVoteRepositoryTest extends TestCase { + use RefreshDatabase; + + protected $seed = true; + /** @test * @throws BindingResolutionException */