Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/Maker/MakeCrud.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen
$repositoryClassName = $repositoryClassDetails->getFullName();

$repositoryVars = [
'repository_full_class_name' => $repositoryClassName,
'repository_class_name' => $repositoryClassDetails->getShortName(),
'repository_var' => lcfirst($this->inflector->singularize($repositoryClassDetails->getShortName())),
];
Expand Down
8 changes: 5 additions & 3 deletions src/Resources/skeleton/crud/controller/Controller.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public function new(Request $request, EntityManagerInterface $entityManager): Re

<?php if (isset($repository_full_class_name) && $generator->repositoryHasAddRemoveMethods($repository_full_class_name)) { ?>
if ($form->isSubmitted() && $form->isValid()) {
$<?= $repository_var ?>->add($<?= $entity_var_singular ?>);
$<?= $repository_var ?>->add($<?= $entity_var_singular ?>, true);

return $this->redirectToRoute('<?= $route_name ?>_index', [], Response::HTTP_SEE_OTHER);
}
<?php } else { ?>
Expand Down Expand Up @@ -92,7 +93,8 @@ public function edit(Request $request, <?= $entity_class_name ?> $<?= $entity_va

<?php if (isset($repository_full_class_name) && $generator->repositoryHasAddRemoveMethods($repository_full_class_name)) { ?>
if ($form->isSubmitted() && $form->isValid()) {
$<?= $repository_var ?>->add($<?= $entity_var_singular ?>);
$<?= $repository_var ?>->add($<?= $entity_var_singular ?>, true);

return $this->redirectToRoute('<?= $route_name ?>_index', [], Response::HTTP_SEE_OTHER);
}
<?php } else { ?>
Expand Down Expand Up @@ -125,7 +127,7 @@ public function delete(Request $request, <?= $entity_class_name ?> $<?= $entity_
{
<?php if (isset($repository_full_class_name) && $generator->repositoryHasAddRemoveMethods($repository_full_class_name)) { ?>
if ($this->isCsrfTokenValid('delete'.$<?= $entity_var_singular ?>->get<?= ucfirst($entity_identifier) ?>(), $request->request->get('_token'))) {
$<?= $repository_var ?>->remove($<?= $entity_var_singular ?>);
$<?= $repository_var ?>->remove($<?= $entity_var_singular ?>, true);
}
<?php } else { ?>
if ($this->isCsrfTokenValid('delete'.$<?= $entity_var_singular ?>->get<?= ucfirst($entity_identifier) ?>(), $request->request->get('_token'))) {
Expand Down
25 changes: 20 additions & 5 deletions tests/Maker/MakeCrudTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

class MakeCrudTest extends MakerTestCase
{
use TestHelpersTrait;

protected function getMakerClass(): string
{
return MakeCrud::class;
Expand All @@ -28,7 +30,7 @@ public function getTestDetails(): \Generator
yield 'it_generates_basic_crud' => [$this->createMakerTest()
->run(function (MakerTestRunner $runner) {
$runner->copy(
'make-crud/SweetFood.php',
$this->getFixturePath('SweetFood.php', $runner),
'src/Entity/SweetFood.php'
);

Expand All @@ -48,7 +50,7 @@ public function getTestDetails(): \Generator
yield 'it_generates_crud_with_custom_controller' => [$this->createMakerTest()
->run(function (MakerTestRunner $runner) {
$runner->copy(
'make-crud/SweetFood.php',
$this->getFixturePath('SweetFood.php', $runner),
'src/Entity/SweetFood.php'
);

Expand All @@ -74,7 +76,7 @@ public function getTestDetails(): \Generator
);

$runner->copy(
'make-crud/SweetFood-custom-namespace.php',
$this->getFixturePath('SweetFood-custom-namespace.php', $runner),
'src/Entity/SweetFood.php'
);

Expand All @@ -94,7 +96,7 @@ public function getTestDetails(): \Generator
yield 'it_generates_crud_using_custom_repository' => [$this->createMakerTest()
->run(function (MakerTestRunner $runner) {
$runner->copy(
'make-crud/SweetFood.php',
$this->getFixturePath('SweetFoodCustomRepository.php', $runner),
'src/Entity/SweetFood.php'
);
$runner->copy(
Expand All @@ -112,13 +114,18 @@ public function getTestDetails(): \Generator
$this->assertStringContainsString('created: src/Form/SweetFoodType.php', $output);

$this->runCrudTest($runner, 'it_generates_basic_crud.php');

self::assertFileEquals(
sprintf('%s/fixtures/%s', \dirname(__DIR__), $this->getFixturePath('expected/WithCustomRepository.php', $runner)),
$runner->getPath('src/Controller/SweetFoodController.php')
);
}),
];

yield 'it_generates_crud_with_no_base_template' => [$this->createMakerTest()
->run(function (MakerTestRunner $runner) {
$runner->copy(
'make-crud/SweetFood.php',
$this->getFixturePath('SweetFood.php', $runner),
'src/Entity/SweetFood.php'
);

Expand All @@ -138,6 +145,14 @@ public function getTestDetails(): \Generator
];
}

// @legacy - remove when annotations are no longer supported
private function getFixturePath(string $sourceName, MakerTestRunner $runner): string
{
$path = $this->useAttributes($runner) ? 'make-crud' : 'make-crud/legacy';

return sprintf('%s/%s', $path, $sourceName);
}

private function runCrudTest(MakerTestRunner $runner, string $filename): void
{
$runner->copy(
Expand Down
10 changes: 2 additions & 8 deletions tests/Maker/MakeEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Bundle\MakerBundle\Tests\Maker;

use Doctrine\ORM\Mapping\Driver\AttributeReader;
use Symfony\Bundle\MakerBundle\Maker\MakeEntity;
use Symfony\Bundle\MakerBundle\Test\MakerTestCase;
use Symfony\Bundle\MakerBundle\Test\MakerTestDetails;
Expand All @@ -21,6 +20,8 @@

class MakeEntityTest extends MakerTestCase
{
use TestHelpersTrait;

protected function getMakerClass(): string
{
return MakeEntity::class;
Expand Down Expand Up @@ -671,13 +672,6 @@ private function changeToXmlMapping(MakerTestRunner $runner): void
);
}

private function useAttributes(MakerTestRunner $runner): bool
{
return \PHP_VERSION_ID >= 80000
&& $runner->doesClassExist(AttributeReader::class)
&& $runner->getSymfonyVersion() >= 50200;
}

private function copyEntity(MakerTestRunner $runner, string $filename): void
{
$entityClassName = substr(
Expand Down
30 changes: 30 additions & 0 deletions tests/Maker/TestHelpersTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?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\Tests\Maker;

use Doctrine\ORM\Mapping\Driver\AttributeReader;
use Symfony\Bundle\MakerBundle\Test\MakerTestRunner;

/**
* @author Jesse Rushlow <[email protected]>
*
* @internal
*/
trait TestHelpersTrait
{
// @legacy - remove when annotations are no longer supported
protected function useAttributes(MakerTestRunner $runner): bool
{
return \PHP_VERSION_ID >= 80000
&& $runner->doesClassExist(AttributeReader::class);
}
}
28 changes: 9 additions & 19 deletions tests/fixtures/make-crud/SweetFood-custom-namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,31 @@

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
*/
#[ORM\Entity()]
class SweetFood
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;

/**
* @ORM\Column(name="title", type="string", length=255)
*/
#[ORM\Column(type: 'string', length: 255)]
private $title;

public function getId()
{
return $this->id;
}

/**
* @return mixed
*/
public function getTitle()
public function getTitle(): string
{
return $this->title;
}

/**
* @param mixed $title
*/
public function setTitle($title)
public function setTitle(string $title): self
{
$this->title = $title;

return $this;
}
}
28 changes: 9 additions & 19 deletions tests/fixtures/make-crud/SweetFood.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,31 @@

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
*/
#[ORM\Entity()]
class SweetFood
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;

/**
* @ORM\Column(name="title", type="string", length=255)
*/
#[ORM\Column(type: 'string', length: 255)]
private $title;

public function getId()
{
return $this->id;
}

/**
* @return mixed
*/
public function getTitle()
public function getTitle(): string
{
return $this->title;
}

/**
* @param mixed $title
*/
public function setTitle($title)
public function setTitle(string $title): self
{
$this->title = $title;

return $this;
}
}
35 changes: 35 additions & 0 deletions tests/fixtures/make-crud/SweetFoodCustomRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Entity;

use App\Repository\SweetFoodRepository;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: SweetFoodRepository::class)]
class SweetFood
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column(type: 'integer')]
private $id;

#[ORM\Column(type: 'string', length: 255)]
private $title;

public function getId()
{
return $this->id;
}

public function getTitle(): string
{
return $this->title;
}

public function setTitle(string $title): self
{
$this->title = $title;

return $this;
}
}
27 changes: 17 additions & 10 deletions tests/fixtures/make-crud/SweetFoodRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Doctrine\Persistence\ManagerRegistry;

/**
* @extends ServiceEntityRepository<SweetFood>
*
* @method SweetFood|null find($id, $lockMode = null, $lockVersion = null)
* @method SweetFood|null findOneBy(array $criteria, array $orderBy = null)
* @method SweetFood[] findAll()
Expand All @@ -19,16 +21,21 @@ public function __construct(ManagerRegistry $registry)
parent::__construct($registry, SweetFood::class);
}

/*
public function findBySomething($value)
public function add(SweetFood $entity, bool $flush = false): void
{
return $this->createQueryBuilder('t')
->where('t.something = :value')->setParameter('value', $value)
->orderBy('t.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
($em = $this->getEntityManager())->persist($entity);

if ($flush) {
$em->flush();
}
}

public function remove(SweetFood $entity, bool $flush = false): void
{
($em = $this->getEntityManager())->remove($entity);

if ($flush) {
$em->flush();
}
}
*/
}
Loading