Skip to content

Commit

Permalink
Increase code coverage 100%. (#24)
Browse files Browse the repository at this point in the history
* Increase code coverage.
* Fix minor corrections.
* Update dev dependencies.
* Fix style.ci.
* Add more test support array options['class'].
* Fix issue psalm.
  • Loading branch information
terabytesoftw committed Dec 9, 2020
1 parent d5c6a72 commit b7d446f
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 24 deletions.
6 changes: 3 additions & 3 deletions composer.json
Expand Up @@ -30,10 +30,10 @@
"yiisoft/widget": "@dev"
},
"require-dev": {
"phpunit/phpunit": "^9.4",
"roave/infection-static-analysis-plugin": "^1.5",
"phpunit/phpunit": "^9.5",
"roave/infection-static-analysis-plugin": "^1.",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.2",
"vimeo/psalm": "^4.3",
"yiisoft/di": "^3.0@dev"
},
"autoload": {
Expand Down
5 changes: 3 additions & 2 deletions src/Breadcrumbs.php
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Yii\Bulma;

use InvalidArgumentException;
use JsonException;
use Yiisoft\Arrays\ArrayHelper;
use Yiisoft\Html\Html;
Expand Down Expand Up @@ -214,7 +215,7 @@ private function renderIcon(string $icon, array $iconOptions): string
* @param string $template the template to be used to rendered the link. The token "{link}" will be replaced by the
* link.
*
* @throws \InvalidArgumentException|JsonException if `$link` does not have "label" element.
* @throws InvalidArgumentException|JsonException if `$link` does not have "label" element.
*
* @return string the rendering result
*/
Expand All @@ -238,7 +239,7 @@ private function renderItem(array $link, string $template): string
if (array_key_exists('label', $link)) {
$label = $encodeLabel ? Html::encode($link['label']) : $link['label'];
} else {
throw new \InvalidArgumentException('The "label" element is required for each link.');
throw new InvalidArgumentException('The "label" element is required for each link.');
}

if (isset($link['template'])) {
Expand Down
5 changes: 3 additions & 2 deletions src/Dropdown.php
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Yii\Bulma;

use InvalidArgumentException;
use JsonException;
use Yiisoft\Arrays\ArrayHelper;
use Yiisoft\Html\Html;
Expand Down Expand Up @@ -288,7 +289,7 @@ private function buildOptions(): void
* @param array $items the menu items to be rendered
* @param array $itemsOptions the container HTML attributes
*
* @throws \InvalidArgumentException|JsonException if the label option is not specified in one of the items.
* @throws InvalidArgumentException|JsonException if the label option is not specified in one of the items.
*
* @return string the rendering result.
*/
Expand All @@ -303,7 +304,7 @@ private function renderItems(array $items, array $itemsOptions = []): string
}

if (!isset($item['label']) && $item !== '-') {
throw new \InvalidArgumentException('The "label" option is required.');
throw new InvalidArgumentException('The "label" option is required.');
}

$this->encodeLabels = $item['encode'] ?? $this->encodeLabels;
Expand Down
5 changes: 3 additions & 2 deletions src/Modal.php
Expand Up @@ -5,6 +5,7 @@
namespace Yiisoft\Yii\Bulma;

use InvalidArgumentException;
use JsonException;
use Yiisoft\Html\Html;

/**
Expand Down Expand Up @@ -219,7 +220,7 @@ public function toggleButtonEnabled(bool $value): self
/**
* Renders the toggle button.
*
* @throws \JsonException
* @throws JsonException
*
* @return string
*/
Expand Down Expand Up @@ -287,7 +288,7 @@ public function closeButtonEnabled(bool $value): self
/**
* Renders the close button.
*
* @throws \JsonException
* @throws JsonException
*
* @return string
*/
Expand Down
21 changes: 11 additions & 10 deletions src/Nav.php
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Yii\Bulma;

use InvalidArgumentException;
use JsonException;
use Yiisoft\Arrays\ArrayHelper;
use Yiisoft\Html\Html;
Expand Down Expand Up @@ -128,7 +129,7 @@ public function items(array $value): self
* @param array $parentItem the parent item information. Please refer to {@see items} for the structure of this
* array.
*
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*
* @return string the rendering result.
*/
Expand Down Expand Up @@ -170,7 +171,7 @@ private function isChildActive(array $items, bool &$active): array
$items[$i]['items'] = $this->isChildActive($childItems, $activeParent);

if ($activeParent) {
$items[$i]['options'] ??= [];
$items[$i]['options'] ??= ['class' => ''];
Html::addCssClass($items[$i]['options'], 'active');
$active = $activeParent;
}
Expand Down Expand Up @@ -198,11 +199,11 @@ private function isItemActive($item): bool
return ArrayHelper::getValue($item, 'active');
}

