-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from nymo/master
Added Twig Form Extension as Helper
- Loading branch information
Showing
4 changed files
with
233 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace Naucon\Form\Helper\Twig; | ||
|
||
use InvalidArgumentException; | ||
use Naucon\Form\FormHelper; | ||
use Naucon\Form\FormInterface; | ||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFunction; | ||
|
||
class NcFormExtension extends AbstractExtension | ||
{ | ||
public function getFunctions() | ||
{ | ||
return [ | ||
new TwigFunction('ncform_start', [$this, 'formStart'], ['is_safe' => ['html']]), | ||
new TwigFunction('ncform_end', [$this, 'formEnd'], ['is_safe' => ['html']]), | ||
new TwigFunction('ncform_field', [$this, 'field'], ['is_safe' => ['html']]) | ||
]; | ||
} | ||
|
||
public function formStart( | ||
FormInterface $form, | ||
string $method = 'post', | ||
?string $action = null, | ||
?string $enctype = null, | ||
array $options = [] | ||
): string | ||
{ | ||
$formHelper = new FormHelper($form); | ||
|
||
return $formHelper->formStart($method, $action, $enctype, $options); | ||
} | ||
|
||
public function formEnd(FormInterface $form): string | ||
{ | ||
$formHelper = new FormHelper($form); | ||
|
||
return $formHelper->formEnd(); | ||
} | ||
|
||
public function field(FormInterface $form, string $type, string $field, ?array $options = []): string | ||
{ | ||
$formHelper = new FormHelper($form); | ||
$validOptions = ['style', 'class', 'id', 'value', 'maxlength', 'required', 'data-']; | ||
|
||
foreach (array_keys($options) as $option) { | ||
$isInvalid = true; | ||
foreach ($validOptions as $validOption) { | ||
if (strpos($option, $validOption) !== false) { | ||
$isInvalid = false; | ||
} | ||
} | ||
if ($isInvalid) { | ||
throw new InvalidArgumentException( | ||
'Only following options are allowed: ' . implode(', ', $validOptions) | ||
); | ||
} | ||
} | ||
|
||
return $formHelper->formField($type, $field, $options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php | ||
|
||
namespace Naucon\Form\Tests\Helper\Twig; | ||
|
||
use Naucon\Form\Configuration; | ||
use Naucon\Form\FormInterface; | ||
use Naucon\Form\Helper\AbstractFormHelperField; | ||
use Naucon\Form\Helper\Twig\NcFormExtension; | ||
use Naucon\Form\Mapper\EntityContainer; | ||
use Naucon\Form\Mapper\Property; | ||
use Naucon\Utility\Iterator; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Twig\TwigFunction; | ||
|
||
class NcFormExtensionTest extends TestCase | ||
{ | ||
|
||
/** | ||
* @var FormInterface&MockObject | ||
*/ | ||
private $form; | ||
/** | ||
* @var Configuration&MockObject | ||
*/ | ||
private $configuration; | ||
/** | ||
* @var Iterator&MockObject | ||
*/ | ||
private $iterator; | ||
/** | ||
* @var AbstractFormHelperField&MockObject | ||
*/ | ||
private $formHelperField; | ||
/** | ||
* @var Property&MockObject | ||
*/ | ||
private $property; | ||
/** | ||
* @var EntityContainer&MockObject | ||
*/ | ||
private $entityContainer; | ||
|
||
public function testFormEnd() | ||
{ | ||
$extension = $this->createExtension(); | ||
$this->configureMocks(); | ||
|
||
$html = $extension->formEnd($this->form); | ||
$this->assertStringContainsString('</form>', $html); | ||
|
||
} | ||
|
||
public function testField() | ||
{ | ||
$extension = $this->createExtension(); | ||
$this->configureMocks(); | ||
$this->iterator | ||
->expects($this->once()) | ||
->method('current') | ||
->willReturn($this->formHelperField); | ||
|
||
$this->formHelperField | ||
->expects($this->once()) | ||
->method('getProperty') | ||
->willReturn($this->property); | ||
|
||
$this->property | ||
->expects($this->once()) | ||
->method('getEntityContainer') | ||
->willReturn($this->entityContainer); | ||
|
||
$this->entityContainer | ||
->expects($this->once()) | ||
->method('hasError') | ||
->willReturn(false); | ||
|
||
$html = $extension->field($this->form, 'text', 'my-name', ['class' => 'css-class', 'data-foo' => 'some-attribute']); | ||
$this->assertStringContainsString('<input type="text"', $html); | ||
$this->assertStringContainsString('class="css-class"', $html); | ||
$this->assertStringContainsString('data-foo="some-attribute"', $html); | ||
|
||
} | ||
|
||
public function testGetFunctions() | ||
{ | ||
$extension = $this->createExtension(); | ||
$functions = $extension->getFunctions(); | ||
|
||
$this->assertCount(3, $functions); | ||
$this->assertContainsOnlyInstancesOf(TwigFunction::class, $functions); | ||
|
||
|
||
} | ||
|
||
public function testFormStart() | ||
{ | ||
$extension = $this->createExtension(); | ||
$this->configureMocks(); | ||
|
||
$html = $extension->formStart($this->form); | ||
$this->assertStringContainsString('<form', $html); | ||
$this->assertStringContainsString('method="post"', $html); | ||
|
||
} | ||
|
||
protected function createExtension(): NcFormExtension | ||
{ | ||
return new NcFormExtension(); | ||
} | ||
|
||
protected function configureMocks() | ||
{ | ||
$this->form | ||
->expects($this->once()) | ||
->method('getConfiguration') | ||
->willReturn($this->configuration); | ||
|
||
$this->form | ||
->expects($this->once()) | ||
->method('getEntityContainerIterator') | ||
->willReturn($this->iterator); | ||
|
||
$this->configuration | ||
->expects($this->once()) | ||
->method('get') | ||
->with('csrf_protection') | ||
->willReturn(false); | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
$this->form = $this->createMock(FormInterface::class); | ||
$this->configuration = $this->createMock(Configuration::class); | ||
$this->iterator = $this->createMock(Iterator::class); | ||
$this->formHelperField = $this->createMock(AbstractFormHelperField::class); | ||
$this->property = $this->createMock(Property::class); | ||
$this->entityContainer = $this->createMock(EntityContainer::class); | ||
} | ||
} |