Skip to content

Commit

Permalink
Merge pull request #73 from scify/master
Browse files Browse the repository at this point in the history
Fix for seed
  • Loading branch information
PavlosIsaris authored Oct 4, 2024
2 parents dc813f3 + 7f2b8fd commit 03fe565
Show file tree
Hide file tree
Showing 18 changed files with 118 additions and 75 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
78 changes: 49 additions & 29 deletions .github/workflows/laravel-tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Laravel
name: "Run Laravel Tests"

on:
push:
Expand All @@ -8,34 +8,54 @@ on:

jobs:
laravel-tests:

runs-on: ubuntu-latest

steps:
- uses: shivammathur/setup-php@15c43e89cdef867065b0213be354c2841860869e
with:
php-version: '8.0'
- uses: actions/checkout@v4
- name: Copy .env
run: .env.testing.example .env.testing
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- 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
- name: Migrate
run: php artisan migrate --database=sqlite_testing
- name: Seed
run: php artisan db:seed --database=sqlite_testing
- name: Cache
run: php artisan config:cache --env=testing
- name: Execute tests (Unit and Feature tests) via PHPUnit/Pest
env:
DB_CONNECTION: sqlite
DB_DATABASE: database/database.sqlite
run: php artisan test
- uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv
coverage: none

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'

- name: Run composer install
run: composer install -n --prefer-dist --no-ansi --no-progress --no-suggest --no-interaction --no-scripts

- name: Install npm dependencies
run: npm install

- name: Build assets with Vite
run: npm run build

- name: Copy .env
run: cp .env.testing.example .env.testing && cp .env.testing .env

- 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
- name: Migrate
run: php artisan migrate --database=sqlite_testing

- name: Seed
run: php artisan db:seed --database=sqlite_testing

- name: Cache
run: php artisan config:cache --env=testing

- name: Execute tests (Unit and Feature tests) via PHPUnit/Pest
run: php artisan test
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
3 changes: 2 additions & 1 deletion database/seeders/LanguagesLkpTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,9 @@ public function run() {
'available_for_platform_translation' => false,
],
];
$index = 1;
foreach ($languages as $languageObj) {
$this->languagesRepository->updateOrCreate(['language_code' => $languageObj['code']],
$this->languagesRepository->updateOrCreate(['id' => $index++],
[
'language_code' => $languageObj['code'],
'language_name' => $languageObj['name'],
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 03fe565

Please sign in to comment.