Skip to content

Commit 346d1df

Browse files
committed
Merge branch '6.2' into 6.3
* 6.2: Remove unneeded versionadded directives [HttpClient] Mention list of callbacks for `MockHttpClient` [Validator] Add `Cascade` constraint documentation
2 parents 485969b + 7c50428 commit 346d1df

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

http_client.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1839,6 +1839,28 @@ responses dynamically when it's called::
18391839
$client = new MockHttpClient($callback);
18401840
$response = $client->request('...'); // calls $callback to get the response
18411841

1842+
You can also pass a list of callbacks if you need to perform specific
1843+
assertions on the request before returning the mocked response::
1844+
1845+
$expectedRequests = [
1846+
function ($method, $url, $options) {
1847+
$this->assertSame('GET', $method);
1848+
$this->assertSame('https://example.com/api/v1/customer', $url);
1849+
1850+
return new MockResponse('...');
1851+
},
1852+
function ($method, $url, $options) {
1853+
$this->assertSame('POST', $method);
1854+
$this->assertSame('https://example.com/api/v1/customer/1/products', $url);
1855+
1856+
return new MockResponse('...');
1857+
},
1858+
];
1859+
1860+
$client = new MockHttpClient($expectedRequest);
1861+
1862+
// ...
1863+
18421864
.. tip::
18431865

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

reference/constraints/Cascade.rst

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