Skip to content

Commit 1b7cfbc

Browse files
committed
Complete first test
1 parent 1053920 commit 1b7cfbc

File tree

5 files changed

+115
-4
lines changed

5 files changed

+115
-4
lines changed

composer.json

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,29 @@
1818
"role": "Developer"
1919
}
2020
],
21+
"repositories": [
22+
{
23+
"type": "vcs",
24+
"url": "https://github.com/codeigniter4/CodeIgniter4"
25+
}
26+
],
27+
"minimum-stability": "dev",
28+
"prefer-stable": true,
2129
"require": {
2230
"php" : ">=7.2"
2331
},
2432
"require-dev": {
25-
"codeigniter4/framework": "dev-master"
33+
"phpunit/phpunit": "^8.5",
34+
"fzaninotto/faker": "^1.9@dev",
35+
"codeigniter4/codeigniter4": "dev-develop"
2636
},
2737
"autoload": {
2838
"psr-4": {
2939
"Tatter\\Audits\\": "src"
3040
}
3141
},
3242
"scripts": {
43+
"test": "phpunit",
3344
"post-update-cmd": [
3445
"composer dump-autoload"
3546
]

src/Audits.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ public function sessionUserId(): int
4545
return session($this->config->sessionUserId) ?? 0;
4646
}
4747

48+
/**
49+
* Return the current queue (mostly for testing)
50+
*
51+
* @return array
52+
*/
53+
public function getQueue(): array
54+
{
55+
return $this->queue;
56+
}
57+
4858
/**
4959
* Add an audit row to the queue
5060
*

tests/_support/DatabaseTestCase.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
<?php namespace Tests\Support;
22

33
use CodeIgniter\Test\CIDatabaseTestCase;
4+
use CodeIgniter\Test\Fabricator;
5+
use Config\Services;
6+
use Tatter\Audits\Audits;
7+
use Tests\Support\Models\WidgetModel;
48

59
class DatabaseTestCase extends CIDatabaseTestCase
610
{
@@ -18,12 +22,65 @@ class DatabaseTestCase extends CIDatabaseTestCase
1822
*/
1923
protected $config;
2024

25+
/**
26+
* Instance of the test model
27+
*
28+
* @var Tests\Support\Models\WidgetModel
29+
*/
30+
protected $model;
31+
32+
/**
33+
* Instance of the fabricator primed with our model
34+
*
35+
* @var CodeIgniter\Test\Fabricator
36+
*/
37+
protected $fabricator;
38+
2139
public function setUp(): void
2240
{
2341
parent::setUp();
2442

2543
$config = new \Tatter\Audits\Config\Audits();
2644
$config->silent = false;
2745
$this->config = $config;
46+
47+
// Reset the service
48+
$audits = new Audits($config);
49+
Services::injectMock('audits', $audits);
50+
51+
// Prep model components
52+
$this->model = new WidgetModel();
53+
$this->fabricator = new Fabricator($this->model);
54+
}
55+
56+
/**
57+
* Asserts that an audit with the given properties is in the queue
58+
*
59+
* @param array $values Array of values to confirm
60+
*
61+
* @return self
62+
*/
63+
public function seeAudit(array $values)
64+
{
65+
$queue = service('audits')->getQueue();
66+
$found = false;
67+
68+
// Check each audit in the queue for a match
69+
foreach ($queue as $audit)
70+
{
71+
// Check each value against the audit
72+
foreach ($values as $key => $value)
73+
{
74+
if ($audit[$key] != $value)
75+
{
76+
break 2;
77+
}
78+
}
79+
80+
$found = true;
81+
break;
82+
}
83+
84+
$this->assertTrue($found, 'Audit not found in queue: ' . print_r($values, true));
2885
}
2986
}

tests/_support/Models/WidgetModel.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ class WidgetModel extends Model
2828
*
2929
* @return stdClass
3030
*/
31-
public function fake(Generator &$faker): stdClass
31+
public function fake(Generator &$faker): object
3232
{
33-
return new stdClass([
33+
return (object) [
3434
'name' => $faker->catchPhrase,
3535
'uid' => $faker->word,
3636
'summary' => $faker->sentence,
37-
]);
37+
];
3838
}
3939

4040
/**

tests/unit/TraitTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Tests\Support\DatabaseTestCase;
4+
5+
class TraitTest extends DatabaseTestCase
6+
{
7+
public function setUp(): void
8+
{
9+
parent::setUp();
10+
11+
}
12+
13+
public function testInsertAddsAudit()
14+
{
15+
$widget = $this->fabricator->make();
16+
17+
$result = $this->model->insert($widget);
18+
$this->assertIsInt($result);
19+
20+
$expected = [
21+
'source' => 'widgets',
22+
'source_id' => $result,
23+
'event' => 'insert',
24+
'summary' => '5 fields',
25+
'user_id' => -1,
26+
];
27+
28+
$queue = service('audits')->getQueue();
29+
30+
$this->assertCount(1, $queue);
31+
$this->seeAudit($expected);
32+
}
33+
}

0 commit comments

Comments
 (0)