Skip to content

Commit

Permalink
Test deep rebinding attribute callbacks after clone
Browse files Browse the repository at this point in the history
  • Loading branch information
lippserd committed Feb 10, 2023
1 parent cee10c9 commit ba08f7e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
30 changes: 30 additions & 0 deletions tests/Lib/FormProvidingAttributeCallbacks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace ipl\Tests\Html\Lib;

use ipl\Html\Form;
use ipl\Html\FormElement\FieldsetElement;

class FormProvidingAttributeCallbacks extends Form
{
public function getFormaction(): ?string
{
return $this->getAction();
}
public function getFormmethod(): ?string
{
return $this->getMethod();
}
protected function assemble()
{
$submit = $this->createElement('submit', 'submit');
$submit
->getAttributes()
->registerAttributeCallback('formaction', [$this, 'getFormaction'])
->registerAttributeCallback('formmethod', [$this, 'getFormmethod']);
/** @var FieldsetElement $fieldset */
$fieldset = $this->createElement('fieldset', 'fieldset');
$fieldset->addElement($submit);
$this->addElement($fieldset);
}
}
29 changes: 29 additions & 0 deletions tests/RebindAttributeCallbacksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use ipl\Html\Attributes;
use ipl\Html\BaseHtmlElement;
use ipl\Tests\Html\Lib\ElementWithAttributeCallbacks;
use ipl\Tests\Html\Lib\FormProvidingAttributeCallbacks;
use ReflectionFunction;
use ReflectionProperty;

Expand Down Expand Up @@ -113,6 +114,34 @@ public function testElementCallbacksCloning(): void
$this->assertCallbacksFor($clone);
}

public function testDeepRebinding(): void
{
$form = (new FormProvidingAttributeCallbacks())
->ensureAssembled();

$clone = (clone $form)
->setAction('action')
->setMethod('GET');

$originalHtml = <<<'HTML'
<form method="POST">
<fieldset name="fieldset">
<input name="fieldset[submit]" type="submit" value="submit" formmethod="POST">
</fieldset>
</form>
HTML;
$this->assertHtml($originalHtml, $form);

$cloneHtml = <<<'HTML'
<form action="action" method="GET">
<fieldset name="fieldset">
<input name="fieldset[submit]" type="submit" value="submit" formaction="action" formmethod="GET">
</fieldset>
</form>
HTML;
$this->assertHtml($cloneHtml, $clone);
}

protected function getCallbackThis(callable $callback): ?object
{
if (! $callback instanceof Closure) {
Expand Down

0 comments on commit ba08f7e

Please sign in to comment.