Skip to content

Commit

Permalink
Merge pull request #476 from thor/fix/tests-and-sqlite-migrations
Browse files Browse the repository at this point in the history
fix: tests and sqlite migrations
  • Loading branch information
blt950 authored Jan 4, 2023
2 parents a623a0b + a3fa3f9 commit c095114
Show file tree
Hide file tree
Showing 16 changed files with 164 additions and 92 deletions.
28 changes: 18 additions & 10 deletions .github/workflows/test-application.yaml
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
name: Test App
name: Run Tests

on:
pull_request:
branches: [ master ]

jobs:
test-app:
runs-on: ubuntu-latest

runs-on: ubuntu-18.04

steps:
# Test app
- uses: actions/checkout@v2

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

- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-suggest --no-progress --prefer-dist && npm install

- 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: Execute tests (Unit and Feature tests) via PHPUnit
env:
DB_CONNECTION: sqlite
DB_DATABASE: database/database.sqlite
run: vendor/bin/phpunit
touch database/testing.sqlite
- name: Execute unit and feature tests via PHPUnit
run: ./vendor/bin/phpunit --log-junit=report.junit.xml

- name: Upload test results
uses: actions/upload-artifact@v2
if: success() || failure()
with:
name: test-results
path: report.junit.xml
17 changes: 17 additions & 0 deletions .github/workflows/test-report.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: 'Test Report'
on:
workflow_run:
workflows:
- "Run Tests"
types:
- completed
jobs:
report:
runs-on: ubuntu-latest
steps:
- uses: dorny/test-reporter@v1
with:
artifact: test-results
name: Laravel/PHPUnit Tests
path: '*.xml'
reporter: jest-junit
42 changes: 38 additions & 4 deletions CONTRIBUTE.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,55 @@ Contributions are much appreciated to help everyone move this service forward wi

In order to keep a collaborative project in the same style and understandable, it's important to follow some conventions:

##### GitHub Branches
### GitHub Branches
We name branches with `topic/name-here` including fixes and features, for instance `topic/new-api` or `topic/mentor-mail-fix`

##### Models/SQL
### Testing

We strive to create tests for the features we do. This helps reduce the risk of us breaking the feature in the future.
It also helps us to keep the code cleaner. Imagine how you'd like to use your own feature if it were provided as a library to yourself. From that, try to create some accompanying tests, whether they're unit or feature tests.

#### Getting started

To get you started these instructions overlap a little with typical Laravel project setup.
For additional details, see Laravel documentation on setting up a new project.

```shell
# 0. Install the dependencies
composer install

# 1. Setup a local environment variables file
cp -n .env.example .env

# 2. Generate and set a Laravel application key in your environment variables
php artisan key:generate

# 3. Create a local database
php artisan migrate --database sqlite-testing
```

You should now be able to run the tests locally. To the next section.

#### Running the tests

Once you've got your setup locally, running the tests is a brief affair.

```shell
php artisan test
```

### Models/SQL
* MySQL tables are named in plural e.g `training_reports`, not `training_report`
* Models are named in singular e.g. `Training`, not `Trainings`
* Models names don't have any specific suffix or prefix
* Models are per Laravel 8 located in root of `App/Models` folder.

