Skip to content

Commit

Permalink
Merge pull request #4 from dansysanalyst/add_tests
Browse files Browse the repository at this point in the history
[TESTS] Add basic tests
  • Loading branch information
arukompas committed Aug 22, 2022
2 parents b7ce9bb + ac9d37d commit 5b8e022
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 2 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
"spatie/laravel-package-tools": "^1.9.2"
},
"require-dev": {
"laravel/pint": "^1.0",
"itsgoingd/clockwork": "^5.1",
"laravel/pint": "^1.0",
"nunomaduro/collision": "^6.0",
"orchestra/testbench": "^7.0",
"orchestra/testbench": "^7.6",
"pestphp/pest": "^1.21",
"pestphp/pest-plugin-laravel": "^1.1",
"phpunit/phpunit": "^9.5"
Expand Down
20 changes: 20 additions & 0 deletions tests/Feature/HomePageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use function Pest\Laravel\get;

it('properly displays & configure back label', function () {
get(route('blv.index'))->assertSeeText('Back to Laravel');

$label = 'My Cool App';

config()->set('log-viewer.back_to_system_label', $label);

get(route('blv.index'))->assertSeeText($label);
});

test('home page loads Livewire component', function ($livewireComponent) {
get(route('blv.index'))->assertSeeLivewire($livewireComponent);
})->with([
'log-viewer::log-list',
'log-viewer::file-list',
]);
21 changes: 21 additions & 0 deletions tests/Feature/LogViewerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Opcodes\LogViewer\Facades\LogViewer;
use function Pest\Laravel\get;

beforeEach(function () {
LogViewer::clearFileCache();
generateLogFiles(['laravel.log', 'other.log']);
});

it('properly includes log files', function () {
get(route('blv.index'))->assertSeeText('laravel.log')
->assertSeeText('other.log');
});

it('properly excludes log files', function () {
config()->set('log-viewer.exclude_files', ['*other*']);

get(route('blv.index'))->assertSeeText('laravel.log')
->assertDontSee('other.log');
});
37 changes: 37 additions & 0 deletions tests/Feature/RoutesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

use Illuminate\Routing\RouteCollection;
use Illuminate\Support\Facades\Route;
use function Pest\Laravel\get;

test('testing route', function ($route) {
get(route($route))->assertOK();
})->with([
'blv.index',
]);

test('the default url can be changed', function () {
config()->set('log-viewer.route_path', 'new-log-route');

reloadRoutes();

expect(route('blv.index'))->toContain('new-log-route');

get(route('blv.index'))->assertOK();
});

/*
|--------------------------------------------------------------------------
| HELPERS
|--------------------------------------------------------------------------
*/

function reloadRoutes(): void
{
$router = Route::getFacadeRoot();
$router->setRoutes((new RouteCollection()));

Route::middleware('web')
->namespace('App\Http\Controllers')
->group('routes/web.php');
}
28 changes: 28 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
<?php

use Illuminate\Support\Facades\File;
use Opcodes\LogViewer\Tests\TestCase;

uses(TestCase::class)->in(__DIR__);

/*
|--------------------------------------------------------------------------
| HELPERS
|--------------------------------------------------------------------------
*/

/**
* Generate log files with random data
*
* @param array <int, string> $files
* @return void
*/
function generateLogFiles(array $files): void
{
foreach ($files as $file) {
$file = storage_path('logs/'.$file);

if (File::exists($file)) {
File::delete($file);
}

File::put($file, str()->random());

test()->assertFileExists($file);
}
}
9 changes: 9 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Opcodes\LogViewer\Tests;

use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider;
use Livewire\LivewireServiceProvider;
use Opcodes\LogViewer\LogViewerServiceProvider;
use Orchestra\Testbench\TestCase as Orchestra;

Expand All @@ -12,6 +14,10 @@ protected function setUp(): void
{
parent::setUp();

if (! defined('LARAVEL_START')) {
define('LARAVEL_START', microtime(true));
}

Factory::guessFactoryNamesUsing(
fn (string $modelName) => 'Opcodes\\LogViewer\\Database\\Factories\\'.class_basename($modelName).'Factory'
);
Expand All @@ -20,13 +26,16 @@ protected function setUp(): void
protected function getPackageProviders($app)
{
return [
LivewireServiceProvider::class,
LogViewerServiceProvider::class,
RouteServiceProvider::class,
];
}

public function getEnvironmentSetUp($app)
{
config()->set('database.default', 'testing');
config()->set('app.key', 'base64:yTtQNlEOB1IqYydLG9Z5pKRSxhZffdOxT1iuZIJi+eM=');

/*
$migration = include __DIR__.'/../database/migrations/create_log-viewer_table.php.stub';
Expand Down

0 comments on commit 5b8e022

Please sign in to comment.