@@ -377,6 +377,71 @@ public function testAskPasswordStyle() : void
377
377
static ::assertEquals ('red ' , $ password ->getStyle ()->getFg ());
378
378
}
379
379
380
+ public function testAddCustomControlMappingThrowsExceptionWhenOverwritingExistingDefaultControls () : void
381
+ {
382
+ $ this ->expectException (\InvalidArgumentException::class);
383
+ $ this ->expectExceptionMessage ('Cannot rebind this input ' );
384
+
385
+ $ menu = new CliMenu ('PHP School FTW ' , []);
386
+ $ menu ->addCustomControlMapping (' ' , function () {
387
+ });
388
+ }
389
+
390
+ public function testAddCustomControlMappingThrowsExceptionWhenAttemptingToOverwriteAddedCustomControlMap () : void
391
+ {
392
+ $ this ->expectException (\InvalidArgumentException::class);
393
+ $ this ->expectExceptionMessage ('Cannot rebind this input ' );
394
+
395
+ $ menu = new CliMenu ('PHP School FTW ' , []);
396
+ $ menu ->addCustomControlMapping ('c ' , function () {
397
+ });
398
+ $ menu ->addCustomControlMapping ('c ' , function () {
399
+ });
400
+ }
401
+
402
+ public function testAddCustomControlMapping () : void
403
+ {
404
+ $ this ->terminal ->expects ($ this ->once ())
405
+ ->method ('read ' )
406
+ ->willReturn ('c ' );
407
+
408
+ $ style = $ this ->getStyle ($ this ->terminal );
409
+
410
+ $ action = function (CliMenu $ menu ) {
411
+ $ menu ->close ();
412
+ };
413
+ $ item = new SelectableItem ('Item 1 ' , $ action );
414
+
415
+ $ menu = new CliMenu ('PHP School FTW ' , [$ item ], $ this ->terminal , $ style );
416
+ $ menu ->addCustomControlMapping ('c ' , $ action );
417
+ $ menu ->open ();
418
+
419
+ static ::assertStringEqualsFile ($ this ->getTestFile (), $ this ->output ->fetch ());
420
+ }
421
+
422
+ public function testRemoveCustomControlMappingThrowsExceptionIfNoSuchMappingExists () : void
423
+ {
424
+ $ this ->expectException (\InvalidArgumentException::class);
425
+ $ this ->expectExceptionMessage ('This input is not registered ' );
426
+
427
+ $ menu = new CliMenu ('PHP School FTW ' , []);
428
+ $ menu ->removeCustomControlMapping ('c ' );
429
+ }
430
+
431
+ public function testRemoveCustomControlMapping () : void
432
+ {
433
+ $ action = function (CliMenu $ menu ) {
434
+ $ menu ->close ();
435
+ };
436
+
437
+ $ menu = new CliMenu ('PHP School FTW ' , [], $ this ->terminal );
438
+ $ menu ->addCustomControlMapping ('c ' , $ action );
439
+ self ::assertSame (['c ' => $ action ], $ this ->readAttribute ($ menu , 'customControlMappings ' ));
440
+
441
+ $ menu ->removeCustomControlMapping ('c ' );
442
+ self ::assertSame ([], $ this ->readAttribute ($ menu , 'customControlMappings ' ));
443
+ }
444
+
380
445
private function getTestFile () : string
381
446
{
382
447
return sprintf ('%s/res/%s.txt ' , __DIR__ , $ this ->getName ());
0 commit comments