Merge project-tests into admin scaffold

This commit is contained in:
Matt Gatner 2020-02-23 02:07:36 +00:00
parent 3c984684bb
commit c1bb4377b4
No known key found for this signature in database
GPG Key ID: 98F2DFA2FF210CCC
8 changed files with 303 additions and 0 deletions

View File

@ -0,0 +1,61 @@
<?php namespace Tests\Support\Database\Migrations;
use CodeIgniter\Database\Migration;
class ExampleMigration extends Migration
{
protected $DBGroup = 'tests';
public function up()
{
$fields = [
'name' => [
'type' => 'varchar',
'constraint' => 31,
],
'uid' => [
'type' => 'varchar',
'constraint' => 31,
],
'class' => [
'type' => 'varchar',
'constraint' => 63,
],
'icon' => [
'type' => 'varchar',
'constraint' => 31,
],
'summary' => [
'type' => 'varchar',
'constraint' => 255,
],
'created_at' => [
'type' => 'datetime',
'null' => true,
],
'updated_at' => [
'type' => 'datetime',
'null' => true,
],
'deleted_at' => [
'type' => 'datetime',
'null' => true,
],
];
$this->forge->addField('id');
$this->forge->addField($fields);
$this->forge->addKey('name');
$this->forge->addKey('uid');
$this->forge->addKey(['deleted_at', 'id']);
$this->forge->addKey('created_at');
$this->forge->createTable('factories');
}
public function down()
{
$this->forge->dropTable('factories');
}
}

View File

@ -0,0 +1,40 @@
<?php namespace Tests\Support\Database\Seeds;
use CodeIgniter\Database\Seeder;
class ExampleSeeder extends Seeder
{
public function run()
{
$factories = [
[
'name' => 'Test Factory',
'uid' => 'test001',
'class' => 'Factories\Tests\NewFactory',
'icon' => 'fas fa-puzzle-piece',
'summary' => 'Longer sample text for testing',
],
[
'name' => 'Widget Factory',
'uid' => 'widget',
'class' => 'Factories\Tests\WidgetPlant',
'icon' => 'fas fa-puzzle-piece',
'summary' => 'Create widgets in your factory',
],
[
'name' => 'Evil Factory',
'uid' => 'evil-maker',
'class' => 'Factories\Evil\MyFactory',
'icon' => 'fas fa-book-dead',
'summary' => 'Abandon all hope, ye who enter here',
],
];
$builder = $this->db->table('factories');
foreach ($factories as $factory)
{
$builder->insert($factory);
}
}
}

View File

@ -0,0 +1,51 @@
<?php namespace Tests\Support;
class DatabaseTestCase extends \CodeIgniter\Test\CIDatabaseTestCase
{
/**
* Should the database be refreshed before each test?
*
* @var boolean
*/
protected $refresh = true;
/**
* The seed file(s) used for all tests within this test case.
* Should be fully-namespaced or relative to $basePath
*
* @var string|array
*/
protected $seed = 'Tests\Support\Database\Seeds\ExampleSeeder';
/**
* The path to the seeds directory.
* Allows overriding the default application directories.
*
* @var string
*/
protected $basePath = SUPPORTPATH . 'Database/';
/**
* The namespace(s) to help us find the migration classes.
* Empty is equivalent to running `spark migrate -all`.
* Note that running "all" runs migrations in date order,
* but specifying namespaces runs them in namespace order (then date)
*
* @var string|array|null
*/
protected $namespace = 'Tests\Support';
public function setUp(): void
{
parent::setUp();
// Extra code to run before each test
}
public function tearDown(): void
{
parent::tearDown();
// Extra code to run after each test
}
}

View File

@ -0,0 +1,26 @@
<?php namespace Tests\Support\Models;
use CodeIgniter\Model;
class ExampleModel extends Model
{
protected $table = 'factories';
protected $primaryKey = 'id';
protected $returnType = 'object';
protected $useSoftDeletes = false;
protected $allowedFields = [
'name',
'uid',
'class',
'icon',
'summary',
];
protected $useTimestamps = true;
protected $validationRules = [];
protected $validationMessages = [];
protected $skipValidation = false;
}

View File

@ -0,0 +1,32 @@
<?php namespace Tests\Support;
use CodeIgniter\Session\Handlers\ArrayHandler;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockSession;
class SessionTestCase extends CIUnitTestCase
{
/**
* @var SessionHandler
*/
protected $session;
public function setUp(): void
{
parent::setUp();
$this->mockSession();
}
/**
* Pre-loads the mock session driver into $this->session.
*
* @var string
*/
protected function mockSession()
{
$config = config('App');
$this->session = new MockSession(new ArrayHandler($config, '0.0.0.0'), $config);
\Config\Services::injectMock('session', $this->session);
}
}

View File

@ -0,0 +1,41 @@
<?php
use Tests\Support\Models\ExampleModel;
class ExampleDatabaseTest extends \Tests\Support\DatabaseTestCase
{
public function setUp(): void
{
parent::setUp();
// Extra code to run before each test
}
public function testModelFindAll()
{
$model = new ExampleModel();
// Get every row created by ExampleSeeder
$objects = $model->findAll();
// Make sure the count is as expected
$this->assertCount(3, $objects);
}
public function testSoftDeleteLeavesRow()
{
$model = new ExampleModel();
$this->setPrivateProperty($model, 'useSoftDeletes', true);
$object = $model->first();
$model->delete($object->id);
// The model should no longer find it
$this->assertNull($model->find($object->id));
// ... but it should still be in the database
$result = $model->builder()->where('id', $object->id)->get()->getResult();
$this->assertCount(1, $result);
}
}

View File

@ -0,0 +1,18 @@
<?php
class ExampleSessionTest extends \Tests\Support\SessionTestCase
{
public function setUp(): void
{
parent::setUp();
}
public function testSessionSimple()
{
$this->session->set('logged_in', 123);
$value = $this->session->get('logged_in');
$this->assertEquals(123, $value);
}
}

View File

@ -0,0 +1,34 @@
<?php
class ExampleTest extends \CodeIgniter\Test\CIUnitTestCase
{
public function setUp(): void
{
parent::setUp();
}
public function testIsDefinedAppPath()
{
$test = defined('APPPATH');
$this->assertTrue($test);
}
public function testBaseUrlHasBeenSet()
{
$env = $config = false;
// First check in .env
$dotenv = new \CodeIgniter\Config\DotEnv(HOMEPATH);
if ($dotenv->load())
{
// Check any line with "app.baseUrl" to see if it actually has a value set
foreach (preg_grep('/^app\.baseURL', file(HOMEPATH . '.env')) as $line)
{
}
}
$this->assertTrue($test);
}
}