Skip to content
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
21 changes: 21 additions & 0 deletions src/Exception/CannotShrinkMenuException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace PhpSchool\CliMenu\Exception;

use InvalidArgumentException;

class CannotShrinkMenuException extends InvalidArgumentException
{
public static function fromMarginAndTerminalWidth(int $margin, int $terminalWidth) : self
{
return new self(
sprintf(
'Cannot shrink menu. Margin: %s * 2 with terminal width: %s leaves no space for menu',
$margin,
$terminalWidth
)
);
}
}
15 changes: 10 additions & 5 deletions src/MenuStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PhpSchool\CliMenu;

use PhpSchool\CliMenu\Exception\CannotShrinkMenuException;
use PhpSchool\CliMenu\Terminal\TerminalFactory;
use PhpSchool\CliMenu\Util\ColourUtil;
use PhpSchool\CliMenu\Util\StringUtil as s;
Expand Down Expand Up @@ -400,11 +401,11 @@ public function setWidth(int $width) : self
Assertion::greaterOrEqualThan($width, 0);

$this->requestedWidth = $width;
$width = $this->maybeShrinkWidth($this->margin, $width);

$this->width = $width;
$this->width = $this->maybeShrinkWidth($this->marginAuto ? 0 : $this->margin, $width);

if ($this->marginAuto) {
$this->calculateMarginAuto($width);
$this->calculateMarginAuto($this->width);
}

$this->calculateContentWidth();
Expand All @@ -416,8 +417,12 @@ public function setWidth(int $width) : self

private function maybeShrinkWidth(int $margin, int $width) : int
{
if ($width + $margin >= $this->terminal->getWidth()) {
$width = $this->terminal->getWidth() - $margin;
if ($width + ($margin * 2) >= $this->terminal->getWidth()) {
$width = $this->terminal->getWidth() - ($margin * 2);

if ($width <= 0) {
throw CannotShrinkMenuException::fromMarginAndTerminalWidth($margin, $this->terminal->getWidth());
}
}

return $width;
Expand Down
8 changes: 7 additions & 1 deletion test/Builder/CliMenuBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,13 @@ public function marginBelowZeroProvider() : array
*/
public function testSetMarginAcceptsZeroAndPositiveIntegers(int $value) : void
{
$menu = (new CliMenuBuilder)->setMargin($value)->build();
$terminal = self::createMock(Terminal::class);
$terminal
->expects($this->any())
->method('getWidth')
->will($this->returnValue(200));

$menu = (new CliMenuBuilder($terminal))->setMargin($value)->build();

self::assertSame($value, $menu->getStyle()->getMargin());
}
Expand Down
2 changes: 1 addition & 1 deletion test/CliMenuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function setUp() : void

$this->terminal->expects($this->any())
->method('getWidth')
->willReturn(46);
->willReturn(48);

$this->terminal->expects($this->any())
->method('write')
Expand Down
2 changes: 1 addition & 1 deletion test/Dialogue/ConfirmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function setUp() : void

$this->terminal->expects($this->any())
->method('getWidth')
->willReturn(46);
->willReturn(48);

$this->terminal->expects($this->any())
->method('write')
Expand Down
2 changes: 1 addition & 1 deletion test/Dialogue/FlashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function setUp() : void

$this->terminal->expects($this->any())
->method('getWidth')
->willReturn(46);
->willReturn(48);

$this->terminal->expects($this->any())
->method('write')
Expand Down
20 changes: 20 additions & 0 deletions test/Exception/CannotShrinkMenuExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace PhpSchool\CliMenuTest\Exception;

use PhpSchool\CliMenu\Exception\CannotShrinkMenuException;
use PHPUnit\Framework\TestCase;

class CannotShrinkMenuExceptionTest extends TestCase
{
public function testException() : void
{
$e = CannotShrinkMenuException::fromMarginAndTerminalWidth(10, 15);
$this->assertEquals(
'Cannot shrink menu. Margin: 10 * 2 with terminal width: 15 leaves no space for menu',
$e->getMessage()
);
}
}
25 changes: 23 additions & 2 deletions test/MenuStyleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PhpSchool\CliMenuTest;

use PhpSchool\CliMenu\Builder\CliMenuBuilder;
use PhpSchool\CliMenu\Exception\CannotShrinkMenuException;
use PhpSchool\CliMenu\Exception\InvalidInstantiationException;
use PhpSchool\CliMenu\MenuStyle;
use PhpSchool\Terminal\UnixTerminal;
Expand Down Expand Up @@ -313,11 +314,11 @@ public function testSetMarginWhenWidthIsLargerThanTerminal() : void
$style->setMargin(20);

self::assertSame(20, $style->getMargin());
self::assertSame(480, $style->getWidth());
self::assertSame(460, $style->getWidth());

$style->setMargin(35);
self::assertSame(35, $style->getMargin());
self::assertSame(465, $style->getWidth());
self::assertSame(430, $style->getWidth());
}

public function testMarginAutoCenters() : void
Expand Down Expand Up @@ -712,4 +713,24 @@ public function testDebugMode() : void
$style->getBorderTopRows()
);
}

/**
* @dataProvider cannotShrinkProvider
*/
public function testSetWidthThrowsExceptionIfMenuTooWideAndMarginConsumesTerminal(int $margin) : void
{
$this->expectException(CannotShrinkMenuException::class);
$this->expectExceptionMessage(
"Cannot shrink menu. Margin: $margin * 2 with terminal width: 100 leaves no space for menu"
);

$style = $this->getMenuStyle(8, 100);
$style->setMargin($margin);
$style->setWidth(10);
}

public function cannotShrinkProvider() : array
{
return [[50], [51], [100]];
}
}
14 changes: 7 additions & 7 deletions test/res/testSimpleOpenCloseWithMarginAutoAndBorders.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@


 
   
  PHP School FTW  
  ======================  
  ● Item 1  
   
 
 
   
  PHP School FTW  
  ======================  
  ● Item 1