Skip to content

Commit 9e2ffd8

Browse files
committed
fix: tests
1 parent dddbbee commit 9e2ffd8

3 files changed

Lines changed: 47 additions & 37 deletions

File tree

database/migrations/create_request_analytics_table.php

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,30 @@ public function up()
1111
$tableName = config('request-analytics.database.table', 'request_analytics');
1212
$connection = config('request-analytics.database.connection');
1313

14-
Schema::connection($connection)->create($tableName, function (Blueprint $table) {
15-
$table->id();
16-
$table->string('path');
17-
$table->string('page_title');
18-
$table->string('ip_address');
19-
$table->string('operating_system')->nullable();
20-
$table->string('browser')->nullable();
21-
$table->string('device')->nullable();
22-
$table->string('screen')->nullable();
23-
$table->string('referrer')->nullable();
24-
$table->string('country')->nullable();
25-
$table->string('city')->nullable();
26-
$table->string('language')->nullable();
27-
$table->tinyText('query_params')->nullable();
28-
$table->string('session_id');
29-
$table->string('visitor_id')->nullable()->index();
30-
$table->unsignedBigInteger('user_id')->nullable();
31-
$table->string('http_method');
32-
$table->string('request_category');
33-
$table->bigInteger('response_time')->nullable()->comment('Response time in milliseconds');
34-
$table->timestamp('visited_at');
35-
});
14+
if (! Schema::connection($connection)->hasTable($tableName)) {
15+
Schema::connection($connection)->create($tableName, function (Blueprint $table) {
16+
$table->id();
17+
$table->string('path');
18+
$table->string('page_title')->nullable();
19+
$table->string('ip_address');
20+
$table->string('operating_system')->nullable();
21+
$table->string('browser')->nullable();
22+
$table->string('device')->nullable();
23+
$table->string('screen')->nullable();
24+
$table->string('referrer')->nullable();
25+
$table->string('country')->nullable();
26+
$table->string('city')->nullable();
27+
$table->string('language')->nullable();
28+
$table->text('query_params')->nullable();
29+
$table->string('session_id');
30+
$table->string('visitor_id')->nullable();
31+
$table->unsignedBigInteger('user_id')->nullable();
32+
$table->string('http_method');
33+
$table->string('request_category');
34+
$table->bigInteger('response_time')->nullable();
35+
$table->timestamp('visited_at');
36+
});
37+
}
3638
}
3739

3840
public function down()

src/RequestAnalyticsServiceProvider.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,10 @@ class RequestAnalyticsServiceProvider extends PackageServiceProvider
1515
{
1616
public function configurePackage(Package $package): void
1717
{
18+
// Manually publish migrations with proper timestamp
1819
$this->publishes([
19-
__DIR__.'/../resources/assets' => public_path('/'),
20-
], 'laravel-request-analytics-assets');
21-
22-
$this->publishes([
23-
__DIR__.'/../config/request-analytics.php' => config_path('request-analytics.php'),
24-
], 'laravel-request-analytics-config');
20+
__DIR__.'/../database/migrations/create_request_analytics_table.php' => database_path('migrations/'.date('Y_m_d_His').'_create_request_analytics_table.php'),
21+
], 'laravel-request-analytics-migrations');
2522

2623
$package
2724
->name('laravel-request-analytics')
@@ -30,7 +27,6 @@ public function configurePackage(Package $package): void
3027
->hasRoute('web')
3128
->hasRoute('api')
3229
->hasAssets()
33-
->hasMigration('create_request_analytics_table')
3430
->hasCommand(RequestAnalyticsCommand::class);
3531

3632
$this->registerMiddlewareAsAliases();

tests/Feature/Commands/SetupCommandTest.php

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,33 @@
44

55
namespace MeShaon\RequestAnalytics\Tests\Feature\Commands;
66

7+
use Illuminate\Foundation\Testing\RefreshDatabase;
78
use Illuminate\Support\Facades\File;
89
use Illuminate\Support\Facades\Schema;
9-
use MeShaon\RequestAnalytics\Tests\TestCase;
10+
use MeShaon\RequestAnalytics\RequestAnalyticsServiceProvider;
11+
use Orchestra\Testbench\TestCase as Orchestra;
1012
use PHPUnit\Framework\Attributes\Test;
1113

12-
class SetupCommandTest extends TestCase
14+
class SetupCommandTest extends Orchestra
1315
{
14-
protected function setUp(): void
16+
use RefreshDatabase;
17+
18+
protected function getPackageProviders($app): array
19+
{
20+
return [RequestAnalyticsServiceProvider::class];
21+
}
22+
23+
public function getEnvironmentSetUp($app): void
1524
{
16-
parent::setUp();
25+
config()->set('database.default', 'testing');
26+
config()->set('database.connections.testing', [
27+
'driver' => 'sqlite',
28+
'database' => ':memory:',
29+
'prefix' => '',
30+
]);
1731

18-
// Drop the table if it exists since we want to test the migration process
19-
if (Schema::hasTable('request_analytics')) {
20-
Schema::dropIfExists('request_analytics');
21-
}
32+
// Only essential config needed for setup command testing
33+
config()->set('request-analytics.database.table', 'request_analytics');
2234
}
2335

2436
#[Test]

0 commit comments

Comments
 (0)