@@ -45,13 +45,13 @@ public function testValidConstructor()
45
45
{
46
46
$ ikp = new InlineKeyboardPagination ($ this ->items , $ this ->command , $ this ->selected_page , $ this ->items_per_page );
47
47
48
- $ data = $ ikp ->paginate ();
48
+ $ data = $ ikp ->getPagination ();
49
49
50
50
$ this ->assertCount ($ this ->items_per_page , $ data ['items ' ]);
51
51
$ this ->assertArrayHasKey ('keyboard ' , $ data );
52
- $ this ->assertArrayHasKey (0 , $ data ['keyboard ' ]);
53
- $ this ->assertArrayHasKey ('text ' , $ data ['keyboard ' ][0 ]);
54
- $ this ->assertStringStartsWith ($ this ->command , $ data ['keyboard ' ][0 ]['callback_data ' ]);
52
+ $ this ->assertArrayHasKey (1 , $ data ['keyboard ' ][ 0 ]);
53
+ $ this ->assertArrayHasKey ('text ' , $ data ['keyboard ' ][0 ][ 1 ] );
54
+ $ this ->assertStringStartsWith ($ this ->command , $ data ['keyboard ' ][0 ][1 ][ 'callback_data ' ]);
55
55
}
56
56
57
57
/**
@@ -60,17 +60,17 @@ public function testValidConstructor()
60
60
public function testInvalidConstructor ()
61
61
{
62
62
$ ikp = new InlineKeyboardPagination ($ this ->items , $ this ->command , 10000 , $ this ->items_per_page );
63
- $ ikp ->paginate ();
63
+ $ ikp ->getPagination ();
64
64
}
65
65
66
- public function testValidPaginate ()
66
+ public function testValidPagination ()
67
67
{
68
68
$ ikp = new InlineKeyboardPagination ($ this ->items , $ this ->command , $ this ->selected_page , $ this ->items_per_page );
69
69
70
70
$ length = (int ) ceil (count ($ this ->items ) / $ this ->items_per_page );
71
71
72
72
for ($ i = 1 ; $ i < $ length ; $ i ++) {
73
- $ ikp ->paginate ($ i );
73
+ $ ikp ->getPagination ($ i );
74
74
}
75
75
76
76
$ this ->assertTrue (true );
@@ -79,14 +79,14 @@ public function testValidPaginate()
79
79
/**
80
80
* @expectedException \TelegramBot\InlineKeyboardPagination\Exceptions\InlineKeyboardPaginationException
81
81
*/
82
- public function testInvalidPaginate ()
82
+ public function testInvalidPagination ()
83
83
{
84
84
$ ikp = new InlineKeyboardPagination ($ this ->items , $ this ->command , $ this ->selected_page , $ this ->items_per_page );
85
85
86
86
$ length = (int ) ceil (count ($ this ->items ) / $ this ->items_per_page ) + 1 ;
87
87
88
88
for ($ i = $ length ; $ i < $ length * 2 ; $ i ++) {
89
- $ ikp ->paginate ($ i );
89
+ $ ikp ->getPagination ($ i );
90
90
}
91
91
}
92
92
@@ -97,7 +97,7 @@ public function testInvalidMaxButtons()
97
97
{
98
98
$ ikp = new InlineKeyboardPagination (range (1 , 240 ));
99
99
$ ikp ->setMaxButtons (2 );
100
- $ ikp ->paginate ();
100
+ $ ikp ->getPagination ();
101
101
}
102
102
103
103
/**
@@ -107,16 +107,118 @@ public function testInvalidSelectedPage()
107
107
{
108
108
$ ikp = new InlineKeyboardPagination (range (1 , 240 ));
109
109
$ ikp ->setSelectedPage (-5 );
110
- $ ikp ->paginate ();
110
+ $ ikp ->getPagination ();
111
111
}
112
112
113
- /**
114
- * @expectedException \TelegramBot\InlineKeyboardPagination\Exceptions\InlineKeyboardPaginationException
115
- */
116
- public function testInvalidWrapSelectedButton ()
113
+
114
+ public function testButtonLabels ()
117
115
{
118
- $ ikp = new InlineKeyboardPagination (range (1 , 240 ));
119
- $ ikp ->setWrapSelectedButton ('$sdlfk$ ' );
120
- $ ikp ->paginate ();
116
+ $ cbdata = '%s?currentPage=%d&nextPage=%d ' ;
117
+ $ command = 'cbdata ' ;
118
+ $ ikp1 = new InlineKeyboardPagination (range (1 , 1 ), $ command );
119
+ $ ikp10 = new InlineKeyboardPagination (range (1 , 50 ), $ command );
120
+
121
+ // current
122
+ $ keyboard = $ ikp1 ->getPagination (1 )['keyboard ' ];
123
+ self ::assertAllButtonPropertiesEqual ([
124
+ ['· 1 · ' ],
125
+ ], 'text ' , $ keyboard );
126
+ self ::assertAllButtonPropertiesEqual ([
127
+ [
128
+ sprintf ($ cbdata , $ command , 1 , 1 ),
129
+ ],
130
+ ], 'callback_data ' , $ keyboard );
131
+
132
+ // first, previous, current, next, last
133
+ $ keyboard = $ ikp10 ->getPagination (5 )['keyboard ' ];
134
+ self ::assertAllButtonPropertiesEqual ([
135
+ ['« 1 ' , '‹ 4 ' , '· 5 · ' , '6 › ' , '10 » ' ],
136
+ ], 'text ' , $ keyboard );
137
+ self ::assertAllButtonPropertiesEqual ([
138
+ [
139
+ sprintf ($ cbdata , $ command , 5 , 1 ),
140
+ sprintf ($ cbdata , $ command , 5 , 4 ),
141
+ sprintf ($ cbdata , $ command , 5 , 5 ),
142
+ sprintf ($ cbdata , $ command , 5 , 6 ),
143
+ sprintf ($ cbdata , $ command , 5 , 10 ),
144
+ ],
145
+ ], 'callback_data ' , $ keyboard );
146
+
147
+ // first, previous, current, last
148
+ $ keyboard = $ ikp10 ->getPagination (9 )['keyboard ' ];
149
+ self ::assertAllButtonPropertiesEqual ([
150
+ ['« 1 ' , '‹ 7 ' , '8 ' , '· 9 · ' , '10 ' ],
151
+ ], 'text ' , $ keyboard );
152
+ self ::assertAllButtonPropertiesEqual ([
153
+ [
154
+ sprintf ($ cbdata , $ command , 9 , 1 ),
155
+ sprintf ($ cbdata , $ command , 9 , 7 ),
156
+ sprintf ($ cbdata , $ command , 9 , 8 ),
157
+ sprintf ($ cbdata , $ command , 9 , 9 ),
158
+ sprintf ($ cbdata , $ command , 9 , 10 ),
159
+ ],
160
+ ], 'callback_data ' , $ keyboard );
161
+
162
+ // first, previous, current
163
+ $ keyboard = $ ikp10 ->getPagination (10 )['keyboard ' ];
164
+ self ::assertAllButtonPropertiesEqual ([
165
+ ['« 1 ' , '‹ 7 ' , '8 ' , '9 ' , '· 10 · ' ],
166
+ ], 'text ' , $ keyboard );
167
+ self ::assertAllButtonPropertiesEqual ([
168
+ [
169
+ sprintf ($ cbdata , $ command , 10 , 1 ),
170
+ sprintf ($ cbdata , $ command , 10 , 7 ),
171
+ sprintf ($ cbdata , $ command , 10 , 8 ),
172
+ sprintf ($ cbdata , $ command , 10 , 9 ),
173
+ sprintf ($ cbdata , $ command , 10 , 10 ),
174
+ ],
175
+ ], 'callback_data ' , $ keyboard );
176
+
177
+ // custom labels, skipping some buttons
178
+ // first, previous, current, next, last
179
+ $ ikp10 ->setLabels ([
180
+ 'first ' => '' ,
181
+ 'previous ' => 'previous %d ' ,
182
+ 'current ' => null ,
183
+ 'next ' => '%d next ' ,
184
+ 'last ' => 'last ' ,
185
+ ]);
186
+ $ keyboard = $ ikp10 ->getPagination (5 )['keyboard ' ];
187
+ self ::assertAllButtonPropertiesEqual ([
188
+ ['previous 4 ' , '6 next ' , 'last ' ],
189
+ ], 'text ' , $ keyboard );
190
+ self ::assertAllButtonPropertiesEqual ([
191
+ [
192
+ sprintf ($ cbdata , $ command , 5 , 4 ),
193
+ sprintf ($ cbdata , $ command , 5 , 6 ),
194
+ sprintf ($ cbdata , $ command , 5 , 10 ),
195
+ ],
196
+ ], 'callback_data ' , $ keyboard );
197
+ }
198
+
199
+ public static function assertButtonPropertiesEqual ($ value , $ property , $ keyboard , $ row , $ column , $ message = '' )
200
+ {
201
+ $ row_raw = array_values ($ keyboard )[$ row ];
202
+ $ column_raw = array_values ($ row_raw )[$ column ];
203
+
204
+ self ::assertSame ($ value , $ column_raw [$ property ], $ message );
205
+ }
206
+
207
+ public static function assertRowButtonPropertiesEqual (array $ values , $ property , $ keyboard , $ row , $ message = '' )
208
+ {
209
+ $ column = 0 ;
210
+ foreach ($ values as $ value ) {
211
+ self ::assertButtonPropertiesEqual ($ value , $ property , $ keyboard , $ row , $ column ++, $ message );
212
+ }
213
+ self ::assertCount (count (array_values ($ keyboard )[$ row ]), $ values );
214
+ }
215
+
216
+ public static function assertAllButtonPropertiesEqual (array $ all_values , $ property , $ keyboard , $ message = '' )
217
+ {
218
+ $ row = 0 ;
219
+ foreach ($ all_values as $ values ) {
220
+ self ::assertRowButtonPropertiesEqual ($ values , $ property , $ keyboard , $ row ++, $ message );
221
+ }
222
+ self ::assertCount (count ($ keyboard ), $ all_values );
121
223
}
122
224
}
0 commit comments