|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpSchool\CliMenuTest; |
| 4 | + |
| 5 | +use PhpSchool\CliMenu\CliMenuBuilder; |
| 6 | +use PhpSchool\CliMenu\Exception\InvalidInstantiationException; |
| 7 | +use PhpSchool\CliMenu\MenuStyle; |
| 8 | +use PhpSchool\CliMenu\Terminal\UnixTerminal; |
| 9 | +use PHPUnit_Framework_TestCase; |
| 10 | + |
| 11 | +/** |
| 12 | + * Class MenuStyleTest |
| 13 | + * @author Michael Woodward <mikeymike.mw@gmail.com> |
| 14 | + */ |
| 15 | +class MenuStyleTest extends PHPUnit_Framework_TestCase |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @return MenuStyle |
| 19 | + */ |
| 20 | + private function getMenuStyle() |
| 21 | + { |
| 22 | + // Use the CliMenuBuilder & reflection to get the style Obj |
| 23 | + $builder = new CliMenuBuilder(); |
| 24 | + $menu = $builder->build(); |
| 25 | + |
| 26 | + $reflectionMenu = new \ReflectionObject($menu); |
| 27 | + $styleProperty = $reflectionMenu->getProperty('style'); |
| 28 | + $styleProperty->setAccessible(true); |
| 29 | + $style = $styleProperty->getValue($menu); |
| 30 | + |
| 31 | + $reflectionStyle = new \ReflectionObject($style); |
| 32 | + $terminalProperty = $reflectionStyle->getProperty('terminal'); |
| 33 | + $terminalProperty->setAccessible(true); |
| 34 | + $terminalProperty->setValue($style, $this->getMockTerminal()); |
| 35 | + |
| 36 | + // Force recalculate terminal widths now terminal is set |
| 37 | + $style->setWidth(100); |
| 38 | + |
| 39 | + return $style; |
| 40 | + } |
| 41 | + |
| 42 | + private function getMockTerminal() |
| 43 | + { |
| 44 | + $terminal = $this |
| 45 | + ->getMockBuilder(UnixTerminal::class) |
| 46 | + ->disableOriginalConstructor() |
| 47 | + ->setMethods(['getWidth']) |
| 48 | + ->getMock(); |
| 49 | + |
| 50 | + $terminal |
| 51 | + ->expects(static::any()) |
| 52 | + ->method('getWidth') |
| 53 | + ->will(static::returnValue(500)); |
| 54 | + |
| 55 | + return $terminal; |
| 56 | + } |
| 57 | + |
| 58 | + public function testMenuStyleCannotBeInstantiatedByDisallowedConsumers() |
| 59 | + { |
| 60 | + $this->setExpectedException( |
| 61 | + InvalidInstantiationException::class, |
| 62 | + 'The MenuStyle must be instantiated by "PhpSchool\CliMenu\CliMenuBuilder"' |
| 63 | + ); |
| 64 | + |
| 65 | + new MenuStyle(); |
| 66 | + } |
| 67 | + |
| 68 | + public function testMenuStyleCanBeInstantiatedByCliMenuBuilder() |
| 69 | + { |
| 70 | + $builder = new CliMenuBuilder(); |
| 71 | + $menu = $builder->build(); |
| 72 | + |
| 73 | + $reflectionMenu = new \ReflectionObject($menu); |
| 74 | + static::assertTrue($reflectionMenu->hasProperty('style')); |
| 75 | + |
| 76 | + $styleProperty = $reflectionMenu->getProperty('style'); |
| 77 | + $styleProperty->setAccessible(true); |
| 78 | + $style = $styleProperty->getValue($menu); |
| 79 | + |
| 80 | + static::assertSame(MenuStyle::class, get_class($style)); |
| 81 | + } |
| 82 | + |
| 83 | + public function testAvailableColours() |
| 84 | + { |
| 85 | + static::assertSame([ |
| 86 | + 'black', |
| 87 | + 'red', |
| 88 | + 'green', |
| 89 | + 'yellow', |
| 90 | + 'blue', |
| 91 | + 'magenta', |
| 92 | + 'cyan', |
| 93 | + 'white', |
| 94 | + 'default' |
| 95 | + ], MenuStyle::getAvailableColours()); |
| 96 | + } |
| 97 | + |
| 98 | + public function testGetSelectedSetCode() |
| 99 | + { |
| 100 | + static::assertSame("\e[47;34m", $this->getMenuStyle()->getSelectedSetCode()); |
| 101 | + } |
| 102 | + |
| 103 | + public function testGetSelectedUnsetCode() |
| 104 | + { |
| 105 | + static::assertSame("\e[49;39m", $this->getMenuStyle()->getSelectedUnsetCode()); |
| 106 | + } |
| 107 | + |
| 108 | + public function testGetUnselectedSetCode() |
| 109 | + { |
| 110 | + static::assertSame("\e[44;37m", $this->getMenuStyle()->getUnselectedSetCode()); |
| 111 | + } |
| 112 | + |
| 113 | + public function testGetUnselectedUnsetCode() |
| 114 | + { |
| 115 | + static::assertSame("\e[49;39m", $this->getMenuStyle()->getUnselectedUnsetCode()); |
| 116 | + } |
| 117 | + |
| 118 | + public function testGetterAndSetters() |
| 119 | + { |
| 120 | + $style = $this->getMenuStyle(); |
| 121 | + |
| 122 | + static::assertSame('blue', $style->getBg()); |
| 123 | + static::assertSame('white', $style->getFg()); |
| 124 | + static::assertSame('○', $style->getUnselectedMarker()); |
| 125 | + static::assertSame('●', $style->getSelectedMarker()); |
| 126 | + static::assertSame('✔', $style->getItemExtra()); |
| 127 | + static::assertFalse($style->getDisplaysExtra()); |
| 128 | + static::assertSame('=', $style->getTitleSeparator()); |
| 129 | + static::assertSame(100, $style->getWidth()); |
| 130 | + static::assertSame(2, $style->getMargin()); |
| 131 | + static::assertSame(2, $style->getPadding()); |
| 132 | + |
| 133 | + $style->setBg('red'); |
| 134 | + $style->setFg('yellow'); |
| 135 | + $style->setUnselectedMarker('-'); |
| 136 | + $style->setSelectedMarker('>'); |
| 137 | + $style->setItemExtra('EXTRA!'); |
| 138 | + $style->setDisplaysExtra(true); |
| 139 | + $style->setTitleSeparator('+'); |
| 140 | + $style->setWidth(200); |
| 141 | + $style->setMargin(10); |
| 142 | + $style->setPadding(10); |
| 143 | + |
| 144 | + static::assertSame('red', $style->getBg()); |
| 145 | + static::assertSame('yellow', $style->getFg()); |
| 146 | + static::assertSame('-', $style->getUnselectedMarker()); |
| 147 | + static::assertSame('>', $style->getSelectedMarker()); |
| 148 | + static::assertSame('EXTRA!', $style->getItemExtra()); |
| 149 | + static::assertTrue($style->getDisplaysExtra()); |
| 150 | + static::assertSame('+', $style->getTitleSeparator()); |
| 151 | + static::assertSame(200, $style->getWidth()); |
| 152 | + static::assertSame(10, $style->getMargin()); |
| 153 | + static::assertSame(10, $style->getPadding()); |
| 154 | + } |
| 155 | + |
| 156 | + public function testGetMarkerReturnsTheCorrectMarkers() |
| 157 | + { |
| 158 | + $style = $this->getMenuStyle(); |
| 159 | + |
| 160 | + $style->setSelectedMarker('>'); |
| 161 | + $style->setUnselectedMarker('x'); |
| 162 | + |
| 163 | + static::assertSame('>', $style->getMarker(true)); |
| 164 | + static::assertSame('x', $style->getMarker(false)); |
| 165 | + } |
| 166 | + |
| 167 | + public function testWidthCalculation() |
| 168 | + { |
| 169 | + $style = $this->getMenuStyle(); |
| 170 | + |
| 171 | + $style->setWidth(300); |
| 172 | + $style->setPadding(5); |
| 173 | + $style->setMargin(5); |
| 174 | + |
| 175 | + static::assertSame(280, $style->getContentWidth()); |
| 176 | + } |
| 177 | + |
| 178 | + public function testRightHandPaddingCalculation() |
| 179 | + { |
| 180 | + $style = $this->getMenuStyle(); |
| 181 | + |
| 182 | + $style->setWidth(300); |
| 183 | + $style->setPadding(5); |
| 184 | + $style->setMargin(5); |
| 185 | + |
| 186 | + static::assertSame(235, $style->getRightHandPadding(50)); |
| 187 | + } |
| 188 | +} |
0 commit comments