Skip to content

Commit

Permalink
Merge pull request bcit-ci#1394 from toopay/db-test-suite
Browse files Browse the repository at this point in the history
Escape like tests, bcit-ci#136 verification
  • Loading branch information
narfbg committed May 24, 2012
2 parents 21cb2d3 + 6a43244 commit b24a389
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tests/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// Get vfsStream either via PEAR or composer
foreach (explode(PATH_SEPARATOR, get_include_path()) as $path)
{
if (file_exists($path.DIRECTORY_SEPARATOR.'vfsStream/vfsStream.phps'))
if (file_exists($path.DIRECTORY_SEPARATOR.'vfsStream/vfsStream.php'))
{
require_once 'vfsStream/vfsStream.php';
break;
Expand Down
47 changes: 47 additions & 0 deletions tests/codeigniter/database/query_builder/escape_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

class Escape_test extends CI_TestCase {

/**
* @var object Database/Query Builder holder
*/
protected $db;

public function set_up()
{
$this->db = Mock_Database_Schema_Skeleton::init(DB_DRIVER);

Mock_Database_Schema_Skeleton::create_tables();
Mock_Database_Schema_Skeleton::create_data();
}

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

/**
* @see ./mocks/schema/skeleton.php
*/
public function test_escape_like_percent_sign()
{
$string = $this->db->escape_like_str('\%foo');
$sql = "SELECT `value` FROM `misc` WHERE `key` LIKE '$string%';";
$res = $this->db->query($sql)->result_array();

// Check the result
$this->assertEquals(1, count($res));
}

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

/**
* @see ./mocks/schema/skeleton.php
*/
public function test_escape_like_backslash_sign()
{
$string = $this->db->escape_like_str('\\');
$sql = "SELECT `value` FROM `misc` WHERE `key` LIKE '$string%';";
$res = $this->db->query($sql)->result_array();

// Check the result
$this->assertEquals(2, count($res));
}
}
21 changes: 21 additions & 0 deletions tests/mocks/database/schema/skeleton.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,23 @@ public static function create_tables()
));
static::$forge->add_key('id', TRUE);
static::$forge->create_table('job', (strpos(static::$driver, 'pgsql') === FALSE));

// Misc Table
static::$forge->add_field(array(
'id' => array(
'type' => 'INTEGER',
'constraint' => 3,
),
'key' => array(
'type' => 'VARCHAR',
'constraint' => 40,
),
'value' => array(
'type' => 'TEXT',
),
));
static::$forge->add_key('id', TRUE);
static::$forge->create_table('misc', (strpos(static::$driver, 'pgsql') === FALSE));
}

/**
Expand All @@ -111,6 +128,10 @@ public static function create_data()
array('id' => 3, 'name' => 'Accountant', 'description' => 'Boring job, but you will get free snack at lunch'),
array('id' => 4, 'name' => 'Musician', 'description' => 'Only Coldplay can actually called Musician'),
),
'misc' => array(
array('id' => 1, 'key' => '\\xxxfoo456', 'value' => 'Entry with \\xxx'),
array('id' => 2, 'key' => '\\%foo456', 'value' => 'Entry with \\%'),
),
);

foreach ($data as $table => $dummy_data)
Expand Down

0 comments on commit b24a389

Please sign in to comment.