8000 Merge branch '5.4' into 6.2 · symfony/symfony-docs@db31ccf · GitHub
[go: up one dir, main page]

Skip to content

Commit db31ccf

Browse files
committed
Merge branch '5.4' into 6.2
* 5.4: [HttpClient] Mention list of callbacks for `MockHttpClient` [Validator] Add `Cascade` constraint documentation
2 parents c690164 + c01f434 commit db31ccf

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

http_client.rst

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,6 +1761,33 @@ responses dynamically when it's called::
17611761
$client = new MockHttpClient($callback);
17621762
$response = $client->request('...'); // calls $callback to get the response
17631763

1764+
You can also pass a list of callbacks if you need to perform specific
1765+
assertions on the request before returning the mocked response::
1766+
1767+
$expectedRequests = [
1768+
function ($method, $url, $options) {
1769+
$this->assertSame('GET', $method);
1770+
$this->assertSame('https://example.com/api/v1/customer', $url);
1771+
1772+
return new MockResponse('...');
1773+
},
1774+
function ($method, $url, $options) {
1775+
$this->assertSame('POST', $method);
1776+
$this->assertSame('https://example.com/api/v1/customer/1/products', $url);
1777+
1778+
return new MockResponse('...');
1779+
},
1780+
];
1781+
1782+
$client = new MockHttpClient($expectedRequest);
1783+
1784+
// ...
1785+
1786+
.. versionadded:: 5.1
1787+
1788+
Passing a list of callbacks to the ``MockHttpClient`` was introduced
1789+
in Symfony 5.1.
1790+
17641791
.. tip::
17651792

17661793
Instead of using the first argument, you can also set the (list of)

reference/constraints/Cascade.rst

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
Cascade
2+
=======
3+
4+
.. versionadded:: 5.2
5+
6+
The :class:`Symfony\\Component\\Validator\\Constraints\\Cascade` was
7+
introduced in Symfony 5.2 and requires PHP 7.4.
8+
9+
The Cascade constraint is used to validate a whole class, including all the
10+
objects that might be stored in its properties. Thanks to this constraint,
11+
you don't need to add the :doc:`/reference/constraints/Valid` constraint on
12+
every child object that you want to validate in your class.
13+
14+
========== ===================================================================
15+
Applies to :ref:`class <validation-class-target>`
16+
Class :class:`Symfony\\Component\\Validator\\Constraints\\Cascade`
17+
========== ===================================================================
18+
19+
Basic Usage
20+
-----------
21+
22+
In the following example, the
23+
:class:`Symfony\\Component\\Validator\\Constraints\\Cascade` constraint
24+
will tell the validator to validate all properties of the class, including
25+
constraints that are set in the child classes ``BookMetadata`` and
26+
``Author``:
27+
28+
.. configuration-block::
29+
30+
.. code-block:: php-annotations
31+
32+
// src/Model/BookCollection.php
33+
namespace App\Model;
34+
35+
use App\Model\Author;
36+
use App\Model\BookMetadata;
37+
use Symfony\Component\Validator\Constraints as Assert;
38+
39+
/**
40+
* @Assert\Cascade
41+
*/
42+
class BookCollection
43+
{
44+
/**
45+
* @Assert\NotBlank
46+
*/
47+
protected $name = '';
48+
49+
public BookMetadata $metadata;
50+
51+
public Author $author;
52+
53+
// ...
54+
}
55+
56+
.. code-block:: php-attributes
57+
58+
// src/Model/BookCollection.php
59+
namespace App\Model;
60+
61+
use App\Model\Author;
62+
use App\Model\BookMetadata;
63+
use Symfony\Component\Validator\Constraints as Assert;
64+
65+
#[Assert\Cascade]
66+
class BookCollection
67+
{
68+
#[Assert\NotBlank]
69+
protected $name = '';
70+
71+
public BookMetadata $metadata;
72+
73+
public Author $author;
74+
75+
// ...
76+
}
77+
78+
.. code-block:: yaml
79+
80+
# config/validator/validation.yaml
81+
App\Entity\BookCollection:
82+
constraints:
83+
- Cascade: ~
84+
85+
.. code-block:: xml
86+
87+
<!-- config/validator/validation.xml -->
88+
<?xml version="1.0" encoding="UTF-8" ?>
89+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
90+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
91+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
92+
93+
<class name="App\Entity\BookCollection">
94+
<constraint name="Cascade"/>
95+
</class>
96+
</constraint-mapping>
97+
98+
.. code-block:: php
99+
100+
// src/Entity/BookCollection.php
101+
namespace App\Entity;
102+
103+
use Symfony\Component\Validator\Constraints as Assert;
104+
use Symfony\Component\Validator\Mapping\ClassMetadata;
105+
106+
class BookCollection
107+
{
108+
// ...
109+
110+
public static function loadValidatorMetadata(ClassMetadata $metadata)
111+
{
112+
$metadata->addConstraint(new Assert\Cascade());
113+
}
114+
}
115+
116+
Options
117+
-------
118+
119+
The ``groups`` option is not available for this constraint.
120+
121+
.. include:: /reference/constraints/_payload-option.rst.inc

reference/constraints/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ Other Constraints
9797
* :doc:`When </reference/constraints/When>`
9898
* :doc:`All </reference/constraints/All>`
9999
* :doc:`Valid </reference/constraints/Valid>`
100+
* :doc:`Cascade </reference/constraints/Cascade>`
100101
* :doc:`Traverse </reference/constraints/Traverse>`
101102
* :doc:`Collection </reference/constraints/Collection>`
102103
* :doc:`Count </reference/constraints/Count>`

0 commit comments

Comments
 (0)
0