|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PhpSchool\CliMenuTest\MenuItem; |
| 4 | + |
| 5 | +use PhpSchool\CliMenu\MenuItem\CheckableItem; |
| 6 | +use PhpSchool\CliMenu\MenuStyle; |
| 7 | +use PhpSchool\Terminal\Terminal; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | + |
| 10 | +class CheckableItemTest extends TestCase |
| 11 | +{ |
| 12 | + public function testCanSelectIsTrue() : void |
| 13 | + { |
| 14 | + $item = new CheckableItem('Item', function () { |
| 15 | + }); |
| 16 | + $this->assertTrue($item->canSelect()); |
| 17 | + } |
| 18 | + |
| 19 | + public function testGetSelectAction() : void |
| 20 | + { |
| 21 | + $callable = function () { |
| 22 | + }; |
| 23 | + $item = new CheckableItem('Item', $callable); |
| 24 | + $this->assertSame($callable, $item->getSelectAction()); |
| 25 | + } |
| 26 | + |
| 27 | + public function testShowsItemExtra() : void |
| 28 | + { |
| 29 | + $item = new CheckableItem('Item', function () { |
| 30 | + }); |
| 31 | + $this->assertFalse($item->showsItemExtra()); |
| 32 | + |
| 33 | + $item = new CheckableItem('Item', function () { |
| 34 | + }, true); |
| 35 | + $this->assertTrue($item->showsItemExtra()); |
| 36 | + } |
| 37 | + |
| 38 | + public function testGetText() : void |
| 39 | + { |
| 40 | + $item = new CheckableItem('Item', function () { |
| 41 | + }); |
| 42 | + $this->assertEquals('Item', $item->getText()); |
| 43 | + } |
| 44 | + |
| 45 | + public function testGetRows() : void |
| 46 | + { |
| 47 | + $terminal = $this->createMock(Terminal::class); |
| 48 | + $terminal->expects($this->any())->method('getWidth')->willReturn(100); |
| 49 | + |
| 50 | + $menuStyle = new MenuStyle($terminal); |
| 51 | + $menuStyle->setPaddingLeftRight(0); |
| 52 | + $menuStyle->setWidth(8); |
| 53 | + |
| 54 | + $item = new CheckableItem('Item', function () { |
| 55 | + }); |
| 56 | + |
| 57 | + $itemChecked = new CheckableItem('Item', function () { |
| 58 | + }); |
| 59 | + $itemChecked->toggle(); |
| 60 | + $this->assertEquals(['[ ] Item'], $item->getRows($menuStyle)); |
| 61 | + $this->assertEquals(['[ ] Item'], $item->getRows($menuStyle, false)); |
| 62 | + $this->assertEquals(['[✔] Item'], $itemChecked->getRows($menuStyle, true)); |
| 63 | + } |
| 64 | + |
| 65 | + public function testSetText() : void |
| 66 | + { |
| 67 | + $terminal = $this->createMock(Terminal::class); |
| 68 | + $terminal->expects($this->any())->method('getWidth')->willReturn(100); |
| 69 | + |
| 70 | + $menuStyle = new MenuStyle($terminal); |
| 71 | + $menuStyle->setPaddingLeftRight(0); |
| 72 | + $menuStyle->setWidth(12); |
| 73 | + |
| 74 | + $item = new CheckableItem('Item', function () { |
| 75 | + }); |
| 76 | + $item->setText('New Text'); |
| 77 | + |
| 78 | + $itemChecked = new CheckableItem('Item', function () { |
| 79 | + }); |
| 80 | + $itemChecked->setText('New Text'); |
| 81 | + $itemChecked->toggle(); |
| 82 | + $this->assertEquals(['[ ] New Text'], $item->getRows($menuStyle)); |
| 83 | + $this->assertEquals(['[ ] New Text'], $item->getRows($menuStyle, false)); |
| 84 | + $this->assertEquals(['[✔] New Text'], $itemChecked->getRows($menuStyle, true)); |
| 85 | + } |
| 86 | + |
| 87 | + public function testTogglesMarker() : void |
| 88 | + { |
| 89 | + $terminal = $this->createMock(Terminal::class); |
| 90 | + $terminal->expects($this->any())->method('getWidth')->willReturn(100); |
| 91 | + |
| 92 | + $menuStyle = new MenuStyle($terminal); |
| 93 | + $menuStyle->setPaddingLeftRight(0); |
| 94 | + $menuStyle->setWidth(12); |
| 95 | + |
| 96 | + $item = new CheckableItem('Item', function () { |
| 97 | + }); |
| 98 | + |
| 99 | + $itemChecked = new CheckableItem('Item', function () { |
| 100 | + }); |
| 101 | + $itemChecked->toggle(); |
| 102 | + $this->assertEquals(['[ ] Item'], $item->getRows($menuStyle)); |
| 103 | + $this->assertEquals(['[ ] Item'], $item->getRows($menuStyle, false)); |
| 104 | + $this->assertEquals(['[✔] Item'], $itemChecked->getRows($menuStyle, true)); |
| 105 | + |
| 106 | + $itemChecked->toggle(); |
| 107 | + |
| 108 | + $this->assertEquals(['[ ] Item'], $itemChecked->getRows($menuStyle, true)); |
| 109 | + } |
| 110 | + |
| 111 | + public function testGetRowsWithItemExtra() : void |
| 112 | + { |
| 113 | + $terminal = $this->createMock(Terminal::class); |
| 114 | + $terminal->expects($this->any())->method('getWidth')->willReturn(100); |
| 115 | + |
| 116 | + $menuStyle = new MenuStyle($terminal); |
| 117 | + $menuStyle->setPaddingLeftRight(0); |
| 118 | + $menuStyle->setWidth(20); |
| 119 | + $menuStyle->setItemExtra('[EXTRA]'); |
| 120 | + $menuStyle->setDisplaysExtra(true); |
| 121 | + |
| 122 | + $item = new CheckableItem('Item', function () { |
| 123 | + }, true); |
| 124 | + $this->assertEquals(['[ ] Item [EXTRA]'], $item->getRows($menuStyle)); |
| 125 | + } |
| 126 | + |
| 127 | + public function testGetRowsWithMultipleLinesWithItemExtra() : void |
| 128 | + { |
| 129 | + $terminal = $this->createMock(Terminal::class); |
| 130 | + $terminal->expects($this->any())->method('getWidth')->willReturn(100); |
| 131 | + |
| 132 | + $menuStyle = new MenuStyle($terminal); |
| 133 | + $menuStyle->setPaddingLeftRight(0); |
| 134 | + $menuStyle->setWidth(20); |
| 135 | + $menuStyle->setItemExtra('[EXTRA]'); |
| 136 | + $menuStyle->setDisplaysExtra(true); |
| 137 | + |
| 138 | + $item = new CheckableItem('LONG ITEM LINE', function () { |
| 139 | + }, true); |
| 140 | + $this->assertEquals( |
| 141 | + [ |
| 142 | + "[ ] LONG [EXTRA]", |
| 143 | + " ITEM LINE", |
| 144 | + ], |
| 145 | + $item->getRows($menuStyle) |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + public function testHideAndShowItemExtra() : void |
| 150 | + { |
| 151 | + $item = new CheckableItem('Item', function () { |
| 152 | + }); |
| 153 | + |
| 154 | + $this->assertFalse($item->showsItemExtra()); |
| 155 | + $item->showItemExtra(); |
| 156 | + $this->assertTrue($item->showsItemExtra()); |
| 157 | + $item->hideItemExtra(); |
| 158 | + $this->assertFalse($item->showsItemExtra()); |
| 159 | + } |
| 160 | +} |
0 commit comments