##### Controllers
### Controllers
* Controllers are suffixed with `Controller`, for instance `TrainingController`
* Controllers are named in singular e.g. `TrainingController`, not `TrainingsController`
* The controllers should mainly consist of the methods of "7 restful controller actions" [Check out this video](https://laracasts.com/series/laravel-6-from-scratch/episodes/21?autoplay=true)

##### Other
### Other
* We name our views with blade suffix for clarity, like `header.blade.php`
* For more in-depth conventions which we try to follow, check out [Laravel best practices](https://github.com/alexeymezenin/laravel-best-practices/blob/master/README.md#contents)
* We tab with 4 spaces for increased readability
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,8 @@ There's quite a few automations in Control Center that are running through the c
- Daily member cleanup, if a member leaves the division, their training will be automatically closed. Same for mentors. Does not apply to visitors.
- Other misc cleanups

## Contribution, conventions and intro to Laravel
Read the [CONTRIBUTE.md](CONTRIBUTE.md) for details.
## Contributing, conventions and intro to Laravel

Do you want to help us with improving Control Center? Curious about whether we use testing? Stylistic choices?

[Read the `CONTRIBUTE.md`](CONTRIBUTE.md) for details.
6 changes: 6 additions & 0 deletions config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],

'sqlite-testing' => [
'driver' => 'sqlite',
'database' => database_path('testing.sqlite'),
'foreign_key_constraints' => true,
],

'mysql' => [
'driver' => 'mysql',
'url' => env('DATABASE_URL'),
Expand Down
2 changes: 1 addition & 1 deletion database/factories/TrainingReportFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function definition()
$date = $this->faker->dateTimeBetween($startDate = '-1 years', $endDate = 'now');

return [
'report_date' => $date,
'report_date' => $date->format("Y-M-d"),
'content' => $this->faker->paragraph(),
'contentimprove' => $this->faker->paragraph(),
'position' => Position::inRandomOrder()->first()->callsign,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function up()
? config('database.connections.' . $connection . '.prefix')
: '';

if (env('DB_CONNECTION') != 'sqlite') {
if (Schema::getConnection()->getDriverName() != 'sqlite') {
$table->dropForeign($prefix . 'users_country_foreign');
$table->dropForeign($prefix . 'users_group_foreign');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ public function up()
{
Schema::rename('vatbooks', 'bookings');
Schema::table('bookings', function (Blueprint $table) {
$table->dropColumn('eu_id');
$table->dropColumn('local_id');
$table->dropColumn('cid');
$table->dropColumn(['eu_id', 'local_id', 'cid']);
});
}

Expand Down
3 changes: 1 addition & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
<server name="MAIL_DRIVER" value="array"/>
<server name="QUEUE_CONNECTION" value="sync"/>
<server name="SESSION_DRIVER" value="array"/>
<server name="DB_CONNECTION" value="sqlite"/>
<server name="DB_DATABASE" value=":memory:"/>
<server name="DB_CONNECTION" value="sqlite-testing"/>
<server name="FILESYSTEM_DISK" value="test"/>
</php>
</phpunit>
15 changes: 8 additions & 7 deletions tests/Feature/FilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Feature;

use App\Models\File;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Http\UploadedFile;
Expand All @@ -29,7 +30,7 @@ protected function tearDown(): void
/** @test */
public function mentor_can_upload_a_pdf_file()
{
$user = \App\Models\User::factory()->create(['id' => 10000001]);
$user = User::factory()->create(['id' => 10000001]);
$user->groups()->attach(3, ['area_id' => 1]);
$file = UploadedFile::fake()->create($this->faker->word . '.pdf', 2048, 'application/pdf');

Expand All @@ -42,7 +43,7 @@ public function mentor_can_upload_a_pdf_file()
/** @test */
public function mentor_can_upload_an_image_file()
{
$user = \App\Models\User::factory()->create(['id' => 10000001]);
$user = User::factory()->create(['id' => 10000001]);
$user->groups()->attach(3, ['area_id' => 1]);
$file = UploadedFile::fake()->image($this->faker->word . '.jpg');

Expand All @@ -55,7 +56,7 @@ public function mentor_can_upload_an_image_file()
/** @test */
public function user_can_see_a_file_they_uploaded()
{
$user = \App\Models\User::factory()->create(['id' => 10000001]);
$user = User::factory()->create(['id' => 10000001]);
$user->groups()->attach(3, ['area_id' => 1]);
$file = UploadedFile::fake()->image($this->faker->word . '.jpg');

Expand All @@ -71,7 +72,7 @@ public function user_can_see_a_file_they_uploaded()
/** @test */
public function regular_user_cant_upload_a_file()
{
$user = \App\Models\User::factory()->create(['id' => 10000001]);
$user = User::factory()->create(['id' => 10000001]);
$file = UploadedFile::fake()->image($this->faker->word);

$this->actingAs($user)->postJson(route('file.store'), ['file' => $file])
Expand All @@ -81,7 +82,7 @@ public function regular_user_cant_upload_a_file()
/** @test */
public function owner_can_delete_their_own_files()
{
$user = \App\Models\User::factory()->create(['id' => 10000001]);
$user = User::factory()->create(['id' => 10000001]);
$user->groups()->attach(3, ['area_id' => 1]);
$file = UploadedFile::fake()->image($this->faker->word . '.jpg');
$response = $this->actingAs($user)->postJson(route('file.store'), ['file' => $file]);
Expand All @@ -96,14 +97,14 @@ public function owner_can_delete_their_own_files()
/** @test */
public function moderator_can_delete_another_users_file()
{
$user = \App\Models\User::factory()->create(['id' => 10000001]);
$user = User::factory()->create(['id' => 10000001]);
$user->groups()->attach(3, ['area_id' => 1]);
$file = UploadedFile::fake()->image($this->faker->word . '.jpg');
$response = $this->actingAs($user)->postJson(route('file.store'), ['file' => $file]);
$file_id = $response->json('file_id');
$response->assertStatus(200)->assertJsonFragment(['message' => 'File successfully uploaded']);

$moderator = \App\Models\User::factory()->create();
$moderator = User::factory()->create();
$moderator->groups()->attach(2, ['area_id' => 1]);

$this->actingAs($moderator)->delete(route('file.delete', ['file' => $file_id]))->assertRedirect()->assertSessionHas('success', 'File successfully deleted');
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/FrontpageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace Tests\Feature;

use Tests\TestCase;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Auth;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class FrontpageTest extends TestCase
{
Expand Down
10 changes: 5 additions & 5 deletions tests/Feature/TrainingExaminationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Tests\Feature;

use Carbon\Carbon;
use Tests\TestCase;
use App\Models\User;
use App\Models\Training;
use App\Models\TrainingExamination;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class TrainingExaminationsTest extends TestCase
{
Expand All @@ -29,7 +29,7 @@ protected function setUp(): void
]),
]);

$this->examination->examiner->groups()->attach(3, ['area_id' => $this->examination->training->area]);
$this->examination->examiner->groups()->attach(3, ['area_id' => $this->examination->training->area->id]);

$this->training = $this->examination->training;
$this->training->mentors()->attach($this->examination->examiner, ['expire_at' => now()->addMonths(12)]);
Expand Down
10 changes: 5 additions & 5 deletions tests/Feature/TrainingObjectAttachmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

namespace Tests\Feature;

use Tests\TestCase;
use App\Models\File;
use App\Models\User;
use App\Models\Training;
use App\Models\TrainingObjectAttachment;
use App\Models\TrainingReport;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
use App\Models\TrainingObjectAttachment;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;

class TrainingObjectAttachmentTest extends TestCase
{
Expand Down
Loading

0 comments on commit c095114

Please sign in to comment.