Skip to content

Commit

Permalink
Merge pull request #2211 from MGatner/db-underscore
Browse files Browse the repository at this point in the history
Add listTable() tests
  • Loading branch information
lonnieezell authored Sep 19, 2019
2 parents 91e1c5d + 7c369aa commit 1f4cd89
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/system/Database/Live/ForgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ public function testCreateTableWithArrayFieldConstraints()
{
$this->assertEquals('enum', $fields[0]->type);
}

$this->forge->dropTable('forge_array_constraint', true);
}
else
{
Expand Down
85 changes: 85 additions & 0 deletions tests/system/Database/Live/MetadataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php namespace CodeIgniter\Database\Live;

use CodeIgniter\Test\CIDatabaseTestCase;

/**
* @group DatabaseLive
*/
class MetadataTest extends CIDatabaseTestCase
{
protected $refresh = true;

protected $seed = 'Tests\Support\Database\Seeds\CITestSeeder';

/**
* Array of expected tables.
*
* @var array
*/
protected $expectedTables;

protected function setUp()
{
parent::setUp();

// Prepare the array of expected tables once
$prefix = $this->db->getPrefix();
$this->expectedTables = [
$prefix . 'migrations',
$prefix . 'user',
$prefix . 'job',
$prefix . 'misc',
$prefix . 'empty',
$prefix . 'secondary'
];
}

public function testListTables()
{
$result = $this->db->listTables();

$this->assertEquals($this->expectedTables, array_values($result));
}

//--------------------------------------------------------------------

public function testListTablesConstrainPrefix()
{
$result = $this->db->listTables(true);

$this->assertEquals($this->expectedTables, array_values($result));
}

//--------------------------------------------------------------------

public function testConstrainPrefixIgnoresOtherTables()
{
$this->forge = \Config\Database::forge($this->DBGroup);

// Stash the prefix and change it
$DBPrefix = $this->db->getPrefix();
$this->db->setPrefix('tmp_');

// Create a table with the new prefix
$fields = [
'name' => ['type' => 'varchar', 'constraint' => 31],
'created_at' => ['type' => 'datetime', 'null' => true],
];
$this->forge->addField($fields);
$this->forge->createTable('widgets');

// Restore the prefix and get the tables with the original prefix
$this->db->setPrefix($DBPrefix);
$result = $this->db->listTables(true);

$this->assertEquals($this->expectedTables, array_values($result));

// Clean up temporary table
$this->db->setPrefix('tmp_');
$this->forge->dropTable('widgets');
$this->db->setPrefix($DBPrefix);
}

//--------------------------------------------------------------------

}

0 comments on commit 1f4cd89

Please sign in to comment.