Skip to content

Commit 79f6c05

Browse files
[VarDumper] Add doc for assertDump* assertions
1 parent d6a838a commit 79f6c05

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

components/var_dumper/introduction.rst

+55
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,61 @@ original value. You can configure the limits in terms of:
122122
<config max-items="250" max-string-length="-1" />
123123
</container>
124124
125+
Using the VarDumper component in your PHPUnit test suite
126+
--------------------------------------------------------
127+
128+
.. versionadded:: 2.7
129+
The :class:`Symfony\\Component\\VarDumper\\Test\\VarDumperTestTrait` was
130+
introduced in Symfony 2.7.
131+
132+
The VarDumper component provides
133+
:class:`a trait <Symfony\\Component\\VarDumper\\Test\\VarDumperTestTrait>`
134+
that can help writing some of your tests for PHPUnit.
135+
You can use it by importing it into your test classes::
136+
137+
class YourTest extends \PHPUnit_Framework_TestCase
138+
{
139+
use \Symfony\Component\VarDumper\Test\VarDumperTestTrait;
140+
141+
// ...
142+
}
143+
144+
This will provide you with two new assertions:
145+
146+
:method:`Symfony\\Component\\VarDumper\\Test\\VarDumperTestTrait::assertDumpEquals`
147+
verifies that the dump of the variable given as the second argument matches
148+
the expected dump provided as a string in the first argument.
149+
150+
:method:`Symfony\\Component\\VarDumper\\Test\\VarDumperTestTrait::assertDumpMatchesFormat`
151+
is like the previous method but accepts placeholders in the expected dump,
152+
based on the ``assertStringMatchesFormat`` method provided by PHPUnit.
153+
154+
Example::
155+
156+
class ExampleTest extends \PHPUnit_Framework_TestCase
157+
{
158+
use \Symfony\Component\VarDumper\Test\VarDumperTestTrait;
159+
160+
public function testWithDumpEquals()
161+
{
162+
$testedVar = array(123, 'foo');
163+
164+
$expectedDump = <<<EOTXT
165+
array:2 [
166+
0 => 123
167+
1 => "foo"
168+
]
169+
EOTXT;
170+
171+
$this->assertDumpEquals($expectedDump, $testedVar);
172+
}
173+
}
174+
175+
.. tip::
176+
177+
If you still use PHP 5.3, you can extend the
178+
:class:`Symfony\\Component\\VarDumper\\Test\\VarDumperTestClass` instead.
179+
125180
Dump Examples and Output
126181
------------------------
127182

0 commit comments

Comments
 (0)