@@ -289,4 +289,70 @@ public function test_null_values_are_handled() {
289289 ];
290290 $ this ->assertSame ( $ expected , $ out , 'Null values should be safely converted to empty strings in table output. ' );
291291 }
292+
293+ public function test_default_alignment () {
294+ $ table = new cli \Table ();
295+ $ table ->setHeaders ( array ( 'Header1 ' , 'Header2 ' ) );
296+ $ table ->addRow ( array ( 'Row1Col1 ' , 'Row1Col2 ' ) );
297+
298+ $ out = $ table ->getDisplayLines ();
299+
300+ // By default, columns should be left-aligned.
301+ $ this ->assertStringContainsString ( '| Header1 | Header2 | ' , $ out [1 ] );
302+ $ this ->assertStringContainsString ( '| Row1Col1 | Row1Col2 | ' , $ out [3 ] );
303+ }
304+
305+ public function test_right_alignment () {
306+ $ table = new cli \Table ();
307+ $ table ->setHeaders ( array ( 'Header1 ' , 'Header2 ' ) );
308+ $ table ->setAlignments ( array ( 'Header1 ' => \cli \table \Column::ALIGN_RIGHT , 'Header2 ' => \cli \table \Column::ALIGN_RIGHT ) );
309+ $ table ->addRow ( array ( 'R1C1 ' , 'R1C2 ' ) );
310+
311+ $ out = $ table ->getDisplayLines ();
312+
313+ $ this ->assertStringContainsString ( '| Header1 | Header2 | ' , $ out [1 ] );
314+ $ this ->assertStringContainsString ( '| R1C1 | R1C2 | ' , $ out [3 ] );
315+ }
316+
317+ public function test_center_alignment () {
318+ $ table = new cli \Table ();
319+ $ table ->setHeaders ( array ( 'Header1 ' , 'Header2 ' ) );
320+ $ table ->setAlignments ( array ( 'Header1 ' => \cli \table \Column::ALIGN_CENTER , 'Header2 ' => \cli \table \Column::ALIGN_CENTER ) );
321+ $ table ->addRow ( array ( 'R1C1 ' , 'R1C2 ' ) );
322+
323+ $ out = $ table ->getDisplayLines ();
324+
325+ $ this ->assertStringContainsString ( '| Header1 | Header2 | ' , $ out [1 ] );
326+ $ this ->assertStringContainsString ( '| R1C1 | R1C2 | ' , $ out [3 ] );
327+ }
328+
329+ public function test_mixed_alignments () {
330+ $ table = new cli \Table ();
331+ $ table ->setHeaders ( array ( 'Left ' , 'Right ' , 'Center ' ) );
332+ $ table ->setAlignments ( array (
333+ 'Left ' => \cli \table \Column::ALIGN_LEFT ,
334+ 'Right ' => \cli \table \Column::ALIGN_RIGHT ,
335+ 'Center ' => \cli \table \Column::ALIGN_CENTER ,
336+ ) );
337+ $ table ->addRow ( array ( 'l ' , 'r ' , 'c ' ) );
338+
339+ $ out = $ table ->getDisplayLines ();
340+
341+ $ this ->assertStringContainsString ( '| Left | Right | Center | ' , $ out [1 ] );
342+ $ this ->assertStringContainsString ( '| l | r | c | ' , $ out [3 ] );
343+ }
344+
345+ public function test_invalid_alignment_value () {
346+ $ this ->expectException ( \InvalidArgumentException::class );
347+ $ table = new cli \Table ();
348+ $ table ->setHeaders ( array ( 'Header1 ' ) );
349+ $ table ->setAlignments ( array ( 'Header1 ' => 'invalid-alignment ' ) );
350+ }
351+
352+ public function test_invalid_alignment_column () {
353+ $ this ->expectException ( \InvalidArgumentException::class );
354+ $ table = new cli \Table ();
355+ $ table ->setHeaders ( array ( 'Header1 ' ) );
356+ $ table ->setAlignments ( array ( 'NonExistent ' => \cli \table \Column::ALIGN_LEFT ) );
357+ }
292358}
0 commit comments