KnockoutFormTest::testKnockoutForm()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 105
Code Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 82
nc 1
nop 0
dl 0
loc 105
rs 8.3927
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace AntonyThorpe\Knockout\Tests;
4
5
use SilverStripe\Dev\FunctionalTest;
6
use SilverStripe\Core\Config\Config;
7
use AntonyThorpe\Knockout\Tests\KnockoutFormTestController;
8
9
/**
10
 * KnockoutFormTest
11
 *
12
 * Controller tests
13
 */
14
class KnockoutFormTest extends FunctionalTest
15
{
16
    protected static $disable_theme = true;
17
18
    protected static $extra_controllers = [
19
        KnockoutFormTestController::class
20
    ];
21
22
    public function setUp(): void
23
    {
24
        parent::setUp();
25
        Config::modify()->set('SSViewer', 'source_file_comments', true);
26
    }
27
28
    public function testKnockoutForm()
29
    {
30
        $page = $this->get('KnockoutFormTestController');
31
        $this->assertEquals(200, $page->getStatusCode(), "a page should load");
32
        $body = $page->getBody();
33
34
        $this->assertStringContainsString(
35
            'data-bind="submit: addToCart2"',
36
            $body,
37
            'form element has submit binding to javascript function'
38
        );
39
        $this->assertStringContainsString(
40
            '<input data-bind="textInput: spaceship2, hasFocus: true"',
41
            $body,
42
            'Databind attribute in input element'
43
        );
44
        $this->assertStringContainsString(
45
            "setKnockout:{value:'Enterprise\'s Voyage'}",
46
            $body,
47
            'Comma escaped in HTML for javascript'
48
        );
49
        $this->assertStringContainsString(
50
            '<select data-bind="value: menu"',
51
            $body,
52
            'Databind attribute applied to select element'
53
        );
54
        $this->assertStringContainsString(
55
            '<input data-bind="textInput: seatNumber, setKnockout:{value:4}"',
56
            $body,
57
            'KnockoutNumericField works'
58
        );
59
        $this->assertStringContainsString(
60
            '<input data-bind="enable: canSaveInterGalacticAction, css:{ \'FormAction_Disabled\': !canSaveInterGalacticAction() }" type="submit"',
61
            $body,
62
            'Databind attribute in submit button'
63
        );
64
        $this->assertStringContainsString(
65
            '<input data-bind="textInput: email, setKnockout:{value:\'[email protected]\'}"',
66
            $body,
67
            'Databind attribute applied to input element for email field'
68
        );
69
        $this->assertStringContainsString(
70
            'class="email text"',
71
            $body,
72
            'KnockoutEmailField has a class of "email text"'
73
        );
74
        $this->assertStringContainsString(
75
            '<textarea data-bind="textInput: comments"',
76
            $body,
77
            'Databind attribute applied to the textareafield'
78
        );
79
        $this->assertStringContainsString(
80
            'class="knockouttextarea textarea"',
81
            $body,
82
            'KnockoutTextareaField has a class of "textarea text"'
83
        );
84
        $this->assertStringContainsString(
85
            'data-bind="checked: accessories, setKnockout:{value:\'Zero Gravity Pillow\'}, blah: someOtherFunction"',
86
            $body,
87
            'Databind attribute applied to the radio buttons'
88
        );
89
        $this->assertStringContainsString(
90
            'class="radio"',
91
            $body,
92
            'KnockoutOptionsetField has a class of "radio"'
93
        );
94
        $this->assertStringContainsString(
95
            'data-bind="enable: canSaveInterGalacticAction',
96
            $body,
97
            'KnockoutFormAction has an observable of "canSaveInterGalacticAction"'
98
        );
99
        $this->assertStringContainsString(
100
            'data-bind="textInput: password',
101
            $body,
102
            'KnockoutConfirmedPasswordField has a child with an observable of "password"'
103
        );
104
        $this->assertStringContainsString(
105
            'data-bind="textInput: confirmedPassword',
106
            $body,
107
            'KnockoutConfirmedPasswordField has a child with an observable of "confirmedPassword"'
108
        );
109
        $this->assertStringContainsString(
110
            'data-bind="checked: checkboxField"',
111
            $body,
112
            'KnockoutCheckboxField has an observable of "checkboxField"'
113
        );
114
        $this->assertStringContainsString(
115
            'data-bind="checked: switchField"',
116
            $body,
117
            'KnockoutSwitchField has an observable of "switchField"'
118
        );
119
        $this->assertStringContainsString(
120
            ' <button data-bind="click: function(){ compositeButtonField(!compositeButtonField());',
121
            $body,
122
            'KnockoutToggleCompositeButtonField has a button that shows/hides based upon the observable of "compositeButtonField"'
123
        );
124
        $this->assertStringContainsString(
125
            '<div data-bind="visible: compositeButtonField"',
126
            $body,
127
            'KnockoutToggleCompositeButtonField has an observable of "compositeButtonField"'
128
        );
129
        $this->assertSame(
130
            substr_count($body, 'label class="form-check-label left"'),
131
            1,
132
            'the label element with class "left" appears once'
133
        );
134
135
        // add additional tests here after adding to the form in KnockoutFormTestController.php
136
    }
137
}
138