Skip to content

Commit f5ae627

Browse files
committed
Update tests
1 parent 8a5fa25 commit f5ae627

File tree

6 files changed

+91
-12
lines changed

6 files changed

+91
-12
lines changed

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
}
2424
},
2525
"autoload-dev": {
26-
"classmap": [
27-
"tests"
28-
]
26+
"psr-4": {
27+
"Barryvdh\\Form\\Tests\\": "tests/"
28+
}
2929
},
3030
"extra": {
3131
"branch-alias": {

phpunit.xml renamed to phpunit.xml.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
processIsolation="false"
1010
stopOnError="false"
1111
stopOnFailure="false"
12-
syntaxCheck="true"
1312
verbose="true"
1413
>
1514
<testsuites>

tests/BladeTest.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
2+
namespace Barryvdh\Form\Tests;
23

34
use Barryvdh\Form\Facade\FormFactory;
5+
use Barryvdh\Form\Tests\Types\UserFormType;
46
use Symfony\Component\Form\Extension\Core\Type\FormType;
57
use Symfony\Component\Form\Extension\Core\Type\TextType;
68
use Symfony\Component\Form\Extension\Core\Type\EmailType;
@@ -22,11 +24,7 @@ protected function getEnvironmentSetUp($app)
2224
$app['router']->any('create', function () {
2325
$user = [];
2426

25-
$form = FormFactory::create(FormType::class, $user)
26-
->add('name', TextType::class)
27-
->add('email', EmailType::class, [
28-
'rules' => 'email',
29-
])
27+
$form = FormFactory::create(UserFormType::class, $user)
3028
->add('save', SubmitType::class, ['label' => 'Save user']);
3129

3230
$form->handleRequest();
@@ -52,7 +50,7 @@ public function testInlineForm()
5250
{
5351
$crawler = $this->call('GET', 'create');
5452

55-
$this->assertContains('<form name="form" method="post">', $crawler->getContent());
53+
$this->assertContains('<form name="user_form" method="post">', $crawler->getContent());
5654
}
5755

5856
/**
@@ -63,7 +61,7 @@ public function testInlineForm()
6361
public function testPostFormInvalid()
6462
{
6563
$crawler = $this->call('POST', 'create', [
66-
'form' => ['save' => true]
64+
'user_form' => ['save' => true]
6765
]);
6866

6967
$this->assertEquals('invalid', $crawler->getContent());
@@ -77,7 +75,7 @@ public function testPostFormInvalid()
7775
public function testPostForm()
7876
{
7977
$crawler = $this->call('POST', 'create', [
80-
'form' => [
78+
'user_form' => [
8179
'name' => 'Barry',
8280
'email' => 'barryvdh@gmail.com',
8381
'save' => true

tests/TestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
namespace Barryvdh\Form\Tests;
23

34
use Barryvdh\Form\ServiceProvider;
45
use Orchestra\Testbench\TestCase as TestbenchTestCase;

tests/Types/UserFormType.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
namespace Barryvdh\Form\Tests\Types;
3+
4+
use Symfony\Component\Form\AbstractType;
5+
use Symfony\Component\Form\Extension\Core\Type\EmailType;
6+
use Symfony\Component\Form\Extension\Core\Type\TextType;
7+
use Symfony\Component\Form\FormBuilderInterface;
8+
9+
class UserFormType extends AbstractType
10+
{
11+
public function buildForm(FormBuilderInterface $builder, array $options): void
12+
{
13+
$builder
14+
->add('name', TextType::class)
15+
->add('email', EmailType::class, [
16+
'rules' => 'email',
17+
])
18+
;
19+
}
20+
}

tests/ValidationTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
namespace Barryvdh\Form\Tests;
3+
4+
use Barryvdh\Form\Facade\FormFactory;
5+
use Barryvdh\Form\Tests\Types\UserFormType;
6+
use Symfony\Component\Form\Extension\Core\Type\FormType;
7+
use Symfony\Component\Form\Extension\Core\Type\TextType;
8+
use Symfony\Component\Form\Extension\Core\Type\EmailType;
9+
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
10+
use Symfony\Component\HttpFoundation\Request;
11+
12+
class ValidationTest extends TestCase
13+
{
14+
15+
public function testValidForm()
16+
{
17+
/** @var UserFormType $form */
18+
$form = FormFactory::create(UserFormType::class, [])
19+
->add('save', SubmitType::class, ['label' => 'Save user']);
20+
21+
$request = $this->createPostRequest([
22+
'user_form' => [
23+
'name' => 'Barry',
24+
'email' => 'barry@example.com',
25+
'save' => true,
26+
]
27+
]);
28+
29+
$form->handleRequest($request);
30+
31+
$this->assertTrue($form->isSubmitted());
32+
$this->assertTrue($form->isValid());
33+
}
34+
35+
public function testInvalidForm()
36+
{
37+
/** @var UserFormType $form */
38+
$form = FormFactory::create(UserFormType::class, [])
39+
->add('save', SubmitType::class, ['label' => 'Save user']);
40+
41+
$request = $this->createPostRequest([
42+
'user_form' => [
43+
'name' => 'Barry',
44+
'email' => 'foo',
45+
'save' => true,
46+
]
47+
]);
48+
49+
$form->handleRequest($request);
50+
51+
$this->assertTrue($form->isSubmitted());
52+
$this->assertFalse($form->isValid());
53+
}
54+
55+
private function createPostRequest($data)
56+
{
57+
return new Request([], $data, [], [], [], [
58+
'REQUEST_METHOD' => 'POST'
59+
]);
60+
}
61+
}

0 commit comments

Comments
 (0)