Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for generating UUID id fields in entities #1329

Merged
merged 11 commits into from
Mar 22, 2024
Prev Previous commit
Next Next commit
Fixed coding standards issue
Fixed tests
  • Loading branch information
Coffee2CodeNL authored and jrushlow committed Mar 20, 2024
commit 9414f65055b179095e62eb913bf6f1c5efc432f0
1 change: 0 additions & 1 deletion src/Doctrine/EntityClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use ApiPlatform\Metadata\ApiResource;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\DBAL\Types\Type;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\MakerBundle\Generator;
use Symfony\Bundle\MakerBundle\Str;
Expand Down
3 changes: 1 addition & 2 deletions tests/Maker/MakeEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function getTestDetails(): \Generator
];

yield 'it_creates_a_new_class_with_uuid' => [$this->createMakeEntityTest()
->addExtraDependencies('uid')
->addExtraDependencies('symfony/uid')
->run(function (MakerTestRunner $runner) {
$runner->runMaker([
// entity class name
Expand All @@ -157,7 +157,6 @@ public function getTestDetails(): \Generator

$this->runEntityTest($runner);
}),

];

yield 'it_creates_a_new_class_with_fields' => [$this->createMakeEntityTest()
Expand Down
25 changes: 0 additions & 25 deletions tests/Maker/MakeRegistrationFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,31 +124,6 @@ public function getTestDetails(): \Generator
}),
];

yield 'it_generates_registration_with_uuid_entity_and_authenticator' => [$this->createRegistrationFormTest()
->run(function (MakerTestRunner $runner) {
$this->makeUser($runner);

$runner->modifyYamlFile('config/packages/security.yaml', function (array $data) {
$data['security']['firewalls']['main']['custom_authenticator'] = 'App\\Security\\StubAuthenticator';

return $data;
});

$runner->runMaker([
// user class guessed,
// username field guessed
// password guessed
'', // yes to add UniqueEntity
'n', // verify user
// firewall name guessed
'', // yes authenticate after
// 1 authenticator will be guessed
], '--uuid_id');

$this->runRegistrationTest($runner, 'it_generates_registration_with_uuid_entity_and_authenticator.php');
}),
];

yield 'it_generates_registration_form_with_no_guessing' => [$this->createRegistrationFormTest()
->run(function (MakerTestRunner $runner) {
$this->makeUser($runner, 'emailAlt');
Expand Down
65 changes: 65 additions & 0 deletions tests/Maker/MakeResetPasswordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,71 @@ 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',
'jr@rushlow.dev',
'SymfonyCasts',
], '--uuid_id');

$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'
);

if (60000 > $runner->getSymfonyVersion()) {
// @legacy - In 5.4 tests, we need to tell Symfony to look for the route attributes in `src/Controller`
$runner->copy('router-annotations.yaml', 'config/routes/annotations.yaml');
}

$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)
Expand Down
2 changes: 1 addition & 1 deletion tests/Maker/MakeUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function getTestDetails(): \Generator

yield 'it_generates_entity_with_password_and_uuid' => [$this->createMakerTest()
->addExtraDependencies('doctrine')
->addExtraDependencies('uid')
->addExtraDependencies('symfony/uid')
->run(function (MakerTestRunner $runner) {
$runner->copy(
'make-user/standard_setup',
Expand Down

This file was deleted.