blob: 25bd4accbb459ae3001149c9ce99c1b89091f864 [file] [log] [blame]
Taufan Adityad6b41bb2012-05-04 15:05:22 +07001<?php
2
3class 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 Andreevc1862882012-06-09 23:16:58 +030037
Andrey Andreev49aa45b2012-07-06 16:22:21 +030038 // ------------------------------------------------------------------------
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
Andrey Andreevb6490f52012-07-06 16:32:16 +030055 $this->assertEquals($expected, $result);
Andrey Andreev49aa45b2012-07-06 16:22:21 +030056 }
57
Taufan Adityad6b41bb2012-05-04 15:05:22 +070058}