Complete tests

This commit is contained in:
Bubka 2024-03-30 15:42:54 +01:00
parent eb3e38f4a6
commit f43fc97523
5 changed files with 218 additions and 0 deletions

View File

@ -13,6 +13,8 @@ class UserObserver
{
/**
* Handle the User "created" event.
*
* @codeCoverageIgnore
*/
public function created(User $user) : void
{
@ -21,6 +23,8 @@ public function created(User $user) : void
/**
* Handle the User "updated" event.
*
* @codeCoverageIgnore
*/
public function updated(User $user) : void
{
@ -81,6 +85,8 @@ public function deleted(User $user) : void
/**
* Handle the User "restored" event.
*
* @codeCoverageIgnore
*/
public function restored(User $user) : void
{
@ -89,6 +95,8 @@ public function restored(User $user) : void
/**
* Handle the User "force deleted" event.
*
* @codeCoverageIgnore
*/
public function forceDeleted(User $user) : void
{

View File

@ -0,0 +1,103 @@
<?php
namespace Tests\Feature\Console;
use App\Console\Commands\Install;
use Jackiedo\DotenvEditor\DotenvEditor;
use PHPUnit\Framework\Attributes\CoversClass;
use Tests\FeatureTestCase;
/**
* InstallTest test class
*/
#[CoversClass(Install::class)]
class InstallTest extends FeatureTestCase
{
/**
* @test
*/
public function test_install_completes()
{
$this->artisan('2fauth:install')
->expectsConfirmation('Existing .env file found. Do you wish to review its vars?', 'no')
->assertSuccessful();
}
/**
* @test
*/
public function test_install_informs_about_no_interaction()
{
$this->artisan('2fauth:install', ['--no-interaction' => true])
->expectsOutput('(Running in no-interaction mode)')
->expectsConfirmation('Existing .env file found. Do you wish to review its vars?', 'no')
->assertSuccessful();
}
/**
* @test
*/
public function test_install_generates_an_app_key()
{
config(['app.key' => '']);
$this->assertEquals('', config('app.key'));
$this->artisan('2fauth:install')
->expectsConfirmation('Existing .env file found. Do you wish to review its vars?', 'no')
->assertSuccessful();
$this->assertNotEquals('', config('app.key'));
}
/**
* @test
*/
public function test_install_gives_2fauth_address()
{
$this->artisan('2fauth:install')
->expectsConfirmation('Existing .env file found. Do you wish to review its vars?', 'no')
->expectsOutputToContain(config('app.url'))
->assertSuccessful();
}
/**
* @test
*/
public function test_install_informs_about_sponsoring()
{
$this->artisan('2fauth:install')
->expectsConfirmation('Existing .env file found. Do you wish to review its vars?', 'no')
->expectsOutputToContain('https://ko-fi.com/bubka')
->expectsOutputToContain('https://github.com/sponsors/Bubka')
->assertSuccessful();
}
/**
* @test
*/
public function test_install_fails_with_exception_message()
{
$mock = $this->mock(DotenvEditor::class);
$mock->shouldReceive('load')
->andThrow(new \Exception('exception message'));
$this->artisan('2fauth:install')
->expectsOutputToContain('exception message')
->assertFailed();
}
/**
* @test
*/
public function test_install_fails_with_link_to_online_help()
{
$mock = $this->mock(DotenvEditor::class);
$mock->shouldReceive('load')
->andThrow(new \Exception());
$this->artisan('2fauth:install')
->expectsOutputToContain(config('2fauth.installDocUrl'))
->assertFailed();
}
}

View File

@ -6,6 +6,7 @@
use App\Http\Controllers\Auth\RegisterController;
use App\Http\Requests\UserStoreRequest;
use App\Models\User;
use App\Rules\ComplyWithEmailRestrictionPolicy;
use Illuminate\Support\Facades\DB;
use PHPUnit\Framework\Attributes\CoversClass;
use Tests\FeatureTestCase;
@ -15,6 +16,7 @@
*/
#[CoversClass(RegisterController::class)]
#[CoversClass(UserStoreRequest::class)]
#[CoversClass(ComplyWithEmailRestrictionPolicy::class)]
class RegisterControllerTest extends FeatureTestCase
{
private const USERNAME = 'john doe';

View File

@ -6,6 +6,7 @@
use App\Models\User;
use App\Notifications\TestEmailSettingNotification;
use App\Services\ReleaseRadarService;
use Illuminate\Support\Facades\Lang;
use Illuminate\Support\Facades\Notification;
use PHPUnit\Framework\Attributes\CoversClass;
use Tests\FeatureTestCase;
@ -14,6 +15,8 @@
* SystemControllerTest test class
*/
#[CoversClass(SystemController::class)]
#[CoversClass(TestEmailSettingNotification::class)]
class SystemControllerTest extends FeatureTestCase
{
/**
@ -133,6 +136,19 @@ public function test_testEmail_sends_a_notification()
Notification::assertSentTo($this->admin, TestEmailSettingNotification::class);
}
/**
* @test
*/
public function test_testEmail_renders_to_email()
{
$mail = (new TestEmailSettingNotification('test_token'))->toMail($this->user)->render();
$this->assertStringContainsString(
Lang::get('notifications.test_email_settings.reason'),
$mail
);
}
/**
* @test
*/

View File

@ -0,0 +1,89 @@
<?php
namespace Tests\Unit\Rules;
use App\Rules\IsValidEmailList;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Support\Facades\Validator;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use Tests\TestCase;
/**
* IsValidEmailListTest test class
*/
#[CoversClass(IsValidEmailList::class)]
class IsValidEmailListTest extends TestCase
{
use WithoutMiddleware;
/**
* @test
*/
#[DataProvider('provideValidData')]
public function test_valid_data(array $data) : void
{
$validator = Validator::make($data, ['value' => [new IsValidEmailList]]);
$this->assertFalse($validator->fails());
}
/**
* Provide Valid data for validation test
*/
public static function provideValidData() : array
{
return [
[[
'value' => 'johndoe@example.com',
]],
[[
'value' => 'johndoe@example.com|janedoe@example.com',
]],
[[
'value' => '|johndoe@example.com|janedoe@example.com',
]],
[[
'value' => 'johndoe@example.com|janedoe@example.com|',
]],
];
}
/**
* @test
*/
#[DataProvider('provideInvalidData')]
public function test_invalid_data(array $data) : void
{
$validator = Validator::make($data, ['value' => [new IsValidEmailList]]);
$this->assertTrue($validator->fails());
}
/**
* Provide Valid data for validation test
*/
public static function provideInvalidData() : array
{
return [
[[
'value' => 'johndoeexamplecom',
]],
[[
'value' => 'johndoe@example.com|janedoeexamplecom',
]],
[[
'value' => 'johndoe@example.com,janedoe@example.com',
]],
[[
'value' => 'johndoe@example.com;janedoe@example.com|',
]],
[[
'value' => 'johndoe@example.com janedoe@example.com',
]],
[[
'value' => 'johndoe@example.com | janedoe@example.com',
]],
];
}
}