Skip to content

Commit 5f4d197

Browse files
committed
bug #186 fix crud template - entity field with underscore (LeJeanbono)
This PR was squashed before being merged into the 1.0-dev branch (closes #186). Discussion ---------- fix crud template - entity field with underscore #176 Commits ------- 3c956d4 fix crud template - cs fixer bc14d02 fix crud template - entity field with underscore 5bcf6c7 fix crud template - entity field with underscore
2 parents 5927f01 + 3c956d4 commit 5f4d197

File tree

3 files changed

+108
-1
lines changed

3 files changed

+108
-1
lines changed

src/GeneratorTwigHelper.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ public function __construct(FileManager $fileManager)
2525

2626
public function getEntityFieldPrintCode($entity, $field): string
2727
{
28-
$printCode = $entity.'.'.$field['fieldName'];
28+
$twigField = preg_replace_callback('/(?!^)_([a-z0-9])/', function ($s) {
29+
return strtoupper($s[1]);
30+
}, $field['fieldName']);
31+
$printCode = $entity.'.'.str_replace('_', '', $twigField);
2932

3033
switch ($field['type']) {
3134
case 'datetime':

tests/GeneratorTwigHelperTest.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace Symfony\Bundle\MakerBundle\Tests;
4+
5+
use Symfony\Bundle\MakerBundle\FileManager;
6+
use Symfony\Bundle\MakerBundle\GeneratorTwigHelper;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class GeneratorTwigHelperTest extends TestCase
10+
{
11+
12+
/**
13+
* @dataProvider getEntityFieldPrintCodeTests
14+
* @param string $entity
15+
* @param string $fieldName
16+
* @param string $fieldType
17+
* @param string $expect
18+
*/
19+
public function testGetEntityFieldPrintCode(string $entity, string $fieldName, string $fieldType, string $expect)
20+
{
21+
$generator = new GeneratorTwigHelper($this->createMock(FileManager::class));
22+
$field = [];
23+
$field['fieldName'] = $fieldName;
24+
$field['type'] = $fieldType;
25+
26+
$result = $generator->getEntityFieldPrintCode($entity, $field);
27+
28+
$this->assertSame($expect, $result);
29+
}
30+
31+
public function getEntityFieldPrintCodeTests()
32+
{
33+
yield 'normal' => [
34+
'entity',
35+
'normal',
36+
'string',
37+
'entity.normal'
38+
];
39+
40+
yield 'normal_upper' => [
41+
'entity',
42+
'normalUpper',
43+
'string',
44+
'entity.normalUpper'
45+
];
46+
47+
yield 'underscore' => [
48+
'entity',
49+
'with_underscore',
50+
'string',
51+
'entity.withUnderscore'
52+
];
53+
54+
yield 'underscore_number' => [
55+
'entity',
56+
'field_100',
57+
'string',
58+
'entity.field100'
59+
];
60+
61+
yield 'underscore_first' => [
62+
'entity',
63+
'_field',
64+
'string',
65+
'entity.field'
66+
];
67+
68+
yield 'normal_datetime' => [
69+
'entity',
70+
'normal',
71+
'datetime',
72+
'entity.normal ? entity.normal|date(\'Y-m-d H:i:s\') : \'\''
73+
];
74+
75+
yield 'normal_date' => [
76+
'entity',
77+
'normal',
78+
'date',
79+
'entity.normal ? entity.normal|date(\'Y-m-d\') : \'\''
80+
];
81+
82+
yield 'normal_time' => [
83+
'entity',
84+
'normal',
85+
'time',
86+
'entity.normal ? entity.normal|date(\'H:i:s\') : \'\''
87+
];
88+
89+
yield 'normal_array' => [
90+
'entity',
91+
'normal',
92+
'array',
93+
'entity.normal ? entity.normal|join(\', \') : \'\''
94+
];
95+
96+
yield 'normal_boolean' => [
97+
'entity',
98+
'normal',
99+
'boolean',
100+
'entity.normal ? \'Yes\' : \'No\''
101+
];
102+
}
103+
}

tests/fixtures/MakeCrud/src/Entity/SweetFood.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,5 @@ public function setTitle($title)
4141
{
4242
$this->title = $title;
4343
}
44+
4445
}

0 commit comments

Comments
 (0)