Skip to content

Commit

Permalink
Also test form element decorators
Browse files Browse the repository at this point in the history
  • Loading branch information
lippserd committed Jul 27, 2021
1 parent 501cab1 commit 29a6c0d
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 0 deletions.
64 changes: 64 additions & 0 deletions tests/FormElementDecoratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace ipl\Tests\Html;

use ipl\Html\Form;
use ipl\Tests\Html\TestDummy\PositionedFormElementDecorator;
use ipl\Tests\Html\TestDummy\SimpleFormElementDecorator;
use ipl\Tests\Html\TestDummy\WithinContainerFormElementDecorator;

class FormElementDecoratorTest extends TestCase
{
public function testPositionedFormElementDecorator()
{
$form = (new Form())
->setDefaultElementDecorator(new PositionedFormElementDecorator())
->addElement('text', 'decorated-form-element');

$html = <<<'HTML'
<form method="POST">
<div class="positioned-decorator">
<input type="text" name="decorated-form-element">
</div>
</form>
HTML;

$this->assertHtml($html, $form);
}

public function testSimpleFormElementDecorator()
{
$form = (new Form())
->setDefaultElementDecorator(new SimpleFormElementDecorator())
->addElement('text', 'decorated-form-element');

$html = <<<'HTML'
<form method="POST">
<div class="simple-decorator">
<input type="text" name="decorated-form-element">
</div>
</form>
HTML;

$this->assertHtml($html, $form);
}

public function testWithinContainerFormElementDecorator()
{
$form = (new Form())
->setDefaultElementDecorator(new WithinContainerFormElementDecorator())
->addElement('text', 'decorated-form-element');

$html = <<<'HTML'
<form method="POST">
<div class="within-container-decorator">
<div class="container">
<input type="text" name="decorated-form-element">
</div>
</div>
</form>
HTML;

$this->assertHtml($html, $form);
}
}
32 changes: 32 additions & 0 deletions tests/TestDummy/PositionedFormElementDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace ipl\Tests\Html\TestDummy;

use ipl\Html\BaseHtmlElement;
use ipl\Html\Contract\FormElement;
use ipl\Html\Contract\FormElementDecorator;

class PositionedFormElementDecorator extends BaseHtmlElement implements FormElementDecorator
{
protected $tag = 'div';

protected $defaultAttributes = ['class' => 'positioned-decorator'];

/** @var FormElement */
protected $formElement;

public function decorate(FormElement $formElement)
{
$decorator = new static();
$decorator->formElement = $formElement;

$formElement->prependWrapper($decorator);

return $decorator;
}

protected function assemble()
{
$this->add($this->formElement);
}
}
27 changes: 27 additions & 0 deletions tests/TestDummy/SimpleFormElementDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace ipl\Tests\Html\TestDummy;

use ipl\Html\BaseHtmlElement;
use ipl\Html\Contract\FormElement;
use ipl\Html\Contract\FormElementDecorator;

class SimpleFormElementDecorator extends BaseHtmlElement implements FormElementDecorator
{
protected $tag = 'div';

protected $defaultAttributes = ['class' => 'simple-decorator'];

/** @var FormElement */
protected $formElement;

public function decorate(FormElement $formElement)
{
$decorator = new static();
$decorator->formElement = $formElement;

$formElement->prependWrapper($decorator);

return $decorator;
}
}
33 changes: 33 additions & 0 deletions tests/TestDummy/WithinContainerFormElementDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace ipl\Tests\Html\TestDummy;

use ipl\Html\BaseHtmlElement;
use ipl\Html\Contract\FormElement;
use ipl\Html\Contract\FormElementDecorator;
use ipl\Html\Html;

class WithinContainerFormElementDecorator extends BaseHtmlElement implements FormElementDecorator
{
protected $tag = 'div';

protected $defaultAttributes = ['class' => 'within-container-decorator'];

/** @var FormElement */
protected $formElement;

public function decorate(FormElement $formElement)
{
$decorator = new static();
$decorator->formElement = $formElement;

$formElement->prependWrapper($decorator);

return $decorator;
}

protected function assemble()
{
$this->add(Html::tag('div', ['class' => 'container'], $this->formElement));
}
}

0 comments on commit 29a6c0d

Please sign in to comment.