Skip to content

Commit 55c0431

Browse files
authored
Merge pull request #3 from tattersoftware/tests
Add test scaffold and first test
2 parents 29a331a + c69dcb7 commit 55c0431

File tree

12 files changed

+361
-6
lines changed

12 files changed

+361
-6
lines changed

.github/workflows/test.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
name: PHPUnit
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- develop
7+
8+
jobs:
9+
main:
10+
name: Build and test
11+
12+
strategy:
13+
matrix:
14+
php-versions: ['7.2', '7.3', '7.4']
15+
16+
runs-on: ubuntu-latest
17+
18+
if: "!contains(github.event.head_commit.message, '[ci skip]')"
19+
20+
steps:
21+
- name: Checkout
22+
uses: actions/checkout@v2
23+
24+
- name: Setup PHP, with composer and extensions
25+
uses: shivammathur/setup-php@v2
26+
with:
27+
php-version: ${{ matrix.php-versions }}
28+
tools: composer, pecl, phpunit
29+
extensions: intl, json, mbstring, mysqlnd, xdebug, xml, sqlite3
30+
coverage: xdebug
31+
32+
- name: Get composer cache directory
33+
id: composer-cache
34+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
35+
36+
- name: Cache composer dependencies
37+
uses: actions/cache@v1
38+
with:
39+
path: ${{ steps.composer-cache.outputs.dir }}
40+
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
41+
restore-keys: ${{ runner.os }}-composer-
42+
43+
- name: Install dependencies
44+
run: composer install --no-progress --no-suggest --no-interaction --prefer-dist --optimize-autoloader
45+
env:
46+
COMPOSER_AUTH: ${{ secrets.COMPOSER_AUTH }}
47+
48+
- name: Test with phpunit
49+
run: vendor/bin/phpunit --coverage-text

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
vendor/
2+
build/
3+
phpunit*.xml
4+
phpunit
5+
*.cache
6+
composer.lock
7+
.DS_Store

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Lightweight object logging for CodeIgniter 4
55

66
1. Install with Composer: `> composer require tatter/audits`
77
2. Update the database: `> php spark migrate -all`
8-
3. Setup your models:
8+
3. Set up your models:
99

