First Local Commit - After Clean up.
Signed-off-by: Rick Hays <rhays@haysgang.com>
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
<?php namespace Tests\Support\Database\Migrations;
|
||||
|
||||
class Migration_Create_test_tables extends \CodeIgniter\Database\Migration
|
||||
{
|
||||
public function up()
|
||||
{
|
||||
// SQLite3 uses auto increment different
|
||||
$unique_or_auto = $this->db->DBDriver === 'SQLite3' ? 'unique' : 'auto_increment';
|
||||
|
||||
// User Table
|
||||
$this->forge->addField([
|
||||
'id' => [
|
||||
'type' => 'INTEGER',
|
||||
'constraint' => 3,
|
||||
$unique_or_auto => true,
|
||||
],
|
||||
'name' => [
|
||||
'type' => 'VARCHAR',
|
||||
'constraint' => 80,
|
||||
],
|
||||
'email' => [
|
||||
'type' => 'VARCHAR',
|
||||
'constraint' => 100,
|
||||
],
|
||||
'country' => [
|
||||
'type' => 'VARCHAR',
|
||||
'constraint' => 40,
|
||||
],
|
||||
'created_at' => [
|
||||
'type' => 'DATETIME',
|
||||
'null' => true,
|
||||
],
|
||||
'updated_at' => [
|
||||
'type' => 'DATETIME',
|
||||
'null' => true,
|
||||
],
|
||||
'deleted_at' => [
|
||||
'type' => 'DATETIME',
|
||||
'null' => true,
|
||||
],
|
||||
]);
|
||||
$this->forge->addKey('id', true);
|
||||
$this->forge->createTable('user', true);
|
||||
|
||||
// Job Table
|
||||
$this->forge->addField([
|
||||
'id' => [
|
||||
'type' => 'INTEGER',
|
||||
'constraint' => 3,
|
||||
$unique_or_auto => true,
|
||||
],
|
||||
'name' => [
|
||||
'type' => 'VARCHAR',
|
||||
'constraint' => 40,
|
||||
],
|
||||
'description' => [
|
||||
'type' => 'TEXT',
|
||||
'null' => true,
|
||||
],
|
||||
'created_at' => [
|
||||
'type' => 'INTEGER',
|
||||
'constraint' => 11,
|
||||
'null' => true,
|
||||
],
|
||||
'updated_at' => [
|
||||
'type' => 'INTEGER',
|
||||
'constraint' => 11,
|
||||
'null' => true,
|
||||
],
|
||||
'deleted_at' => [
|
||||
'type' => 'INTEGER',
|
||||
'constraint' => 11,
|
||||
'null' => true,
|
||||
],
|
||||
]);
|
||||
$this->forge->addKey('id', true);
|
||||
$this->forge->createTable('job', true);
|
||||
|
||||
// Misc Table
|
||||
$this->forge->addField([
|
||||
'id' => [
|
||||
'type' => 'INTEGER',
|
||||
'constraint' => 3,
|
||||
$unique_or_auto => true,
|
||||
],
|
||||
'key' => [
|
||||
'type' => 'VARCHAR',
|
||||
'constraint' => 40,
|
||||
],
|
||||
'value' => ['type' => 'TEXT'],
|
||||
]);
|
||||
$this->forge->addKey('id', true);
|
||||
$this->forge->createTable('misc', true);
|
||||
|
||||
// Empty Table
|
||||
$this->forge->addField([
|
||||
'id' => [
|
||||
'type' => 'INTEGER',
|
||||
'constraint' => 3,
|
||||
$unique_or_auto => true,
|
||||
],
|
||||
'name' => [
|
||||
'type' => 'VARCHAR',
|
||||
'constraint' => 40,
|
||||
],
|
||||
'created_at' => [
|
||||
'type' => 'DATE',
|
||||
'null' => true,
|
||||
],
|
||||
'updated_at' => [
|
||||
'type' => 'DATE',
|
||||
'null' => true,
|
||||
],
|
||||
]);
|
||||
$this->forge->addKey('id', true);
|
||||
$this->forge->createTable('empty', true);
|
||||
|
||||
// Secondary Table
|
||||
$this->forge->addField([
|
||||
'id' => [
|
||||
'type' => 'INTEGER',
|
||||
'constraint' => 3,
|
||||
$unique_or_auto => true,
|
||||
],
|
||||
'key' => [
|
||||
'type' => 'VARCHAR',
|
||||
'constraint' => 40,
|
||||
],
|
||||
'value' => ['type' => 'TEXT'],
|
||||
]);
|
||||
$this->forge->addKey('id', true);
|
||||
$this->forge->createTable('secondary', true);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
public function down()
|
||||
{
|
||||
$this->forge->dropTable('user', true);
|
||||
$this->forge->dropTable('job', true);
|
||||
$this->forge->dropTable('misc', true);
|
||||
$this->forge->dropTable('empty', true);
|
||||
$this->forge->dropTable('secondary', true);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
}
|
||||
15
tests/_support/Database/MockBuilder.php
Normal file
15
tests/_support/Database/MockBuilder.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php namespace Tests\Support\Database;
|
||||
|
||||
use CodeIgniter\Database\BaseBuilder;
|
||||
use CodeIgniter\Database\ConnectionInterface;
|
||||
|
||||
class MockBuilder extends BaseBuilder {
|
||||
|
||||
public function __construct($tableName, ConnectionInterface &$db, array $options = null)
|
||||
{
|
||||
parent::__construct($tableName, $db, $options);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
}
|
||||
298
tests/_support/Database/MockConnection.php
Normal file
298
tests/_support/Database/MockConnection.php
Normal file
@@ -0,0 +1,298 @@
|
||||
<?php namespace Tests\Support\Database;
|
||||
|
||||
use CodeIgniter\CodeIgniter;
|
||||
use CodeIgniter\Database\BaseConnection;
|
||||
|
||||
class MockConnection extends BaseConnection
|
||||
{
|
||||
protected $returnValues = [];
|
||||
|
||||
public $database;
|
||||
|
||||
public $lastQuery;
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
public function shouldReturn(string $method, $return)
|
||||
{
|
||||
$this->returnValues[$method] = $return;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Orchestrates a query against the database. Queries must use
|
||||
* Database\Statement objects to store the query and build it.
|
||||
* This method works with the cache.
|
||||
*
|
||||
* Should automatically handle different connections for read/write
|
||||
* queries if needed.
|
||||
*
|
||||
* @param string $sql
|
||||
* @param mixed ...$binds
|
||||
* @param boolean $setEscapeFlags
|
||||
* @param string $queryClass
|
||||
*
|
||||
* @return \CodeIgniter\Database\BaseResult|\CodeIgniter\Database\Query|false
|
||||
*/
|
||||
|
||||
public function query(string $sql, $binds = null, bool $setEscapeFlags = true, string $queryClass = 'CodeIgniter\\Database\\Query')
|
||||
{
|
||||
$queryClass = str_replace('Connection', 'Query', get_class($this));
|
||||
|
||||
$query = new $queryClass($this);
|
||||
|
||||
$query->setQuery($sql, $binds, $setEscapeFlags);
|
||||
|
||||
if (! empty($this->swapPre) && ! empty($this->DBPrefix))
|
||||
{
|
||||
$query->swapPrefix($this->DBPrefix, $this->swapPre);
|
||||
}
|
||||
|
||||
$startTime = microtime(true);
|
||||
|
||||
$this->lastQuery = $query;
|
||||
|
||||
// Run the query
|
||||
if (false === ($this->resultID = $this->simpleQuery($query->getQuery())))
|
||||
{
|
||||
$query->setDuration($startTime, $startTime);
|
||||
|
||||
// @todo deal with errors
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
$query->setDuration($startTime);
|
||||
|
||||
$resultClass = str_replace('Connection', 'Result', get_class($this));
|
||||
|
||||
return new $resultClass($this->connID, $this->resultID);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Connect to the database.
|
||||
*
|
||||
* @param boolean $persistent
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function connect(bool $persistent = false)
|
||||
{
|
||||
$return = $this->returnValues['connect'] ?? true;
|
||||
|
||||
if (is_array($return))
|
||||
{
|
||||
// By removing the top item here, we can
|
||||
// get a different value for, say, testing failover connections.
|
||||
$return = array_shift($this->returnValues['connect']);
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Keep or establish the connection if no queries have been sent for
|
||||
* a length of time exceeding the server's idle timeout.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function reconnect(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Select a specific database table to use.
|
||||
*
|
||||
* @param string $databaseName
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function setDatabase(string $databaseName)
|
||||
{
|
||||
$this->database = $databaseName;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns a string containing the version of the database being used.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getVersion(): string
|
||||
{
|
||||
return CodeIgniter::CI_VERSION;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Executes the query against the database.
|
||||
*
|
||||
* @param string $sql
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function execute(string $sql)
|
||||
{
|
||||
return $this->returnValues['execute'];
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns the total number of rows affected by this query.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function affectedRows(): int
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns the last error code and message.
|
||||
*
|
||||
* Must return an array with keys 'code' and 'message':
|
||||
*
|
||||
* return ['code' => null, 'message' => null);
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function error(): array
|
||||
{
|
||||
return [
|
||||
'code' => null,
|
||||
'message' => null,
|
||||
];
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Insert ID
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function insertID(): int
|
||||
{
|
||||
return $this->connID->insert_id;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Generates the SQL for listing tables in a platform-dependent manner.
|
||||
*
|
||||
* @param boolean $constrainByPrefix
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function _listTables(bool $constrainByPrefix = false): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Generates a platform-specific query string so that the column names can be fetched.
|
||||
*
|
||||
* @param string $table
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
protected function _listColumns(string $table = ''): string
|
||||
{
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $table
|
||||
* @return array
|
||||
*/
|
||||
protected function _fieldData(string $table): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $table
|
||||
* @return array
|
||||
*/
|
||||
protected function _indexData(string $table): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $table
|
||||
* @return array
|
||||
*/
|
||||
protected function _foreignKeyData(string $table): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Close the connection.
|
||||
*/
|
||||
protected function _close()
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Begin Transaction
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
protected function _transBegin(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Commit Transaction
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
protected function _transCommit(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Rollback Transaction
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
protected function _transRollback(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
}
|
||||
8
tests/_support/Database/MockQuery.php
Normal file
8
tests/_support/Database/MockQuery.php
Normal file
@@ -0,0 +1,8 @@
|
||||
<?php namespace Tests\Support\Database;
|
||||
|
||||
use CodeIgniter\Database\Query;
|
||||
|
||||
class MockQuery extends Query
|
||||
{
|
||||
|
||||
}
|
||||
93
tests/_support/Database/MockResult.php
Normal file
93
tests/_support/Database/MockResult.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php namespace Tests\Support\Database;
|
||||
|
||||
use CodeIgniter\Database\BaseResult;
|
||||
|
||||
class MockResult extends BaseResult
|
||||
{
|
||||
/**
|
||||
* Gets the number of fields in the result set.
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function getFieldCount(): int
|
||||
{
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Generates an array of column names in the result set.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getFieldNames(): array
|
||||
{
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Generates an array of objects representing field meta-data.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getFieldData(): array
|
||||
{
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Frees the current result.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function freeResult()
|
||||
{
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Moves the internal pointer to the desired offset. This is called
|
||||
* internally before fetching results to make sure the result set
|
||||
* starts at zero.
|
||||
*
|
||||
* @param integer $n
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function dataSeek($n = 0)
|
||||
{
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns the result set as an array.
|
||||
*
|
||||
* Overridden by driver classes.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function fetchAssoc()
|
||||
{
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Returns the result set as an object.
|
||||
*
|
||||
* Overridden by child classes.
|
||||
*
|
||||
* @param string $className
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
protected function fetchObject($className = 'stdClass')
|
||||
{
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
}
|
||||
7
tests/_support/Database/MockTestClass.php
Normal file
7
tests/_support/Database/MockTestClass.php
Normal file
@@ -0,0 +1,7 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Support\Database;
|
||||
|
||||
class MockTestClass
|
||||
{
|
||||
}
|
||||
78
tests/_support/Database/Seeds/CITestSeeder.php
Normal file
78
tests/_support/Database/Seeds/CITestSeeder.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php namespace Tests\Support\Database\Seeds;
|
||||
|
||||
class CITestSeeder extends \CodeIgniter\Database\Seeder
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
// Job Data
|
||||
$data = [
|
||||
'user' => [
|
||||
[
|
||||
'name' => 'Derek Jones',
|
||||
'email' => 'derek@world.com',
|
||||
'country' => 'US',
|
||||
],
|
||||
[
|
||||
'name' => 'Ahmadinejad',
|
||||
'email' => 'ahmadinejad@world.com',
|
||||
'country' => 'Iran',
|
||||
],
|
||||
[
|
||||
'name' => 'Richard A Causey',
|
||||
'email' => 'richard@world.com',
|
||||
'country' => 'US',
|
||||
],
|
||||
[
|
||||
'name' => 'Chris Martin',
|
||||
'email' => 'chris@world.com',
|
||||
'country' => 'UK',
|
||||
],
|
||||
],
|
||||
'job' => [
|
||||
[
|
||||
'name' => 'Developer',
|
||||
'description' => 'Awesome job, but sometimes makes you bored',
|
||||
],
|
||||
[
|
||||
'name' => 'Politician',
|
||||
'description' => 'This is not really a job',
|
||||
],
|
||||
[
|
||||
'name' => 'Accountant',
|
||||
'description' => 'Boring job, but you will get free snack at lunch',
|
||||
],
|
||||
[
|
||||
'name' => 'Musician',
|
||||
'description' => 'Only Coldplay can actually called Musician',
|
||||
],
|
||||
],
|
||||
'misc' => [
|
||||
[
|
||||
'key' => '\\xxxfoo456',
|
||||
'value' => 'Entry with \\xxx',
|
||||
],
|
||||
[
|
||||
'key' => '\\%foo456',
|
||||
'value' => 'Entry with \\%',
|
||||
],
|
||||
[
|
||||
'key' => 'spaces and tabs',
|
||||
'value' => ' One two three tab',
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
foreach ($data as $table => $dummy_data)
|
||||
{
|
||||
$this->db->table($table)->truncate();
|
||||
|
||||
foreach ($dummy_data as $single_dummy_data)
|
||||
{
|
||||
$this->db->table($table)->insert($single_dummy_data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php namespace App\Database\Migrations;
|
||||
|
||||
class Migration_some_migration extends \CodeIgniter\Database\Migration
|
||||
{
|
||||
public function up()
|
||||
{
|
||||
$this->forge->addField([
|
||||
'key' => [
|
||||
'type' => 'VARCHAR',
|
||||
'constraint' => 255,
|
||||
],
|
||||
]);
|
||||
$this->forge->createTable('foo', true);
|
||||
|
||||
$this->db->table('foo')->insert([
|
||||
'key' => 'foobar',
|
||||
]);
|
||||
}
|
||||
|
||||
public function down()
|
||||
{
|
||||
$this->forge->dropTable('foo', true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user