Skip to content

Commit e1e6719

Browse files
tcochGromNaN
authored andcommitted
[make:crud] Use CamelCase for getter and setter
1 parent 8843412 commit e1e6719

4 files changed

Lines changed: 100 additions & 4 deletions

File tree

templates/crud/test/Test.EntityManager.tpl.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function testShow(): void
6161
$this->markTestIncomplete();
6262
$fixture = new <?= $entity_class_name; ?>();
6363
<?php foreach ($form_fields as $form_field => $typeOptions): ?>
64-
$fixture->set<?= ucfirst($form_field); ?>('My Title');
64+
$fixture->set<?= Str::asCamelCase($form_field); ?>('My Title');
6565
<?php endforeach; ?>
6666

6767
$this->manager->persist($fixture);
@@ -80,7 +80,7 @@ public function testEdit(): void
8080
$this->markTestIncomplete();
8181
$fixture = new <?= $entity_class_name; ?>();
8282
<?php foreach ($form_fields as $form_field => $typeOptions): ?>
83-
$fixture->set<?= ucfirst($form_field); ?>('Value');
83+
$fixture->set<?= Str::asCamelCase($form_field); ?>('Value');
8484
<?php endforeach; ?>
8585

8686
$this->manager->persist($fixture);
@@ -99,7 +99,7 @@ public function testEdit(): void
9999
$fixture = $this-><?= lcfirst($entity_var_singular); ?>Repository->findAll();
100100

101101
<?php foreach ($form_fields as $form_field => $typeOptions): ?>
102-
self::assertSame('Something New', $fixture[0]->get<?= ucfirst($form_field); ?>());
102+
self::assertSame('Something New', $fixture[0]->get<?= Str::asCamelCase($form_field); ?>());
103103
<?php endforeach; ?>
104104
}
105105

@@ -108,7 +108,7 @@ public function testRemove(): void
108108
$this->markTestIncomplete();
109109
$fixture = new <?= $entity_class_name; ?>();
110110
<?php foreach ($form_fields as $form_field => $typeOptions): ?>
111-
$fixture->set<?= ucfirst($form_field); ?>('Value');
111+
$fixture->set<?= Str::asCamelCase($form_field); ?>('Value');
112112
<?php endforeach; ?>
113113

114114
$this->manager->persist($fixture);

tests/Maker/MakeCrudTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,28 @@ public function getTestDetails(): \Generator
8787
}),
8888
];
8989

90+
yield 'it_generates_correct_class_methods' => [$this->createMakerTest()
91+
->addExtraDependencies('symfony/test-pack')
92+
->run(function (MakerTestRunner $runner) {
93+
$runner->copy(
94+
'make-crud/Foo.php',
95+
'src/Entity/Foo.php'
96+
);
97+
98+
$output = $runner->runMaker([
99+
'Foo', // Entity Class Name
100+
'', // Default Controller,
101+
'y', // Generate Tests
102+
]);
103+
104+
$this->assertStringContainsString('src/Controller/FooController.php', $output);
105+
$this->assertStringContainsString('src/Form/FooType.php', $output);
106+
$this->assertStringContainsString('tests/Controller/FooControllerTest.php', $output);
107+
108+
$this->runCrudTest($runner, 'it_generates_correct_class_methods.php');
109+
}),
110+
];
111+
90112
yield 'it_generates_crud_custom_repository_with_test' => [$this->createMakerTest()
91113
->addExtraDependencies('symfony/test-pack')
92114
->run(function (MakerTestRunner $runner) {

tests/fixtures/make-crud/Foo.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace App\Entity;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
7+
#[ORM\Entity()]
8+
class Foo
9+
{
10+
#[ORM\Id]
11+
#[ORM\GeneratedValue]
12+
#[ORM\Column()]
13+
private ?int $id = null;
14+
15+
#[ORM\Column(length: 255)]
16+
private ?string $foo = null;
17+
18+
#[ORM\Column(length: 255)]
19+
private ?string $foo_bar = null;
20+
21+
public function getId()
22+
{
23+
return $this->id;
24+
}
25+
26+
public function getFoo(): string
27+
{
28+
return $this->foo;
29+
}
30+
31+
public function setFoo(string $foo): self
32+
{
33+
$this->foo = $foo;
34+
35+
return $this;
36+
}
37+
38+
public function getFooBar(): string
39+
{
40+
return $this->foo_bar;
41+
}
42+
43+
public function setFooBar(string $foo_bar): self
44+
{
45+
$this->foo_bar = $foo_bar;
46+
47+
return $this;
48+
}
49+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
4+
namespace App\Tests;
5+
6+
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
7+
8+
class GeneratedCrudControllerTest extends WebTestCase
9+
{
10+
/**
11+
* Refers to the source folder where all generated files will be placed.
12+
* Facilitates access to src/, templates/ and tests/ folders.
13+
* @var string
14+
*/
15+
private string $rootFolder = __DIR__ . "/..";
16+
17+
public function testGeneratedTestsHasCorrectGettersAndSetters()
18+
{
19+
$testFileContent = file_get_contents($this->rootFolder . '/tests/Controller/FooControllerTest.php');
20+
21+
$this->assertStringContainsString("getFooBar()", $testFileContent);
22+
$this->assertMatchesRegularExpression("/setFooBar(.*)/", $testFileContent);
23+
$this->assertStringNotContainsString("getFoo_bar()", $testFileContent);
24+
}
25+
}

0 commit comments

Comments
 (0)