blob: 8a7a09ddbb04a868e4b9b4a3c3a800e4b113964e [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 {
Andrey Andreevc1862882012-06-09 23:16:58 +030025 $job1 = $this->db->where('id', 1)->get('job')->row();
Taufan Aditya12f54752012-05-04 15:46:39 +070026
Taufan Aditya12f54752012-05-04 15:46:39 +070027 $this->assertEquals('1', $job1->id);
28 $this->assertEquals('Developer', $job1->name);
29 }
30
31 // ------------------------------------------------------------------------
32
33 /**
34 * @see ./mocks/schema/skeleton.php
35 */
36 public function test_where_custom_key_value()
37 {
Andrey Andreevc1862882012-06-09 23:16:58 +030038 $jobs = $this->db->where('id !=', 1)->get('job')->result_array();
Andrey Andreev20d9b0a2017-12-20 19:57:39 +020039 $this->assertCount(3, $jobs);
Taufan Aditya12f54752012-05-04 15:46:39 +070040 }
41
42 // ------------------------------------------------------------------------
43
44 /**
45 * @see ./mocks/schema/skeleton.php
46 */
47 public function test_where_associative_array()
48 {
49 $where = array('id >' => 2, 'name !=' => 'Accountant');
Andrey Andreevc1862882012-06-09 23:16:58 +030050 $jobs = $this->db->where($where)->get('job')->result_array();
Taufan Aditya12f54752012-05-04 15:46:39 +070051
Andrey Andreev20d9b0a2017-12-20 19:57:39 +020052 $this->assertCount(1, $jobs);
Taufan Aditya12f54752012-05-04 15:46:39 +070053
54 // Should be Musician
55 $job = current($jobs);
Taufan Aditya12f54752012-05-04 15:46:39 +070056 $this->assertEquals('Musician', $job['name']);
57 }
58
59 // ------------------------------------------------------------------------
60
61 /**
62 * @see ./mocks/schema/skeleton.php
63 */
64 public function test_where_custom_string()
65 {
66 $where = "id > 2 AND name != 'Accountant'";
Andrey Andreevc1862882012-06-09 23:16:58 +030067 $jobs = $this->db->where($where)->get('job')->result_array();
Taufan Aditya12f54752012-05-04 15:46:39 +070068
Andrey Andreev20d9b0a2017-12-20 19:57:39 +020069 $this->assertCount(1, $jobs);
Taufan Aditya12f54752012-05-04 15:46:39 +070070
71 // Should be Musician
72 $job = current($jobs);
Taufan Aditya12f54752012-05-04 15:46:39 +070073 $this->assertEquals('Musician', $job['name']);
74 }
75
76 // ------------------------------------------------------------------------
77
78 /**
79 * @see ./mocks/schema/skeleton.php
80 */
81 public function test_where_or()
82 {
83 $jobs = $this->db->where('name !=', 'Accountant')
84 ->or_where('id >', 3)
85 ->get('job')
86 ->result_array();
87
Andrey Andreev20d9b0a2017-12-20 19:57:39 +020088 $this->assertCount(3, $jobs);
Taufan Aditya12f54752012-05-04 15:46:39 +070089 $this->assertEquals('Developer', $jobs[0]['name']);
90 $this->assertEquals('Politician', $jobs[1]['name']);
91 $this->assertEquals('Musician', $jobs[2]['name']);
92 }
93
94 // ------------------------------------------------------------------------
95
96 /**
97 * @see ./mocks/schema/skeleton.php
98 */
99 public function test_where_in()
100 {
101 $jobs = $this->db->where_in('name', array('Politician', 'Accountant'))
102 ->get('job')
103 ->result_array();
104
Andrey Andreev20d9b0a2017-12-20 19:57:39 +0200105 $this->assertCount(2, $jobs);
Taufan Aditya12f54752012-05-04 15:46:39 +0700106 $this->assertEquals('Politician', $jobs[0]['name']);
107 $this->assertEquals('Accountant', $jobs[1]['name']);
108 }
109
110 // ------------------------------------------------------------------------
111
112 /**
113 * @see ./mocks/schema/skeleton.php
114 */
115 public function test_where_not_in()
116 {
117 $jobs = $this->db->where_not_in('name', array('Politician', 'Accountant'))
118 ->get('job')
119 ->result_array();
120
Andrey Andreev20d9b0a2017-12-20 19:57:39 +0200121 $this->assertCount(2, $jobs);
Taufan Aditya12f54752012-05-04 15:46:39 +0700122 $this->assertEquals('Developer', $jobs[0]['name']);
123 $this->assertEquals('Musician', $jobs[1]['name']);
124 }
Andrey Andreevc1862882012-06-09 23:16:58 +0300125
Andrey Andreev554b4522015-09-01 13:51:26 +0300126 // ------------------------------------------------------------------------
127
128 public function test_issue4093()
129 {
130 $input = 'bar and baz or qux';
131 $sql = $this->db->where('foo', $input)->get_compiled_select('dummy');
132 $this->assertEquals("'".$input."'", substr($sql, -20));
133 }
Andrey Andreev20d9b0a2017-12-20 19:57:39 +0200134}