Skip to content
This repository was archived by the owner on May 18, 2024. It is now read-only.

Commit 47ebca2

Browse files
committed
Initial upload of scaffold
1 parent 3d7a087 commit 47ebca2

File tree

8 files changed

+266
-0
lines changed

8 files changed

+266
-0
lines changed

src/phpunit.xml.dist

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="tests/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="all">
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+
</exclude>
23+
</whitelist>
24+
</filter>
25+
26+
<logging>
27+
<log type="coverage-clover" target="build/logs/clover.xml"/>
28+
<log type="coverage-html" target="build/logs/html"/>
29+
<log type="coverage-text" target="php://stdout" showUncoveredFiles="true"/>
30+
</logging>
31+
32+
<php>
33+
<env name="app.baseURL" value="http://example.com"/>
34+
<env name="CI_ENVIRONMENT" value="testing"/>
35+
<!-- <env name="database.tests.DBDriver" value="MySQLi"/>-->
36+
<!-- <env name="database.tests.database" value="mythauth"/>-->
37+
<!-- <env name="database.tests.username" value="root"/>-->
38+
<env name="database.tests.DBDriver" value="SQLite3"/>
39+
<env name="database.tests.database" value=":memory:"/>
40+
<!-- <env name="database.tests.DBPrefix" value="handlers_"/>-->
41+
</php>
42+
</phpunit>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php namespace CIModuleTests\Support\Database\Migrations;
2+
3+
use CodeIgniter\Database\Migration;
4+
5+
class CreateTestTables extends Migration
6+
{
7+
public function up()
8+
{
9+
$fields = [
10+
'name' => ['type' => 'varchar', 'constraint' => 31],
11+
'uid' => ['type' => 'varchar', 'constraint' => 31],
12+
'class' => ['type' => 'varchar', 'constraint' => 63],
13+
'icon' => ['type' => 'varchar', 'constraint' => 31],
14+
'summary' => ['type' => 'varchar', 'constraint' => 255],
15+
'created_at' => ['type' => 'datetime', 'null' => true],
16+
'updated_at' => ['type' => 'datetime', 'null' => true],
17+
'deleted_at' => ['type' => 'datetime', 'null' => true],
18+
];
19+
20+
$this->forge->addField('id');
21+
$this->forge->addField($fields);
22+
23+
$this->forge->addKey('name');
24+
$this->forge->addKey('uid');
25+
$this->forge->addKey(['deleted_at', 'id']);
26+
$this->forge->addKey('created_at');
27+
28+
$this->forge->createTable('factories');
29+
}
30+
31+
public function down()
32+
{
33+
$this->forge->dropTable('factories');
34+
}
35+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php namespace CIModuleTests\Support\Database\Seeds;
2+
3+
class ExampleSeeder extends \CodeIgniter\Database\Seeder
4+
{
5+
public function run()
6+
{
7+
$factories = [
8+
[
9+
'name' => 'Test Factory',
10+
'uid' => 'test001',
11+
'class' => 'Factories\Tests\NewFactory',
12+
'icon' => 'fas fa-puzzle-piece',
13+
'summary' => 'Longer sample text for testing',
14+
],
15+
[
16+
'name' => 'Widget Factory',
17+
'uid' => 'widget',
18+
'class' => 'Factories\Tests\WidgetPlant',
19+
'icon' => 'fas fa-puzzle-piece',
20+
'summary' => 'Create widgets in your factory',
21+
],
22+
[
23+
'name' => 'Evil Factory',
24+
'uid' => 'evil-maker',
25+
'class' => 'Factories\Evil\MyFactory',
26+
'icon' => 'fas fa-book-dead',
27+
'summary' => 'Abandon all hope, ye who enter here',
28+
]
29+
];
30+
31+
$builder = $this->db->table('factories');
32+
33+
foreach ($factories as $factory)
34+
{
35+
$builder->insert($factory);
36+
}
37+
}
38+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php namespace CIModuleTests\Support;
2+
3+
class DatabaseTestCase extends \CodeIgniter\Test\CIDatabaseTestCase
4+
{
5+
/**
6+
* Should the database be refreshed before each test?
7+
*
8+
* @var boolean
9+
*/
10+
protected $refresh = true;
11+
12+
/**
13+
* The name of a seed file used for all tests within this test case.
14+
*
15+
* @var string
16+
*/
17+
protected $seed = 'ExampleSeeder';
18+
19+
/**
20+
* The path to where we can find the test Seeds directory.
21+
*
22+
* @var string
23+
*/
24+
protected $basePath = SUPPORTPATH . 'Database/';
25+
26+
/**
27+
* The namespace to help us find the migration classes.
28+
*
29+
* @var string
30+
*/
31+
protected $namespace = 'CIModuleTests\Support';
32+
33+
/**
34+
* @var SessionHandler
35+
*/
36+
protected $session;
37+
38+
public function setUp(): void
39+
{
40+
parent::setUp();
41+
42+
}
43+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php namespace CIModuleTests\Support\Models;
2+
3+
use CodeIgniter\Model;
4+
5+
class ExampleModel extends Model
6+
{
7+
protected $table = 'factories';
8+
protected $primaryKey = 'id';
9+
10+
protected $returnType = 'object';
11+
protected $useSoftDeletes = false;
12+
13+
protected $allowedFields = ['name', 'uid', 'class', 'icon', 'summary'];
14+
15+
protected $useTimestamps = true;
16+
17+
protected $validationRules = [];
18+
protected $validationMessages = [];
19+
protected $skipValidation = false;
20+
}

src/tests/bootstrap.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
ini_set('error_reporting', E_ALL);
4+
5+
ini_set('display_errors', '1');
6+
ini_set('display_startup_errors', '1');
7+
8+
// Make sure it recognizes that we're testing.
9+
$_SERVER['CI_ENVIRONMENT'] = 'testing';
10+
define('ENVIRONMENT', 'testing');
11+
12+
// Load our paths config file
13+
require __DIR__ . '/../vendor/codeigniter4/codeigniter4/app/Config/Paths.php';
14+
$paths = new Config\Paths();
15+
16+
// Define necessary framework path constants
17+
define('APPPATH', realpath($paths->appDirectory) . DIRECTORY_SEPARATOR);
18+
define('ROOTPATH', realpath(APPPATH . '../') . DIRECTORY_SEPARATOR);
19+
define('FCPATH', realpath(ROOTPATH . 'public') . DIRECTORY_SEPARATOR);
20+
define('SYSTEMPATH', realpath($paths->systemDirectory) . DIRECTORY_SEPARATOR);
21+
define('WRITEPATH', realpath($paths->writableDirectory) . DIRECTORY_SEPARATOR);
22+
23+
// Define necessary module test path constants
24+
define('MODULEPATH', realpath(__DIR__ . '/../') . DIRECTORY_SEPARATOR);
25+
define('TESTPATH', realpath(__DIR__) . DIRECTORY_SEPARATOR);
26+
define('SUPPORTPATH', realpath(TESTPATH . '_support/') . DIRECTORY_SEPARATOR);
27+
define('COMPOSER_PATH', MODULEPATH . 'vendor/autoload.php');
28+
29+
// Set environment values that would otherwise stop the framework from functioning during tests.
30+
if (! isset($_SERVER['app.baseURL']))
31+
{
32+
$_SERVER['app.baseURL'] = 'http://example.com';
33+
}
34+
35+
// Load necessary modules
36+
require_once APPPATH . 'Config/Autoload.php';
37+
require_once APPPATH . 'Config/Constants.php';
38+
require_once APPPATH . 'Config/Modules.php';
39+
40+
require_once SYSTEMPATH . 'Autoloader/Autoloader.php';
41+
require_once SYSTEMPATH . 'Config/BaseService.php';
42+
require_once APPPATH . 'Config/Services.php';
43+
44+
// Use Config\Services as CodeIgniter\Services
45+
if (! class_exists('CodeIgniter\Services', false))
46+
{
47+
class_alias('Config\Services', 'CodeIgniter\Services');
48+
}
49+
50+
// Launch the autoloader to gather namespaces (includes composer.json's "autoload-dev")
51+
$loader = \CodeIgniter\Services::autoloader();
52+
$loader->initialize(new Config\Autoload(), new Config\Modules());
53+
$loader->register(); // Register the loader with the SPL autoloader stack.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
class ExampleDatabaseTest extends CIModuleTests\Support\DatabaseTestCase
4+
{
5+
public function setUp(): void
6+
{
7+
parent::setUp();
8+
9+
}
10+
11+
public function simpleDatabaseTest()
12+
{
13+
$model = new \CIModuleTests\Support\Models\ExampleModel();
14+
15+
$objects = $model->findAll();
16+
17+
$this->assertCount(3, $objects);
18+
}
19+
}

src/tests/unit/ExampleTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
class ExampleTest extends \CodeIgniter\Test\CIDatabaseTestCase
4+
{
5+
public function setUp(): void
6+
{
7+
parent::setUp();
8+
9+
}
10+
11+
public function simpleTest()
12+
{
13+
$test = defined('APPPATH');
14+
$this->assertTrue($test);
15+
}
16+
}

0 commit comments

Comments
 (0)