Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pierot committed May 18, 2017
1 parent fba2b64 commit a2738f6
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 190 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# ActivityLog

[![Latest Version on Packagist](https://img.shields.io/packagist/v/jackjoe/activity-log.svg?style=flat-square)](https://packagist.org/packages/jackjoe/activity-log)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/jackjoe/activity-log/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/jackjoe/activity-log/?branch=master)
[![Total Downloads](https://img.shields.io/packagist/dt/jackjoe/activity-log.svg?style=flat-square)](https://packagist.org/packages/jackjoe/activity-log)

- [Installation](#installation)
Expand Down
4 changes: 3 additions & 1 deletion src/ActivityLogServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public function boot()

$this->publishes([
__DIR__ . '/migrations' => database_path('migrations'),
], 'migrations');
], 'migrations');

$this->mergeConfigFrom(__DIR__ . '/config/activity-log.php', 'activity-log');
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Models/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function user()
public static function log($data = [])
{
// set the defaults from config
$defaults = config('log.defaults');
$defaults = config('activity-log.defaults');
if(!is_array($defaults)) {
$defaults = [];
}
Expand All @@ -58,9 +58,9 @@ public static function log($data = [])
$data = (array) $data;
}

// set the user ID
if(config('log.auto_set_user_id') && !isset($data['userId'])) {
$user = call_user_func(config('log.auth_method'));
// set the user ID
if(config('activity-log.auto_set_user_id') && !isset($data['userId'])) {
$user = call_user_func(config('activity-log.auth_method'));
$data['userId'] = isset($user->id) ? $user->id : null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/config/activity-log.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
'auto_set_user_id' => true,

/**
* Auth Method
* Auth driver
*
* If you are using any alternative packages for Authentication
* and User management then you can put in the appropriate
Expand Down
220 changes: 49 additions & 171 deletions tests/ActivityLogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,185 +14,63 @@ public function setUp()
}

/**
* check return instance
* check user relation
* check all data fields are saved
* check auto_set_user_id
* check ip address
* check user agent
* check version set
*/

/** @test */
public function it_returns_instance_of_activity_model()
{
$result = Activity::log(['contentType' => 'Something']);

$this->assertInstanceOf(Activity::class, $result);
}

/** @test */
public function it_has_a_relation_to_a_user()
{
$user = User::first();
$result = Activity::log(['contentType' => 'Something', 'userId' => $user->id]);

$this->assertInstanceOf(User::class, $result->user);
$this->assertEquals($user->id, $result->user->id);
}

/** @test */
public function it_can_log_an_activity()
public function it_has_a_relation_to_the_logged_in_user_when_not_provided_with_userid()
{
// activity()->log($this->activityDescription);
//
// $this->assertEquals($this->activityDescription, $this->getLastActivity()->description);
$user = User::first();
Auth::login($user);
$result = Activity::log(['contentType' => 'Something']);

$this->assertInstanceOf(User::class, $result->user);
$this->assertEquals($user->id, $result->user->id);
}

// /** @test */
// public function it_will_not_log_an_activity_when_the_log_is_not_enabled()
// {
// config(['laravel-activitylog.enabled' => false]);
//
// activity()->log($this->activityDescription);
//
// $this->assertNull($this->getLastActivity());
// }
//
// /** @test */
// public function it_will_log_an_activity_when_enabled_option_is_null()
// {
// config(['laravel-activitylog.enabled' => null]);
//
// activity()->log($this->activityDescription);
//
// $this->assertEquals($this->activityDescription, $this->getLastActivity()->description);
// }
//
// /** @test */
// public function it_will_log_to_the_default_log_by_default()
// {
// activity()->log($this->activityDescription);
//
// $this->assertEquals(config('laravel-activitylog.default_log_name'), $this->getLastActivity()->log_name);
// }
//
// /** @test */
// public function it_can_log_an_activity_to_a_specific_log()
// {
// $customLogName = 'secondLog';
//
// activity($customLogName)->log($this->activityDescription);
// $this->assertEquals($customLogName, $this->getLastActivity()->log_name);
//
// activity()->useLog($customLogName)->log($this->activityDescription);
// $this->assertEquals($customLogName, $this->getLastActivity()->log_name);
// }
//
// /** @test */
// public function it_can_log_an_activity_with_a_subject()
// {
// $subject = Article::first();
//
// activity()
// ->performedOn($subject)
// ->log($this->activityDescription);
//
// $firstActivity = Activity::first();
//
// $this->assertEquals($subject->id, $firstActivity->subject->id);
// $this->assertInstanceOf(Article::class, $firstActivity->subject);
// }
//
// /** @test */
// public function it_can_log_an_activity_with_a_causer()
// {
// $user = User::first();
//
// activity()
// ->causedBy($user)
// ->log($this->activityDescription);
//
// $firstActivity = Activity::first();
//
// $this->assertEquals($user->id, $firstActivity->causer->id);
// $this->assertInstanceOf(User::class, $firstActivity->causer);
// }
//
// /** @test */
// public function it_can_log_activity_with_properties()
// {
// $properties = [
// 'property' => [
// 'subProperty' => 'value',
// ],
// ];
//
// activity()
// ->withProperties($properties)
// ->log($this->activityDescription);
//
// $firstActivity = Activity::first();
//
// $this->assertInstanceOf(Collection::class, $firstActivity->properties);
// $this->assertEquals('value', $firstActivity->getExtraProperty('property.subProperty'));
// }
//
// /** @test */
// public function it_can_log_activity_with_a_single_properties()
// {
// activity()
// ->withProperty('key', 'value')
// ->log($this->activityDescription);
//
// $firstActivity = Activity::first();
//
// $this->assertInstanceOf(Collection::class, $firstActivity->properties);
// $this->assertEquals('value', $firstActivity->getExtraProperty('key'));
// }
//
// /** @test */
// public function it_can_translate_a_given_causer_id_to_an_object()
// {
// $userId = User::first()->id;
//
// activity()
// ->causedBy($userId)
// ->log($this->activityDescription);
//
// $firstActivity = Activity::first();
//
// $this->assertInstanceOf(User::class, $firstActivity->causer);
// $this->assertEquals($userId, $firstActivity->causer->id);
// }
//
// /**
// * @test
// *
// * @requires !Travis
// */
// public function it_will_throw_an_exception_if_it_cannot_translate_a_causer_id()
// {
// $this->setExpectedException(CouldNotLogActivity::class);
//
// activity()->causedBy(999);
// }
//
// /**
// * @test
// *
// * @requires !Travis
// */
// public function it_will_use_the_logged_in_user_as_the_causer_by_default()
// {
// $userId = 1;
//
// Auth::login(User::find($userId));
//
// activity()->log('hello poetsvrouwman');
//
// $this->assertInstanceOf(User::class, $this->getLastActivity()->causer);
// $this->assertEquals($userId, $this->getLastActivity()->causer->id);
// }
//
// /** @test */
// public function it_can_replace_the_placeholders()
// {
// $article = Article::create(['name' => 'article name']);
//
// $user = Article::create(['name' => 'user name']);
//
// activity()
// ->performedOn($article)
// ->causedBy($user)
// ->withProperties(['key' => 'value', 'key2' => ['subkey' => 'subvalue']])
// ->log('Subject name is :subject.name, causer name is :causer.name and property key is :properties.key and sub key :properties.key2.subkey');
//
// $expectedDescription = 'Subject name is article name, causer name is user name and property key is value and sub key subvalue';
//
// $this->assertEquals($expectedDescription, $this->getLastActivity()->description);
// }
/** @test */
public function it_will_save_all_fields()
{
$user = User::first();
Auth::login($user);
$result = Activity::log([
'contentType' => 'NewsItem',
'contentId' => 1,
'description' => 'Something',
'details' => 'DETAILS',
'data' => ['iets' => (object) ['prop' => 2, 'erty' => 3]]
]);

$result->setHidden(['id', 'created_at', 'updated_at']);
$this->assertEquals([
"content_type" => "NewsItem",
"content_id" => 1,
"description" => "Something",
"details" => "DETAILS",
"data" => '{"iets":{"prop":2,"erty":3}}',
"user_id" => 1,
"ip_address" => "127.0.0.1",
"user_agent" => "No User Agent",
], $result->toArray());
}
}
25 changes: 12 additions & 13 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ protected function setUpDatabase()
{
$this->resetDatabase();
$this->createActivityLogTable();
$this->createTables('users');
$this->seedModels(User::class);
$this->createTableUsers();
$this->seedUsers();
}

protected function resetDatabase()
Expand All @@ -65,30 +65,29 @@ protected function createActivityLogTable()
(new \CreateActivityLogTable())->up();
}

public function getTempDirectory(): string
public function getTempDirectory()
{
return __DIR__ . '/temp';
}

protected function createTables(...$tableNames)
protected function createTableUsers()
{
collect($tableNames)->each(function (string $tableName) {
$this->app['db']->connection()->getSchemaBuilder()->create($tableName, function (Blueprint $table) use ($tableName) {
$this->app['db']
->connection()
->getSchemaBuilder()
->create("users", function (Blueprint $table) {
$table->increments('id');
$table->string('name')->nullable();
$table->string('text')->nullable();
$table->timestamps();
$table->softDeletes();
});
});
}

protected function seedModels(...$modelClasses)
protected function seedUsers()
{
collect($modelClasses)->each(function (string $modelClass) {
foreach(range(1, 0) as $index) {
$modelClass::create(['name' => "name {$index}"]);
}
});
foreach(range(1, 0) as $index) {
User::create(['name' => "name {$index}"]);
}
}
}

0 comments on commit a2738f6

Please sign in to comment.