Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add database connection #568

Merged
merged 6 commits into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ build
composer.lock
vendor
.phpunit.result.cache
/.idea
Gummibeer marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 6 additions & 0 deletions config/activitylog.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,10 @@
* used by the Activity model shipped with this package.
*/
'table_name' => 'activity_log',

/*
* This is the database connection used to migrate the table and
Gummibeer marked this conversation as resolved.
Show resolved Hide resolved
* used by the Activity model shipped with this package.
*/
'database_connection' => env('ACTIVITY_LOGGER_DB_CONNECTION', 'mysql'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this should default to DB_CONNECTION instead of mysql, since a lot of unit tests set only DB_CONNECTION in phpunit.xml and therefore test suites will break without explicitly setting this config. Case in point, sqlite :memory: connections.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point - now the philosophic question: should we use another env() in the fallback or just use DB_CONNECTION?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @drbyte to pointed out. I was in doubt when I did the pull request but I think this is best solution!
I will think twice about this config. It'll be helpfull for future projects! 👍

];
3 changes: 0 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,4 @@
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<php>
<env name="DB_CONNECTION" value="testing" />
</php>
</phpunit>
4 changes: 4 additions & 0 deletions src/Models/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ class Activity extends Model implements ActivityContract

public function __construct(array $attributes = [])
{
if (! isset($this->connection)) {
$this->setConnection(config('activitylog.database_connection'));
}

if (! isset($this->table)) {
$this->setTable(config('activitylog.table_name'));
}
Expand Down
37 changes: 37 additions & 0 deletions tests/CustomDatabaseConnectionActivityModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Spatie\Activitylog\Test;

use Spatie\Activitylog\Models\Activity;
use Spatie\Activitylog\Test\Models\CustomDatabaseConnectionOnActivityModel;

class CustomDatabaseConnectionActivityModelTest extends TestCase
{
/** @test */
public function it_uses_the_database_connection_from_the_configuration()
{
$model = new Activity();

$this->assertEquals($model->getConnectionName(), config('activitylog.database_connection'));
}

/** @test */
public function it_uses_a_custom_database_connection()
{
$model = new Activity();

$model->setConnection('custom_sqlite');

$this->assertNotEquals($model->getConnectionName(), config('activitylog.database_connection'));
$this->assertEquals($model->getConnectionName(), 'custom_sqlite');
}

/** @test */
public function it_uses_the_database_connection_from_model()
{
$model = new CustomDatabaseConnectionOnActivityModel();

$this->assertNotEquals($model->getConnectionName(), config('activitylog.database_connection'));
$this->assertEquals($model->getConnectionName(), 'custom_connection_name');
}
}
10 changes: 10 additions & 0 deletions tests/Models/CustomDatabaseConnectionOnActivityModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Spatie\Activitylog\Test\Models;

use Spatie\Activitylog\Models\Activity;

class CustomDatabaseConnectionOnActivityModel extends Activity
{
protected $connection = 'custom_connection_name';
}
7 changes: 7 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ protected function getPackageProviders($app)

public function getEnvironmentSetUp($app)
{
$app['config']->set('activitylog.database_connection', 'sqlite');
$app['config']->set('database.default', 'sqlite');
$app['config']->set('database.connections.sqlite', [
'driver' => 'sqlite',
'database' => ':memory:',
]);

$app['config']->set('auth.providers.users.model', User::class);
$app['config']->set('app.key', 'base64:'.base64_encode(
Encrypter::generateKey($app['config']['app.cipher'])
Expand Down