Skip to content

Commit 1053920

Browse files
committed
Add testing scaffold
1 parent 29a331a commit 1053920

File tree

8 files changed

+248
-4
lines changed

8 files changed

+248
-4
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

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/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
}
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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php namespace Tests\Support;
2+
3+
use CodeIgniter\Test\CIDatabaseTestCase;
4+
5+
class DatabaseTestCase extends CIDatabaseTestCase
6+
{
7+
/**
8+
* Should the database be refreshed before each test?
9+
*
10+
* @var boolean
11+
*/
12+
protected $refresh = true;
13+
14+
/**
15+
* Our configuration
16+
*
17+
* @var Tatter\Audits\Config\Audits
18+
*/
19+
protected $config;
20+
21+
public function setUp(): void
22+
{
23+
parent::setUp();
24+
25+
$config = new \Tatter\Audits\Config\Audits();
26+
$config->silent = false;
27+
$this->config = $config;
28+
}
29+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php namespace Tests\Support\Models;
2+
3+
use CodeIgniter\Model;
4+
use Faker\Generator;
5+
6+
class WidgetModel extends Model
7+
{
8+
use \Tatter\Audits\Traits\AuditsTrait;
9+
10+
protected $table = 'widgets';
11+
protected $primaryKey = 'id';
12+
protected $returnType = 'object';
13+
14+
protected $useTimestamps = true;
15+
protected $useSoftDeletes = true;
16+
protected $skipValidation = true;
17+
18+
protected $allowedFields = ['name', 'uid', 'summary'];
19+
20+
protected $afterInsert = ['auditInsert'];
21+
protected $afterUpdate = ['auditUpdate'];
22+
protected $afterDelete = ['auditDelete'];
23+
24+
/**
25+
* Faked data for Fabricator.
26+
*
27+
* @param Generator $faker
28+
*
29+
* @return stdClass
30+
*/
31+
public function fake(Generator &$faker): stdClass
32+
{
33+
return new stdClass([
34+
'name' => $faker->catchPhrase,
35+
'uid' => $faker->word,
36+
'summary' => $faker->sentence,
37+
]);
38+
}
39+
40+
/**
41+
* Toggle an event callback
42+
*
43+
* @param string $event
44+
* @param bool $active
45+
*
46+
* @return self
47+
*/
48+
public function useEvent(string $event, bool $active = true): self
49+
{
50+
$target = 'after' . $event;
51+
$this->$target = $active ? ['audit' . $event] : [];
52+
return $this;
53+
}
54+
}

0 commit comments

Comments
 (0)