return (bool) (isset($item['url']) && $this->currentPath !== '/' && $item['url'] === $this->currentPath && $this->activateItems)



;
return
isset($item['url']) &&
$this->currentPath !== '/' &&
$item['url'] === $this->currentPath &&
$this->activateItems;
}

private function renderIcon(string $label, string $icon, array $iconOptions): string
Expand All @@ -220,7 +221,7 @@ private function renderIcon(string $label, string $icon, array $iconOptions): st
/**
* Renders widget items.
*
* @throws \InvalidArgumentException|JsonException
* @throws InvalidArgumentException|JsonException
*
* @return string
*/
Expand All @@ -242,14 +243,14 @@ private function renderItems(): string
*
* @param array $item the item to render.
*
* @throws \InvalidArgumentException|JsonException
* @throws InvalidArgumentException|JsonException
*
* @return string the rendering result.
*/
private function renderItem(array $item): string
{
if (!isset($item['label'])) {
throw new \InvalidArgumentException('The "label" option is required.');
throw new InvalidArgumentException('The "label" option is required.');
}

$this->encodeLabels = $item['encode'] ?? $this->encodeLabels;
Expand Down
6 changes: 4 additions & 2 deletions src/NavBar.php
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Yii\Bulma;

use InvalidArgumentException;
use JsonException;
use Yiisoft\Arrays\ArrayHelper;
use Yiisoft\Html\Html;
Expand Down Expand Up @@ -45,7 +46,7 @@ public function begin(): ?string
$navOptions = $this->options;
$navTag = ArrayHelper::remove($navOptions, 'tag', 'nav');
if (!is_string($navTag) && !is_bool($navTag) && $navTag !== null) {
throw new \InvalidArgumentException('Tag should be either string, bool or null.');
throw new InvalidArgumentException('Tag should be either string, bool or null.');
}

return
Expand All @@ -59,7 +60,7 @@ protected function run(): string
{
$tag = ArrayHelper::remove($this->options, 'tag', 'nav');
if (!is_string($tag) && !is_bool($tag) && $tag !== null) {
throw new \InvalidArgumentException('Tag should be either string, bool or null.');
throw new InvalidArgumentException('Tag should be either string, bool or null.');
}

return
Expand Down Expand Up @@ -278,6 +279,7 @@ private function initItemsOptions(): void
if (isset($this->itemsOptions['class'])) {
$itemsClass = $this->itemsOptions['class'];
unset($this->itemsOptions['class']);

if (is_array($itemsClass)) {
$itemsClass = implode(' ', $itemsClass);
}
Expand Down
1 change: 1 addition & 0 deletions src/Widget.php
Expand Up @@ -85,6 +85,7 @@ protected function addOptions(array $options, string $defaultClass): array
if (isset($options['class'])) {
$class = $options['class'];
unset($options['class']);

if (is_array($class)) {
$class = implode(' ', $class);
}
Expand Down
23 changes: 22 additions & 1 deletion tests/BreadcrumbsTest.php
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Yii\Bulma\Tests;

use InvalidArgumentException;
use Yiisoft\Yii\Bulma\Breadcrumbs;

final class BreadcrumbsTest extends TestCase
Expand Down Expand Up @@ -209,7 +210,7 @@ public function testBreadcrumbsLinksException(): void
{
Breadcrumbs::counter(0);

$this->expectException(\InvalidArgumentException::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The "label" element is required for each link.');
$html = Breadcrumbs::widget()
->items([['url' => '/about', 'template' => '<div>{link}</div>']])
Expand Down Expand Up @@ -304,6 +305,26 @@ public function testBreadcrumbsIcons(): void
<li><span class="icon"><i class="fas fa-thumbs-up"></i></span><a href="/about">About</a></li>
</ul>
</nav>
HTML;

$this->assertEqualsWithoutLE($expected, $html);
}

public function testBreadcrumbsWithoutHomeItem(): void
{
Breadcrumbs::counter(0);

$html = Breadcrumbs::widget()
->items([['label' => 'About', 'url' => '/about']])
->withoutHomeItem()
->render();

$expected = <<<HTML
<nav id="w1-breadcrumbs" class="breadcrumb" aria-label="breadcrumbs">
<ul>
<li><a href="/about">About</a></li>
</ul>
</nav>
HTML;

$this->assertEqualsWithoutLE($expected, $html);
Expand Down
49 changes: 49 additions & 0 deletions tests/ModalCardTest.php
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Yii\Bulma\Tests;

use InvalidArgumentException;
use Yiisoft\Yii\Bulma\ModalCard;

final class ModalCardTest extends TestCase
Expand Down Expand Up @@ -464,6 +465,54 @@ public function testTitleOptions(): void
<footer class="modal-card-foot"></footer>
</div>
</div>
HTML;

$this->assertEqualsWithoutLE($expectedHtml, $html);
}

public function testExceptionToggleButtonSize(): void
{
$this->expectException(InvalidArgumentException::class);

ModalCard::widget()->toggleButtonSize('is-non-existent')->begin();
}

public function testExceptionToggleButtonColor(): void
{
$this->expectException(InvalidArgumentException::class);

ModalCard::widget()->toggleButtonColor('is-non-existent')->begin();
}

public function testExceptionToggleCloseButtonSize(): void
{
$this->expectException(InvalidArgumentException::class);

ModalCard::widget()->closeButtonSize('is-non-existent')->begin();
}

public function testModalCardOptions(): void
{
ModalCard::counter(0);

$html = ModalCard::widget()->options(['class' => 'testMe'])->begin();
$html .= 'Say hello...';
$html .= ModalCard::end();

$expectedHtml = <<<HTML
<button type="button" class="button" data-target="#w1-modal" aria-haspopup="true">Toggle button</button>
<div id="w1-modal" class="modal testMe">
<div class="modal-background"></div>
<div class="modal-card">
<header class="modal-card-head">
<p class="modal-card-title"></p>
<button type="button" class="delete" aria-label="close"></button>
</header>
<section class="modal-card-body">
Say hello...</section>
<footer class="modal-card-foot"></footer>
</div>
</div>
HTML;

$this->assertEqualsWithoutLE($expectedHtml, $html);
Expand Down
40 changes: 38 additions & 2 deletions tests/ModalTest.php
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Yii\Bulma\Tests;

use InvalidArgumentException;
use Yiisoft\Yii\Bulma\Modal;

final class ModalTest extends TestCase
Expand Down Expand Up @@ -232,12 +233,47 @@ public function testContentOptions(): void
$this->assertEqualsWithoutLE($expectedHtml, $html);
}

public function testException()
public function testExceptionToggleButtonSize(): void
{
$this->expectException(\InvalidArgumentException::class);
$this->expectException(InvalidArgumentException::class);

Modal::widget()->toggleButtonSize('is-non-existent');
}

public function testExceptionToggleButtonColor(): void
{
$this->expectException(InvalidArgumentException::class);

Modal::widget()->toggleButtonColor('is-non-existent');
}

public function testExceptionToggleCloseButtonSize(): void
{
$this->expectException(InvalidArgumentException::class);

Modal::widget()->closeButtonSize('is-non-existent');
}

public function testToggleButtonOptions(): void
{
Modal::counter(0);

$html = Modal::widget()
->closeButtonEnabled(false)
->toggleButtonOptions(['class' => 'testMe'])
->begin();
$html .= Modal::end();

$expectedHtml = <<<HTML
<button type="button" class="button testMe" data-target="#w1-modal" aria-haspopup="true">Toggle button</button>
<div id="w1-modal" class="modal">
<div class="modal-background"></div>
<div class="modal-content">
</div>
</div>
HTML;

$this->assertEqualsWithoutLE($expectedHtml, $html);
}
}

0 comments on commit b7d446f

Please sign in to comment.