Skip to content

Commit

Permalink
Merge pull request #20 from tattersoftware/tests
Browse files Browse the repository at this point in the history
More Tests
  • Loading branch information
MGatner authored Jul 10, 2022
2 parents b0eb92e + de5bbd1 commit 8d1f111
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
48 changes: 48 additions & 0 deletions tests/FilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Tatter\Visits\Filters\VisitsFilter;
use Tests\Support\TestCase;
use Tests\Support\Transformers\BananaTransformer;
use Tests\Support\Transformers\DecepticonTransformer;

/**
* @internal
Expand All @@ -26,6 +27,17 @@ public function testRecords(): void
$this->seeInDatabase('visits', ['path' => '/index.php']);
}

public function testIncrements(): void
{
config('Visits')->trackingMethod = 'user_id';
service('auth')->login(42);

$this->call();
$this->call();

$this->seeInDatabase('visits', ['views' => 2]);
}

public function testBeforeRecords(): void
{
$this->call('before');
Expand Down Expand Up @@ -124,4 +136,40 @@ public function testAppliesTransformers(): void
'query' => 'banana',
]);
}

public function testShortCircuitsTransformers(): void
{
config('Visits')->transformers = [
DecepticonTransformer::class,
BananaTransformer::class,
];

$this->call();

$this->seeNumRecords(0, 'visits', []);
}

public function testRequiresIncomingRequest(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage(VisitsFilter::class . ' requires an IncomingRequest object.');

$this->request = service('clirequest');

$this->call();
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testRequiresValidVisit(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Failed to create visit record: The host field is required.');

config('App')->baseURL = '0';

$this->call();
}
}
10 changes: 10 additions & 0 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ public function testMakeFromRequest(): void
}
}

public function testMakeFromRequestUsesUserId(): void
{
service('auth')->login(42);

$result = $this->model->makeFromRequest(service('request'));

$this->assertInstanceOf(Visit::class, $result);
$this->assertSame(42, $result->user_id);
}

public function testMakeFromRequestRespectsBaseUrl(): void
{
config('App')->baseURL = 'http://foo.bar/folder/';
Expand Down
20 changes: 20 additions & 0 deletions tests/_support/Transformers/DecepticonTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Tests\Support\Transformers;

use CodeIgniter\HTTP\IncomingRequest;
use Tatter\Visits\Entities\Visit;
use Tatter\Visits\Interfaces\Transformer;

abstract class DecepticonTransformer implements Transformer
{
/**
* Stops other Transformers.
*
* @return null
*/
public static function transform(Visit $visit, IncomingRequest $request): ?Visit
{
return null;
}
}

0 comments on commit 8d1f111

Please sign in to comment.