test: Add feature tests

This commit is contained in:
Attila Kerekes 2022-12-04 10:14:07 +01:00 committed by Attila Kerekes
parent 52620bc331
commit 45cc84c99c
17 changed files with 360 additions and 31 deletions

View file

@ -41,7 +41,7 @@ jobs:
run: yarn && yarn dev
- name: Run tests
run: ./vendor/bin/phpunit
run: php artisan test
env:
APP_ENV: testing

View file

@ -2,6 +2,7 @@
namespace App;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\Pivot;
/**
@ -22,4 +23,5 @@ use Illuminate\Database\Eloquent\Relations\Pivot;
*/
class ItemTag extends Pivot
{
use HasFactory;
}

View file

@ -137,8 +137,11 @@ class AppServiceProvider extends ServiceProvider
{
$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()) {

View file

@ -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;
@ -45,6 +46,8 @@ class User extends Authenticatable
{
use Notifiable;
use HasFactory;
/**
* The attributes that are mass assignable.
*

View file

@ -4,7 +4,6 @@ namespace Database\Factories;
use App\Item;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class ItemFactory extends Factory
{
@ -20,7 +19,7 @@ class ItemFactory extends Factory
*
* @return array
*/
public function definition()
public function definition(): array
{
return [
'title' => $this->faker->unique()->text(),

View file

@ -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 [];
}
}

View file

@ -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.
*
@ -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),
];
}

File diff suppressed because one or more lines are too long

50
tests/.env.testing Normal file
View file

@ -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}"

View file

@ -15,6 +15,8 @@ trait CreatesApplication
{
$app = require __DIR__.'/../bootstrap/app.php';
$app->loadEnvironmentFrom('tests/.env.testing');
$app->make(Kernel::class)->bootstrap();
return $app;

View file

@ -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');
}
}

View file

@ -1,23 +0,0 @@
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class ExampleTest extends TestCase
{
use RefreshDatabase;
/**
* A basic test example.
*
* @return void
*/
public function test_app_loads()
{
$response = $this->get('/');
$response->assertStatus(200);
}
}

View file

@ -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');
}
}

View file

@ -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');
}
}

View file

@ -0,0 +1,32 @@
<?php
namespace Tests\Feature;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class SettingsTest extends TestCase
{
use RefreshDatabase;
public function test_displays_the_settings_page()
{
$this->seed();
$response = $this->get('/settings');
$response->assertStatus(200);
$response->assertSeeInOrder([
'Version',
'Language',
'Support',
'Donate',
'Background Image',
'Homepage Search',
'Default Search Provider',
'Link opens in',
'Custom CSS',
'Custom JavaScript',
]);
}
}

View file

@ -0,0 +1,35 @@
<?php
namespace Tests\Feature;
use App\Item;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class TagListTest extends TestCase
{
use RefreshDatabase;
private function addTagWithTitleToDB($title)
{
Item::factory()
->create([
'title' => $title,
'type' => 1,
]);
}
public function test_displays_the_tags_on_the_tag_list_page()
{
$this->addTagWithTitleToDB('Tag 1');
$this->addTagWithTitleToDB('Tag 2');
$this->addTagWithTitleToDB('Tag 3');
$response = $this->get('/tags');
$response->assertStatus(200);
$response->assertSee('Tag 1');
$response->assertSee('Tag 2');
$response->assertSee('Tag 3');
}
}

View file

@ -0,0 +1,42 @@
<?php
namespace Tests\Feature;
use App\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class UserListTest extends TestCase
{
use RefreshDatabase;
protected function addUserWithNameToDB($name)
{
User::factory()
->create([
'username' => $name
]);
}
public function test_displays_admin_on_user_list_page_when_default_install()
{
$this->seed();
$response = $this->get('/users');
$response->assertStatus(200);
$response->assertSee('admin');
}
public function test_displays_users_on_user_list_page()
{
$this->seed();
$this->addUserWithNameToDB('User 1');
$response = $this->get('/users');
$response->assertStatus(200);
$response->assertSee('User 1');
}
}