Skip to content

Add setText to Items + small AsciiArt fix #153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 19 commits into from
May 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions src/MenuItem/AsciiArtItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,9 @@ public function __construct(string $text, string $position = self::POSITION_CENT
{
Assertion::inArray($position, [self::POSITION_CENTER, self::POSITION_RIGHT, self::POSITION_LEFT]);

$this->text = implode("\n", array_map(function (string $line) {
return rtrim($line, ' ');
}, explode("\n", $text)));
$this->setText($text);
$this->position = $position;
$this->alternateText = $alt;
$this->artLength = max(array_map('mb_strlen', explode("\n", $text)));
}

/**
Expand Down Expand Up @@ -103,6 +100,26 @@ public function getText() : string
return $this->text;
}

/**
* Set the raw string of text
*/
public function setText(string $text) : void
{
$this->text = implode("\n", array_map(function (string $line) {
return rtrim($line, ' ');
}, explode("\n", $text)));

$this->calculateArtLength();
}

/**
* Calculate the length of the art
*/
private function calculateArtLength() : void
{
$this->artLength = max(array_map('mb_strlen', explode("\n", $this->text)));
}

/**
* Return the length of the art
*/
Expand Down
8 changes: 8 additions & 0 deletions src/MenuItem/LineBreakItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ public function getText() : string
return $this->breakChar;
}

/**
* Set the raw string of text
*/
public function setText(string $text) : void
{
$this->breakChar = $text;
}

/**
* Whether or not the menu item is showing the menustyle extra value
*/
Expand Down
8 changes: 8 additions & 0 deletions src/MenuItem/MenuMenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public function getText() : string
{
return $this->text;
}

/**
* Set the raw string of text
*/
public function setText(string $text) : void
{
$this->text = $text;
}

/**
* Returns the sub menu
Expand Down
8 changes: 8 additions & 0 deletions src/MenuItem/SelectableItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,12 @@ public function getText() : string
{
return $this->text;
}

/**
* Set the raw string of text
*/
public function setText(string $text) : void
{
$this->text = $text;
}
}
8 changes: 8 additions & 0 deletions src/MenuItem/StaticItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ public function getText() : string
return $this->text;
}

/**
* Set the raw string of text
*/
public function setText(string $text) : void
{
$this->text = $text;
}

/**
* Whether or not the menu item is showing the menustyle extra value
*/
Expand Down
47 changes: 47 additions & 0 deletions test/MenuItem/AsciiArtItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,20 @@ public function testGetRowsReturnsStaticAltItemWhenWidthIsTooSmall() : void
self::assertSame(['my alt'], $item->getRows($menuStyle));
}

public function testGetRowsDoesNotReturnsStaticAltItemWhenOnlySpacesOverflow() : void
{
$menuStyle = $this->createMock(MenuStyle::class);

$menuStyle
->expects($this->any())
->method('getContentWidth')
->will($this->returnValue(15));

$item = new AsciiArtItem('NOT TOO LONG ', AsciiArtItem::POSITION_LEFT, 'my alt');

self::assertSame(['NOT TOO LONG'], $item->getRows($menuStyle));
}

public function testWithRealAsciiArtCenterAligned() : void
{
$menuStyle = $this->createMock(MenuStyle::class);
Expand Down Expand Up @@ -236,4 +250,37 @@ public function testWithRealAsciiArtCenterAlignedWithWhiteSpaceAtTheEndOfEachLin
$item->getRows($menuStyle)
);
}

public function testSetText() : void
{
$menuStyle = $this->createMock(MenuStyle::class);

$menuStyle
->expects($this->any())
->method('getContentWidth')
->will($this->returnValue(30));

$art = <<<ART
_ __ _
/ |..| \
\/ || \/
|_''_|
PHP SCHOOL
LEARNING FOR ELEPHANTS
ART;
$item = new AsciiArtItem("//", AsciiArtItem::POSITION_CENTER);
$item->setText($art);

$this->assertEquals(
[
' _ __ _',
' / |..| \\',
' \/ || \/',
" |_''_|",
' PHP SCHOOL',
' LEARNING FOR ELEPHANTS'
],
$item->getRows($menuStyle)
);
}
}
14 changes: 14 additions & 0 deletions test/MenuItem/LineBreakItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@ public function testGetRowsWithMultiByteChars() : void
$this->assertEquals(['❅❅❅❅❅', '❅❅❅❅❅'], $item->getRows($menuStyle));
}

public function testSetText() : void
{
$menuStyle = $this->createMock(MenuStyle::class);

$menuStyle
->expects($this->any())
->method('getContentWidth')
->will($this->returnValue(5));

$item = new LineBreakItem('ABC', 2);
$item->setText('❅-');
$this->assertEquals(['❅-❅-❅', '❅-❅-❅'], $item->getRows($menuStyle));
}

public function testHideAndShowItemExtraHasNoEffect() : void
{
$item = new LineBreakItem('*');
Expand Down
17 changes: 17 additions & 0 deletions test/MenuItem/SelectableItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ public function testGetRows() : void
$this->assertEquals([' Item'], $item->getRows($menuStyle, true));
}

public function testSetText() : void
{
$menuStyle = $this->createMock(MenuStyle::class);

$menuStyle
->expects($this->any())
->method('getContentWidth')
->will($this->returnValue(10));

$item = new SelectableItem('Item', function () {
});
$item->setText('New Text');
$this->assertEquals([' New Text'], $item->getRows($menuStyle));
$this->assertEquals([' New Text'], $item->getRows($menuStyle, false));
$this->assertEquals([' New Text'], $item->getRows($menuStyle, true));
}

public function testGetRowsWithUnSelectedMarker() : void
{
$menuStyle = $this->createMock(MenuStyle::class);
Expand Down
18 changes: 18 additions & 0 deletions test/MenuItem/StaticItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,24 @@ public function testGetRowsWithContentWhichDoesNotFitOnOneLineIsWrapped() : void
);
}

public function testSetText() : void
{
$menuStyle = $this->createMock(MenuStyle::class);

$menuStyle
->expects($this->once())
->method('getContentWidth')
->will($this->returnValue(10));

$item = new StaticItem('CONTENT');
$item->setText('CONTENT 2 LINES');

$this->assertEquals(
['CONTENT 2', 'LINES'],
$item->getRows($menuStyle)
);
}

public function testHideAndShowItemExtraHasNoEffect() : void
{
$item = new StaticItem('CONTENT 1 LINE');
Expand Down