Skip to content

Commit 3f314ba

Browse files
committed
Closure builder binding
1 parent 061c36f commit 3f314ba

File tree

4 files changed

+108
-28
lines changed

4 files changed

+108
-28
lines changed

src/Builder/CliMenuBuilder.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,11 @@ public function addAsciiArt(string $art, string $position = AsciiArtItem::POSITI
131131
return $this;
132132
}
133133

134-
public function addSubMenu(string $text, callable $callback) : self
134+
public function addSubMenu(string $text, \Closure $callback) : self
135135
{
136136
$builder = self::newSubMenu($this->terminal);
137137

138+
$callback = $callback->bindTo($builder);
138139
$callback($builder);
139140

140141
$menu = $builder->build();
@@ -175,10 +176,11 @@ public function addSubMenuFromBuilder(string $text, CliMenuBuilder $builder) : s
175176
return $this;
176177
}
177178

178-
public function addSplitItem(callable $callback) : self
179+
public function addSplitItem(\Closure $callback) : self
179180
{
180181
$builder = new SplitItemBuilder($this->menu);
181-
182+
183+
$callback = $callback->bindTo($builder);
182184
$callback($builder);
183185

184186
$this->menu->addItem($builder->build());

src/Builder/SplitItemBuilder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ public function addLineBreak(string $breakChar = ' ', int $lines = 1) : self
5555
return $this;
5656
}
5757

58-
public function addSubMenu(string $text, callable $callback) : self
58+
public function addSubMenu(string $text, \Closure $callback) : self
5959
{
6060
$builder = CliMenuBuilder::newSubMenu($this->menu->getTerminal());
6161

62+
$callback = $callback->bindTo($builder);
6263
$callback($builder);
6364

6465
$menu = $builder->build();

test/Builder/CliMenuBuilderTest.php

Lines changed: 67 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function testDefaultItems() : void
3030
],
3131
];
3232

33-
$this->checkItems($menu, $expected);
33+
$this->checkMenuItems($menu, $expected);
3434
}
3535

3636
public function testModifyExitButtonText() : void
@@ -46,7 +46,7 @@ public function testModifyExitButtonText() : void
4646
],
4747
];
4848

49-
$this->checkItems($menu, $expected);
49+
$this->checkMenuItems($menu, $expected);
5050
}
5151

5252
public function testModifyStyles() : void
@@ -324,7 +324,7 @@ public function testAddItem() : void
324324
],
325325
];
326326

327-
$this->checkItems($menu, $expected);
327+
$this->checkMenuItems($menu, $expected);
328328
}
329329

330330
public function testAddMultipleItems() : void
@@ -351,7 +351,7 @@ public function testAddMultipleItems() : void
351351
],
352352
];
353353

354-
$this->checkItems($menu, $expected);
354+
$this->checkMenuItems($menu, $expected);
355355
}
356356

357357
public function testAddStaticItem() : void
@@ -369,7 +369,7 @@ public function testAddStaticItem() : void
369369
]
370370
];
371371

372-
$this->checkItems($menu, $expected);
372+
$this->checkMenuItems($menu, $expected);
373373
}
374374

375375
public function testAddLineBreakItem() : void
@@ -387,7 +387,7 @@ public function testAddLineBreakItem() : void
387387
]
388388
];
389389

390-
$this->checkItems($menu, $expected);
390+
$this->checkMenuItems($menu, $expected);
391391
}
392392

393393
public function testAddLineBreakItemWithNumLines() : void
@@ -405,7 +405,7 @@ public function testAddLineBreakItemWithNumLines() : void
405405
]
406406
];
407407

408-
$this->checkItems($menu, $expected);
408+
$this->checkMenuItems($menu, $expected);
409409
}
410410

411411
public function testAsciiArtWithDefaultPosition() : void
@@ -423,7 +423,7 @@ public function testAsciiArtWithDefaultPosition() : void
423423
]
424424
];
425425

426-
$this->checkItems($menu, $expected);
426+
$this->checkMenuItems($menu, $expected);
427427
}
428428

429429
public function testAsciiArtWithSpecificPosition() : void
@@ -441,7 +441,7 @@ public function testAsciiArtWithSpecificPosition() : void
441441
]
442442
];
443443

444-
$this->checkItems($menu, $expected);
444+
$this->checkMenuItems($menu, $expected);
445445
}
446446

447447
public function testAsciiArtWithAlt() : void
@@ -460,7 +460,7 @@ public function testAsciiArtWithAlt() : void
460460
]
461461
];
462462

463-
$this->checkItems($menu, $expected);
463+
$this->checkMenuItems($menu, $expected);
464464
}
465465

466466
public function testAddSubMenu() : void
@@ -472,7 +472,7 @@ public function testAddSubMenu() : void
472472

473473
$menu = $builder->build();
474474

