Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Maker/MakeFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <javier.eguiluz@gmail.com>
Expand Down Expand Up @@ -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();
Expand Down
13 changes: 13 additions & 0 deletions src/Maker/MakeRegistrationForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -311,6 +312,17 @@ private function generateFormClass(ClassNameDetails $userClassDetails, Generator
'max' => 4096,
]),
],
EOF
],
'agreeTerms' => [
'type' => CheckboxType::class,
'options_code' => <<<EOF
'mapped' => false,
'constraints' => [
new IsTrue([
'message' => 'You should agree to our terms.',
]),
],
EOF
],
];
Expand All @@ -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',
]
);

Expand Down
1 change: 1 addition & 0 deletions src/Resources/skeleton/registration/twig_template.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
{{ form_start(registrationForm) }}
{{ form_row(registrationForm.<?= $username_field ?>) }}
{{ form_row(registrationForm.plainPassword) }}
{{ form_row(registrationForm.agreeTerms) }}

<button class="btn">Register</button>
{{ form_end(registrationForm) }}
Expand Down
11 changes: 7 additions & 4 deletions src/Resources/skeleton/security/Voter.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ class <?= $class_name ?> 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)
Expand Down
7 changes: 6 additions & 1 deletion src/Resources/skeleton/serializer/Normalizer.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
5 changes: 5 additions & 0 deletions src/Resources/skeleton/test/Functional.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ public function testSomething()
$client = static::createClient();
$crawler = $client->request('GET', '/');

<?php if($web_assertions_are_available) { ?>
$this->assertResponseIsSuccessful();
$this->assertSelectorTextContains('h1', 'Hello World');
<?php } else { ?>
$this->assertSame(200, $client->getResponse()->getStatusCode());
$this->assertContains('Hello World', $crawler->filter('h1')->text());
<?php } ?>
}
}
5 changes: 5 additions & 0 deletions src/Resources/skeleton/validator/Validator.tpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public function validate($value, Constraint $constraint)
{
/* @var $constraint \<?= $constraint_class_name ?> */

if (null === $value || '' === $value) {
return;
}

// TODO: implement the validation here
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $value)
->addViolation();
Expand Down
2 changes: 1 addition & 1 deletion src/Util/ClassSourceManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ private function quoteAnnotationValue($value)
return 'null';
}

if (\is_int($value)) {
if (\is_int($value) || '0' === $value) {
return $value;
}

Expand Down
18 changes: 12 additions & 6 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -65,7 +71,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;
Expand Down Expand Up @@ -181,7 +187,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);
}
Expand All @@ -194,15 +200,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 are no registered entities; please create an entity before using 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;
Expand All @@ -213,7 +219,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;
Expand Down
11 changes: 11 additions & 0 deletions tests/Util/ClassSourceManipulatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
];
}

/**
Expand Down
40 changes: 40 additions & 0 deletions tests/Util/fixtures/add_entity_field/User_simple_prop_zero.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
*/
class User
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;

/**
* @ORM\Column(type="decimal", precision=6, scale=0)
*/
private $decimal;

public function getId(): ?int
{
return $this->id;
}

public function getDecimal()
{
return $this->decimal;
}

public function setDecimal($decimal): self
{
$this->decimal = $decimal;

return $this;
}
}
8 changes: 8 additions & 0 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,12 @@ 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);
$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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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());
Expand Down Expand Up @@ -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()
);
}
}