Taufan Aditya | d6b41bb | 2012-05-04 15:05:22 +0700 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | class Join_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_join_simple() |
| 24 | { |
| 25 | $job_user = $this->db->select('job.id as job_id, job.name as job_name, user.id as user_id, user.name as user_name') |
| 26 | ->from('job') |
| 27 | ->join('user', 'user.id = job.id') |
| 28 | ->get() |
| 29 | ->result_array(); |
| 30 | |
| 31 | // Check the result |
| 32 | $this->assertEquals('1', $job_user[0]['job_id']); |
| 33 | $this->assertEquals('1', $job_user[0]['user_id']); |
| 34 | $this->assertEquals('Derek Jones', $job_user[0]['user_name']); |
| 35 | $this->assertEquals('Developer', $job_user[0]['job_name']); |
| 36 | } |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 37 | |
Andrey Andreev | 49aa45b | 2012-07-06 16:22:21 +0300 | [diff] [blame^] | 38 | // ------------------------------------------------------------------------ |
| 39 | |
| 40 | public function test_join_escape_multiple_conditions() |
| 41 | { |
| 42 | // We just need a valid query produced, not one that makes sense |
| 43 | $fields = array($this->db->protect_identifiers('table1.field1'), $this->db->protect_identifiers('table2.field2')); |
| 44 | |
| 45 | $expected = 'SELECT '.implode(', ', $fields) |
| 46 | ."\nFROM ".$this->db->escape_identifiers('table1') |
| 47 | ."\nLEFT JOIN ".$this->db->escape_identifiers('table2').' ON '.implode(' = ', $fields) |
| 48 | .' AND '.$fields[0]." = 'foo' AND ".$fields[1].' = 0'; |
| 49 | |
| 50 | $result = $this->db->select('table1.field1, table2.field2') |
| 51 | ->from('table1') |
| 52 | ->join('table2', "table1.field1 = table2.field2 AND table1.field1 = 'foo' AND table2.field2 = 0", 'LEFT') |
| 53 | ->get_compiled_select(); |
| 54 | |
| 55 | $this->assertEquals($sql, $result); |
| 56 | } |
| 57 | |
Taufan Aditya | d6b41bb | 2012-05-04 15:05:22 +0700 | [diff] [blame] | 58 | } |