1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Janisbiz\LightOrm\Tests\Unit\Dms\MySQL\QueryBuilder\Traits; |
4
|
|
|
|
5
|
|
|
use Janisbiz\LightOrm\Dms\MySQL\Enum\ConditionEnum; |
6
|
|
|
use Janisbiz\LightOrm\Dms\MySQL\Enum\KeywordEnum; |
7
|
|
|
use Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\QueryBuilderException; |
8
|
|
|
use Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\Traits\OrderByTrait; |
9
|
|
|
|
10
|
|
|
class OrderByTraitTest extends AbstractTraitTestCase |
11
|
|
|
{ |
12
|
|
|
const ORDER_BY_DEFAULT = [ |
13
|
|
|
'col1 DESC', |
14
|
|
|
'col2 ASC', |
15
|
|
|
]; |
16
|
|
|
const ORDER_BY_COLUMN = 'col3'; |
17
|
|
|
const ORDER_BY_COLUMNS = [ |
18
|
|
|
'col4', |
19
|
|
|
'col5', |
20
|
|
|
]; |
21
|
|
|
const ORDER_BY_EMPTY = null; |
22
|
|
|
const ORDER_BY_INVALID_KEYWORD = 'INVALID'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var OrderByTrait |
26
|
|
|
*/ |
27
|
|
|
private $orderByTraitClass; |
28
|
|
|
|
29
|
|
|
public function setUp() |
30
|
|
|
{ |
31
|
|
|
$this->orderBy = static::ORDER_BY_DEFAULT; |
|
|
|
|
32
|
|
|
$this->orderByTraitClass = new class (OrderByTraitTest::ORDER_BY_DEFAULT) |
33
|
|
|
{ |
34
|
|
|
use OrderByTrait; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param array $orderByDataDefault |
38
|
|
|
*/ |
39
|
|
|
public function __construct(array $orderByDataDefault) |
40
|
|
|
{ |
41
|
|
|
$this->orderBy = $orderByDataDefault; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
|
|
public function orderByData(): array |
48
|
|
|
{ |
49
|
|
|
return $this->orderBy; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function clearOrderByData() |
53
|
|
|
{ |
54
|
|
|
$this->orderBy = []; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return null|string |
59
|
|
|
*/ |
60
|
|
|
public function buildOrderByQueryPartPublic(): ?string |
61
|
|
|
{ |
62
|
|
|
return $this->buildOrderByQueryPart(); |
63
|
|
|
} |
64
|
|
|
}; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @dataProvider orderByData |
69
|
|
|
* |
70
|
|
|
* @param array|string $columns |
71
|
|
|
* @param string $keyword |
72
|
|
|
*/ |
73
|
|
|
public function testOrderBy($columns, $keyword) |
74
|
|
|
{ |
75
|
|
|
$object = $this->orderByTraitClass->orderBy($columns, $keyword); |
76
|
|
|
$this->assertObjectUsesTrait(OrderByTrait::class, $object); |
77
|
|
|
$this->assertEquals( |
78
|
|
|
\array_merge( |
79
|
|
|
static::ORDER_BY_DEFAULT, |
80
|
|
|
\array_map( |
81
|
|
|
function ($column) use ($keyword) { |
82
|
|
|
return \sprintf('%s %s', $column, $keyword); |
83
|
|
|
}, |
84
|
|
|
\is_array($columns) ? $columns : [$columns] |
85
|
|
|
) |
86
|
|
|
), |
87
|
|
|
$this->orderByTraitClass->orderByData() |
|
|
|
|
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @dataProvider orderByData |
93
|
|
|
* |
94
|
|
|
* @param array|string $columns |
95
|
|
|
* @param string $keyword |
96
|
|
|
*/ |
97
|
|
|
public function testBuildOrderByQueryPart($columns, $keyword) |
98
|
|
|
{ |
99
|
|
|
$this->orderByTraitClass->orderBy($columns, $keyword); |
100
|
|
|
|
101
|
|
|
$this->assertEquals( |
102
|
|
|
\sprintf('%s %s', ConditionEnum::ORDER_BY, \implode(', ', $this->orderByTraitClass->orderByData())), |
103
|
|
|
$this->orderByTraitClass->buildOrderByQueryPartPublic() |
|
|
|
|
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function testBuildOrderByQueryPartWhenEmpty() |
108
|
|
|
{ |
109
|
|
|
$this->orderByTraitClass->clearOrderByData(); |
|
|
|
|
110
|
|
|
|
111
|
|
|
$this->assertEquals(null, $this->orderByTraitClass->buildOrderByQueryPartPublic()); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
|
|
public function orderByData() |
119
|
|
|
{ |
120
|
|
|
return [ |
121
|
|
|
[ |
122
|
|
|
static::ORDER_BY_COLUMN, |
123
|
|
|
KeywordEnum::ASC, |
124
|
|
|
], |
125
|
|
|
[ |
126
|
|
|
static::ORDER_BY_COLUMN, |
127
|
|
|
KeywordEnum::DESC, |
128
|
|
|
], |
129
|
|
|
[ |
130
|
|
|
static::ORDER_BY_COLUMNS, |
131
|
|
|
KeywordEnum::ASC, |
132
|
|
|
], |
133
|
|
|
[ |
134
|
|
|
static::ORDER_BY_COLUMNS, |
135
|
|
|
KeywordEnum::DESC, |
136
|
|
|
], |
137
|
|
|
]; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function testOrderByWithEmptyOrderBy() |
141
|
|
|
{ |
142
|
|
|
$this->expectException(QueryBuilderException::class); |
143
|
|
|
$this->expectExceptionMessage('You must pass $orderBy to orderBy method!'); |
144
|
|
|
|
145
|
|
|
$this->orderByTraitClass->orderBy(static::ORDER_BY_EMPTY); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function testOrderByWithInvalidKeyword() |
149
|
|
|
{ |
150
|
|
|
$this->expectException(QueryBuilderException::class); |
151
|
|
|
$this->expectExceptionMessage('Invalid $keyword "INVALID" for orderBy!'); |
152
|
|
|
|
153
|
|
|
$this->orderByTraitClass->orderBy(static::ORDER_BY_COLUMN, static::ORDER_BY_INVALID_KEYWORD); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|