diff --git a/src/Maker/MakeForm.php b/src/Maker/MakeForm.php index a5d36ab68..616d121fb 100644 --- a/src/Maker/MakeForm.php +++ b/src/Maker/MakeForm.php @@ -18,6 +18,7 @@ use Symfony\Bundle\MakerBundle\Generator; use Symfony\Bundle\MakerBundle\InputConfiguration; use Symfony\Bundle\MakerBundle\Str; +use Symfony\Bundle\MakerBundle\Util\ClassDetails; use Symfony\Bundle\MakerBundle\Validator; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; @@ -95,6 +96,9 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen if (null !== $doctrineEntityDetails) { $formFields = $doctrineEntityDetails->getFormFields(); + } else { + $classDetails = new ClassDetails($boundClassDetails->getFullName()); + $formFields = $classDetails->getFormFields(); } $boundClassVars = [ diff --git a/src/Util/ClassDetails.php b/src/Util/ClassDetails.php new file mode 100644 index 000000000..6d326f85f --- /dev/null +++ b/src/Util/ClassDetails.php @@ -0,0 +1,51 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Bundle\MakerBundle\Util; + +/** + * @internal + */ +final class ClassDetails +{ + private $fullClassName; + + public function __construct(string $fullClassName) + { + $this->fullClassName = $fullClassName; + } + + /** + * Get list of property names except "id" for use in a make:form context. + * + * @return array|null + */ + public function getFormFields(): array + { + $properties = $this->getProperties(); + + return array_diff($properties, ['id']); + } + + private function getProperties(): array + { + $reflect = new \ReflectionClass($this->fullClassName); + $props = $reflect->getProperties(); + + $propertiesList = []; + + foreach ($props as $prop) { + $propertiesList[] = $prop->getName(); + } + + return $propertiesList; + } +} diff --git a/tests/Maker/FunctionalTest.php b/tests/Maker/FunctionalTest.php index af1038973..a7702c6bb 100644 --- a/tests/Maker/FunctionalTest.php +++ b/tests/Maker/FunctionalTest.php @@ -188,6 +188,16 @@ public function getCommandTests() ]) ->addExtraDependencies('orm') ->setFixtureFilesPath(__DIR__.'/../fixtures/MakeFormForEntity') + ]; + + yield 'form_for_non_entity_dto' => [MakerTestDetails::createTest( + $this->getMakerInstance(MakeForm::class), + [ + // Entity name + 'TaskType', + '\\App\\Form\\Data\\TaskData', + ]) + ->setFixtureFilesPath(__DIR__.'/../fixtures/MakeFormForNonEntityDto') ]; yield 'form_for_sti_entity' => [MakerTestDetails::createTest( diff --git a/tests/fixtures/MakeFormForNonEntityDto/src/Form/Data/TaskData.php b/tests/fixtures/MakeFormForNonEntityDto/src/Form/Data/TaskData.php new file mode 100644 index 000000000..14b791313 --- /dev/null +++ b/tests/fixtures/MakeFormForNonEntityDto/src/Form/Data/TaskData.php @@ -0,0 +1,22 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace App\Form\Data; + +/** + * Data transfer object for Task. + */ +class TaskData +{ + public $task; + + public $dueDate; +} diff --git a/tests/fixtures/MakeFormForNonEntityDto/tests/GeneratedFormTest.php b/tests/fixtures/MakeFormForNonEntityDto/tests/GeneratedFormTest.php new file mode 100644 index 000000000..31ace6937 --- /dev/null +++ b/tests/fixtures/MakeFormForNonEntityDto/tests/GeneratedFormTest.php @@ -0,0 +1,49 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace App\Tests; + +use App\Form\Data\TaskData; +use App\Form\TaskType; +use Symfony\Component\Form\Test\TypeTestCase; + +class GeneratedFormTest extends TypeTestCase +{ + public function testGeneratedForm() + { + $dateTimeObject = new \DateTime(); + + $formData = [ + 'task' => 'Acme', + 'dueDate' => $dateTimeObject, + ]; + + $objectToCompare = new TaskData(); + + $form = $this->factory->create(TaskType::class, $objectToCompare); + $form->submit($formData); + + $object = new TaskData(); + $object->task = 'Acme'; + $object->dueDate = $dateTimeObject; + + $this->assertTrue($form->isSynchronized()); + $this->assertEquals($object, $objectToCompare); + $this->assertEquals($object, $form->getData()); + + $view = $form->createView(); + $children = $view->children; + + foreach (array_keys($formData) as $key) { + $this->assertArrayHasKey($key, $children); + } + } +}