Taufan Aditya | 12f5475 | 2012-05-04 15:46:39 +0700 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | class 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 Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 25 | $job1 = $this->db->where('id', 1)->get('job')->row(); |
Taufan Aditya | 12f5475 | 2012-05-04 15:46:39 +0700 | [diff] [blame] | 26 | |
Taufan Aditya | 12f5475 | 2012-05-04 15:46:39 +0700 | [diff] [blame] | 27 | $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 Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 38 | $jobs = $this->db->where('id !=', 1)->get('job')->result_array(); |
Andrey Andreev | 20d9b0a | 2017-12-20 19:57:39 +0200 | [diff] [blame] | 39 | $this->assertCount(3, $jobs); |
Taufan Aditya | 12f5475 | 2012-05-04 15:46:39 +0700 | [diff] [blame] | 40 | } |
| 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 Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 50 | $jobs = $this->db->where($where)->get('job')->result_array(); |
Taufan Aditya | 12f5475 | 2012-05-04 15:46:39 +0700 | [diff] [blame] | 51 | |
Andrey Andreev | 20d9b0a | 2017-12-20 19:57:39 +0200 | [diff] [blame] | 52 | $this->assertCount(1, $jobs); |
Taufan Aditya | 12f5475 | 2012-05-04 15:46:39 +0700 | [diff] [blame] | 53 | |
| 54 | // Should be Musician |
| 55 | $job = current($jobs); |
Taufan Aditya | 12f5475 | 2012-05-04 15:46:39 +0700 | [diff] [blame] | 56 | $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 Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 67 | $jobs = $this->db->where($where)->get('job')->result_array(); |
Taufan Aditya | 12f5475 | 2012-05-04 15:46:39 +0700 | [diff] [blame] | 68 | |
Andrey Andreev | 20d9b0a | 2017-12-20 19:57:39 +0200 | [diff] [blame] | 69 | $this->assertCount(1, $jobs); |
Taufan Aditya | 12f5475 | 2012-05-04 15:46:39 +0700 | [diff] [blame] | 70 | |
| 71 | // Should be Musician |
| 72 | $job = current($jobs); |
Taufan Aditya | 12f5475 | 2012-05-04 15:46:39 +0700 | [diff] [blame] | 73 | $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 Andreev | 20d9b0a | 2017-12-20 19:57:39 +0200 | [diff] [blame] | 88 | $this->assertCount(3, $jobs); |
Taufan Aditya | 12f5475 | 2012-05-04 15:46:39 +0700 | [diff] [blame] | 89 | $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 Andreev | 20d9b0a | 2017-12-20 19:57:39 +0200 | [diff] [blame] | 105 | $this->assertCount(2, $jobs); |
Taufan Aditya | 12f5475 | 2012-05-04 15:46:39 +0700 | [diff] [blame] | 106 | $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 Andreev | 20d9b0a | 2017-12-20 19:57:39 +0200 | [diff] [blame] | 121 | $this->assertCount(2, $jobs); |
Taufan Aditya | 12f5475 | 2012-05-04 15:46:39 +0700 | [diff] [blame] | 122 | $this->assertEquals('Developer', $jobs[0]['name']); |
| 123 | $this->assertEquals('Musician', $jobs[1]['name']); |
| 124 | } |
Andrey Andreev | c186288 | 2012-06-09 23:16:58 +0300 | [diff] [blame] | 125 | |
Andrey Andreev | 554b452 | 2015-09-01 13:51:26 +0300 | [diff] [blame] | 126 | // ------------------------------------------------------------------------ |
| 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 Andreev | 20d9b0a | 2017-12-20 19:57:39 +0200 | [diff] [blame] | 134 | } |