Skip to content

Instantly share code, notes, and snippets.

@terabytesoftw
Last active December 13, 2021 12:46
Show Gist options
  • Save terabytesoftw/a1d0aaba2ba4cc1faeeacd495531d577 to your computer and use it in GitHub Desktop.
Save terabytesoftw/a1d0aaba2ba4cc1faeeacd495531d577 to your computer and use it in GitHub Desktop.
Proposal form.
<?php
declare(strict_types=1);
namespace Yii\Extension\Simple\Forms\Tests;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Yii\Extension\Simple\Forms\FormBuilder;
use Yii\Extension\Simple\Forms\Password;
use Yii\Extension\Simple\Forms\ResetButton;
use Yii\Extension\Simple\Forms\SubmitButton;
use Yii\Extension\Simple\Forms\Tests\TestSupport\Form\TypeForm;
use Yii\Extension\Simple\Forms\Tests\TestSupport\Form\TypeWithHintForm;
use Yii\Extension\Simple\Forms\Tests\TestSupport\TestTrait;
use Yii\Extension\Simple\Forms\Text;
use Yiisoft\Html\Html;
final class FormBuilderTest extends TestCase
{
use TestTrait;
public function testHintClass(): void
{
$this->setInaccessibleProperty(new Html(), 'generateIdCounter', []);
$expectec = <<<HTML
<form id="w1-form" method="POST">
<div>
<label for="typewithhintform-login">Login</label>
<input type="text" id="typewithhintform-login" name="TypeWithHintForm[login]">
<div class="help-block">Please enter your login.</div>
</div>
<div>
<label for="typewithhintform-password">Password</label>
<input type="password" id="typewithhintform-password" name="TypeWithHintForm[password]">
<div class="help-block">Please enter your password.</div>
</div>
</form>
HTML;
$this->assertEqualsWithoutLE(
$expectec,
FormBuilder::widget()
->addFields(
Text::widget()->for(new TypeWithHintForm(), 'login'),
Password::widget()->for(new TypeWithHintForm(), 'password'),
)
->hintClass('help-block')
->render(),
);
}
public function testIndividualHintClass(): void
{
$this->setInaccessibleProperty(new Html(), 'generateIdCounter', []);
$expectec = <<<HTML
<form id="w1-form" method="POST">
<div>
<label for="typewithhintform-login">Login</label>
<input type="text" id="typewithhintform-login" name="TypeWithHintForm[login]">
<div class="test-class-1">Please enter your login.</div>
</div>
<div>
<label for="typewithhintform-password">Password</label>
<input type="password" id="typewithhintform-password" name="TypeWithHintForm[password]">
<div class="test-class-2">Please enter your password.</div>
</div>
</form>
HTML;
$this->assertEqualsWithoutLE(
$expectec,
FormBuilder::widget()
->addFields(
Text::widget()->for(new TypeWithHintForm(), 'login'),
Password::widget()->for(new TypeWithHintForm(), 'password'),
)
->individualHintClass(['login' => 'test-class-1', 'password' => 'test-class-2'])
->render(),
);
}
public function testIndividualInputClass(): void
{
$this->setInaccessibleProperty(new Html(), 'generateIdCounter', []);
$expectec = <<<HTML
<form id="w1-form" method="POST">
<div>
<label for="typeform-login">Login</label>
<input type="text" id="typeform-login" class="test-class-1" name="TypeForm[login]">
</div>
<div>
<label for="typeform-password">Password</label>
<input type="password" id="typeform-password" class="test-class-2" name="TypeForm[password]">
</div>
</form>
HTML;
$this->assertEqualsWithoutLE(
$expectec,
FormBuilder::widget()
->addFields(
Text::widget()->for(new TypeForm(), 'login'),
Password::widget()->for(new TypeForm(), 'password'),
)
->individualInputClass(['login' => 'test-class-1', 'password' => 'test-class-2'])
->render(),
);
}
public function testIndividualLabelText(): void
{
$this->setInaccessibleProperty(new Html(), 'generateIdCounter', []);
// Remove label tag attribute login.
$expectec = <<<HTML
<form id="w1-form" method="POST">
<div>
<input type="text" id="typeform-login" name="TypeForm[login]">
</div>
<div>
<label for="typeform-password">Password</label>
<input type="password" id="typeform-password" name="TypeForm[password]">
</div>
</form>
HTML;
$this->assertEqualsWithoutLE(
$expectec,
FormBuilder::widget()
->addFields(
Text::widget()->for(new TypeForm(), 'login'),
Password::widget()->for(new TypeForm(), 'password'),
)
->individualLabelText(['login' => null])
->render(),
);
// Set custom label text for attribute.
$expectec = <<<HTML
<form id="w2-form" method="POST">
<div>
<label for="typeform-login">Login:</label>
<input type="text" id="typeform-login" name="TypeForm[login]">
</div>
<div>
<label for="typeform-password">Password:</label>
<input type="password" id="typeform-password" name="TypeForm[password]">
</div>
</form>
HTML;
$this->assertEqualsWithoutLE(
$expectec,
FormBuilder::widget()
->addFields(
Text::widget()->for(new TypeForm(), 'login'),
Password::widget()->for(new TypeForm(), 'password'),
)
->individualLabelText(['login' => 'Login:', 'password' => 'Password:'])
->render(),
);
}
public function testIndividualTemplateField(): void
{
$this->setInaccessibleProperty(new Html(), 'generateIdCounter', []);
$expectec = <<<HTML
<form id="w1-form" method="POST">
<div>
<label for="typeform-login">Login</label>
<input type="text" id="typeform-login" name="TypeForm[login]">
</div>
<div>
<input type="password" id="typeform-password" name="TypeForm[password]">
<label for="typeform-password">Password</label>
</div>
</form>
HTML;
$this->assertEqualsWithoutLE(
$expectec,
FormBuilder::widget()
->addFields(
Text::widget()->for(new TypeForm(), 'login'),
Password::widget()->for(new TypeForm(), 'password'),
)
->individualTemplateField(
['login' => "{label}\n{input}\n{hint}\n{error}", 'password' => "{input}\n{label}\n{hint}\n{error}"],
)
->render(),
);
}
public function testInputClass(): void
{
$this->setInaccessibleProperty(new Html(), 'generateIdCounter', []);
$expectec = <<<HTML
<form id="w1-form" method="POST">
<div>
<label for="typeform-login">Login</label>
<input type="text" id="typeform-login" class="form-control" name="TypeForm[login]">
</div>
<div>
<label for="typeform-password">Password</label>
<input type="password" id="typeform-password" class="form-control" name="TypeForm[password]">
</div>
</form>
HTML;
$this->assertEqualsWithoutLE(
$expectec,
FormBuilder::widget()
->addFields(
Text::widget()->for(new TypeForm(), 'login'),
Password::widget()->for(new TypeForm(), 'password'),
)
->inputClass('form-control')
->render(),
);
}
public function testLabelClass(): void
{
$this->setInaccessibleProperty(new Html(), 'generateIdCounter', []);
$expectec = <<<HTML
<form id="w1-form" method="POST">
<div>
<label class="form-label" for="typeform-login">Login</label>
<input type="text" id="typeform-login" name="TypeForm[login]">
</div>
<div>
<label class="form-label" for="typeform-password">Password</label>
<input type="password" id="typeform-password" name="TypeForm[password]">
</div>
</form>
HTML;
$this->assertEqualsWithoutLE(
$expectec,
FormBuilder::widget()
->addFields(
Text::widget()->for(new TypeForm(), 'login'),
Password::widget()->for(new TypeForm(), 'password'),
)
->labelClass('form-label')
->render(),
);
}
public function testRender(): void
{
$this->setInaccessibleProperty(new Html(), 'generateIdCounter', []);
$this->assertSame('<form id="w1-form" method="POST"></form>', FormBuilder::widget()->render());
}
public function testTemplateField(): void
{
$this->setInaccessibleProperty(new Html(), 'generateIdCounter', []);
$expectec = <<<HTML
<form id="w1-form" method="POST">
<div>
<input type="text" id="typeform-login" name="TypeForm[login]">
<label for="typeform-login">Login</label>
</div>
<div>
<input type="password" id="typeform-password" name="TypeForm[password]">
<label for="typeform-password">Password</label>
</div>
</form>
HTML;
$this->assertEqualsWithoutLE(
$expectec,
FormBuilder::widget()
->addFields(
Text::widget()->for(new TypeForm(), 'login'),
Password::widget()->for(new TypeForm(), 'password'),
)
->templateField("{input}\n{label}\n{hint}\n{error}")
->render(),
);
}
public function testTemplateForm(): void
{
$this->setInaccessibleProperty(new Html(), 'generateIdCounter', []);
// Set template buttons to first form.
$expectec = <<<HTML
<form id="w1-form" method="POST">
<div>
<input type="reset" id="w2-reset" class="btn btn-danger btn-lg" name="w2-reset" value="Reset">
<input type="submit" id="w3-submit" class="btn btn-danger btn-lg" name="w3-submit" value="Submit">
</div>
<div>
<label for="typeform-login">Login</label>
<input type="text" id="typeform-login" name="TypeForm[login]">
</div>
</form>
HTML;
$this->assertEqualsWithoutLE(
$expectec,
FormBuilder::widget()
->addButtons(
ResetButton::widget()->attributes(['class' => 'btn btn-danger btn-lg'])->value('Reset'),
SubmitButton::widget()->attributes(['class' => 'btn btn-danger btn-lg'])->value('Submit'),
)
->addFields(
Text::widget()->for(new TypeForm(), 'login'),
)
->templateForm("{buttons}\n{form}")
->render(),
);
// Set template buttons to last form.
$expectec = <<<HTML
<form id="w4-form" method="POST">
<div>
<label for="typeform-login">Login</label>
<input type="text" id="typeform-login" name="TypeForm[login]">
</div>
<div>
<input type="reset" id="w5-reset" class="btn btn-danger btn-lg" name="w5-reset" value="Reset">
<input type="submit" id="w6-submit" class="btn btn-danger btn-lg" name="w6-submit" value="Submit">
</div>
</form>
HTML;
$this->assertEqualsWithoutLE(
$expectec,
FormBuilder::widget()
->addButtons(
ResetButton::widget()->attributes(['class' => 'btn btn-danger btn-lg'])->value('Reset'),
SubmitButton::widget()->attributes(['class' => 'btn btn-danger btn-lg'])->value('Submit'),
)
->addFields(
Text::widget()->for(new TypeForm(), 'login'),
)
->templateForm("{form}\n{buttons}")
->render(),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment