|
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\QueryBuilder\QueryBuilderException; |
|
7
|
|
|
use Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\Traits\BindTrait; |
|
8
|
|
|
use Janisbiz\LightOrm\Dms\MySQL\QueryBuilder\Traits\HavingTrait; |
|
9
|
|
|
|
|
10
|
|
|
class HavingTraitTest extends AbstractTraitTestCase |
|
11
|
|
|
{ |
|
12
|
|
|
const HAVING_CONDITION_DEFAULT = [ |
|
13
|
|
|
'column1 = :value1Bind', |
|
14
|
|
|
]; |
|
15
|
|
|
const HAVING_CONDITION_BIND_DEFAULT = [ |
|
16
|
|
|
'value1Bind' => 'value1', |
|
17
|
|
|
]; |
|
18
|
|
|
const HAVING_CONDITION_EMPTY = ''; |
|
19
|
|
|
const HAVING_CONDITION = 'column2 = :value2Bind'; |
|
20
|
|
|
const HAVING_CONDITION_BIND = [ |
|
21
|
|
|
'value2Bind' => 'value2', |
|
22
|
|
|
]; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var HavingTrait|BindTrait |
|
26
|
|
|
*/ |
|
27
|
|
|
private $havingTraitClass; |
|
28
|
|
|
|
|
29
|
|
|
public function setUp() |
|
30
|
|
|
{ |
|
31
|
|
|
$this->havingTraitClass = new class ( |
|
32
|
|
|
HavingTraitTest::HAVING_CONDITION_BIND_DEFAULT, |
|
33
|
|
|
HavingTraitTest::HAVING_CONDITION_DEFAULT |
|
34
|
|
|
) { |
|
35
|
|
|
use BindTrait; |
|
36
|
|
|
use HavingTrait; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param array $bindDefaultData |
|
40
|
|
|
* @param array $havingDefaultData |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(array $bindDefaultData, array $havingDefaultData) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->bind = $bindDefaultData; |
|
45
|
|
|
$this->having = $havingDefaultData; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
|
|
public function havingData(): array |
|
52
|
|
|
{ |
|
53
|
|
|
return $this->having; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function clearHavingData() |
|
57
|
|
|
{ |
|
58
|
|
|
$this->having = []; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @return null|string |
|
63
|
|
|
*/ |
|
64
|
|
|
public function buildHavingQueryPartPublic(): ?string |
|
65
|
|
|
{ |
|
66
|
|
|
return $this->buildHavingQueryPart(); |
|
67
|
|
|
} |
|
68
|
|
|
}; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function testHaving() |
|
72
|
|
|
{ |
|
73
|
|
|
$object = $this->havingTraitClass->having(static::HAVING_CONDITION, static::HAVING_CONDITION_BIND); |
|
|
|
|
|
|
74
|
|
|
$this->assertObjectUsesTrait(BindTrait::class, $object); |
|
75
|
|
|
$this->assertObjectUsesTrait(HavingTrait::class, $object); |
|
76
|
|
|
$this->assertEquals( |
|
77
|
|
|
\array_merge(static::HAVING_CONDITION_DEFAULT, [static::HAVING_CONDITION]), |
|
78
|
|
|
$this->havingTraitClass->havingData() |
|
|
|
|
|
|
79
|
|
|
); |
|
80
|
|
|
$this->assertEquals( |
|
81
|
|
|
\array_merge(static::HAVING_CONDITION_BIND_DEFAULT, static::HAVING_CONDITION_BIND), |
|
82
|
|
|
$this->havingTraitClass->bindData() |
|
|
|
|
|
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function testHavingWithEmptyCondition() |
|
87
|
|
|
{ |
|
88
|
|
|
$this->expectException(QueryBuilderException::class); |
|
89
|
|
|
$this->expectExceptionMessage('You must pass $condition to having function!'); |
|
90
|
|
|
|
|
91
|
|
|
$this->havingTraitClass->having(static::HAVING_CONDITION_EMPTY); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function testBuildHavingQueryPart() |
|
95
|
|
|
{ |
|
96
|
|
|
$this->havingTraitClass->having(static::HAVING_CONDITION, static::HAVING_CONDITION_BIND); |
|
97
|
|
|
|
|
98
|
|
|
$this->assertEquals( |
|
99
|
|
|
\sprintf( |
|
100
|
|
|
'%s %s', |
|
101
|
|
|
ConditionEnum::HAVING, |
|
102
|
|
|
\implode(' AND ', \array_unique($this->havingTraitClass->havingData())) |
|
103
|
|
|
), |
|
104
|
|
|
$this->havingTraitClass->buildHavingQueryPartPublic() |
|
|
|
|
|
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function testBuildHavingQueryPartWhenEmpty() |
|
109
|
|
|
{ |
|
110
|
|
|
$this->havingTraitClass->clearHavingData(); |
|
|
|
|
|
|
111
|
|
|
|
|
112
|
|
|
$this->assertEquals(null, $this->havingTraitClass->buildHavingQueryPartPublic()); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|