@@ -289,4 +289,95 @@ 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 ->setRenderer ( new cli \Table \Ascii () );
296+ $ table ->setHeaders ( array ( 'Header1 ' , 'Header2 ' ) );
297+ $ table ->addRow ( array ( 'Row1Col1 ' , 'Row1Col2 ' ) );
298+
299+ $ out = $ table ->getDisplayLines ();
300+
301+ // By default, columns should be left-aligned.
302+ $ this ->assertStringContainsString ( '| Header1 | Header2 | ' , $ out [1 ] );
303+ $ this ->assertStringContainsString ( '| Row1Col1 | Row1Col2 | ' , $ out [3 ] );
304+ }
305+
306+ public function test_right_alignment () {
307+ $ table = new cli \Table ();
308+ $ table ->setRenderer ( new cli \Table \Ascii () );
309+ $ table ->setHeaders ( array ( 'Name ' , 'Size ' ) );
310+ $ table ->setAlignments ( array ( 'Name ' => \cli \table \Column::ALIGN_RIGHT , 'Size ' => \cli \table \Column::ALIGN_RIGHT ) );
311+ $ table ->addRow ( array ( 'file.txt ' , '1024 B ' ) );
312+
313+ $ out = $ table ->getDisplayLines ();
314+
315+ // Headers should be right-aligned in their columns
316+ $ this ->assertStringContainsString ( '| Name | Size | ' , $ out [1 ] );
317+ // Data should be right-aligned
318+ $ this ->assertStringContainsString ( '| file.txt | 1024 B | ' , $ out [3 ] );
319+ }
320+
321+ public function test_center_alignment () {
322+ $ table = new cli \Table ();
323+ $ table ->setRenderer ( new cli \Table \Ascii () );
324+ $ table ->setHeaders ( array ( 'A ' , 'B ' ) );
325+ $ table ->setAlignments ( array ( 'A ' => \cli \table \Column::ALIGN_CENTER , 'B ' => \cli \table \Column::ALIGN_CENTER ) );
326+ $ table ->addRow ( array ( 'test ' , 'data ' ) );
327+
328+ $ out = $ table ->getDisplayLines ();
329+
330+ // Headers should be center-aligned
331+ $ this ->assertStringContainsString ( '| A | B | ' , $ out [1 ] );
332+ // Data should be center-aligned
333+ $ this ->assertStringContainsString ( '| test | data | ' , $ out [3 ] );
334+ }
335+
336+ public function test_mixed_alignments () {
337+ $ table = new cli \Table ();
338+ $ table ->setRenderer ( new cli \Table \Ascii () );
339+ $ table ->setHeaders ( array ( 'Name ' , 'Count ' , 'Status ' ) );
340+ $ table ->setAlignments ( array (
341+ 'Name ' => \cli \table \Column::ALIGN_LEFT ,
342+ 'Count ' => \cli \table \Column::ALIGN_RIGHT ,
343+ 'Status ' => \cli \table \Column::ALIGN_CENTER ,
344+ ) );
345+ $ table ->addRow ( array ( 'Item ' , '42 ' , 'OK ' ) );
346+
347+ $ out = $ table ->getDisplayLines ();
348+
349+ // Headers line should show all three with proper alignment
350+ $ this ->assertStringContainsString ( '| Name | Count | Status | ' , $ out [1 ] );
351+ // Data line: Name left, Count right, Status center
352+ $ this ->assertStringContainsString ( '| Item | 42 | OK | ' , $ out [3 ] );
353+ }
354+
355+ public function test_invalid_alignment_value () {
356+ $ this ->expectException ( \InvalidArgumentException::class );
357+ $ table = new cli \Table ();
358+ $ table ->setHeaders ( array ( 'Header1 ' ) );
359+ $ table ->setAlignments ( array ( 'Header1 ' => 'invalid-alignment ' ) );
360+ }
361+
362+ public function test_invalid_alignment_column () {
363+ $ this ->expectException ( \InvalidArgumentException::class );
364+ $ table = new cli \Table ();
365+ $ table ->setHeaders ( array ( 'Header1 ' ) );
366+ $ table ->setAlignments ( array ( 'NonExistent ' => \cli \table \Column::ALIGN_LEFT ) );
367+ }
368+
369+ public function test_alignment_before_headers () {
370+ // Test that alignments can be set before headers without throwing an error
371+ $ table = new cli \Table ();
372+ $ table ->setRenderer ( new cli \Table \Ascii () );
373+ $ table ->setAlignments ( array ( 'Name ' => \cli \table \Column::ALIGN_RIGHT ) );
374+ $ table ->setHeaders ( array ( 'Name ' ) );
375+ $ table ->addRow ( array ( 'LongName ' ) );
376+
377+ $ out = $ table ->getDisplayLines ();
378+
379+ // Should be right-aligned - "Name" is 4 chars, "LongName" is 8 chars, so column width is 8
380+ $ this ->assertStringContainsString ( '| Name | ' , $ out [1 ] );
381+ $ this ->assertStringContainsString ( '| LongName | ' , $ out [3 ] );
382+ }
292383}
0 commit comments