Taufan Aditya | 2d57445 | 2012-05-25 04:03:56 +0700 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | class Escape_test extends CI_TestCase { |
| 4 | |
| 5 | /** |
| 6 | * @var object Database/Query Builder holder |
| 7 | */ |
| 8 | protected $db; |
| 9 | |
| 10 | public function set_up() |
| 11 | { |
| 12 | $this->db = Mock_Database_Schema_Skeleton::init(DB_DRIVER); |
| 13 | |
| 14 | Mock_Database_Schema_Skeleton::create_tables(); |
| 15 | Mock_Database_Schema_Skeleton::create_data(); |
| 16 | } |
| 17 | |
| 18 | // ------------------------------------------------------------------------ |
| 19 | |
| 20 | /** |
| 21 | * @see ./mocks/schema/skeleton.php |
| 22 | */ |
| 23 | public function test_escape_like_percent_sign() |
| 24 | { |
| 25 | $string = $this->db->escape_like_str('\%foo'); |
Taufan Aditya | 98dcac7 | 2012-05-25 05:07:51 +0700 | [diff] [blame^] | 26 | $sql = "SELECT `value` FROM `misc` WHERE `key` LIKE '$string%'"; |
| 27 | $sql .= (strpos(DB_DRIVER, 'mysql') !== FALSE) ? ";" : "ESCAPE '!';"; |
| 28 | |
Taufan Aditya | 6a43244 | 2012-05-25 04:09:38 +0700 | [diff] [blame] | 29 | $res = $this->db->query($sql)->result_array(); |
Taufan Aditya | 2d57445 | 2012-05-25 04:03:56 +0700 | [diff] [blame] | 30 | |
| 31 | // Check the result |
| 32 | $this->assertEquals(1, count($res)); |
| 33 | } |
| 34 | |
| 35 | // ------------------------------------------------------------------------ |
| 36 | |
| 37 | /** |
| 38 | * @see ./mocks/schema/skeleton.php |
| 39 | */ |
| 40 | public function test_escape_like_backslash_sign() |
| 41 | { |
| 42 | $string = $this->db->escape_like_str('\\'); |
Taufan Aditya | 98dcac7 | 2012-05-25 05:07:51 +0700 | [diff] [blame^] | 43 | $sql = "SELECT `value` FROM `misc` WHERE `key` LIKE '$string%'"; |
| 44 | $sql .= (strpos(DB_DRIVER, 'mysql') !== FALSE) ? ";" : "ESCAPE '!';"; |
Taufan Aditya | 6a43244 | 2012-05-25 04:09:38 +0700 | [diff] [blame] | 45 | $res = $this->db->query($sql)->result_array(); |
Taufan Aditya | 2d57445 | 2012-05-25 04:03:56 +0700 | [diff] [blame] | 46 | |
| 47 | // Check the result |
| 48 | $this->assertEquals(2, count($res)); |
| 49 | } |
| 50 | } |