Skip to content

Commit

Permalink
Aligned tests and refactored testing strategy & setup
Browse files Browse the repository at this point in the history
  • Loading branch information
PavlosIsaris committed Oct 3, 2024
1 parent 9bf785e commit b43f429
Show file tree
Hide file tree
Showing 16 changed files with 67 additions and 45 deletions.
16 changes: 4 additions & 12 deletions .env.testing.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,19 @@ 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
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
[email protected]
MAIL_FROM_NAME="Crowdsourcing Platform"

Expand Down
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
18 changes: 7 additions & 11 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<php>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="MAIL_MAILER" value="array"/>
</php>
<testsuites>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
Expand All @@ -11,19 +17,9 @@
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="sqlite_testing"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="MAIL_MAILER" value="array"/>
</php>

<source>
<include>
<directory suffix=".php">./app</directory>
</include>
</source>
</phpunit>
</phpunit>
21 changes: 13 additions & 8 deletions resources/views/crowdsourcing-project/partials/navbar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@
<span class="navbar-toggler-icon"></span>
</button>

<div class="collapse navbar-collapse pull-right" id="top-menu-content">
<ul class="nav navbar-nav ml-auto">
@if(!isset($onErrorPage))
@include("partials.login-menu-options")
@endif
@include('partials.content-languages', ['languages' => $viewModel->project->languages])
</ul>
</div>
@if(isset($viewModel->project))
<div class="collapse navbar-collapse pull-right" id="top-menu-content">
<ul class="nav navbar"
<div class=" collapse navbar-collapse pull-right
" id="top-menu-content">
<ul class="nav navbar-nav ml-auto">
@if(!isset($onErrorPage))
@include("partials.login-menu-options")
@endif
@include('partials.content-languages', ['languages' => $viewModel->project->languages])
</ul>
</div>
@endif
</nav>
</div>
2 changes: 1 addition & 1 deletion resources/views/loggedin-environment/my-account.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class="col-sm-4 control-label">{{ __("my-account.re_enter_password") }}</label>
<div class="card-body">
<div class="row">
<div class="col-md-3">
<a href="{{ route('downloadMyData') }}" data-widget="tooltip"
<a href="{{ route('my-data.download') }}" data-widget="tooltip"
title="This includes all your responses to questionnaires" target="_blank"
class="btn btn-primary">{{ __("my-account.download_my_data") }}</a>
</div>
Expand Down
1 change: 1 addition & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
class CrowdSourcingProjectControllerTest extends TestCase {
use RefreshDatabase;

protected $seed = true;
protected CrowdSourcingProjectRepository $crowdSourcingProjectRepository;

protected function setUp(): void {
Expand Down
5 changes: 5 additions & 0 deletions tests/Feature/Controllers/FileControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
class QuestionnaireControllerTest extends TestCase {
use RefreshDatabase;

protected $seed = true;

/**
* @test
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
class QuestionnaireResponseControllerTest extends TestCase {
use RefreshDatabase;

protected $seed = true;

/** @test */
public function testStoreInvalidData() {
$user = User::factory()->create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 0 additions & 3 deletions tests/Feature/Controllers/UserControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 1 addition & 5 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit b43f429

Please sign in to comment.