Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johannesschobel committed Jan 28, 2018
1 parent 9bfec36 commit 31eda95
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 3 deletions.
9 changes: 6 additions & 3 deletions ActivityLog/Tasks/CreateActivityLogEntryTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Containers\User\Models\User;
use App\Ship\Parents\Tasks\Task;
use Spatie\Activitylog\Models\Activity;

/**
* Class CreateActivityLogEntryTask
Expand All @@ -23,20 +24,22 @@ class CreateActivityLogEntryTask extends Task
* @param array $options additional key/value information
* @param string|null $log the log file to add this entry
*
* @return void
* @return Activity
*/
public function run(User $causer, $model, $message = '', $options = [], $log = null)
public function run(User $causer, $model, $message = '', $options = [], $log = null) : Activity
{
// get the default log name
if ($log === null) {
$log = config('activitylog.default_log_name', 'default');
}

// create the log entry
activity($log)
$action = activity($log)
->causedBy($causer)
->performedOn($model)
->withProperties($options)
->log($message);

return $action;
}
}
15 changes: 15 additions & 0 deletions ActivityLog/Tests/ApiTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Containers\ActivityLog\Tests;

use App\Containers\ActivityLog\Tests\TestCase as BaseTestCase;

/**
* Class ApiTestCase.
*
* This is the container API TestCase class. Use this class to add your container specific API related helper functions.
*/
class ApiTestCase extends BaseTestCase
{
// ..
}
15 changes: 15 additions & 0 deletions ActivityLog/Tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace App\Containers\ActivityLog\Tests;

use App\Ship\Parents\Tests\PhpUnit\TestCase as ShipTestCase;

/**
* Class TestCase.
*
* This is the container Main TestCase class. Use this class to add your container specific helper functions.
*/
class TestCase extends ShipTestCase
{
// ..
}
63 changes: 63 additions & 0 deletions ActivityLog/Tests/Unit/CreateActivityLogEntryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace App\Containers\ActivityLog\Tests\Unit;

use Apiato\Core\Foundation\Facades\Apiato;
use App\Containers\ActivityLog\Tasks\CreateActivityLogEntryTask;
use App\Containers\ActivityLog\Tests\TestCase;
use App\Containers\User\Models\User;
use Illuminate\Support\Facades\Config;
use Spatie\Activitylog\Models\Activity;

/**
* Class CreateActivityLogEntryTest.
*
* @group activitylog
* @group unit
*/
class CreateActivityLogEntryTest extends TestCase
{

/**
* @test
*/
public function test_if_activity_is_created_and_logged_to_database()
{
// get one user
$causer = $this->getTestingUser();
$subject = $causer;

$description = 'Test Entry Created';
$options = [
'operation' => 'create',
'variables' => [
'test' => true,
],
];
$logname = 'test';

/** @var Activity $activity */
$activity = Apiato::call(CreateActivityLogEntryTask::class, [
$causer,
$subject,
$description,
$options,
$logname,
]);

// check if values are correctly set
$this->assertInstanceOf(Activity::class, $activity);

// check if this entry is in the database
$this->assertDatabaseHas('activity_log',
[
'id' => $activity->id,

'causer_id' => $causer->id,
'causer_type' => get_class($causer),

'description' => $description,
'log_name' => $logname
]);
}
}
66 changes: 66 additions & 0 deletions ActivityLog/UI/API/Tests/Functional/GetMyActivitiesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace App\Containers\ActivityLog\UI\API\Tests\Functional;

use Apiato\Core\Foundation\Facades\Apiato;
use App\Containers\ActivityLog\Tasks\CreateActivityLogEntryTask;
use App\Containers\ActivityLog\Tests\ApiTestCase;

/**
* Class GetMyActivitiesTest.
*
* @group activitylog
* @group api
*/
class GetMyActivitiesTest extends ApiTestCase
{

// the endpoint to be called within this test (e.g., get@v1/users)
protected $endpoint = 'get@v1/my/activities';

// fake some access rights
protected $access = [
'permissions' => '',
'roles' => '',
];

/**
* @test
*/
public function test_with_user_that_has_no_entries()
{
$data = [
];

// send the HTTP request
$response = $this->makeCall($data);

// assert the response status
$response->assertSuccessful();

// test for empty content
$response->assertJson(['data' => []]);
}

/**
* @test
*/
public function test_with_user_that_has_some_entries()
{
$user = $this->getTestingUser();

// create 2 activities
$activity1 = Apiato::call(CreateActivityLogEntryTask::class, [$user, $user, 'test 1', [], 'test']);
$activity2 = Apiato::call(CreateActivityLogEntryTask::class, [$user, $user, 'test 2', [], 'test']);

$data = [
];

$response = $this->makeCall($data);

// assert the response status
$response->assertSuccessful();
$response->assertJsonCount(2, 'data');
}

}

0 comments on commit 31eda95

Please sign in to comment.