Skip to content

Commit

Permalink
test2
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Feb 11, 2024
1 parent d7ed810 commit bf0cfea
Show file tree
Hide file tree
Showing 9 changed files with 302 additions and 183 deletions.
12 changes: 12 additions & 0 deletions src/ButtonDropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ final class ButtonDropdown extends Widget
private bool $encodeTags = false;
/** @psalm-var class-string|Dropdown */
private string|Dropdown $dropdownClass = Dropdown::class;
private ?array $dropdownOptions = null;
private bool $renderContainer = true;

public function getId(?string $suffix = '-button-dropdown'): ?string
Expand Down Expand Up @@ -151,6 +152,13 @@ public function dropdownClass(string|Dropdown $value): self
return $new;
}

public function dropdownOptions(?array $options): self
{
$new = clone $this;
$new->dropdownOptions = $options;
return $new;
}

/**
* When tags Labels HTML should not be encoded.
*/
Expand Down Expand Up @@ -331,6 +339,10 @@ private function renderDropdown(): string
$dropdown = $this->dropdownClass->items($this->items);
}

if ($this->dropdownOptions !== null) {
$dropdown = $dropdown->options($this->dropdownOptions);
}

if ($this->theme && !$this->renderContainer) {
$dropdown = $dropdown->withTheme($this->theme);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ButtonGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ final class ButtonGroup extends Widget
public function render(): string
{
if (!isset($this->options['id'])) {
$this->options['id'] = "{$this->getId()}-button-group";
$this->options['id'] = $this->getId();
}

/** @psalm-suppress InvalidArgument */
Expand Down
2 changes: 1 addition & 1 deletion src/ButtonToolbar.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ final class ButtonToolbar extends Widget
public function render(): string
{
if (!isset($this->options['id'])) {
$this->options['id'] = "{$this->getId()}-button-toolbar";
$this->options['id'] = $this->getId();
}

/** @psalm-suppress InvalidArgument */
Expand Down
152 changes: 93 additions & 59 deletions tests/AccordionTest.php

Large diffs are not rendered by default.

100 changes: 69 additions & 31 deletions tests/ButtonDropdownTest.php

Large diffs are not rendered by default.

52 changes: 28 additions & 24 deletions tests/ButtonGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,87 +8,91 @@
use Yiisoft\Yii\Bootstrap5\ButtonGroup;

/**
* Tests for Button widget.
*
* ButtonGroupTest
* Tests for `ButtonGroup` widget.
*/
final class ButtonGroupTest extends TestCase
{
public function testRender(): void
{
$html = ButtonGroup::widget()
->id('TEST_ID')
->buttons([
['label' => 'button-A'],
['label' => 'button-B', 'visible' => true],
['label' => 'button-A', 'options' => ['id' => 'BTN1']],
['label' => 'button-B', 'visible' => true, 'options' => ['id' => 'BTN2']],
['label' => 'button-C', 'visible' => false],
Button::widget()
->id('BTN4')
->label('button-D')
->render(),
])
->render();
$expected = <<<'HTML'
<div id="w1-button-group" class="btn-group" role="group"><button type="button" id="w2-button" class="btn">button-A</button>
<button type="button" id="w3-button" class="btn">button-B</button>
<button id="w0-button" class="btn">button-D</button></div>
<div id="TEST_ID" class="btn-group" role="group"><button type="button" id="BTN1" class="btn">button-A</button>
<button type="button" id="BTN2" class="btn">button-B</button>
<button id="BTN4" class="btn">button-D</button></div>
HTML;
$this->assertEqualsWithoutLE($expected, $html);
}

public function testButtonOptions(): void
{
$html = ButtonGroup::widget()
->id('TEST_ID')
->buttons([
['label' => 'button-A', 'options' => ['class' => 'btn-primary', 'type' => 'submit']],
['label' => 'button-B'],
['label' => 'button-A', 'options' => ['class' => 'btn-primary', 'type' => 'submit', 'id' => 'BTN1']],
['label' => 'button-B', 'options' => ['id' => 'BTN2']],
])
->render();
$expected = <<<'HTML'
<div id="w0-button-group" class="btn-group" role="group"><button type="submit" id="w1-button" class="btn-primary btn">button-A</button>
<button type="button" id="w2-button" class="btn">button-B</button></div>
<div id="TEST_ID" class="btn-group" role="group"><button type="submit" id="BTN1" class="btn-primary btn">button-A</button>
<button type="button" id="BTN2" class="btn">button-B</button></div>
HTML;
$this->assertEqualsWithoutLE($expected, $html);
}

public function testEncodeLabes(): void
{
$html = ButtonGroup::widget()
->id('TEST_ID')
->buttons([
['label' => 'button-A', 'options' => ['class' => 'btn-primary', 'type' => 'submit']],
['label' => '<span><i class=fas fas-test></i>button-B</span>'],
['label' => 'button-A', 'options' => ['class' => 'btn-primary', 'type' => 'submit', 'id' => 'BTN1']],
['label' => '<span><i class=fas fas-test></i>button-B</span>', 'options' => ['id' => 'BTN2']],
])
->render();
$expected = <<<'HTML'
<div id="w0-button-group" class="btn-group" role="group"><button type="submit" id="w1-button" class="btn-primary btn">button-A</button>
<button type="button" id="w2-button" class="btn">&lt;span&gt;&lt;i class=fas fas-test&gt;&lt;/i&gt;button-B&lt;/span&gt;</button></div>
<div id="TEST_ID" class="btn-group" role="group"><button type="submit" id="BTN1" class="btn-primary btn">button-A</button>
<button type="button" id="BTN2" class="btn">&lt;span&gt;&lt;i class=fas fas-test&gt;&lt;/i&gt;button-B&lt;/span&gt;</button></div>
HTML;
$this->assertEqualsWithoutLE($expected, $html);

$html = ButtonGroup::widget()
->id('TEST_ID')
->buttons([
['label' => 'button-A', 'options' => ['class' => 'btn-primary', 'type' => 'submit']],
['label' => '<span><i class=fas fas-test></i>button-B</span>'],
['label' => 'button-A', 'options' => ['class' => 'btn-primary', 'type' => 'submit', 'id' => 'BTN1']],
['label' => '<span><i class=fas fas-test></i>button-B</span>', 'options' => ['id' => 'BTN2']],
])
->withoutEncodeLabels()
->render();
$expected = <<<'HTML'
<div id="w3-button-group" class="btn-group" role="group"><button type="submit" id="w4-button" class="btn-primary btn">button-A</button>
<button type="button" id="w5-button" class="btn"><span><i class=fas fas-test></i>button-B</span></button></div>
<div id="TEST_ID" class="btn-group" role="group"><button type="submit" id="BTN1" class="btn-primary btn">button-A</button>
<button type="button" id="BTN2" class="btn"><span><i class=fas fas-test></i>button-B</span></button></div>
HTML;
$this->assertEqualsWithoutLE($expected, $html);
}

public function testOptions(): void
{
$html = ButtonGroup::widget()
->id('TEST_ID')
->buttons([
['label' => 'button-A', 'options' => ['class' => 'btn-primary', 'type' => 'submit']],
['label' => 'button-B'],
['label' => 'button-A', 'options' => ['class' => 'btn-primary', 'type' => 'submit', 'id' => 'BTN1']],
['label' => 'button-B', 'options' => ['id' => 'BTN2']],
])
->options(['class' => 'btn-lg'])
->render();
$expected = <<<'HTML'
<div id="w0-button-group" class="btn-lg btn-group" role="group"><button type="submit" id="w1-button" class="btn-primary btn">button-A</button>
<button type="button" id="w2-button" class="btn">button-B</button></div>
<div id="TEST_ID" class="btn-lg btn-group" role="group"><button type="submit" id="BTN1" class="btn-primary btn">button-A</button>
<button type="button" id="BTN2" class="btn">button-B</button></div>
HTML;
$this->assertEqualsWithoutLE($expected, $html);
}
Expand Down
66 changes: 36 additions & 30 deletions tests/ButtonToolbarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,82 +8,86 @@
use Yiisoft\Yii\Bootstrap5\ButtonToolbar;

/**
* Tests for ButtonToolbar widget.
*
* ButtonToolbarTest
* Tests for `ButtonToolbar` widget.
*/
final class ButtonToolbarTest extends TestCase
{
public function testRender(): void
{
$html = ButtonToolbar::widget()
->id('TEST_ID')
->options([
'aria-label' => 'Toolbar with button groups',
])
->buttonGroups([
ButtonGroup::widget()
->id('BG1')
->options([
'aria-label' => 'First group',
'class' => ['mr-2'],
])
->buttons([
['label' => '1'],
['label' => '2'],
['label' => '3'],
['label' => '4'],
['label' => '1', 'options' => ['id' => 'BTN1']],
['label' => '2', 'options' => ['id' => 'BTN2']],
['label' => '3', 'options' => ['id' => 'BTN3']],
['label' => '4', 'options' => ['id' => 'BTN4']],
])
->render(),
[
'options' => [
'id' => 'BG2',
'aria-label' => 'Second group',
],
'buttons' => [
['label' => '5'],
['label' => '6'],
['label' => '7'],
['label' => '5', 'options' => ['id' => 'BTN5']],
['label' => '6', 'options' => ['id' => 'BTN6']],
['label' => '7', 'options' => ['id' => 'BTN7']],
],
],
])
->render();
$expected = <<<'HTML'
<div id="w5-button-toolbar" class="btn-toolbar" aria-label="Toolbar with button groups" role="toolbar"><div id="w0-button-group" class="mr-2 btn-group" aria-label="First group" role="group"><button type="button" id="w1-button" class="btn">1</button>
<button type="button" id="w2-button" class="btn">2</button>
<button type="button" id="w3-button" class="btn">3</button>
<button type="button" id="w4-button" class="btn">4</button></div>
<div id="w6-button-group" class="btn-group" aria-label="Second group" role="group"><button type="button" id="w7-button" class="btn">5</button>
<button type="button" id="w8-button" class="btn">6</button>
<button type="button" id="w9-button" class="btn">7</button></div></div>
<div id="TEST_ID" class="btn-toolbar" aria-label="Toolbar with button groups" role="toolbar"><div id="BG1" class="mr-2 btn-group" aria-label="First group" role="group"><button type="button" id="BTN1" class="btn">1</button>
<button type="button" id="BTN2" class="btn">2</button>
<button type="button" id="BTN3" class="btn">3</button>
<button type="button" id="BTN4" class="btn">4</button></div>
<div id="BG2" class="btn-group" aria-label="Second group" role="group"><button type="button" id="BTN5" class="btn">5</button>
<button type="button" id="BTN6" class="btn">6</button>
<button type="button" id="BTN7" class="btn">7</button></div></div>
HTML;
$this->assertEqualsWithoutLE($expected, $html);
}

public function testButtonGroupEmpty(): void
{
$html = ButtonToolbar::widget()
->id('TEST_ID')
->buttonGroups([[]])
->render();
$expected = <<<'HTML'
<div id="w0-button-toolbar" class="btn-toolbar" role="toolbar"></div>
<div id="TEST_ID" class="btn-toolbar" role="toolbar"></div>
HTML;
$this->assertEqualsWithoutLE($expected, $html);
}

public function testButtonOptions(): void
{
$html = ButtonToolbar::widget()
->id('TEST_ID')
->buttonGroups([
[
'options' => ['id' => 'BG1'],
'buttons' => [
['label' => '1', 'options' => ['class' => 'btn-secondary', 'tabindex' => 2, 'type' => 'reset']],
['label' => '2', 'options' => ['class' => 'btn-primary', 'tabindex' => 1, 'type' => 'submit']],
['label' => '1', 'options' => ['id' => 'BTN1', 'class' => 'btn-secondary', 'tabindex' => 2, 'type' => 'reset']],
['label' => '2', 'options' => ['id' => 'BTN2', 'class' => 'btn-primary', 'tabindex' => 1, 'type' => 'submit']],
],
'class' => ['mr-2'],
],
])
->render();
$expected = <<<'HTML'
<div id="w0-button-toolbar" class="btn-toolbar" role="toolbar"><div id="w1-button-group" class="btn-group" role="group"><button type="reset" id="w2-button" class="btn-secondary btn" tabindex="2">1</button>
<button type="submit" id="w3-button" class="btn-primary btn" tabindex="1">2</button></div></div>
<div id="TEST_ID" class="btn-toolbar" role="toolbar"><div id="BG1" class="btn-group" role="group"><button type="reset" id="BTN1" class="btn-secondary btn" tabindex="2">1</button>
<button type="submit" id="BTN2" class="btn-primary btn" tabindex="1">2</button></div></div>
HTML;
$this->assertEqualsWithoutLE($expected, $html);
}
Expand All @@ -99,30 +103,32 @@ public function testAdditionalContent(): void
</div>
HTML;
$html = ButtonToolbar::widget()
->id('TEST_ID')
->options([
'aria-label' => 'Toolbar with button groups',
])
->buttonGroups([
[
'options' => [
'id' => 'BG1',
'aria-label' => 'First group',
'class' => ['mr-2'],
],
'buttons' => [
['label' => '1'],
['label' => '2'],
['label' => '3'],
['label' => '4'],
['label' => '1', 'options' => ['id' => 'BTN1']],
['label' => '2', 'options' => ['id' => 'BTN2']],
['label' => '3', 'options' => ['id' => 'BTN3']],
['label' => '4', 'options' => ['id' => 'BTN4']],
],
],
$addHtml,
])
->render();
$expected = <<<'HTML'
<div id="w0-button-toolbar" class="btn-toolbar" aria-label="Toolbar with button groups" role="toolbar"><div id="w1-button-group" class="mr-2 btn-group" aria-label="First group" role="group"><button type="button" id="w2-button" class="btn">1</button>
<button type="button" id="w3-button" class="btn">2</button>
<button type="button" id="w4-button" class="btn">3</button>
<button type="button" id="w5-button" class="btn">4</button></div>
<div id="TEST_ID" class="btn-toolbar" aria-label="Toolbar with button groups" role="toolbar"><div id="BG1" class="mr-2 btn-group" aria-label="First group" role="group"><button type="button" id="BTN1" class="btn">1</button>
<button type="button" id="BTN2" class="btn">2</button>
<button type="button" id="BTN3" class="btn">3</button>
<button type="button" id="BTN4" class="btn">4</button></div>
<div class="input-group">
<div class="input-group-prepend">
<div class="input-group-text" id="btnGroupAddon">@</div>
Expand Down
Loading

0 comments on commit bf0cfea

Please sign in to comment.