@@ -45,13 +45,13 @@ public function testValidConstructor()
4545 {
4646 $ ikp = new InlineKeyboardPagination ($ this ->items , $ this ->command , $ this ->selected_page , $ this ->items_per_page );
4747
48- $ data = $ ikp ->paginate ();
48+ $ data = $ ikp ->getPagination ();
4949
5050 $ this ->assertCount ($ this ->items_per_page , $ data ['items ' ]);
5151 $ 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 ' ]);
5555 }
5656
5757 /**
@@ -60,17 +60,17 @@ public function testValidConstructor()
6060 public function testInvalidConstructor ()
6161 {
6262 $ ikp = new InlineKeyboardPagination ($ this ->items , $ this ->command , 10000 , $ this ->items_per_page );
63- $ ikp ->paginate ();
63+ $ ikp ->getPagination ();
6464 }
6565
66- public function testValidPaginate ()
66+ public function testValidPagination ()
6767 {
6868 $ ikp = new InlineKeyboardPagination ($ this ->items , $ this ->command , $ this ->selected_page , $ this ->items_per_page );
6969
7070 $ length = (int ) ceil (count ($ this ->items ) / $ this ->items_per_page );
7171
7272 for ($ i = 1 ; $ i < $ length ; $ i ++) {
73- $ ikp ->paginate ($ i );
73+ $ ikp ->getPagination ($ i );
7474 }
7575
7676 $ this ->assertTrue (true );
@@ -79,14 +79,14 @@ public function testValidPaginate()
7979 /**
8080 * @expectedException \TelegramBot\InlineKeyboardPagination\Exceptions\InlineKeyboardPaginationException
8181 */
82- public function testInvalidPaginate ()
82+ public function testInvalidPagination ()
8383 {
8484 $ ikp = new InlineKeyboardPagination ($ this ->items , $ this ->command , $ this ->selected_page , $ this ->items_per_page );
8585
8686 $ length = (int ) ceil (count ($ this ->items ) / $ this ->items_per_page ) + 1 ;
8787
8888 for ($ i = $ length ; $ i < $ length * 2 ; $ i ++) {
89- $ ikp ->paginate ($ i );
89+ $ ikp ->getPagination ($ i );
9090 }
9191 }
9292
@@ -97,7 +97,7 @@ public function testInvalidMaxButtons()
9797 {
9898 $ ikp = new InlineKeyboardPagination (range (1 , 240 ));
9999 $ ikp ->setMaxButtons (2 );
100- $ ikp ->paginate ();
100+ $ ikp ->getPagination ();
101101 }
102102
103103 /**
@@ -107,16 +107,118 @@ public function testInvalidSelectedPage()
107107 {
108108 $ ikp = new InlineKeyboardPagination (range (1 , 240 ));
109109 $ ikp ->setSelectedPage (-5 );
110- $ ikp ->paginate ();
110+ $ ikp ->getPagination ();
111111 }
112112
113- /**
114- * @expectedException \TelegramBot\InlineKeyboardPagination\Exceptions\InlineKeyboardPaginationException
115- */
116- public function testInvalidWrapSelectedButton ()
113+
114+ public function testButtonLabels ()
117115 {
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 );
121223 }
122224}
0 commit comments