Skip to content

Commit 4f6d30c

Browse files
committed
bug #132 make:crud skeleton optimization (sadikoff)
This PR was squashed before being merged into the 1.0-dev branch (closes #132). Discussion ---------- make:crud skeleton optimization Lets start from `Controller::delete` action. I don't think we need 2 return statements here. Maybe it will be better to add some flash messages? Commits ------- 97841c1 simplify CrudControllerWebTest (we need only check if index works) 43ce972 added tests for crud generation with entity repository 4d1e2d8 fixing #134 1f4455e skeleton template Controller optimization
2 parents 9db7299 + 97841c1 commit 4f6d30c

File tree

5 files changed

+127
-7
lines changed

5 files changed

+127
-7
lines changed

src/Resources/skeleton/crud/controller/Controller.tpl.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class <?= $class_name ?> extends Controller
2323
<?php if (isset($repository_full_class_name)): ?>
2424
public function index(<?= $repository_class_name ?> $<?= $repository_var ?>): Response
2525
{
26-
return $this->render('<?= $route_name ?>/index.html.twig', ['<?= $entity_var_plural ?>' => $<?= $repository_var ?>->findAll()]);
26+
return $this->render('<?= $route_name ?>/index.html.twig', ['<?= $entity_twig_var_plural ?>' => $<?= $repository_var ?>->findAll()]);
2727
}
2828
<?php else: ?>
2929
public function index(): Response
@@ -92,14 +92,12 @@ public function edit(Request $request, <?= $entity_class_name ?> $<?= $entity_va
9292
*/
9393
public function delete(Request $request, <?= $entity_class_name ?> $<?= $entity_var_singular ?>): Response
9494
{
95-
if (!$this->isCsrfTokenValid('delete'.$<?= $entity_var_singular ?>->get<?= ucfirst($entity_identifier) ?>(), $request->request->get('_token'))) {
96-
return $this->redirectToRoute('<?= $route_name ?>_index');
95+
if ($this->isCsrfTokenValid('delete'.$<?= $entity_var_singular ?>->get<?= ucfirst($entity_identifier) ?>(), $request->request->get('_token'))) {
96+
$em = $this->getDoctrine()->getManager();
97+
$em->remove($<?= $entity_var_singular ?>);
98+
$em->flush();
9799
}
98100

99-
$em = $this->getDoctrine()->getManager();
100-
$em->remove($<?= $entity_var_singular ?>);
101-
$em->flush();
102-
103101
return $this->redirectToRoute('<?= $route_name ?>_index');
104102
}
105103
}

tests/Maker/FunctionalTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,35 @@ public function getCommandTests()
344344
})
345345
];
346346

347+
yield 'crud_repository' => [MakerTestDetails::createTest(
348+
$this->getMakerInstance(MakeCrud::class),
349+
[
350+
// entity class name
351+
'SweetFood',
352+
])
353+
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeCrudRepository')
354+
// need for crud web tests
355+
->addExtraDependencies('symfony/css-selector')
356+
->addReplacement(
357+
'phpunit.xml.dist',
358+
'mysql://db_user:db_password@127.0.0.1:3306/db_name',
359+
'sqlite:///%kernel.project_dir%/var/app.db'
360+
)
361+
->addReplacement(
362+
'.env',
363+
'mysql://db_user:db_password@127.0.0.1:3306/db_name',
364+
'sqlite:///%kernel.project_dir%/var/app.db'
365+
)
366+
->addPreMakeCommand('php bin/console doctrine:schema:create --env=test')
367+
->assert(function(string $output, string $directory) {
368+
$this->assertFileExists($directory.'/src/Controller/SweetFoodController.php');
369+
$this->assertFileExists($directory.'/src/Form/SweetFoodType.php');
370+
371+
$this->assertContains('created: src/Controller/SweetFoodController.php', $output);
372+
$this->assertContains('created: src/Form/SweetFoodType.php', $output);
373+
})
374+
];
375+
347376
yield 'crud_with_no_base' => [MakerTestDetails::createTest(
348377
$this->getMakerInstance(MakeCrud::class),
349378
[
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace App\Entity;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
7+
/**
8+
* @ORM\Entity(repositoryClass="App\Repository\SweetFoodRepository")
9+
*/
10+
class SweetFood
11+
{
12+
/**
13+
* @ORM\Column(name="id", type="integer")
14+
* @ORM\Id
15+
* @ORM\GeneratedValue(strategy="AUTO")
16+
*/
17+
private $id;
18+
19+
/**
20+
* @ORM\Column(name="title", type="string", length=255)
21+
*/
22+
private $title;
23+
24+
public function getId()
25+
{
26+
return $this->id;
27+
}
28+
29+
/**
30+
* @return string
31+
*/
32+
public function getTitle()
33+
{
34+
return $this->title;
35+
}
36+
37+
/**
38+
* @param string $title
39+
*/
40+
public function setTitle($title)
41+
{
42+
$this->title = $title;
43+
}
44+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace App\Repository;
4+
5+
use App\Entity\SweetFood;
6+
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
7+
use Symfony\Bridge\Doctrine\RegistryInterface;
8+
9+
/**
10+
* @method SweetFood|null find($id, $lockMode = null, $lockVersion = null)
11+
* @method SweetFood|null findOneBy(array $criteria, array $orderBy = null)
12+
* @method SweetFood[] findAll()
13+
* @method SweetFood[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
14+
*/
15+
class SweetFoodRepository extends ServiceEntityRepository
16+
{
17+
public function __construct(RegistryInterface $registry)
18+
{
19+
parent::__construct($registry, SweetFood::class);
20+
}
21+
22+
/*
23+
public function findBySomething($value)
24+
{
25+
return $this->createQueryBuilder('t')
26+
->where('t.something = :value')->setParameter('value', $value)
27+
->orderBy('t.id', 'ASC')
28+
->setMaxResults(10)
29+
->getQuery()
30+
->getResult()
31+
;
32+
}
33+
*/
34+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace App\Tests;
4+
5+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6+
7+
class GeneratedCrudControllerTest extends WebTestCase
8+
{
9+
public function testIndexAction()
10+
{
11+
$client = self::createClient();
12+
$client->request('GET', '/sweet/food/');
13+
$this->assertTrue($client->getResponse()->isSuccessful());
14+
}
15+
}

0 commit comments

Comments
 (0)