Passed
Push — fix-9097 ( 5c86f1...9d3fd1 )
by Sam
12:15
created

GroupedDropdownFieldTest::testEmptyString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 64
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 39
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 64
rs 9.296

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 SilverStripe\Forms\Tests;
4
5
use SilverStripe\Dev\SapphireTest;
6
use SilverStripe\Forms\GroupedDropdownField;
7
use SilverStripe\Forms\RequiredFields;
8
9
class GroupedDropdownFieldTest extends SapphireTest
10
{
11
12
    public function testValidation()
13
    {
14
        $field = GroupedDropdownField::create(
15
            'Test',
16
            'Testing',
17
            array(
18
            "1" => "One",
19
            "Group One" => array(
20
                "2" => "Two",
21
                "3" => "Three"
22
            ),
23
            "Group Two" => array(
24
                "4" => "Four"
25
            )
26
            )
27
        );
28
29
        $this->assertEquals(array("1", "2", "3", "4"), $field->getValidValues());
30
31
        $validator = new RequiredFields();
32
33
        $field->setValue("1");
34
        $this->assertTrue($field->validate($validator));
35
36
        //test grouped values
37
        $field->setValue("3");
38
        $this->assertTrue($field->validate($validator));
39
40
        //non-existent value should make the field invalid
41
        $field->setValue("Over 9000");
42
        $this->assertFalse($field->validate($validator));
43
44
        //empty string shouldn't validate
45
        $field->setValue('');
46
        $this->assertFalse($field->validate($validator));
47
48
        //empty field should validate after being set
49
        $field->setEmptyString('Empty String');
50
        $field->setValue('');
51
        $this->assertTrue($field->validate($validator));
52
53
        //disabled items shouldn't validate
54
        $field->setDisabledItems(array('1'));
55
        $field->setValue('1');
56
57
        $this->assertEquals(array("2", "3", "4"), $field->getValidValues());
58
        $this->assertEquals(array("1"), $field->getDisabledItems());
59
60
        $this->assertFalse($field->validate($validator));
61
    }
62
}
63