From 9b7aea36c7405c34d7425f7ea23833981e89848b Mon Sep 17 00:00:00 2001 From: LeJeanbono Date: Mon, 4 Mar 2019 01:48:25 +0100 Subject: [PATCH 1/9] Question helper return zero as string --- src/Util/ClassSourceManipulator.php | 2 +- tests/Util/ClassSourceManipulatorTest.php | 11 +++++ .../User_simple_prop_zero.php | 40 +++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 tests/Util/fixtures/add_entity_field/User_simple_prop_zero.php diff --git a/src/Util/ClassSourceManipulator.php b/src/Util/ClassSourceManipulator.php index 4d54e06e1..a292f53c4 100644 --- a/src/Util/ClassSourceManipulator.php +++ b/src/Util/ClassSourceManipulator.php @@ -404,7 +404,7 @@ private function quoteAnnotationValue($value) return 'null'; } - if (\is_int($value)) { + if (\is_int($value) || '0' === $value) { return $value; } diff --git a/tests/Util/ClassSourceManipulatorTest.php b/tests/Util/ClassSourceManipulatorTest.php index c9e96af2e..3df963b0d 100644 --- a/tests/Util/ClassSourceManipulatorTest.php +++ b/tests/Util/ClassSourceManipulatorTest.php @@ -237,6 +237,17 @@ public function getAddEntityFieldTests() ], 'User_simple_prop_already_exists.php' ]; + + yield 'entity_field_property_zero' => [ + 'User_simple.php', + 'decimal', + [ + 'type' => 'decimal', + 'precision' => 6, + 'scale' => 0, + ], + 'User_simple_prop_zero.php' + ]; } /** diff --git a/tests/Util/fixtures/add_entity_field/User_simple_prop_zero.php b/tests/Util/fixtures/add_entity_field/User_simple_prop_zero.php new file mode 100644 index 000000000..478c57d33 --- /dev/null +++ b/tests/Util/fixtures/add_entity_field/User_simple_prop_zero.php @@ -0,0 +1,40 @@ +id; + } + + public function getDecimal() + { + return $this->decimal; + } + + public function setDecimal($decimal): self + { + $this->decimal = $decimal; + + return $this; + } +} From a319caa55b7b22b6fa1d594ee0f1dfb665779317 Mon Sep 17 00:00:00 2001 From: Oleg Voronkovich Date: Tue, 2 Apr 2019 21:01:39 +0300 Subject: [PATCH 2/9] [make:validator] Custom constraints should ignore null and empty values --- src/Resources/skeleton/validator/Validator.tpl.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Resources/skeleton/validator/Validator.tpl.php b/src/Resources/skeleton/validator/Validator.tpl.php index f25cec2aa..52701e03c 100644 --- a/src/Resources/skeleton/validator/Validator.tpl.php +++ b/src/Resources/skeleton/validator/Validator.tpl.php @@ -11,6 +11,11 @@ public function validate($value, Constraint $constraint) { /* @var $constraint \ */ + if (null === $value || '' === $value) { + return; + } + + // TODO: implement the validation here $this->context->buildViolation($constraint->message) ->setParameter('{{ value }}', $value) ->addViolation(); From e168b5139771144b03fe80e788cab45ccb4fe859 Mon Sep 17 00:00:00 2001 From: Max Kuznetsov Date: Thu, 6 Jun 2019 12:44:58 +0300 Subject: [PATCH 3/9] Turn off generated Voter and Normalizer by default --- src/Resources/skeleton/security/Voter.tpl.php | 11 +++++++---- src/Resources/skeleton/serializer/Normalizer.tpl.php | 7 ++++++- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Resources/skeleton/security/Voter.tpl.php b/src/Resources/skeleton/security/Voter.tpl.php index 181dbcdac..c3ee8561d 100644 --- a/src/Resources/skeleton/security/Voter.tpl.php +++ b/src/Resources/skeleton/security/Voter.tpl.php @@ -10,10 +10,13 @@ class extends Voter { protected function supports($attribute, $subject) { - // replace with your own logic - // https://symfony.com/doc/current/security/voters.html - return in_array($attribute, ['POST_EDIT', 'POST_VIEW']) - && $subject instanceof \App\Entity\BlogPost; + // Replace with your own logic + // See https://symfony.com/doc/current/security/voters.html + // + // return in_array($attribute, ['POST_EDIT', 'POST_VIEW']) + // && $subject instanceof \App\Entity\YourEntity; + + return false; } protected function voteOnAttribute($attribute, $subject, TokenInterface $token) diff --git a/src/Resources/skeleton/serializer/Normalizer.tpl.php b/src/Resources/skeleton/serializer/Normalizer.tpl.php index 6d38f631b..efe438bbe 100644 --- a/src/Resources/skeleton/serializer/Normalizer.tpl.php +++ b/src/Resources/skeleton/serializer/Normalizer.tpl.php @@ -25,6 +25,11 @@ public function normalize($object, $format = null, array $context = array()): ar public function supportsNormalization($data, $format = null): bool { - return $data instanceof \App\Entity\BlogPost; + // Replace with your own logic + // See https://symfony.com/doc/current/serializer/custom_normalizer.html + // + // return $data instanceof \App\Entity\YourEntity; + + return false; } } From b5a3dc57a5c66798ed82a3fa1c15f552a28859d1 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Fri, 26 Apr 2019 07:57:06 +0200 Subject: [PATCH 4/9] fixed typos --- src/Validator.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Validator.php b/src/Validator.php index 94d3580c1..ae57bff65 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -65,7 +65,7 @@ public static function validateClassName(string $className, string $errorMessage public static function notBlank(string $value = null): string { if (null === $value || '' === $value) { - throw new RuntimeCommandException('This value cannot be blank'); + throw new RuntimeCommandException('This value cannot be blank.'); } return $value; @@ -181,7 +181,7 @@ public static function classExists(string $className, string $errorMessage = '') self::notBlank($className); if (!class_exists($className)) { - $errorMessage = $errorMessage ?: sprintf('Class "%s" doesn\'t exists. Please enter existing full class name', $className); + $errorMessage = $errorMessage ?: sprintf('Class "%s" doesn\'t exist; please enter an existing full class name.', $className); throw new RuntimeCommandException($errorMessage); } @@ -194,15 +194,15 @@ public static function entityExists(string $className = null, array $entities = self::notBlank($className); if (empty($entities)) { - throw new RuntimeCommandException('There is no registered entities. Please create entity before use this command'); + throw new RuntimeCommandException('There is no registered entities; please create entity before use this command.'); } if (0 === strpos($className, '\\')) { - self::classExists($className, sprintf('Entity "%s" doesn\'t exists. Please enter existing one or create new', $className)); + self::classExists($className, sprintf('Entity "%s" doesn\'t exist; please enter an existing one or create a new one.', $className)); } if (!\in_array($className, $entities)) { - throw new RuntimeCommandException(sprintf('Entity "%s" doesn\'t exists. Please enter existing one or create new', $className)); + throw new RuntimeCommandException(sprintf('Entity "%s" doesn\'t exist; please enter an existing one or create a new one.', $className)); } return $className; @@ -213,7 +213,7 @@ public static function classDoesNotExist($className): string self::notBlank($className); if (class_exists($className)) { - throw new RuntimeCommandException(sprintf('Class "%s" already exists', $className)); + throw new RuntimeCommandException(sprintf('Class "%s" already exists.', $className)); } return $className; From b147a52c3360b8758ba659c26605a8ae219f2db1 Mon Sep 17 00:00:00 2001 From: Adrien LUCAS Date: Mon, 6 May 2019 11:56:20 +0200 Subject: [PATCH 5/9] Use the new api provided by the WebTestAssertionsTrait --- src/Maker/MakeFunctionalTest.php | 5 ++++- src/Resources/skeleton/test/Functional.tpl.php | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Maker/MakeFunctionalTest.php b/src/Maker/MakeFunctionalTest.php index 174bc543b..204ec186f 100644 --- a/src/Maker/MakeFunctionalTest.php +++ b/src/Maker/MakeFunctionalTest.php @@ -20,6 +20,7 @@ use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\CssSelector\CssSelectorConverter; +use Symfony\Bundle\FrameworkBundle\Test\WebTestAssertionsTrait; /** * @author Javier Eguiluz @@ -52,7 +53,9 @@ public function generate(InputInterface $input, ConsoleStyle $io, Generator $gen $generator->generateClass( $testClassNameDetails->getFullName(), 'test/Functional.tpl.php', - [] + [ + 'web_assertions_are_available' => class_exists(WebTestAssertionsTrait::class), + ] ); $generator->writeChanges(); diff --git a/src/Resources/skeleton/test/Functional.tpl.php b/src/Resources/skeleton/test/Functional.tpl.php index 92c03c066..c56bc59b4 100644 --- a/src/Resources/skeleton/test/Functional.tpl.php +++ b/src/Resources/skeleton/test/Functional.tpl.php @@ -11,7 +11,12 @@ public function testSomething() $client = static::createClient(); $crawler = $client->request('GET', '/'); + + $this->assertResponseIsSuccessful(); + $this->assertSelectorTextContains('h1', 'Hello World'); + $this->assertSame(200, $client->getResponse()->getStatusCode()); $this->assertContains('Hello World', $crawler->filter('h1')->text()); + } } From 42d5fb144b780bf01969211b11919272b706854f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20Bogusz?= Date: Tue, 7 May 2019 01:58:56 +0200 Subject: [PATCH 6/9] [Validator] Add encoding test to validateClassName --- src/Validator.php | 6 ++++++ tests/ValidatorTest.php | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/src/Validator.php b/src/Validator.php index ae57bff65..217e9db0f 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -47,6 +47,12 @@ public static function validateClassName(string $className, string $errorMessage ]; foreach ($pieces as $piece) { + if (!mb_check_encoding($piece, 'UTF-8')) { + $errorMessage = $errorMessage ?: sprintf('"%s" is not a UTF-8-encoded string.', $piece); + + throw new RuntimeCommandException($errorMessage); + } + if (!preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $piece)) { $errorMessage = $errorMessage ?: sprintf('"%s" is not valid as a PHP class name (it must start with a letter or underscore, followed by any number of letters, numbers, or underscores)', $className); diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index 702e445bd..ed95e2b61 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -62,4 +62,11 @@ public function testInvalidClassName() $this->expectExceptionMessage('"Class" is a reserved keyword and thus cannot be used as class name in PHP.'); Validator::validateClassName('App\Entity\Class'); } + + public function testInvalidEncodingInClassName() + { + $this->expectException(RuntimeCommandException::class); + $this->expectExceptionMessage('"�Controller" is not a UTF-8-encoded string.'); + Validator::validateClassName(mb_convert_encoding('Ś', 'ISO-8859-2', 'UTF-8')); + } } From ac701047c17a33a6015a30cf4071027718d4147e Mon Sep 17 00:00:00 2001 From: Smaine Milianni Date: Sun, 19 May 2019 11:30:28 +0200 Subject: [PATCH 7/9] add a agree terms checkbox --- src/Maker/MakeRegistrationForm.php | 13 +++++++++++++ .../skeleton/registration/twig_template.tpl.php | 1 + .../tests/RegistrationFormTest.php | 6 +++++- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Maker/MakeRegistrationForm.php b/src/Maker/MakeRegistrationForm.php index e5a01c30a..82f297ac6 100644 --- a/src/Maker/MakeRegistrationForm.php +++ b/src/Maker/MakeRegistrationForm.php @@ -32,6 +32,7 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\Extension\Core\Type\CheckboxType; use Symfony\Component\Form\Extension\Core\Type\PasswordType; use Symfony\Component\Routing\RouterInterface; use Symfony\Component\Validator\Validation; @@ -311,6 +312,17 @@ private function generateFormClass(ClassNameDetails $userClassDetails, Generator 'max' => 4096, ]), ], +EOF + ], + 'agreeTerms' => [ + 'type' => CheckboxType::class, + 'options_code' => << false, + 'constraints' => [ + new IsTrue([ + 'message' => 'You should agree to our terms.', + ]), + ], EOF ], ]; @@ -322,6 +334,7 @@ private function generateFormClass(ClassNameDetails $userClassDetails, Generator [ 'Symfony\Component\Validator\Constraints\NotBlank', 'Symfony\Component\Validator\Constraints\Length', + 'Symfony\Component\Validator\Constraints\IsTrue', ] ); diff --git a/src/Resources/skeleton/registration/twig_template.tpl.php b/src/Resources/skeleton/registration/twig_template.tpl.php index bae80f72b..277b72ed8 100644 --- a/src/Resources/skeleton/registration/twig_template.tpl.php +++ b/src/Resources/skeleton/registration/twig_template.tpl.php @@ -6,6 +6,7 @@ {{ form_start(registrationForm) }} {{ form_row(registrationForm.) }} {{ form_row(registrationForm.plainPassword) }} + {{ form_row(registrationForm.agreeTerms) }} {{ form_end(registrationForm) }} diff --git a/tests/fixtures/MakeRegistrationFormEntity/tests/RegistrationFormTest.php b/tests/fixtures/MakeRegistrationFormEntity/tests/RegistrationFormTest.php index b26a5f40f..bd0fb1f20 100644 --- a/tests/fixtures/MakeRegistrationFormEntity/tests/RegistrationFormTest.php +++ b/tests/fixtures/MakeRegistrationFormEntity/tests/RegistrationFormTest.php @@ -5,7 +5,6 @@ use Doctrine\ORM\EntityManager; use App\Entity\User; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; -use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder; class RegistrationFormTest extends WebTestCase { @@ -24,6 +23,7 @@ public function testRegistrationSuccessful() $form = $crawler->selectButton('Register')->form(); $form['registration_form[email]'] = 'ryan@symfonycasts.com'; $form['registration_form[plainPassword]'] = '1234yaaay'; + $form['registration_form[agreeTerms]'] = true; $client->submit($form); $this->assertSame(302, $client->getResponse()->getStatusCode()); @@ -63,5 +63,9 @@ public function testRegistrationValidationError() 'Your password should be at least 6 characters', $client->getResponse()->getContent() ); + $this->assertContains( + 'You should agree to our terms.', + $client->getResponse()->getContent() + ); } } From 0c45d1f6882816eebdaf3151204fc45b90640c18 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Sat, 1 Jun 2019 08:37:51 -0400 Subject: [PATCH 8/9] tweaking error language --- src/Validator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Validator.php b/src/Validator.php index 217e9db0f..21e3b2d08 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -200,7 +200,7 @@ public static function entityExists(string $className = null, array $entities = self::notBlank($className); if (empty($entities)) { - throw new RuntimeCommandException('There is no registered entities; please create entity before use this command.'); + throw new RuntimeCommandException('There are no registered entities; please create an entity before using this command.'); } if (0 === strpos($className, '\\')) { From a6c318bf3a75a23e9186d76793fe0587abf26fb1 Mon Sep 17 00:00:00 2001 From: Max Kuznetsov Date: Mon, 10 Jun 2019 22:04:50 +0300 Subject: [PATCH 9/9] Fix Validator test about invalid encoding --- tests/ValidatorTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index ed95e2b61..455193b56 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -66,7 +66,8 @@ public function testInvalidClassName() public function testInvalidEncodingInClassName() { $this->expectException(RuntimeCommandException::class); - $this->expectExceptionMessage('"�Controller" is not a UTF-8-encoded string.'); - Validator::validateClassName(mb_convert_encoding('Ś', 'ISO-8859-2', 'UTF-8')); + $invalidName = mb_convert_encoding('Fôö', 'ISO-8859-2', 'UTF-8'); + $this->expectExceptionMessage(sprintf('"%s" is not a UTF-8-encoded string.', $invalidName)); + Validator::validateClassName($invalidName); } }