Skip to content

Tests for CheckboxStyle & RadioStyle #213

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 1 commit into from
Dec 20, 2019
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
22 changes: 8 additions & 14 deletions src/Style/CheckboxStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,6 @@ class CheckboxStyle
*/
private $displaysExtra;

/**
* @var bool
*/
private $custom = false;

public function __construct()
{
$this->markerOn = self::DEFAULT_STYLES['markerOn'];
Expand All @@ -46,7 +41,14 @@ public function __construct()

public function hasChangedFromDefaults() : bool
{
return $this->custom;
$currentValues = [
$this->markerOn,
$this->markerOff,
$this->itemExtra,
$this->displaysExtra,
];

return $currentValues !== array_values(self::DEFAULT_STYLES);
}

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

public function setMarkerOn(string $marker) : self
{
$this->custom = true;

$this->markerOn = $marker;

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

public function setMarkerOff(string $marker) : self
{
$this->custom = true;

$this->markerOff = $marker;

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

public function setItemExtra(string $itemExtra) : self
{
$this->custom = true;

$this->itemExtra = $itemExtra;

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

public function setDisplaysExtra(bool $displaysExtra) : self
{
$this->custom = true;

$this->displaysExtra = $displaysExtra;

return $this;
Expand Down
17 changes: 8 additions & 9 deletions src/Style/RadioStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ public function __construct()

public function hasChangedFromDefaults() : bool
{
return $this->custom;
$currentValues = [
$this->markerOn,
$this->markerOff,
$this->itemExtra,
$this->displaysExtra,
];

return $currentValues !== array_values(self::DEFAULT_STYLES);
}

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

public function setMarkerOn(string $marker) : self
{
$this->custom = true;

$this->markerOn = $marker;

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

public function setMarkerOff(string $marker) : self
{
$this->custom = true;

$this->markerOff = $marker;

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

public function setItemExtra(string $itemExtra) : self
{
$this->custom = true;

$this->itemExtra = $itemExtra;

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

public function setDisplaysExtra(bool $displaysExtra) : self
{
$this->custom = true;

$this->displaysExtra = $displaysExtra;

return $this;
Expand Down
81 changes: 81 additions & 0 deletions test/Style/CheckboxStyleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

namespace PhpSchool\CliMenuTest\Style;

use PhpSchool\CliMenu\Style\CheckboxStyle;
use PHPUnit\Framework\TestCase;

class CheckboxStyleTest extends TestCase
{
public function testHasChangedFromDefaultsWhenNoStylesChanged() : void
{
self::assertFalse((new CheckboxStyle())->hasChangedFromDefaults());
}

public function testGetMarker() : void
{
$style = new CheckboxStyle;

self::assertSame('[✔] ', $style->getMarker(true));
self::assertSame('[ ] ', $style->getMarker(false));
}

public function testGetSetMarkerOn() : void
{
$style = new CheckboxStyle;

self::assertSame('[✔] ', $style->getMarkerOn());

$style->setMarkerOn('[x] ');

self::assertSame('[x] ', $style->getMarkerOn());
self::assertTrue($style->hasChangedFromDefaults());
}

public function testGetSetMarkerOff() : void
{
$style = new CheckboxStyle;

self::assertSame('[ ] ', $style->getMarkerOff());

$style->setMarkerOff('( ) ');

self::assertSame('( ) ', $style->getMarkerOff());
self::assertTrue($style->hasChangedFromDefaults());
}

public function testGetSetItemExtra() : void
{
$style = new CheckboxStyle;

self::assertSame('✔', $style->getItemExtra());

$style->setItemExtra('[!EXTRA]!');

self::assertSame('[!EXTRA]!', $style->getItemExtra());
self::assertTrue($style->hasChangedFromDefaults());
}

public function testModifyingItemExtraForcesExtraToBeDisplayedWhenNoItemsDisplayExtra() : void
{
$style = new CheckboxStyle;
self::assertFalse($style->getDisplaysExtra());

$style->setItemExtra('[!EXTRA]!');
self::assertTrue($style->getDisplaysExtra());
}

public function testGetSetDisplayExtra() : void
{
$style = new CheckboxStyle;

self::assertFalse($style->getDisplaysExtra());

$style->setDisplaysExtra(true);

self::assertTrue($style->getDisplaysExtra());
self::assertTrue($style->hasChangedFromDefaults());
}
}
81 changes: 81 additions & 0 deletions test/Style/RadioStyleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

namespace PhpSchool\CliMenuTest\Style;

use PhpSchool\CliMenu\Style\RadioStyle;
use PHPUnit\Framework\TestCase;

class RadioStyleTest extends TestCase
{
public function testHasChangedFromDefaultsWhenNoStylesChanged() : void
{
self::assertFalse((new RadioStyle())->hasChangedFromDefaults());
}

public function testGetMarker() : void
{
$style = new RadioStyle;

self::assertSame('[●] ', $style->getMarker(true));
self::assertSame('[○] ', $style->getMarker(false));
}

public function testGetSetMarkerOn() : void
{
$style = new RadioStyle;

self::assertSame('[●] ', $style->getMarkerOn());

$style->setMarkerOn('[x] ');

self::assertSame('[x] ', $style->getMarkerOn());
self::assertTrue($style->hasChangedFromDefaults());
}

public function testGetSetMarkerOff() : void
{
$style = new RadioStyle;

self::assertSame('[○] ', $style->getMarkerOff());

$style->setMarkerOff('( ) ');

self::assertSame('( ) ', $style->getMarkerOff());
self::assertTrue($style->hasChangedFromDefaults());
}

public function testGetSetItemExtra() : void
{
$style = new RadioStyle;

self::assertSame('✔', $style->getItemExtra());

$style->setItemExtra('[!EXTRA]!');

self::assertSame('[!EXTRA]!', $style->getItemExtra());
self::assertTrue($style->hasChangedFromDefaults());
}

public function testModifyingItemExtraForcesExtraToBeDisplayedWhenNoItemsDisplayExtra() : void
{
$style = new RadioStyle;
self::assertFalse($style->getDisplaysExtra());

$style->setItemExtra('[!EXTRA]!');
self::assertTrue($style->getDisplaysExtra());
}

public function testGetSetDisplayExtra() : void
{
$style = new RadioStyle;

self::assertFalse($style->getDisplaysExtra());

$style->setDisplaysExtra(true);

self::assertTrue($style->getDisplaysExtra());
self::assertTrue($style->hasChangedFromDefaults());
}
}