Skip to content

Commit 1a2fe3d

Browse files
committed
Add example tests to module scaffold
1 parent dd52756 commit 1a2fe3d

File tree

9 files changed

+340
-0
lines changed

9 files changed

+340
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php namespace Tests\Support\Database\Migrations;
2+
3+
use CodeIgniter\Database\Migration;
4+
5+
class ExampleMigration extends Migration
6+
{
7+
protected $DBGroup = 'tests';
8+
9+
public function up()
10+
{
11+
$fields = [
12+
'name' => [
13+
'type' => 'varchar',
14+
'constraint' => 31,
15+
],
16+
'uid' => [
17+
'type' => 'varchar',
18+
'constraint' => 31,
19+
],
20+
'class' => [
21+
'type' => 'varchar',
22+
'constraint' => 63,
23+
],
24+
'icon' => [
25+
'type' => 'varchar',
26+
'constraint' => 31,
27+
],
28+
'summary' => [
29+
'type' => 'varchar',
30+
'constraint' => 255,
31+
],
32+
'created_at' => [
33+
'type' => 'datetime',
34+
'null' => true,
35+
],
36+
'updated_at' => [
37+
'type' => 'datetime',
38+
'null' => true,
39+
],
40+
'deleted_at' => [
41+
'type' => 'datetime',
42+
'null' => true,
43+
],
44+
];
45+
46+
$this->forge->addField('id');
47+
$this->forge->addField($fields);
48+
49+
$this->forge->addKey('name');
50+
$this->forge->addKey('uid');
51+
$this->forge->addKey(['deleted_at', 'id']);
52+
$this->forge->addKey('created_at');
53+
54+
$this->forge->createTable('factories');
55+
}
56+
57+
public function down()
58+
{
59+
$this->forge->dropTable('factories');
60+
}
61+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php namespace Tests\Support\Database\Seeds;
2+
3+
use CodeIgniter\Database\Seeder;
4+
5+
class ExampleSeeder extends Seeder
6+
{
7+
public function run()
8+
{
9+
$factories = [
10+
[
11+
'name' => 'Test Factory',
12+
'uid' => 'test001',
13+
'class' => 'Factories\Tests\NewFactory',
14+
'icon' => 'fas fa-puzzle-piece',
15+
'summary' => 'Longer sample text for testing',
16+
],
17+
[
18+
'name' => 'Widget Factory',
19+
'uid' => 'widget',
20+
'class' => 'Factories\Tests\WidgetPlant',
21+
'icon' => 'fas fa-puzzle-piece',
22+
'summary' => 'Create widgets in your factory',
23+
],
24+
[
25+
'name' => 'Evil Factory',
26+
'uid' => 'evil-maker',
27+
'class' => 'Factories\Evil\MyFactory',
28+
'icon' => 'fas fa-book-dead',
29+
'summary' => 'Abandon all hope, ye who enter here',
30+
],
31+
];
32+
33+
$builder = $this->db->table('factories');
34+
35+
foreach ($factories as $factory)
36+
{
37+
$builder->insert($factory);
38+
}
39+
}
40+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php namespace Tests\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 seed file(s) used for all tests within this test case.
14+
* Should be fully-namespaced or relative to $basePath
15+
*
16+
* @var string|array
17+
*/
18+
protected $seed = 'Tests\Support\Database\Seeds\ExampleSeeder';
19+
20+
/**
21+
* The path to the seeds directory.
22+
* Allows overriding the default application directories.
23+
*
24+
* @var string
25+
*/
26+
protected $basePath = SUPPORTPATH . 'Database/';
27+
28+
/**
29+
* The namespace(s) to help us find the migration classes.
30+
* Empty is equivalent to running `spark migrate -all`.
31+
* Note that running "all" runs migrations in date order,
32+
* but specifying namespaces runs them in namespace order (then date)
33+
*
34+
* @var string|array|null
35+
*/
36+
protected $namespace = 'Tests\Support';
37+
38+
public function setUp(): void
39+
{
40+
parent::setUp();
41+
42+
// Extra code to run before each test
43+
}
44+
45+
public function tearDown(): void
46+
{
47+
parent::tearDown();
48+
49+
// Extra code to run after each test
50+
}
51+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
4+
/**
5+
* CodeIgniter
6+
*
7+
* An open source application development framework for PHP
8+
*
9+
* This content is released under the MIT License (MIT)
10+
*
11+
* Copyright (c) 2014-2019 British Columbia Institute of Technology
12+
* Copyright (c) 2019-2020 CodeIgniter Foundation
13+
*
14+
* Permission is hereby granted, free of charge, to any person obtaining a copy
15+
* of this software and associated documentation files (the "Software"), to deal
16+
* in the Software without restriction, including without limitation the rights
17+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18+
* copies of the Software, and to permit persons to whom the Software is
19+
* furnished to do so, subject to the following conditions:
20+
*
21+
* The above copyright notice and this permission notice shall be included in
22+
* all copies or substantial portions of the Software.
23+
*
24+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30+
* THE SOFTWARE.
31+
*
32+
* @package CodeIgniter
33+
* @author CodeIgniter Dev Team
34+
* @copyright 2019-2020 CodeIgniter Foundation
35+
* @license https://opensource.org/licenses/MIT MIT License
36+
* @link https://codeigniter.com
37+
* @since Version 4.0.0
38+
* @filesource
39+
*/
40+
41+
namespace Tests\Support\Libraries;
42+
43+
/**
44+
* Class ConfigReader
45+
*
46+
* An extension of BaseConfig that prevents the constructor from
47+
* loading external values. Used to read actual local values from
48+
* a config file.
49+
*/
50+
class ConfigReader extends \App\Config\App
51+
{
52+
public function __construct()
53+
{
54+
}
55+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php namespace Tests\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 = [
14+
'name',
15+
'uid',
16+
'class',
17+
'icon',
18+
'summary',
19+
];
20+
21+
protected $useTimestamps = true;
22+
23+
protected $validationRules = [];
24+
protected $validationMessages = [];
25+
protected $skipValidation = false;
26+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php namespace Tests\Support;
2+
3+
use CodeIgniter\Session\Handlers\ArrayHandler;
4+
use CodeIgniter\Test\CIUnitTestCase;
5+
use CodeIgniter\Test\Mock\MockSession;
6+
7+
class SessionTestCase extends CIUnitTestCase
8+
{
9+
/**
10+
* @var SessionHandler
11+
*/
12+
protected $session;
13+
14+
public function setUp(): void
15+
{
16+
parent::setUp();
17+
18+
$this->mockSession();
19+
}
20+
21+
/**
22+
* Pre-loads the mock session driver into $this->session.
23+
*
24+
* @var string
25+
*/
26+
protected function mockSession()
27+
{
28+
$config = config('App');
29+
$this->session = new MockSession(new ArrayHandler($config, '0.0.0.0'), $config);
30+
\Config\Services::injectMock('session', $this->session);
31+
}
32+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use Tests\Support\Models\ExampleModel;
4+
5+
class ExampleDatabaseTest extends \Tests\Support\DatabaseTestCase
6+
{
7+
public function setUp(): void
8+
{
9+
parent::setUp();
10+
11+
// Extra code to run before each test
12+
}
13+
14+
public function testModelFindAll()
15+
{
16+
$model = new ExampleModel();
17+
18+
// Get every row created by ExampleSeeder
19+
$objects = $model->findAll();
20+
21+
// Make sure the count is as expected
22+
$this->assertCount(3, $objects);
23+
}
24+
25+
public function testSoftDeleteLeavesRow()
26+
{
27+
$model = new ExampleModel();
28+
$this->setPrivateProperty($model, 'useSoftDeletes', true);
29+
30+
$object = $model->first();
31+
$model->delete($object->id);
32+
33+
// The model should no longer find it
34+
$this->assertNull($model->find($object->id));
35+
36+
// ... but it should still be in the database
37+
$result = $model->builder()->where('id', $object->id)->get()->getResult();
38+
39+
$this->assertCount(1, $result);
40+
}
41+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
class ExampleSessionTest extends \Tests\Support\SessionTestCase
4+
{
5+
public function setUp(): void
6+
{
7+
parent::setUp();
8+
}
9+
10+
public function testSessionSimple()
11+
{
12+
$this->session->set('logged_in', 123);
13+
14+
$value = $this->session->get('logged_in');
15+
16+
$this->assertEquals(123, $value);
17+
}
18+
}
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\CIUnitTestCase
4+
{
5+
public function setUp(): void
6+
{
7+
parent::setUp();
8+
}
9+
10+
public function testIsDefinedAppPath()
11+
{
12+
$test = defined('APPPATH');
13+
14+
$this->assertTrue($test);
15+
}
16+
}

0 commit comments

Comments
 (0)