475-
$this->checkItems($menu, [
475+
$this->checkMenuItems($menu, [
476476
[
477477
'class' => MenuMenuItem::class
478478
]
@@ -489,7 +489,7 @@ public function testAddSubMenuWithBuilder() : void
489489

490490
$menu = $builder->build();
491491

492-
$this->checkItems($menu, [
492+
$this->checkMenuItems($menu, [
493493
[
494494
'class' => MenuMenuItem::class
495495
]
@@ -563,7 +563,7 @@ public function testSubMenuDefaultItems() : void
563563
],
564564
];
565565

566-
$this->checkItems($menu->getItems()[0]->getSubMenu(), $expected);
566+
$this->checkMenuItems($menu->getItems()[0]->getSubMenu(), $expected);
567567
}
568568

569569
public function testModifyExitAndGoBackTextOnSubMenu() : void
@@ -588,7 +588,7 @@ public function testModifyExitAndGoBackTextOnSubMenu() : void
588588
],
589589
];
590590

591-
$this->checkItems($menu->getItems()[0]->getSubMenu(), $expected);
591+
$this->checkMenuItems($menu->getItems()[0]->getSubMenu(), $expected);
592592
}
593593

594594
public function testDisableDefaultItemsDisablesExitAndGoBackOnSubMenu() : void
@@ -736,23 +736,71 @@ public function testSetPaddingLeftAndRight() : void
736736

737737
self::assertEquals(3, $style->getPaddingLeftRight());
738738
}
739+
740+
public function testAddSubMenuWithClosureBinding() : void
741+
{
742+
$builder = new CliMenuBuilder;
743+
$builder->disableDefaultItems();
744+
$builder->addSubMenu('My SubMenu', function () {
745+
$this->disableDefaultItems();
746+
$this->addItem('My Item', function () {
747+
});
748+
});
749+
750+
$menu = $builder->build();
751+
752+
$expected = [
753+
[
754+
'class' => SelectableItem::class,
755+
'text' => 'My Item',
756+
]
757+
];
758+
759+
$this->checkMenuItems($menu->getItems()[0]->getSubMenu(), $expected);
760+
}
761+
762+
public function testAddSplitItemWithClosureBinding() : void
763+
{
764+
$builder = new CliMenuBuilder;
765+
$builder->disableDefaultItems();
766+
$builder->addSplitItem(function () {
767+
$this->addItem('My Item', function () {
768+
});
769+
});
770+
771+
$menu = $builder->build();
772+
773+
$expected = [
774+
[
775+
'class' => SelectableItem::class,
776+
'text' => 'My Item',
777+
]
778+
];
779+
780+
$this->checkItems($menu->getItems()[0]->getItems(), $expected);
781+
}
739782

740-
private function checkItems(CliMenu $menu, array $expected) : void
783+
private function checkMenuItems(CliMenu $menu, array $expected) : void
784+
{
785+
$this->checkItems($this->readAttribute($menu, 'items'), $expected);
786+
}
787+
788+
private function checkItems(array $actualItems, array $expected) : void
741789
{
742-
$actualItems = $this->readAttribute($menu, 'items');
743790
self::assertCount(count($expected), $actualItems);
744-
791+
745792
foreach ($expected as $expectedItem) {
746793
$actualItem = array_shift($actualItems);
747-
794+
748795
self::assertInstanceOf($expectedItem['class'], $actualItem);
749796
unset($expectedItem['class']);
750-
797+
751798
foreach ($expectedItem as $property => $value) {
752799
self::assertEquals($this->readAttribute($actualItem, $property), $value);
753800
}
754801
}
755802
}
803+
756804

757805
private function checkVariable(CliMenu $menu, string $property, $expected) : void
758806
{

test/Builder/SplitItemBuilderTest.php

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function testAddItem() : void
3434
],
3535
];
3636

37-
$this->checkItems($item, $expected);
37+
$this->checkItemItems($item, $expected);
3838
}
3939

4040
public function testAddStaticItem() : void
@@ -52,7 +52,7 @@ public function testAddStaticItem() : void
5252
]
5353
];
5454

55-
$this->checkItems($item, $expected);
55+
$this->checkItemItems($item, $expected);
5656
}
5757

5858
public function testAddSubMenu() : void
@@ -64,7 +64,7 @@ public function testAddSubMenu() : void
6464

6565
$item = $builder->build();
6666

67-
$this->checkItems($item, [
67+
$this->checkItemItems($item, [
6868
[
6969
'class' => MenuMenuItem::class
7070
]
@@ -93,9 +93,38 @@ public function testSetGutter() : void
9393
self::assertEquals(4, self::readAttribute($item, 'gutter'));
9494
}
9595

96-
private function checkItems(SplitItem $item, array $expected) : void
96+
public function testAddSubMenuWithClosureBinding() : void
97+
{
98+
$menu = new CliMenu(null, []);
99+
$builder = new SplitItemBuilder($menu);
100+
$builder->addSubMenu('My SubMenu', function () {
101+
$this->disableDefaultItems();
102+
$this->addItem('My Item', function () {
103+
});
104+
});
105+
106+
$item = $builder->build();
107+
108+
$expected = [
109+
[
110+
'class' => SelectableItem::class,
111+
'text' => 'My Item',
112+
]
113+
];
114+
115+
$this->checkItems(
116+
$item->getItems()[0]->getSubMenu()->getItems(),
117+
$expected
118+
);
119+
}
120+
121+
private function checkItemItems(SplitItem $item, array $expected) : void
122+
{
123+
$this->checkItems($item->getItems(), $expected);
124+
}
125+
126+
private function checkItems(array $actualItems, array $expected) : void
97127
{
98-
$actualItems = $this->readAttribute($item, 'items');
99128
self::assertCount(count($expected), $actualItems);
100129

101130
foreach ($expected as $expectedItem) {

0 commit comments

Comments
 (0)