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\LimitOffsetTrait; |
8
|
|
|
|
9
|
|
|
class LimitOffsetTraitTest extends AbstractTraitTestCase |
10
|
|
|
{ |
11
|
|
|
const LIMIT_INVALID = -1; |
12
|
|
|
const LIMIT_DEFAULT = 1; |
13
|
|
|
const LIMIT = 2; |
14
|
|
|
const OFFSET_INVALID = -1; |
15
|
|
|
const OFFSET = 1; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var LimitOffsetTrait |
19
|
|
|
*/ |
20
|
|
|
private $limitOffsetTraitClass; |
21
|
|
|
|
22
|
|
|
public function setUp() |
23
|
|
|
{ |
24
|
|
|
$this->limitOffsetTraitClass = new class () |
25
|
|
|
{ |
26
|
|
|
use LimitOffsetTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @return null|int |
30
|
|
|
*/ |
31
|
|
|
public function limitData(): ?int |
32
|
|
|
{ |
33
|
|
|
return $this->limit; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return null|int |
38
|
|
|
*/ |
39
|
|
|
public function offsetData(): ?int |
40
|
|
|
{ |
41
|
|
|
return $this->offset; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return null|string |
46
|
|
|
*/ |
47
|
|
|
public function buildLimitQueryPartPublic(): ?string |
48
|
|
|
{ |
49
|
|
|
return $this->buildLimitQueryPart(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return null|string |
54
|
|
|
*/ |
55
|
|
|
public function buildOffsetQueryPartPublic(): ?string |
56
|
|
|
{ |
57
|
|
|
return $this->buildOffsetQueryPart(); |
58
|
|
|
} |
59
|
|
|
}; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function testOffset() |
63
|
|
|
{ |
64
|
|
|
$object = $this->limitOffsetTraitClass->limit(static::LIMIT)->offset(static::OFFSET); |
65
|
|
|
$this->assertObjectUsesTrait(LimitOffsetTrait::class, $object); |
66
|
|
|
$this->assertEquals(static::OFFSET, $this->limitOffsetTraitClass->offsetData()); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testOffsetInvalid() |
70
|
|
|
{ |
71
|
|
|
$this->expectException(QueryBuilderException::class); |
72
|
|
|
$this->expectExceptionMessage('You must pass $offset to offset method!'); |
73
|
|
|
|
74
|
|
|
$this->limitOffsetTraitClass->offset(static::OFFSET_INVALID); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function testOffsetWithEmptyLimit() |
78
|
|
|
{ |
79
|
|
|
$this->expectException(QueryBuilderException::class); |
80
|
|
|
$this->expectExceptionMessage('You must set LIMIT before calling offset method!'); |
81
|
|
|
|
82
|
|
|
$this->limitOffsetTraitClass->offset(static::OFFSET); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testLimitWithOffset() |
86
|
|
|
{ |
87
|
|
|
$object = $this->limitOffsetTraitClass->limitWithOffset(static::LIMIT, static::OFFSET); |
88
|
|
|
$this->assertObjectUsesTrait(LimitOffsetTrait::class, $object); |
89
|
|
|
$this->assertEquals(static::LIMIT, $this->limitOffsetTraitClass->limitData()); |
|
|
|
|
90
|
|
|
$this->assertEquals(static::OFFSET, $this->limitOffsetTraitClass->offsetData()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testLimitWithOffsetWithEmptyLimit() |
94
|
|
|
{ |
95
|
|
|
$this->expectException(QueryBuilderException::class); |
96
|
|
|
$this->expectExceptionMessage('You must pass $limit to limit method!'); |
97
|
|
|
|
98
|
|
|
$this->limitOffsetTraitClass->limitWithOffset(static::LIMIT_INVALID, static::OFFSET); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testLimitWithOffsetWithEmptyOffset() |
102
|
|
|
{ |
103
|
|
|
$this->expectException(QueryBuilderException::class); |
104
|
|
|
$this->expectExceptionMessage('You must pass $offset to offset method!'); |
105
|
|
|
|
106
|
|
|
$this->limitOffsetTraitClass->limitWithOffset(static::LIMIT, static::OFFSET_INVALID); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testLimit() |
110
|
|
|
{ |
111
|
|
|
$object = $this->limitOffsetTraitClass->limit(static::LIMIT); |
112
|
|
|
$this->assertObjectUsesTrait(LimitOffsetTrait::class, $object); |
113
|
|
|
$this->assertEquals(static::LIMIT, $this->limitOffsetTraitClass->limitData()); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function testLimitEmpty() |
117
|
|
|
{ |
118
|
|
|
$this->expectException(QueryBuilderException::class); |
119
|
|
|
$this->expectExceptionMessage('You must pass $limit to limit method!'); |
120
|
|
|
|
121
|
|
|
$this->limitOffsetTraitClass->limit(static::LIMIT_INVALID); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function testBuildLimitQueryPart() |
125
|
|
|
{ |
126
|
|
|
$this->limitOffsetTraitClass->limit(static::LIMIT); |
127
|
|
|
|
128
|
|
|
$this->assertEquals( |
129
|
|
|
\sprintf('%s %d', ConditionEnum::LIMIT, $this->limitOffsetTraitClass->limitData()), |
130
|
|
|
$this->limitOffsetTraitClass->buildLimitQueryPartPublic() |
|
|
|
|
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function testBuildLimitQueryPartWhenEmpty() |
135
|
|
|
{ |
136
|
|
|
$this->assertEquals(null, $this->limitOffsetTraitClass->buildLimitQueryPartPublic()); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
public function testBuildOffsetQueryPart() |
140
|
|
|
{ |
141
|
|
|
$this->limitOffsetTraitClass->limitWithOffset(static::LIMIT, static::OFFSET); |
142
|
|
|
|
143
|
|
|
$this->assertEquals( |
144
|
|
|
\sprintf('%s %d', ConditionEnum::OFFSET, $this->limitOffsetTraitClass->offsetData()), |
145
|
|
|
$this->limitOffsetTraitClass->buildOffsetQueryPartPublic() |
|
|
|
|
146
|
|
|
); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
public function testBuildOffsetQueryPartWhenEmpty() |
150
|
|
|
{ |
151
|
|
|
$this->assertEquals(null, $this->limitOffsetTraitClass->buildOffsetQueryPartPublic()); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.