Skip to content

Commit 8d1f111

Browse files
authored
Merge pull request #20 from tattersoftware/tests
More Tests
2 parents b0eb92e + de5bbd1 commit 8d1f111

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

tests/FilterTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Tatter\Visits\Filters\VisitsFilter;
66
use Tests\Support\TestCase;
77
use Tests\Support\Transformers\BananaTransformer;
8+
use Tests\Support\Transformers\DecepticonTransformer;
89

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

30+
public function testIncrements(): void
31+
{
32+
config('Visits')->trackingMethod = 'user_id';
33+
service('auth')->login(42);
34+
35+
$this->call();
36+
$this->call();
37+
38+
$this->seeInDatabase('visits', ['views' => 2]);
39+
}
40+
2941
public function testBeforeRecords(): void
3042
{
3143
$this->call('before');
@@ -124,4 +136,40 @@ public function testAppliesTransformers(): void
124136
'query' => 'banana',
125137
]);
126138
}
139+
140+
public function testShortCircuitsTransformers(): void
141+
{
142+
config('Visits')->transformers = [
143+
DecepticonTransformer::class,
144+
BananaTransformer::class,
145+
];
146+
147+
$this->call();
148+
149+
$this->seeNumRecords(0, 'visits', []);
150+
}
151+
152+
public function testRequiresIncomingRequest(): void
153+
{
154+
$this->expectException(RuntimeException::class);
155+
$this->expectExceptionMessage(VisitsFilter::class . ' requires an IncomingRequest object.');
156+
157+
$this->request = service('clirequest');
158+
159+
$this->call();
160+
}
161+
162+
/**
163+
* @runInSeparateProcess
164+
* @preserveGlobalState disabled
165+
*/
166+
public function testRequiresValidVisit(): void
167+
{
168+
$this->expectException(RuntimeException::class);
169+
$this->expectExceptionMessage('Failed to create visit record: The host field is required.');
170+
171+
config('App')->baseURL = '0';
172+
173+
$this->call();
174+
}
127175
}

tests/ModelTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ public function testMakeFromRequest(): void
8383
}
8484
}
8585

86+
public function testMakeFromRequestUsesUserId(): void
87+
{
88+
service('auth')->login(42);
89+
90+
$result = $this->model->makeFromRequest(service('request'));
91+
92+
$this->assertInstanceOf(Visit::class, $result);
93+
$this->assertSame(42, $result->user_id);
94+
}
95+
8696
public function testMakeFromRequestRespectsBaseUrl(): void
8797
{
8898
config('App')->baseURL = 'http://foo.bar/folder/';
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Tests\Support\Transformers;
4+
5+
use CodeIgniter\HTTP\IncomingRequest;
6+
use Tatter\Visits\Entities\Visit;
7+
use Tatter\Visits\Interfaces\Transformer;
8+
9+
abstract class DecepticonTransformer implements Transformer
10+
{
11+
/**
12+
* Stops other Transformers.
13+
*
14+
* @return null
15+
*/
16+
public static function transform(Visit $visit, IncomingRequest $request): ?Visit
17+
{
18+
return null;
19+
}
20+
}

0 commit comments

Comments
 (0)