1010
```
1111
class JobModel extends Model

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
]

phpunit.xml.dist

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/codeigniter4/codeigniter4/system/Test/bootstrap.php"
3+
backupGlobals="false"
4+
colors="true"
5+
convertErrorsToExceptions="true"
6+
convertNoticesToExceptions="true"
7+
convertWarningsToExceptions="true"
8+
stopOnError="false"
9+
stopOnFailure="false"
10+
stopOnIncomplete="false"
11+
stopOnSkipped="false">
12+
<testsuites>
13+
<testsuite name="app">
14+
<directory>./tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
18+
<filter>
19+
<whitelist addUncoveredFilesFromWhitelist="true" processUncoveredFilesFromWhitelist="true">
20+
<directory suffix=".php">./src</directory>
21+
<exclude>
22+
<directory suffix=".php">./src/Views</directory>
23+
<file>./src/Config/Routes.php</file>
24+
</exclude>
25+
</whitelist>
26+
</filter>
27+
28+
<logging>
29+
<log type="coverage-html" target="build/logs/html"/>
30+
<log type="coverage-clover" target="build/logs/clover.xml"/>
31+
<log type="coverage-php" target="build/logs/coverage.serialized"/>
32+
<log type="coverage-text" target="php://stdout" showUncoveredFiles="false"/>
33+
<log type="testdox-html" target="build/logs/testdox.html"/>
34+
<log type="testdox-text" target="build/logs/testdox.txt"/>
35+
<log type="junit" target="build/logs/logfile.xml"/>
36+
</logging>
37+
38+
<php>
39+
<server name="app.baseURL" value="http://example.com"/>
40+
41+
<!-- Directory containing phpunit.xml -->
42+
<const name="HOMEPATH" value="./"/>
43+
44+
<!-- Directory containing the Paths config file -->
45+
<const name="CONFIGPATH" value="./vendor/codeigniter4/codeigniter4/app/Config/"/>
46+
47+
<!-- Directory containing the front controller (index.php) -->
48+
<const name="PUBLICPATH" value="./vendor/codeigniter4/codeigniter4/public/"/>
49+
50+
<!-- https://getcomposer.org/xdebug -->
51+
<env name="COMPOSER_DISABLE_XDEBUG_WARN" value="1"/>
52+
53+
<!-- Database configuration -->
54+
<!-- <env name="database.tests.hostname" value="localhost"/> -->
55+
<!-- <env name="database.tests.database" value="tests"/> -->
56+
<!-- <env name="database.tests.username" value="tests_user"/> -->
57+
<!-- <env name="database.tests.password" value=""/> -->
58+
<!-- <env name="database.tests.DBDriver" value="MySQLi"/> -->
59+
<!-- <env name="database.tests.DBPrefix" value="tests_"/> -->
60+
<env name="database.tests.database" value=":memory:"/>
61+
<env name="database.tests.DBDriver" value="SQLite3"/>
62+
</php>
63+
</phpunit>

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
*

src/Config/Services.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php namespace Tatter\Audits\Config;
22

3-
use CodeIgniter\Config\BaseService;
3+
use Tatter\Audits\Audits;
44

5-
class Services extends BaseService
5+
class Services extends \Config\Services
66
{
77
public static function audits(BaseConfig $config = null, bool $getShared = true)
88
{
@@ -17,6 +17,6 @@ public static function audits(BaseConfig $config = null, bool $getShared = true)
1717
$config = config('Audits');
1818
}
1919

20-
return new \Tatter\Audits\Audits($config);
20+
return new Audits($config);
2121
}
2222
}

src/Traits/AuditsTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ protected function auditInsert(array $data)
4747

4848
$audit = [
4949
'source' => $this->table,
50-
'source_id' => $data['result']->connID->insert_id,
50+
'source_id' => $this->db->insertID(),
5151
'event' => 'insert',
5252
'summary' => count($data['data']) . ' fields',
5353
];
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php namespace Tests\Support\Database\Migrations;
2+
3+
use CodeIgniter\Database\Migration;
4+
5+
class CreateTestTables extends Migration
6+
{
7+
public function up()
8+
{
9+
$this->db->disableForeignKeyChecks();
10+
11+
// Widgets
12+
$fields = [
13+
'name' => ['type' => 'varchar', 'constraint' => 31],
14+
'uid' => ['type' => 'varchar', 'constraint' => 31],
15+
'summary' => ['type' => 'varchar', 'constraint' => 255],
16+
'created_at' => ['type' => 'datetime', 'null' => true],
17+
'updated_at' => ['type' => 'datetime', 'null' => true],
18+
'deleted_at' => ['type' => 'datetime', 'null' => true],
19+
];
20+
21+
$this->forge->addField('id');
22+
$this->forge->addField($fields);
23+
24+
$this->forge->addKey('name');
25+
$this->forge->addKey('uid');
26+
$this->forge->addKey(['deleted_at', 'id']);
27+
$this->forge->addKey('created_at');
28+
29+
$this->forge->createTable('widgets');
30+
31+
$this->db->enableForeignKeyChecks();
32+
}
33+
34+
public function down()
35+
{
36+
$this->db->disableForeignKeyChecks();
37+
38+
$this->forge->dropTable('widgets');
39+
40+
$this->db->enableForeignKeyChecks();
41+
}
42+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php namespace Tests\Support;
2+
3+
use CodeIgniter\Test\CIDatabaseTestCase;
4+
use CodeIgniter\Test\Fabricator;
5+
use Config\Services;
6+
use Tatter\Audits\Audits;
7+
use Tests\Support\Models\WidgetModel;
8+
9+
class DatabaseTestCase extends CIDatabaseTestCase
10+
{
11+
/**
12+
* Should the database be refreshed before each test?
13+
*
14+
* @var boolean
15+
*/
16+
protected $refresh = true;
17+
18+
/**
19+
* Our configuration
20+
*
21+
* @var Tatter\Audits\Config\Audits
22+
*/
23+
protected $config;
24+
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+
39+
public function setUp(): void
40+
{
41+
parent::setUp();
42+
43+
$config = new \Tatter\Audits\Config\Audits();
44+
$config->silent = false;
45+
$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));
85+
}
86+
}

0 commit comments

Comments
 (0)