Skip to content

Commit

Permalink
test: Add feature tests
Browse files Browse the repository at this point in the history
  • Loading branch information
keriati committed Dec 4, 2022
1 parent 52620bc commit 45cc84c
Show file tree
Hide file tree
Showing 17 changed files with 360 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
run: yarn && yarn dev

- name: Run tests
run: ./vendor/bin/phpunit
run: php artisan test
env:
APP_ENV: testing

Expand Down
2 changes: 2 additions & 0 deletions app/ItemTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\Pivot;

/**
Expand All @@ -22,4 +23,5 @@
*/
class ItemTag extends Pivot
{
use HasFactory;
}
7 changes: 5 additions & 2 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,11 @@ public function setupDatabase(): void
{
$db_type = config()->get('database.default');

if ($db_type == 'sqlite' && ! is_file(database_path('app.sqlite'))) {
touch(database_path('app.sqlite'));
if ($db_type == 'sqlite') {
$db_file = database_path(env('DB_DATABASE', 'app.sqlite'));
if (! is_file($db_file)) {
touch($db_file);
}
}

if ($this->needsDBUpdate()) {
Expand Down
3 changes: 3 additions & 0 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Foundation\Auth\User as Authenticatable;
Expand Down Expand Up @@ -45,6 +46,8 @@ class User extends Authenticatable
{
use Notifiable;

use HasFactory;

/**
* The attributes that are mass assignable.
*
Expand Down
3 changes: 1 addition & 2 deletions database/factories/ItemFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Item;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class ItemFactory extends Factory
{
Expand All @@ -20,7 +19,7 @@ class ItemFactory extends Factory
*
* @return array
*/
public function definition()
public function definition(): array
{
return [
'title' => $this->faker->unique()->text(),
Expand Down
27 changes: 27 additions & 0 deletions database/factories/ItemTagFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Database\Factories;

use App\Item;
use App\ItemTag;
use Illuminate\Database\Eloquent\Factories\Factory;

class ItemTagFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = ItemTag::class;

/**
* Define the model's default state.
*
* @return array
*/
public function definition(): array
{
return [];
}
}
12 changes: 10 additions & 2 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@

namespace Database\Factories;

use App\User;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;

class UserFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = User::class;

/**
* Define the model's default state.
*
Expand All @@ -15,10 +23,10 @@ class UserFactory extends Factory
public function definition()
{
return [
'name' => $this->faker->name(),
'username' => $this->faker->name(),
'email' => $this->faker->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'public_front' => 1,
'remember_token' => Str::random(10),
];
}
Expand Down
2 changes: 1 addition & 1 deletion storage/app/supportedapps.json

Large diffs are not rendered by default.

50 changes: 50 additions & 0 deletions tests/.env.testing
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
APP_NAME=Heimdall
APP_ENV=local
APP_KEY=
APP_DEBUG=false
APP_URL=http://localhost

LOG_CHANNEL=daily

DB_CONNECTION=sqlite
DB_DATABASE=test.sqlite

#DB_CONNECTION=<mysql | pgsql>
#DB_HOST=<hostname | ip>
#DB_PORT=<port number>
#DB_DATABASE=<database>
#DB_USERNAME=<user>
#DB_PASSWORD=<password>

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
2 changes: 2 additions & 0 deletions tests/CreatesApplication.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';

$app->loadEnvironmentFrom('tests/.env.testing');

$app->make(Kernel::class)->bootstrap();

return $app;
Expand Down
83 changes: 83 additions & 0 deletions tests/Feature/DashTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Tests\Feature;

use App\Item;
use App\ItemTag;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class DashTest extends TestCase
{
use RefreshDatabase;

/**
* Helpers
*/

private function addPinnedItemWithTitleToDB($title)
{
$item = Item::factory()
->create([
'title' => $title,
'pinned' => 1,
]);

ItemTag::factory()->create([
'item_id' => $item->id,
'tag_id' => 0,
]);
}

private function addTagWithTitleToDB($title)
{
Item::factory()
->create([
'title' => $title,
'type' => 1,
]);
}

/**
* Test Cases
*/

public function test_loads_empty_dash()
{
$this->seed();

$response = $this->get('/');

$response->assertStatus(200);
}

public function test_displays_items_on_the_dash()
{
$this->seed();

$this->addPinnedItemWithTitleToDB('Item 1');
$this->addPinnedItemWithTitleToDB('Item 2');
$this->addPinnedItemWithTitleToDB('Item 3');

$response = $this->get('/');

$response->assertStatus(200);
$response->assertSee('Item 1');
$response->assertSee('Item 2');
$response->assertSee('Item 3');
}

public function test_displays_tags_on_the_dash()
{
$this->seed();

$this->addTagWithTitleToDB('Tag 1');
$this->addTagWithTitleToDB('Tag 2');

$response = $this->get('/');

$response->assertStatus(200);
$response->assertSee('Tag 1');
$response->assertSee('Tag 2');
}
}
23 changes: 0 additions & 23 deletions tests/Feature/ExampleTest.php

This file was deleted.

32 changes: 32 additions & 0 deletions tests/Feature/ItemCreateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ItemCreateTest extends TestCase
{
use RefreshDatabase;

public function test_displays_the_item_create_page()
{
$this->seed();

$response = $this->get('/items/create');

$response->assertStatus(200);
}

/**
* @return void
*/
public function test_display_the_home_dashboard_tag()
{
$this->seed();

$response = $this->get('/items/create');

$response->assertSee('Home dashboard');
}
}
34 changes: 34 additions & 0 deletions tests/Feature/ItemListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Tests\Feature;

use App\Item;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ItemListTest extends TestCase
{
use RefreshDatabase;

protected function addItemWithTitleToDB($title)
{
Item::factory()
->create([
'title' => $title
]);
}

public function test_displays_items_on_the_item_list_page()
{
$this->addItemWithTitleToDB('Item 1');
$this->addItemWithTitleToDB('Item 2');
$this->addItemWithTitleToDB('Item 3');

$response = $this->get('/items');

$response->assertStatus(200);
$response->assertSee('Item 1');
$response->assertSee('Item 2');
$response->assertSee('Item 3');
}
}
Loading

0 comments on commit 45cc84c

Please sign in to comment.