Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions test/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Laminas\Validator\ValidatorChain;
use Laminas\Validator\ValidatorInterface;
use Laminas\Validator\ValidatorPluginManager;
use LaminasTest\Form\TestAsset\CustomElementWithRequiredOption;
use LaminasTest\Form\TestAsset\InputFilter;
use LaminasTest\Form\TestAsset\Model;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -849,4 +850,70 @@ public function testCanCreateWithConstructionLogicInOptions(): void
self::assertInstanceOf(TestAsset\FieldsetWithDependency::class, $targetElement);
self::assertInstanceOf(InputFilter::class, $targetElement->getDependency());
}

public function testElementWithRequiredOptionsNestedInFieldsetOrCollection(): void
{
$formManager = $this->factory->getFormElementManager();
$formManager->setFactory(
TestAsset\FieldsetWithDependency::class,
TestAsset\FieldsetWithDependencyFactory::class
);

$collection = $this->factory->create([
'type' => Form\Element\Collection::class,
'name' => 'my_fieldset_collection',
'options' => [
'target_element' => [
'type' => CustomElementWithRequiredOption::class,
'options' => [
'requiredOption' => 'Any String',
],
],
],
]);
self::assertInstanceOf(Form\Element\Collection::class, $collection);
$element = $collection->getTargetElement();
self::assertInstanceOf(CustomElementWithRequiredOption::class, $element);
self::assertSame('Any String', $element->myString);
}

public function testCreateElementWithRequiredOption(): void
{
$element = $this->factory->create([
'type' => CustomElementWithRequiredOption::class,
'name' => 'foo',
'options' => [
'requiredOption' => 'Any String',
],
]);

self::assertInstanceOf(CustomElementWithRequiredOption::class, $element);
self::assertSame('Any String', $element->myString);
}

public function testCanCreateElementWithConstructorDependency(): void
{
$formManager = $this->factory->getFormElementManager();
$formManager->setFactory(
TestAsset\CustomElementWithConstructorDependency::class,
TestAsset\CustomElementWithConstructorDependencyFactory::class
);

$collection = $this->factory->create([
'type' => Form\Element\Collection::class,
'name' => 'my_fieldset_collection',
'options' => [
'target_element' => [
'type' => TestAsset\CustomElementWithConstructorDependency::class,
],
],
]);

self::assertInstanceOf(Form\Element\Collection::class, $collection);

$targetElement = $collection->getTargetElement();

self::assertInstanceOf(TestAsset\CustomElementWithConstructorDependency::class, $targetElement);
self::assertSame('FOO', $targetElement->myString);
}
}
18 changes: 18 additions & 0 deletions test/TestAsset/CustomElementWithConstructorDependency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace LaminasTest\Form\TestAsset;

use Laminas\Form\Element\MultiCheckbox;

final class CustomElementWithConstructorDependency extends MultiCheckbox
{
public function __construct(
string|null $name = null,
iterable $options = [],
public readonly string $myString, // phpcs:ignore
) {
parent::__construct($name, $options);
}
}
22 changes: 22 additions & 0 deletions test/TestAsset/CustomElementWithConstructorDependencyFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace LaminasTest\Form\TestAsset;

use Psr\Container\ContainerInterface;

final class CustomElementWithConstructorDependencyFactory
{
public function __invoke(
ContainerInterface $container,
string $requestedName,
array|null $options = null,
): CustomElementWithConstructorDependency {
return new CustomElementWithConstructorDependency(
null,
$options ?? [],
'FOO',
);
}
}
25 changes: 25 additions & 0 deletions test/TestAsset/CustomElementWithRequiredOption.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace LaminasTest\Form\TestAsset;

use Laminas\Form\Element\MultiCheckbox;

final class CustomElementWithRequiredOption extends MultiCheckbox
{
public readonly string $myString;

/** @param array<array-key, mixed> $options */
public function __construct(string|null $name = null, iterable $options = [])
{
/**
* The null assignment causes a type error which is desired
*
* @psalm-suppress PossiblyNullPropertyAssignmentValue
*/
$this->myString = $options['requiredOption'] ?? null;

parent::__construct($name, $options);
}
}
Loading