Skip to content

Commit ceb7e02

Browse files
committed
Merge branch 'issue-322' of https://github.com/antalaron/maker-bundle into antalaron-issue-322
* 'issue-322' of https://github.com/antalaron/maker-bundle: Swap target entity namespace search Reproduce the error
2 parents d571171 + 1003c21 commit ceb7e02

File tree

4 files changed

+136
-4
lines changed

4 files changed

+136
-4
lines changed

src/Maker/MakeEntity.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -468,16 +468,16 @@ private function askRelationDetails(ConsoleStyle $io, string $generatedEntityCla
468468

469469
$targetEntityClass = $io->askQuestion($question);
470470

471-
if (!class_exists($targetEntityClass)) {
472-
if (!class_exists($this->getEntityNamespace().'\\'.$targetEntityClass)) {
471+
if (!class_exists($designatedTargetClass = $this->getEntityNamespace().'\\'.$targetEntityClass)) {
472+
if (!class_exists($designatedTargetClass = $targetEntityClass)) {
473473
$io->error(sprintf('Unknown class "%s"', $targetEntityClass));
474474
$targetEntityClass = null;
475475

476476
continue;
477477
}
478-
479-
$targetEntityClass = $this->getEntityNamespace().'\\'.$targetEntityClass;
480478
}
479+
480+
$targetEntityClass = $designatedTargetClass;
481481
}
482482

483483
// help the user select the type

tests/Maker/FunctionalTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,36 @@ public function getCommandEntityTests()
906906
->setRequiredPhpVersion(70100)
907907
];
908908

909+
yield 'entity_exists_in_root' => [MakerTestDetails::createTest(
910+
$this->getMakerInstance(MakeEntity::class),
911+
[
912+
// entity class name
913+
'Directory',
914+
// field name
915+
'parentDirectory',
916+
// add a relationship field
917+
'relation',
918+
// the target entity
919+
'Directory',
920+
// relation type
921+
'ManyToOne',
922+
// nullable
923+
'y',
924+
// do you want to generate an inverse relation? (default to yes)
925+
'',
926+
// field name on opposite side
927+
'childDirectories',
928+
// orphanRemoval (default to no)
929+
'',
930+
// finish adding fields
931+
'',
932+
])
933+
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeEntityExistsInRoot')
934+
->configureDatabase()
935+
->updateSchemaAfterCommand()
936+
->setRequiredPhpVersion(70100)
937+
];
938+
909939
yield 'entity_one_to_many_simple' => [MakerTestDetails::createTest(
910940
$this->getMakerInstance(MakeEntity::class),
911941
[
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace App\Entity;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
7+
/**
8+
* @ORM\Entity()
9+
*/
10+
class Directory
11+
{
12+
/**
13+
* @ORM\Id
14+
* @ORM\GeneratedValue
15+
* @ORM\Column(type="integer")
16+
*/
17+
private $id;
18+
19+
/**
20+
* @ORM\Column(type="string", length=255, nullable=true)
21+
*/
22+
private $name;
23+
24+
/**
25+
* @ORM\Column(type="datetime", nullable=true)
26+
*/
27+
private $createdAt;
28+
29+
public function getId()
30+
{
31+
return $this->id;
32+
}
33+
34+
public function getName()
35+
{
36+
return $this->name;
37+
}
38+
39+
public function setName(?string $name)
40+
{
41+
$this->name = $name;
42+
}
43+
44+
public function getCreatedAt(): ?\DateTimeInterface
45+
{
46+
return $this->createdAt;
47+
}
48+
49+
public function setCreatedAt(?\DateTimeInterface $createdAt)
50+
{
51+
$this->createdAt = $createdAt;
52+
}
53+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace App\Tests;
4+
5+
use Doctrine\Common\Collections\ArrayCollection;
6+
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
7+
use Doctrine\ORM\EntityManager;
8+
use App\Entity\Directory;
9+
10+
class GeneratedEntityTest extends KernelTestCase
11+
{
12+
public function testGeneratedEntity()
13+
{
14+
self::bootKernel();
15+
/** @var EntityManager $em */
16+
$em = self::$kernel->getContainer()
17+
->get('doctrine')
18+
->getManager();
19+
20+
$em->createQuery('DELETE FROM App\\Entity\\Directory u')->execute();
21+
22+
$directory = new Directory();
23+
// check that the constructor was instantiated properly
24+
$this->assertInstanceOf(ArrayCollection::class, $directory->getChildDirectories());
25+
// set existing field
26+
$directory->setName('root');
27+
$em->persist($directory);
28+
29+
$subDir = new Directory();
30+
$subDir->setName('settings');
31+
$subDir->setParentDirectory($directory);
32+
$em->persist($subDir);
33+
34+
// set via the inverse side
35+
$subDir2 = new Directory();
36+
$subDir2->setName('fixtures');
37+
$directory->addChildDirectory($subDir2);
38+
$em->persist($subDir2);
39+
40+
$em->flush();
41+
$em->refresh($directory);
42+
43+
$actualDirectory = $em->getRepository(Directory::class)
44+
->findAll();
45+
46+
$this->assertCount(3, $actualDirectory);
47+
$this->assertCount(2, $actualDirectory[0]->getChildDirectories());
48+
}
49+
}

0 commit comments

Comments
 (0)