Skip to content

Commit 1645b06

Browse files
author
Henry Snoek
committed
simplify code example as suggested by @webmozart and @stof
1 parent 523d08c commit 1645b06

File tree

1 file changed

+11
-24
lines changed

1 file changed

+11
-24
lines changed

cookbook/form/unit_testing.rst

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,37 +39,24 @@ The simplest ``TypeTestCase`` implementation looks like the following::
3939
// src/AppBundle/Tests/Form/Type/TestedTypeTest.php
4040
namespace AppBundle\Tests\Form\Type;
4141

42-
use AppBundle\Form\Type\TestedType;
43-
use AppBundle\Model\TestObject;
42+
use Symfony\Component\Form\Extension\Validator\ValidatorExtension;
4443
use Symfony\Component\Form\Test\TypeTestCase;
44+
use Symfony\Component\Validator\ConstraintViolationList;
45+
use Symfony\Component\Validator\Validator\ValidatorInterface;
4546

4647
class TestedTypeTest extends TypeTestCase
4748
{
48-
public function testSubmitValidData()
49+
protected function getExtensions()
4950
{
50-
$formData = array(
51-
'test' => 'test',
52-
'test2' => 'test2',
53-
);
54-
55-
$type = new TestedType();
56-
$form = $this->factory->create($type);
57-
58-
$object = TestObject::fromArray($formData);
59-
60-
// submit the data to the form directly
61-
$form->submit($formData);
62-
63-
$this->assertTrue($form->isSynchronized());
64-
$this->assertEquals($object, $form->getData());
65-
66-
$view = $form->createView();
67-
$children = $view->children;
51+
$validator = $this->getMock('ValidatorInterface::class');
52+
$validator->method('validate')->will($this->returnValue(new ConstraintViolationList()));
6853

69-
foreach (array_keys($formData) as $key) {
70-
$this->assertArrayHasKey($key, $children);
71-
}
54+
return array(
55+
new ValidatorExtension($validator),
56+
);
7257
}
58+
59+
// ... your tests
7360
}
7461

7562
So, what does it test? Here comes a detailed explanation.

0 commit comments

Comments
 (0)