Skip to content

Commit bfdb70e

Browse files
committed
#5 Detect Ascii art that does not fit in the menu
1 parent 9e7c884 commit bfdb70e

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

src/CliMenuBuilder.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,15 @@ public function addAsciiArt($art, $position = AsciiArtItem::POSITION_CENTER)
180180
Assertion::string($art);
181181
Assertion::string($position);
182182

183-
$this->addMenuItem(new AsciiArtItem($art, $position));
183+
$asciiArtItem = new AsciiArtItem($art, $position);
184+
185+
Assertion::lessOrEqualThan(
186+
$asciiArtItem->getArtLength(),
187+
$this->getMenuStyle()->getContentWidth(),
188+
'The provided Ascii art does not fit in the menu'
189+
);
190+
191+
$this->addMenuItem($asciiArtItem);
184192

185193
return $this;
186194
}

src/MenuItem/AsciiArtItem.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,16 @@ public function getText()
116116
return $this->text;
117117
}
118118

119+
/**
120+
* Return the length of the art
121+
*
122+
* @return int
123+
*/
124+
public function getArtLength()
125+
{
126+
return $this->artLength;
127+
}
128+
119129
/**
120130
* Whether or not the menu item is showing the menustyle extra value
121131
*

test/CliMenuBuilderTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,18 @@ public function testAsciiArtWithSpecificPosition()
247247
$this->checkItems($menu, $expected);
248248
}
249249

250+
/**
251+
* @expectedException Assert\InvalidArgumentException
252+
* @expectedExceptionMessage The provided Ascii art does not fit in the menu
253+
*/
254+
public function testAddAsciiArtDetectsArtThatDoesNotFit()
255+
{
256+
$builder = new CliMenuBuilder;
257+
$builder->setWidth(1);
258+
$builder->addAsciiArt("//\n//", AsciiArtItem::POSITION_LEFT);
259+
$menu = $builder->build();
260+
}
261+
250262
public function testEndThrowsExceptionIfNoParentBuilder()
251263
{
252264
$builder = new CliMenuBuilder;

test/MenuItem/AsciiArtItemTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ public function testGetText()
5050
$this->assertEquals('////\\\\', $item->getText());
5151
}
5252

53+
public function testGetArtLength()
54+
{
55+
$item = new AsciiArtItem("//\n//\n///");
56+
$this->assertEquals(3, $item->getArtLength());
57+
}
58+
5359
public function testGetRowsLeftAligned()
5460
{
5561
$menuStyle = $this->createMock(MenuStyle::class);

0 commit comments

Comments
 (0)