Skip to content

Commit 91a5e74

Browse files
authored
feature #849 Add local constraints (Wojdylak)
This PR was merged into the 1.11 branch. Discussion ---------- | Q | A | --------------- | ----- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Related tickets | N/A | License | MIT I have added Symfony constraints that correspond to database constraints, including the `Locale` constraint for enhanced validation, even though it doesn't directly match a database constraint. Commits ------- 2294441 Add local constraints fe67478 Fix psalm error caae244 Add missing return types in TranslatableValidatorTest class
2 parents bd6f4a3 + caae244 commit 91a5e74

File tree

8 files changed

+141
-3
lines changed

8 files changed

+141
-3
lines changed

UPGRADE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ These following services are now Doctrine listeners instead of Doctrine subscrib
2020
* Sylius\Bundle\ResourceBundle\EventListener\ORMRepositoryClassSubscriber
2121
* Sylius\Bundle\ResourceBundle\EventListener\ORMMappedSuperClassSubscriber
2222
23+
Applied the `UniqueEntity` constraint for `locale` and `translatable` fields, and added `NotBlank` & `Locale` constraints for the `locale` property in classes extending `Sylius\Resource\Model\AbstractTranslation`.
24+
25+
Applied the `Valid` constraint for the `getTranslations` method for objects implementing `Sylius\Resource\Model\TranslatableInterface`.
26+
2327

2428
## UPGRADE FOR `1.10.x`
2529

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"symfony/form": "^5.4 || ^6.0",
4343
"symfony/framework-bundle": "^5.4 || ^6.0",
4444
"symfony/http-foundation": "^5.4 || ^6.0",
45+
"symfony/intl": "^5.4 || ^6.0",
4546
"symfony/security-core": "^5.4 || ^6.0",
4647
"symfony/security-csrf": "^5.4 || ^6.0",
4748
"symfony/routing": "^5.4 || ^6.0",

psalm.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@
164164
<file name="src/Bundle/Routing/Configuration.php" />
165165
</errorLevel>
166166
</PossiblyNullReference>
167-
167+
168168
<PossiblyUndefinedArrayOffset>
169169
<errorLevel type="suppress">
170170
<file name="src/Bundle/Controller/ParametersParser.php" />
@@ -198,7 +198,7 @@
198198
<file name="src/Bundle/Form/DataTransformer/CollectionToStringTransformer.php" />
199199
</errorLevel>
200200
</RedundantConditionGivenDocblockType>
201-
201+
202202
<RiskyTruthyFalsyComparison>
203203
<errorLevel type="suppress">
204204
<directory name="src" />
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
5+
This file is part of the Sylius package.
6+
7+
(c) Sylius Sp. z o.o.
8+
9+
For the full copyright and license information, please view the LICENSE
10+
file that was distributed with this source code.
11+
12+
-->
13+
14+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
15+
<class name="Sylius\Resource\Model\AbstractTranslation">
16+
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity">
17+
<option name="fields">
18+
<value>locale</value>
19+
<value>translatable</value>
20+
</option>
21+
<option name="groups">sylius</option>
22+
<option name="message">sylius.resource.translation.locale.unique</option>
23+
</constraint>
24+
<property name="locale">
25+
<constraint name="NotBlank">
26+
<option name="groups">sylius</option>
27+
<option name="message">sylius.resource.translation.locale.not_blank</option>
28+
</constraint>
29+
<constraint name="Locale">
30+
<option name="groups">sylius</option>
31+
<option name="message">sylius.resource.translation.locale.invalid</option>
32+
</constraint>
33+
</property>
34+
</class>
35+
</constraint-mapping>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
5+
This file is part of the Sylius package.
6+
7+
(c) Sylius Sp. z o.o.
8+
9+
For the full copyright and license information, please view the LICENSE
10+
file that was distributed with this source code.
11+
12+
-->
13+
14+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">
15+
<class name="Sylius\Resource\Model\TranslatableInterface">
16+
<getter property="translations">
17+
<constraint name="Valid" />
18+
</getter>
19+
</class>
20+
</constraint-mapping>
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
sylius:
22
resource:
33
not_enabled: "Given resource is disabled"
4-
not_disabled: "Given resource is enabled"
4+
not_disabled: "Given resource is enabled"
5+
translation:
6+
locale:
7+
not_blank: Please enter the locale.
8+
invalid: This value is not a valid locale.
9+
unique: A translation for the {{ value }} locale code already exists.

tests/Application/src/Tests/DataFixtures/ORM/books.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,9 @@ App\Entity\Book:
99
currentLocale: en_US
1010
title: "Game of Thrones"
1111
author: "George R. R. Martin"
12+
13+
App\Entity\BookTranslation:
14+
book1_pl_PL:
15+
title: "Władca Pierścieni"
16+
locale: "pl_PL"
17+
translatable: "@book1"
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Sylius Sp. z o.o.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace App\Tests\Validator;
15+
16+
use ApiTestCase\JsonApiTestCase;
17+
use App\Entity\Book;
18+
use Symfony\Component\Validator\Validator\ValidatorInterface;
19+
20+
final class TranslatableValidatorTest extends JsonApiTestCase
21+
{
22+
/** @test */
23+
public function it_fails_validation_with_empty_locale(): void
24+
{
25+
$this->loadFixturesFromFile('books.yml');
26+
$book = $this->getBookBy();
27+
$book->getTranslation('pl_PL')->setLocale('');
28+
29+
$errors = $this->getValidator()->validate($book, null, ['sylius']);
30+
$this->assertCount(1, $errors);
31+
$this->assertSame('sylius.resource.translation.locale.not_blank', $errors->get(0)->getMessageTemplate());
32+
}
33+
34+
/** @test */
35+
public function it_fails_validation_with_invalid_locale(): void
36+
{
37+
$this->loadFixturesFromFile('books.yml');
38+
$book = $this->getBookBy();
39+
$book->getTranslation('pl_PL')->setLocale('invalid');
40+
41+
$errors = $this->getValidator()->validate($book, null, ['sylius']);
42+
$this->assertCount(1, $errors);
43+
$this->assertSame('sylius.resource.translation.locale.invalid', $errors->get(0)->getMessageTemplate());
44+
}
45+
46+
/** @test */
47+
public function it_fails_validation_with_not_unique_locale(): void
48+
{
49+
$this->loadFixturesFromFile('books.yml');
50+
$book = $this->getBookBy();
51+
$book->getTranslation('pl_PL')->setLocale('en_US');
52+
53+
$errors = $this->getValidator()->validate($book, null, ['sylius']);
54+
$this->assertCount(1, $errors);
55+
$this->assertSame('sylius.resource.translation.locale.unique', $errors->get(0)->getMessageTemplate());
56+
}
57+
58+
private function getValidator(): ValidatorInterface
59+
{
60+
return self::getContainer()->get('validator');
61+
}
62+
63+
private function getBookBy(array $criteria = ['author' => 'J.R.R. Tolkien']): ?Book
64+
{
65+
return self::getContainer()->get('app.repository.book')->findOneBy($criteria);
66+
}
67+
}

0 commit comments

Comments
 (0)