Refactor/optimise starter's tests

This commit is contained in:
John Paul E. Balandan, CPA 2021-10-04 12:17:27 +08:00
parent 56add793a9
commit f3bf386928
No known key found for this signature in database
GPG Key ID: FB7B51499BC27610
5 changed files with 18 additions and 129 deletions

View File

@ -1,61 +0,0 @@
<?php
namespace Tests\Support;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
/**
* @internal
*/
final class DatabaseTestCase extends CIUnitTestCase
{
use DatabaseTestTrait;
/**
* Should the database be refreshed before each test?
*
* @var bool
*/
protected $refresh = true;
/**
* The seed file(s) used for all tests within this test case.
* Should be fully-namespaced or relative to $basePath
*
* @var array|string
*/
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 array|string|null
*/
protected $namespace = 'Tests\Support';
protected function setUp(): void
{
parent::setUp();
// Extra code to run before each test
}
protected function tearDown(): void
{
parent::tearDown();
// Extra code to run after each test
}
}

View File

@ -1,38 +0,0 @@
<?php
namespace Tests\Support;
use CodeIgniter\Session\Handlers\ArrayHandler;
use CodeIgniter\Session\SessionInterface;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\Mock\MockSession;
/**
* @internal
*/
final class SessionTestCase extends CIUnitTestCase
{
/**
* @var SessionInterface
*/
protected $session;
protected 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

@ -1,18 +1,15 @@
<?php
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use Tests\Support\Models\ExampleModel;
/**
* @internal
*/
final class ExampleDatabaseTest extends \Tests\Support\DatabaseTestCase
final class ExampleDatabaseTest extends CIUnitTestCase
{
protected function setUp(): void
{
parent::setUp();
// Extra code to run before each test
}
use DatabaseTestTrait;
public function testModelFindAll()
{

View File

@ -1,21 +1,15 @@
<?php
use CodeIgniter\Test\CIUnitTestCase;
/**
* @internal
*/
final class ExampleSessionTest extends \Tests\Support\SessionTestCase
final class ExampleSessionTest extends CIUnitTestCase
{
protected function setUp(): void
{
parent::setUp();
}
public function testSessionSimple()
{
$this->session->set('logged_in', 123);
$value = $this->session->get('logged_in');
$this->assertSame(123, $value);
$this->assertSame(123, $this->session->get('logged_in'));
}
}

View File

@ -1,39 +1,36 @@
<?php
use CodeIgniter\Test\CIUnitTestCase;
use Config\App;
use Config\Services;
use Tests\Support\Libraries\ConfigReader;
/**
* @internal
*/
final class HealthTest extends \CodeIgniter\Test\CIUnitTestCase
final class HealthTest extends CIUnitTestCase
{
protected function setUp(): void
{
parent::setUp();
}
public function testIsDefinedAppPath()
{
$test = defined('APPPATH');
$this->assertTrue($test);
$this->assertTrue(defined('APPPATH'));
}
public function testBaseUrlHasBeenSet()
{
$validation = Services::validation();
$env = false;
$env = false;
// Check the baseURL in .env
if (is_file(HOMEPATH . '.env')) {
$env = (bool) preg_grep('/^app\.baseURL = ./', file(HOMEPATH . '.env'));
$env = preg_grep('/^app\.baseURL = ./', file(HOMEPATH . '.env')) !== false;
}
if ($env) {
// BaseURL in .env is a valid URL?
// phpunit.xml.dist sets app.baseURL in $_SERVER
// So if you set app.baseURL in .env, it takes precedence
$config = new Config\App();
$config = new App();
$this->assertTrue(
$validation->check($config->baseURL, 'valid_url'),
'baseURL "' . $config->baseURL . '" in .env is not valid URL'
@ -42,7 +39,7 @@ final class HealthTest extends \CodeIgniter\Test\CIUnitTestCase
// Get the baseURL in app/Config/App.php
// You can't use Config\App, because phpunit.xml.dist sets app.baseURL
$reader = new \Tests\Support\Libraries\ConfigReader();
$reader = new ConfigReader();
// BaseURL in app/Config/App.php is a valid URL?
$this->assertTrue(