Skip to content

Commit 0dc0271

Browse files
committed
Tests: convert from PHPUnit to Nette Tester
1 parent b43e93a commit 0dc0271

38 files changed

Lines changed: 697 additions & 729 deletions

tests/BaseTest.php

Lines changed: 0 additions & 15 deletions
This file was deleted.

tests/BootstrapFormTest.php

Lines changed: 0 additions & 39 deletions
This file was deleted.

tests/BootstrapUtilsTest.php

Lines changed: 0 additions & 18 deletions
This file was deleted.

tests/Cases/BootstrapFormTest.phpt

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Tests\Cases;
4+
5+
use Contributte\FormsBootstrap\BootstrapForm;
6+
use Contributte\FormsBootstrap\Enums\BootstrapVersion;
7+
use Contributte\FormsBootstrap\Enums\RenderMode;
8+
use Contributte\Tester\Toolkit;
9+
use Nette\Forms\Rendering\DefaultFormRenderer;
10+
use Tester\Assert;
11+
12+
require __DIR__ . '/../bootstrap.php';
13+
14+
Toolkit::test(function (): void {
15+
Assert::exception(function (): void {
16+
$form = new BootstrapForm();
17+
$form->setRenderer(new DefaultFormRenderer());
18+
}, \Throwable::class, 'Renderer must be a BootstrapRenderer class');
19+
});
20+
21+
Toolkit::test(function (): void {
22+
$form = new BootstrapForm();
23+
$form->setAutoShowValidation(true);
24+
Assert::true($form->isAutoShowValidation());
25+
26+
$form->setAjax(true);
27+
Assert::true($form->isAjax());
28+
29+
$form->setRenderMode(RenderMode::SIDE_BY_SIDE_MODE);
30+
Assert::equal(RenderMode::SIDE_BY_SIDE_MODE, $form->getRenderMode());
31+
32+
BootstrapForm::switchBootstrapVersion(BootstrapVersion::V5);
33+
Assert::equal(BootstrapVersion::V5, BootstrapForm::getBootstrapVersion());
34+
35+
BootstrapForm::switchBootstrapVersion(BootstrapVersion::V4);
36+
Assert::equal(BootstrapVersion::V4, BootstrapForm::getBootstrapVersion());
37+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Tests\Cases;
4+
5+
use Contributte\FormsBootstrap\BootstrapUtils;
6+
use Contributte\Tester\Toolkit;
7+
use Nette\Utils\Html;
8+
use Tester\Assert;
9+
10+
require __DIR__ . '/../bootstrap.php';
11+
12+
Toolkit::test(function (): void {
13+
$html = Html::el('div', ['class' => 'c1 c2']);
14+
BootstrapUtils::standardizeClass($html);
15+
Assert::equal(['c1', 'c2'], $html->class);
16+
});
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Tests\Cases\Grid;
4+
5+
use Contributte\FormsBootstrap\BootstrapForm;
6+
use Contributte\Tester\Toolkit;
7+
use Mockery;
8+
use Nette\Application\UI\Presenter;
9+
use Nette\InvalidArgumentException;
10+
use Tester\Assert;
11+
12+
require __DIR__ . '/../../bootstrap.php';
13+
14+
Toolkit::test(function (): void {
15+
$form = new BootstrapForm();
16+
$row = $form->addRow();
17+
$cell = $row->addCell(12);
18+
$form->setParent(Mockery::mock(Presenter::class)->shouldIgnoreMissing());
19+
20+
$cell->addHtmlClass('new-class');
21+
Assert::same('<div class="new-class col-sm-12"></div>', $cell->render()->render());
22+
});
23+
24+
Toolkit::test(function (): void {
25+
$form = new BootstrapForm();
26+
$row = $form->addRow();
27+
$cell = $row->addCell(12);
28+
$form->setParent(Mockery::mock(Presenter::class)->shouldIgnoreMissing());
29+
30+
$cell->addSubmit('first', 'First');
31+
$cell->addSubmit('second', 'Second');
32+
33+
ob_start();
34+
$form->render();
35+
$output = ob_get_clean();
36+
37+
Assert::same(file_get_contents(__DIR__ . '/../../data/cell_with_2_submits.html'), $output);
38+
});
39+
40+
Toolkit::test(function (): void {
41+
$form = new BootstrapForm();
42+
$row = $form->addRow();
43+
44+
Assert::exception(function () use ($row): void {
45+
$row->addCell(15);
46+
}, InvalidArgumentException::class);
47+
});
48+
49+
Toolkit::test(function (): void {
50+
$form = new BootstrapForm();
51+
$row = $form->addRow();
52+
$cell = $row->addCell(12);
53+
$form->setParent(Mockery::mock(Presenter::class)->shouldIgnoreMissing());
54+
55+
$first = $form->addGroup('first');
56+
$first->add($form->addText('a'));
57+
$form->addGroup('second');
58+
$cell->setCurrentGroup($first);
59+
60+
Assert::same($first, $cell->getCurrentGroup());
61+
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Tests\Cases\Grid;
4+
5+
use Contributte\FormsBootstrap\BootstrapForm;
6+
use Contributte\FormsBootstrap\BootstrapRenderer;
7+
use Contributte\FormsBootstrap\Enums\RenderMode;
8+
use Contributte\Tester\Toolkit;
9+
use Mockery;
10+
use Nette\Application\UI\Presenter;
11+
use Tester\Assert;
12+
13+
require __DIR__ . '/../../bootstrap.php';
14+
15+
Toolkit::test(function (): void {
16+
$form = new BootstrapForm();
17+
$row1 = $form->addRow('line1');
18+
$row1->getElementPrototype()->setAttribute('class', 'class1');
19+
$row1->addCell(6)
20+
->addSelect('category', 'Category', [1 => 'Category 1', 2 => 'Category 2'])
21+
->setHtmlAttribute('class', 'selectpicker form-control')
22+
->setHtmlAttribute('title', 'Choose ...')
23+
->setRequired(true)
24+
->getLabelPrototype()
25+
->setClass('required');
26+
27+
$row1->addCell(6)
28+
->addSelect('type', 'Type', [1 => 'Type 1', 2 => 'Type 2'])
29+
->setHtmlAttribute('class', 'selectpicker form-control')
30+
->setHtmlAttribute('title', 'Choose ...')
31+
->setRequired(true)
32+
->getLabelPrototype()
33+
->setClass('required');
34+
35+
$form->addGroup('Group 1', false)
36+
->add([$row1]);
37+
$form->setRenderer(new BootstrapRenderer(RenderMode::SIDE_BY_SIDE_MODE));
38+
$form->setParent(Mockery::mock(Presenter::class)->shouldIgnoreMissing());
39+
40+
ob_start();
41+
$form->render();
42+
$output = ob_get_clean();
43+
44+
Assert::same(file_get_contents(__DIR__ . '/../../data/bootstrap_group_row_rendering.html'), $output);
45+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Tests\Cases\Inputs;
4+
5+
use Contributte\FormsBootstrap\BootstrapForm;
6+
use Contributte\Tester\Toolkit;
7+
use Tester\Assert;
8+
9+
require __DIR__ . '/../../bootstrap.php';
10+
11+
Toolkit::test(function (): void {
12+
$form = new BootstrapForm();
13+
$btn = $form->addButton('btn', 'caption');
14+
Assert::same('<button type="button" name="btn" class="btn btn-secondary">caption</button>', $btn->getControl()->render());
15+
});
16+
17+
Toolkit::test(function (): void {
18+
$form = new BootstrapForm();
19+
$btn = $form->addButton('btn', 'caption')
20+
->setHtmlAttribute('onclick', 'someFunc()');
21+
Assert::same('<button type="button" name="btn" onclick="someFunc()" class="btn btn-secondary">caption</button>', $btn->getControl()->render());
22+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Tests\Cases\Inputs;
4+
5+
use Contributte\FormsBootstrap\BootstrapForm;
6+
use Contributte\FormsBootstrap\Enums\BootstrapVersion;
7+
use Contributte\Tester\Toolkit;
8+
use Tester\Assert;
9+
10+
require __DIR__ . '/../../bootstrap.php';
11+
12+
Toolkit::test(function (): void {
13+
$form = new BootstrapForm();
14+
$items = [2020 => 2020, 2021 => 2021];
15+
$control = $form->addCheckboxList('numbers', 'numbers', $items);
16+
$html = $control->getControl()->render();
17+
$expectedHtml = '<fieldset><label class="custom-control custom-checkbox"><input type="checkbox" class="custom-control-input" name="numbers[]" id="frm-numbers0" data-nette-rules="[]" value="2020"><label class="custom-control-label" for="frm-numbers0">2020</label></label><label class="custom-control custom-checkbox"><input type="checkbox" class="custom-control-input" name="numbers[]" id="frm-numbers1" data-nette-rules="[]" value="2021"><label class="custom-control-label" for="frm-numbers1">2021</label></label></fieldset>';
18+
Assert::same($expectedHtml, $html);
19+
});
20+
21+
Toolkit::test(function (): void {
22+
BootstrapForm::switchBootstrapVersion(BootstrapVersion::V5);
23+
24+
$form = new BootstrapForm();
25+
$items = [2020 => 2020, 2021 => 2021];
26+
$control = $form->addCheckboxList('numbers', 'numbers', $items);
27+
$html = $control->getControl()->render();
28+
$expectedHtml = '<fieldset><label class="form-check"><input type="checkbox" class="form-check-input" name="numbers[]" id="frm-numbers0" data-nette-rules="[]" value="2020"><label class="form-check-label" for="frm-numbers0">2020</label></label><label class="form-check"><input type="checkbox" class="form-check-input" name="numbers[]" id="frm-numbers1" data-nette-rules="[]" value="2021"><label class="form-check-label" for="frm-numbers1">2021</label></label></fieldset>';
29+
Assert::same($expectedHtml, $html);
30+
31+
BootstrapForm::switchBootstrapVersion(BootstrapVersion::V4);
32+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Tests\Cases\Inputs;
4+
5+
use Contributte\FormsBootstrap\BootstrapForm;
6+
use Contributte\FormsBootstrap\Enums\BootstrapVersion;
7+
use Contributte\Tester\Toolkit;
8+
use Nette\Utils\Html;
9+
use Tester\Assert;
10+
11+
require __DIR__ . '/../../bootstrap.php';
12+
13+
Toolkit::test(function (): void {
14+
$form = new BootstrapForm();
15+
$control = $form->addCheckbox('confirm', Html::fromHtml('<span class="test">Confirm</span>'));
16+
$html = $control->getControl()->render();
17+
$expectedHtml = '<label class="custom-control custom-checkbox"><input type="checkbox" class="custom-control-input" name="confirm" id="frm-confirm" data-nette-rules="[]"><label class="custom-control-label" for="frm-confirm"><span class="test">Confirm</span></label></label>';
18+
Assert::same($expectedHtml, $html);
19+
});
20+
21+
Toolkit::test(function (): void {
22+
BootstrapForm::switchBootstrapVersion(BootstrapVersion::V5);
23+
24+
$form = new BootstrapForm();
25+
$control = $form->addCheckbox('confirm', Html::fromHtml('<span class="test">Confirm</span>'));
26+
$html = $control->getControl()->render();
27+
$expectedHtml = '<label class="form-check"><input type="checkbox" class="form-check-input" name="confirm" id="frm-confirm" data-nette-rules="[]"><label class="form-check-label" for="frm-confirm"><span class="test">Confirm</span></label></label>';
28+
Assert::same($expectedHtml, $html);
29+
30+
BootstrapForm::switchBootstrapVersion(BootstrapVersion::V4);
31+
});

0 commit comments

Comments
 (0)