-
-
Notifications
You must be signed in to change notification settings - Fork 416
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #1329 Add support for generating UUID id fields in entities
Co-authored-by: Jesse Rushlow <[email protected]>
- Loading branch information
1 parent
7217480
commit 970f4c0
Showing
11 changed files
with
260 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?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\Maker\Common; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Uid\Uuid; | ||
|
||
/** | ||
* @author Jesse Rushlow<[email protected]> | ||
* | ||
* @internal | ||
*/ | ||
trait UidTrait | ||
{ | ||
/** | ||
* Set by calling checkIsUsingUuid(). | ||
* Use in a maker's generate() to determine if entity wants to use uuid's. | ||
*/ | ||
protected bool $usesUid = false; | ||
|
||
/** | ||
* Call this in a maker's configure() to consistently allow entity's with UUID's. | ||
*/ | ||
protected function addWithUuidOption(Command $command): Command | ||
{ | ||
$command->addOption('with-uuid', 'u', InputOption::VALUE_NONE, 'Use UUID for entity "id"'); | ||
|
||
return $command; | ||
} | ||
|
||
/** | ||
* Call this as early as possible in a maker's interact(). | ||
*/ | ||
protected function checkIsUsingUid(InputInterface $input): void | ||
{ | ||
if (($this->usesUid = $input->getOption('with-uuid')) && !class_exists(Uuid::class)) { | ||
throw new \RuntimeException('You must install symfony/uid to use Uuid\'s as "id" (composer require symfony/uid)'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -82,6 +82,66 @@ public function getTestDetails(): \Generator | |
}), | ||
]; | ||
|
||
yield 'it_generates_with_uuid' => [$this->createMakerTest() | ||
->setSkippedPhpVersions(80100, 80109) | ||
->addExtraDependencies('symfony/uid') | ||
->run(function (MakerTestRunner $runner) { | ||
$this->makeUser($runner); | ||
|
||
$output = $runner->runMaker([ | ||
'App\Entity\User', | ||
'app_home', | ||
'[email protected]', | ||
'SymfonyCasts', | ||
], '--with-uuid'); | ||
|
||
$this->assertStringContainsString('Success', $output); | ||
|
||
$generatedFiles = [ | ||
'src/Controller/ResetPasswordController.php', | ||
'src/Entity/ResetPasswordRequest.php', | ||
'src/Form/ChangePasswordFormType.php', | ||
'src/Form/ResetPasswordRequestFormType.php', | ||
'src/Repository/ResetPasswordRequestRepository.php', | ||
'templates/reset_password/check_email.html.twig', | ||
'templates/reset_password/email.html.twig', | ||
'templates/reset_password/request.html.twig', | ||
'templates/reset_password/reset.html.twig', | ||
]; | ||
|
||
foreach ($generatedFiles as $file) { | ||
$this->assertFileExists($runner->getPath($file)); | ||
} | ||
|
||
$resetPasswordRequestEntityContents = file_get_contents($runner->getPath('src/Entity/ResetPasswordRequest.php')); | ||
$this->assertStringContainsString('use Symfony\Component\Uid\Uuid;', $resetPasswordRequestEntityContents); | ||
$this->assertStringContainsString('[ORM\CustomIdGenerator(class: \'doctrine.uuid_generator\')]', $resetPasswordRequestEntityContents); | ||
|
||
$configFileContents = file_get_contents($runner->getPath('config/packages/reset_password.yaml')); | ||
|
||
// Flex recipe adds comments in reset_password.yaml, check file was replaced by maker | ||
$this->assertStringNotContainsString('#', $configFileContents); | ||
|
||
$resetPasswordConfig = $runner->readYaml('config/packages/reset_password.yaml'); | ||
|
||
$this->assertSame('App\Repository\ResetPasswordRequestRepository', $resetPasswordConfig['symfonycasts_reset_password']['request_password_repository']); | ||
|
||
$runner->writeFile( | ||
'config/packages/mailer.yaml', | ||
Yaml::dump(['framework' => [ | ||
'mailer' => ['dsn' => 'null://null'], | ||
]]) | ||
); | ||
|
||
$runner->copy( | ||
'make-reset-password/tests/it_generates_with_normal_setup.php', | ||
'tests/ResetPasswordFunctionalTest.php' | ||
); | ||
|
||
$runner->runTests(); | ||
}), | ||
]; | ||
|
||
yield 'it_generates_with_translator_installed' => [$this->createMakerTest() | ||
// @legacy - drop skipped versions when PHP 8.1 is no longer supported. | ||
->setSkippedPhpVersions(80100, 80109) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.