Skip to content

Commit 91e2348

Browse files
authored
Merge pull request #213 from php-school/style-tests
Tests for CheckboxStyle & RadioStyle
2 parents 95df7b9 + 8e9c251 commit 91e2348

File tree

4 files changed

+178
-23
lines changed

4 files changed

+178
-23
lines changed

src/Style/CheckboxStyle.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ class CheckboxStyle
3131
*/
3232
private $displaysExtra;
3333

34-
/**
35-
* @var bool
36-
*/
37-
private $custom = false;
38-
3934
public function __construct()
4035
{
4136
$this->markerOn = self::DEFAULT_STYLES['markerOn'];
@@ -46,7 +41,14 @@ public function __construct()
4641

4742
public function hasChangedFromDefaults() : bool
4843
{
49-
return $this->custom;
44+
$currentValues = [
45+
$this->markerOn,
46+
$this->markerOff,
47+
$this->itemExtra,
48+
$this->displaysExtra,
49+
];
50+
51+
return $currentValues !== array_values(self::DEFAULT_STYLES);
5052
}
5153

5254
public function getMarker(bool $selected) : string
@@ -61,8 +63,6 @@ public function getMarkerOn() : string
6163

6264
public function setMarkerOn(string $marker) : self
6365
{
64-
$this->custom = true;
65-
6666
$this->markerOn = $marker;
6767

6868
return $this;
@@ -75,8 +75,6 @@ public function getMarkerOff() : string
7575

7676
public function setMarkerOff(string $marker) : self
7777
{
78-
$this->custom = true;
79-
8078
$this->markerOff = $marker;
8179

8280
return $this;
@@ -89,8 +87,6 @@ public function getItemExtra() : string
8987

9088
public function setItemExtra(string $itemExtra) : self
9189
{
92-
$this->custom = true;
93-
9490
$this->itemExtra = $itemExtra;
9591

9692
// if we customise item extra, it means we most likely want to display it
@@ -106,8 +102,6 @@ public function getDisplaysExtra() : bool
106102

107103
public function setDisplaysExtra(bool $displaysExtra) : self
108104
{
109-
$this->custom = true;
110-
111105
$this->displaysExtra = $displaysExtra;
112106

113107
return $this;

src/Style/RadioStyle.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,14 @@ public function __construct()
4646

4747
public function hasChangedFromDefaults() : bool
4848
{
49-
return $this->custom;
49+
$currentValues = [
50+
$this->markerOn,
51+
$this->markerOff,
52+
$this->itemExtra,
53+
$this->displaysExtra,
54+
];
55+
56+
return $currentValues !== array_values(self::DEFAULT_STYLES);
5057
}
5158

5259
public function getMarker(bool $selected) : string
@@ -61,8 +68,6 @@ public function getMarkerOn() : string
6168

6269
public function setMarkerOn(string $marker) : self
6370
{
64-
$this->custom = true;
65-
6671
$this->markerOn = $marker;
6772

6873
return $this;
@@ -75,8 +80,6 @@ public function getMarkerOff() : string
7580

7681
public function setMarkerOff(string $marker) : self
7782
{
78-
$this->custom = true;
79-
8083
$this->markerOff = $marker;
8184

8285
return $this;
@@ -89,8 +92,6 @@ public function getItemExtra() : string
8992

9093
public function setItemExtra(string $itemExtra) : self
9194
{
92-
$this->custom = true;
93-
9495
$this->itemExtra = $itemExtra;
9596

9697
// if we customise item extra, it means we most likely want to display it
@@ -106,8 +107,6 @@ public function getDisplaysExtra() : bool
106107

107108
public function setDisplaysExtra(bool $displaysExtra) : self
108109
{
109-
$this->custom = true;
110-
111110
$this->displaysExtra = $displaysExtra;
112111

113112
return $this;

test/Style/CheckboxStyleTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpSchool\CliMenuTest\Style;
6+
7+
use PhpSchool\CliMenu\Style\CheckboxStyle;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class CheckboxStyleTest extends TestCase
11+
{
12+
public function testHasChangedFromDefaultsWhenNoStylesChanged() : void
13+
{
14+
self::assertFalse((new CheckboxStyle())->hasChangedFromDefaults());
15+
}
16+
17+
public function testGetMarker() : void
18+
{
19+
$style = new CheckboxStyle;
20+
21+
self::assertSame('[✔] ', $style->getMarker(true));
22+
self::assertSame('[ ] ', $style->getMarker(false));
23+
}
24+
25+
public function testGetSetMarkerOn() : void
26+
{
27+
$style = new CheckboxStyle;
28+
29+
self::assertSame('[✔] ', $style->getMarkerOn());
30+
31+
$style->setMarkerOn('[x] ');
32+
33+
self::assertSame('[x] ', $style->getMarkerOn());
34+
self::assertTrue($style->hasChangedFromDefaults());
35+
}
36+
37+
public function testGetSetMarkerOff() : void
38+
{
39+
$style = new CheckboxStyle;
40+
41+
self::assertSame('[ ] ', $style->getMarkerOff());
42+
43+
$style->setMarkerOff('( ) ');
44+
45+
self::assertSame('( ) ', $style->getMarkerOff());
46+
self::assertTrue($style->hasChangedFromDefaults());
47+
}
48+
49+
public function testGetSetItemExtra() : void
50+
{
51+
$style = new CheckboxStyle;
52+
53+
self::assertSame('', $style->getItemExtra());
54+
55+
$style->setItemExtra('[!EXTRA]!');
56+
57+
self::assertSame('[!EXTRA]!', $style->getItemExtra());
58+
self::assertTrue($style->hasChangedFromDefaults());
59+
}
60+
61+
public function testModifyingItemExtraForcesExtraToBeDisplayedWhenNoItemsDisplayExtra() : void
62+
{
63+
$style = new CheckboxStyle;
64+
self::assertFalse($style->getDisplaysExtra());
65+
66+
$style->setItemExtra('[!EXTRA]!');
67+
self::assertTrue($style->getDisplaysExtra());
68+
}
69+
70+
public function testGetSetDisplayExtra() : void
71+
{
72+
$style = new CheckboxStyle;
73+
74+
self::assertFalse($style->getDisplaysExtra());
75+
76+
$style->setDisplaysExtra(true);
77+
78+
self::assertTrue($style->getDisplaysExtra());
79+
self::assertTrue($style->hasChangedFromDefaults());
80+
}
81+
}

test/Style/RadioStyleTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpSchool\CliMenuTest\Style;
6+
7+
use PhpSchool\CliMenu\Style\RadioStyle;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class RadioStyleTest extends TestCase
11+
{
12+
public function testHasChangedFromDefaultsWhenNoStylesChanged() : void
13+
{
14+
self::assertFalse((new RadioStyle())->hasChangedFromDefaults());
15+
}
16+
17+
public function testGetMarker() : void
18+
{
19+
$style = new RadioStyle;
20+
21+
self::assertSame('[●] ', $style->getMarker(true));
22+
self::assertSame('[○] ', $style->getMarker(false));
23+
}
24+
25+
public function testGetSetMarkerOn() : void
26+
{
27+
$style = new RadioStyle;
28+
29+
self::assertSame('[●] ', $style->getMarkerOn());
30+
31+
$style->setMarkerOn('[x] ');
32+
33+
self::assertSame('[x] ', $style->getMarkerOn());
34+
self::assertTrue($style->hasChangedFromDefaults());
35+
}
36+
37+
public function testGetSetMarkerOff() : void
38+
{
39+
$style = new RadioStyle;
40+
41+
self::assertSame('[○] ', $style->getMarkerOff());
42+
43+
$style->setMarkerOff('( ) ');
44+
45+
self::assertSame('( ) ', $style->getMarkerOff());
46+
self::assertTrue($style->hasChangedFromDefaults());
47+
}
48+
49+
public function testGetSetItemExtra() : void
50+
{
51+
$style = new RadioStyle;
52+
53+
self::assertSame('', $style->getItemExtra());
54+
55+
$style->setItemExtra('[!EXTRA]!');
56+
57+
self::assertSame('[!EXTRA]!', $style->getItemExtra());
58+
self::assertTrue($style->hasChangedFromDefaults());
59+
}
60+
61+
public function testModifyingItemExtraForcesExtraToBeDisplayedWhenNoItemsDisplayExtra() : void
62+
{
63+
$style = new RadioStyle;
64+
self::assertFalse($style->getDisplaysExtra());
65+
66+
$style->setItemExtra('[!EXTRA]!');
67+
self::assertTrue($style->getDisplaysExtra());
68+
}
69+
70+
public function testGetSetDisplayExtra() : void
71+
{
72+
$style = new RadioStyle;
73+
74+
self::assertFalse($style->getDisplaysExtra());
75+
76+
$style->setDisplaysExtra(true);
77+
78+
self::assertTrue($style->getDisplaysExtra());
79+
self::assertTrue($style->hasChangedFromDefaults());
80+
}
81+
}

0 commit comments

Comments
 (0)