forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrderTest.php
More file actions
95 lines (79 loc) · 2.71 KB
/
Copy pathOrderTest.php
File metadata and controls
95 lines (79 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Database\Live;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use Tests\Support\Database\Seeds\CITestSeeder;
/**
* @group DatabaseLive
*
* @internal
*/
final class OrderTest extends CIUnitTestCase
{
use DatabaseTestTrait;
protected $refresh = true;
protected $seed = CITestSeeder::class;
public function testOrderAscending()
{
$jobs = $this->db->table('job')
->orderBy('name', 'asc')
->get()
->getResult();
$this->assertCount(4, $jobs);
$this->assertSame('Accountant', $jobs[0]->name);
$this->assertSame('Developer', $jobs[1]->name);
$this->assertSame('Musician', $jobs[2]->name);
$this->assertSame('Politician', $jobs[3]->name);
}
public function testOrderDescending()
{
$jobs = $this->db->table('job')
->orderBy('name', 'desc')
->get()
->getResult();
$this->assertCount(4, $jobs);
$this->assertSame('Accountant', $jobs[3]->name);
$this->assertSame('Developer', $jobs[2]->name);
$this->assertSame('Musician', $jobs[1]->name);
$this->assertSame('Politician', $jobs[0]->name);
}
public function testMultipleOrderValues()
{
$users = $this->db->table('user')
->orderBy('country', 'asc')
->orderBy('name', 'desc')
->get()
->getResult();
$this->assertCount(4, $users);
$this->assertSame('Ahmadinejad', $users[0]->name);
$this->assertSame('Chris Martin', $users[1]->name);
$this->assertSame('Richard A Causey', $users[2]->name);
$this->assertSame('Derek Jones', $users[3]->name);
}
public function testOrderRandom()
{
$sql = $this->db->table('job')
->orderBy('name', 'random')
->getCompiledSelect();
$key = 'RANDOM()';
$table = $this->db->protectIdentifiers('job', true);
if ($this->db->DBDriver === 'MySQLi') {
$key = 'RAND()';
} elseif ($this->db->DBDriver === 'SQLSRV') {
$key = 'NEWID()';
$table = '"' . $this->db->getDatabase() . '"."' . $this->db->schema . '".' . $table;
} elseif ($this->db->DBDriver === 'OCI8') {
$key = '"DBMS_RANDOM"."RANDOM"';
}
$expected = 'SELECT * FROM ' . $table . ' ORDER BY ' . $key;
$this->assertSame($expected, str_replace("\n", ' ', $sql));
}
}