blob: 607eaa076fbb07907d018d3b07fbcdbdf56b0bc2 [file] [log] [blame]
Taufan Aditya12f54752012-05-04 15:46:39 +07001<?php
2
3class Where_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_where_simple_key_value()
24 {
25 $job1 = $this->db->where('id', 1)
26 ->get('job')
27 ->row();
28
29 // Check the result
30 $this->assertEquals('1', $job1->id);
31 $this->assertEquals('Developer', $job1->name);
32 }
33
34 // ------------------------------------------------------------------------
35
36 /**
37 * @see ./mocks/schema/skeleton.php
38 */
39 public function test_where_custom_key_value()
40 {
41 $jobs = $this->db->where('id !=', 1)
42 ->get('job')
43 ->result_array();
44
45 // Check the result
46 $this->assertEquals(3, count($jobs));
47 }
48
49 // ------------------------------------------------------------------------
50
51 /**
52 * @see ./mocks/schema/skeleton.php
53 */
54 public function test_where_associative_array()
55 {
56 $where = array('id >' => 2, 'name !=' => 'Accountant');
57 $jobs = $this->db->where($where)
58 ->get('job')
59 ->result_array();
60
61 // Check the result
62 $this->assertEquals(1, count($jobs));
63
64 // Should be Musician
65 $job = current($jobs);
66
67 $this->assertEquals('Musician', $job['name']);
68 }
69
70 // ------------------------------------------------------------------------
71
72 /**
73 * @see ./mocks/schema/skeleton.php
74 */
75 public function test_where_custom_string()
76 {
77 $where = "id > 2 AND name != 'Accountant'";
78 $jobs = $this->db->where($where)
79 ->get('job')
80 ->result_array();
81
82 // Check the result
83 $this->assertEquals(1, count($jobs));
84
85 // Should be Musician
86 $job = current($jobs);
87
88 $this->assertEquals('Musician', $job['name']);
89 }
90
91 // ------------------------------------------------------------------------
92
93 /**
94 * @see ./mocks/schema/skeleton.php
95 */
96 public function test_where_or()
97 {
98 $jobs = $this->db->where('name !=', 'Accountant')
99 ->or_where('id >', 3)
100 ->get('job')
101 ->result_array();
102
103 // Check the result
104 $this->assertEquals(3, count($jobs));
105 $this->assertEquals('Developer', $jobs[0]['name']);
106 $this->assertEquals('Politician', $jobs[1]['name']);
107 $this->assertEquals('Musician', $jobs[2]['name']);
108 }
109
110 // ------------------------------------------------------------------------
111
112 /**
113 * @see ./mocks/schema/skeleton.php
114 */
115 public function test_where_in()
116 {
117 $jobs = $this->db->where_in('name', array('Politician', 'Accountant'))
118 ->get('job')
119 ->result_array();
120
121 // Check the result
122 $this->assertEquals(2, count($jobs));
123 $this->assertEquals('Politician', $jobs[0]['name']);
124 $this->assertEquals('Accountant', $jobs[1]['name']);
125 }
126
127 // ------------------------------------------------------------------------
128
129 /**
130 * @see ./mocks/schema/skeleton.php
131 */
132 public function test_where_not_in()
133 {
134 $jobs = $this->db->where_not_in('name', array('Politician', 'Accountant'))
135 ->get('job')
136 ->result_array();
137
138 // Check the result
139 $this->assertEquals(2, count($jobs));
140 $this->assertEquals('Developer', $jobs[0]['name']);
141 $this->assertEquals('Musician', $jobs[1]['name']);
142 }
143
144}