Passed
Push — develop ( 8f9d97...72aae6 )
by Freddie
02:16
created

AbstractBuilderTest::testItSetRequiredConstraint()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
namespace FlexPHP\Inputs\Tests\Unit\Builder;
4
5
use FlexPHP\Inputs\Builder\AbstractBuilder;
6
use FlexPHP\Inputs\Tests\TestCase;
7
use PHPUnit\Framework\MockObject\MockObject;
8
use Symfony\Component\Form\Extension\Core\Type\TextType;
9
10
class AbstractBuilderTest extends TestCase
11
{
12
    private function getMock(string $name, string $type, array $properties): MockObject
13
    {
14
        $mock = $this->getMockForAbstractClass(
15
            AbstractBuilder::class, [$name, $properties, []], '', true, true, true, ['getType']
16
        );
17
18
        $mock->method('getType')->will($this->returnValue($type));
19
20
        return $mock;
21
    }
22
23
    public function testItDefault(): void
24
    {
25
        $render = $this->getMock('foo', TextType::class, [])->render();
26
27
        $this->assertEquals(<<<'T'
28
<div class="form-group"><label for="form_foo">Foo</label><input type="text" id="form_foo" name="form[foo]" class="form-control" /></div>
29
T, $render);
30
    }
31
32
    public function testItDefaultSlug(): void
33
    {
34
        $render = $this->getMock('foo_bar', TextType::class, [])->render();
35
36
        $this->assertEquals(<<<'T'
37
<div class="form-group"><label for="form_foo_bar">Foo bar</label><input type="text" id="form_foo_bar" name="form[foo_bar]" class="form-control" /></div>
38
T, $render);
39
    }
40
41
    public function testItDefaultSpace(): void
42
    {
43
        $render = $this->getMock('foo bar', TextType::class, [])->render();
44
45
        $this->assertEquals(<<<'T'
46
<div class="form-group"><label for="form_foo_bar">Foo bar</label><input type="text" id="form_foo_bar" name="form[foo_bar]" class="form-control" /></div>
47
T, $render);
48
    }
49
50
    public function testItSetLabel(): void
51
    {
52
        $render = $this->getMock('foo', TextType::class, [
53
            'Label' => 'My Label',
54
        ])->render();
55
56
        $this->assertEquals(<<<'T'
57
<div class="form-group"><label for="form_foo">My Label</label><input type="text" id="form_foo" name="form[foo]" class="form-control" /></div>
58
T, $render);
59
    }
60
61
    public function testItSetDefault(): void
62
    {
63
        $render = $this->getMock('foo', TextType::class, [
64
            'Default' => 'fuz',
65
        ])->render();
66
67
        $this->assertEquals(<<<'T'
68
<div class="form-group"><label for="form_foo">Foo</label><input type="text" id="form_foo" name="form[foo]" class="form-control" value="fuz" /></div>
69
T, $render);
70
    }
71
72
    /**
73
     * @dataProvider getRequiredOptions
74
     *
75
     * @param string|array $required
76
     * @return void
77
     */
78
    public function testItSetRequiredConstraint($required): void
79
    {
80
        $render = $this->getMock('foo', TextType::class, [
81
            'Constraints' => $required,
82
        ])->render();
83
84
        $this->assertEquals(<<<'T'
85
<div class="form-group"><label for="form_foo" class="required">Foo</label><input type="text" id="form_foo" name="form[foo]" required="required" class="form-control" /></div>
86
T, $render);
87
    }
88
89
    public function testItSetInputHelp(): void
90
    {
91
        $render = $this->getMock('foo', TextType::class, [
92
            'InputHelp' => 'A help block',
93
        ])->render();
94
95
        $this->assertEquals(<<<'T'
96
<div class="form-group"><label for="form_foo">Foo</label><input type="text" id="form_foo" name="form[foo]" aria-describedby="form_foo_help" class="form-control" /><small id="form_foo_help" class="form-text text-muted">A help block</small></div>
97
T, $render);
98
    }
99
100
    public function getRequiredOptions(): array
101
    {
102
        return [
103
            [\json_encode(['required'])],
104
            [\json_encode(['required' => true])],
105
            [\json_encode(['required' => 'true'])],
106
            [\json_encode(['required' => 'required'])],
107
            ['required'],
108
        ];
109
    }
110
}
111