Skip to content
Closed
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
4 changes: 4 additions & 0 deletions src/Maker/MakeForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = [
Expand Down
51 changes: 51 additions & 0 deletions src/Util/ClassDetails.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

/*
* This file is part of the Symfony MakerBundle package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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;
}
}
10 changes: 10 additions & 0 deletions tests/Maker/FunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
22 changes: 22 additions & 0 deletions tests/fixtures/MakeFormForNonEntityDto/src/Form/Data/TaskData.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the Symfony MakerBundle package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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;
}
49 changes: 49 additions & 0 deletions tests/fixtures/MakeFormForNonEntityDto/tests/GeneratedFormTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the Symfony MakerBundle package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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);
}
}
}