Completed
Push — master ( 8d1cc4...078b0f )
by Damian
12:41 queued 06:19
created

testValidateFieldDoesntAddErrorOnSuccess()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 2
eloc 14
nc 2
nop 0
dl 0
loc 21
rs 9.3142
1
<?php
2
3
class EditableSpamProtectionFieldTest extends SapphireTest 
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    
6
    protected $usesDatabase = true;
7
8
    public function setUp()
9
    {
10
        parent::setUp();
11
12
        Config::inst()->update(
13
            'FormSpamProtectionExtension', 'default_spam_protector',
14
            'EditableSpamProtectionFieldTest_Protector'
15
        );
16
    }
17
18
    public function testValidateFieldDoesntAddErrorOnSuccess()
19
    {
20
        if (!class_exists('EditableSpamProtectionField')) {
21
            $this->markTestSkipped('"userforms" module not installed');
0 ignored issues
show
Bug introduced by
The method markTestSkipped() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
22
        }
23
24
        $formMock = $this->getFormMock();
25
        $formFieldMock = $this->getEditableFormFieldMock();
26
27
        $formFieldMock
28
            ->getFormField() // mock
29
            ->expects($this->once())
0 ignored issues
show
Bug introduced by
The method once() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
30
            ->method('validate')
31
            ->will($this->returnValue(true));
0 ignored issues
show
Bug introduced by
The method returnValue() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
32
33
        $formMock
34
            ->expects($this->never())
0 ignored issues
show
Bug introduced by
The method never() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
35
            ->method('addErrorMessage');
36
37
        $formFieldMock->validateField(array('MyField' => null), $formMock);
38
    }
39
40
    public function testValidateFieldAddsErrorFromField()
41
    {
42
        if (!class_exists('EditableSpamProtectionField')) {
43
            $this->markTestSkipped('"userforms" module not installed');
0 ignored issues
show
Bug introduced by
The method markTestSkipped() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
44
        }
45
46
        $formMock = $this->getFormMock();
47
        $formFieldMock = $this->getEditableFormFieldMock();
48
49
        $formFieldMock
50
            ->getFormField() // mock
51
            ->expects($this->once())
0 ignored issues
show
Bug introduced by
The method once() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
52
            ->method('validate')
53
            ->will($this->returnValue(false));
0 ignored issues
show
Bug introduced by
The method returnValue() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
54
55
        $formMock->getValidator()->validationError('MyField', 'some field message', 'required');
56
57
        $formMock
58
            ->expects($this->once())
0 ignored issues
show
Bug introduced by
The method once() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59
            ->method('addErrorMessage')
60
            ->with($this->anything(), $this->stringContains('some field message'), $this->anything(), $this->anything());;
0 ignored issues
show
Bug introduced by
The method anything() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method stringContains() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
61
62
        $formFieldMock->validateField(array('MyField' => null), $formMock);
63
    }
64
65
    public function testValidateFieldAddsDefaultError()
66
    {
67
        if (!class_exists('EditableSpamProtectionField')) {
68
            $this->markTestSkipped('"userforms" module not installed');
0 ignored issues
show
Bug introduced by
The method markTestSkipped() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
69
        }
70
71
        $formMock = $this->getFormMock();
72
        $formFieldMock = $this->getEditableFormFieldMock();
73
74
        $formFieldMock
75
            ->getFormField() // mock
76
            ->expects($this->once())
0 ignored issues
show
Bug introduced by
The method once() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
77
            ->method('validate')
78
            ->will($this->returnValue(false));
0 ignored issues
show
Bug introduced by
The method returnValue() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
79
80
        // field doesn't set any validation errors here
81
82
        $formMock
83
            ->expects($this->once())
0 ignored issues
show
Bug introduced by
The method once() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
84
            ->method('addErrorMessage')
85
            ->with($this->anything(), $this->stringContains('default error message'), $this->anything(), $this->anything());
0 ignored issues
show
Bug introduced by
The method anything() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method stringContains() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
86
87
        $formFieldMock->validateField(array('MyField' => null), $formMock);
88
    }
89
90
    protected function getFormMock()
91
    {
92
        $formMock = $this->getMockBuilder('Form', array('addErrorMessage'))
0 ignored issues
show
Bug introduced by
The method getMockBuilder() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
93
            ->disableOriginalConstructor()
94
            ->getMock();
95
        $formMock
96
            ->expects($this->any())
0 ignored issues
show
Bug introduced by
The method any() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
97
            ->method('getValidator')
98
            ->will($this->returnValue(new RequiredFields()));
0 ignored issues
show
Bug introduced by
The method returnValue() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
99
100
        return $formMock;
101
    }
102
103
    protected function getEditableFormFieldMock()
104
    {
105
        $page = new UserDefinedForm();
106
        $page->write();
107
108
        $formFieldMock = $this->getMockBuilder('TextField')
0 ignored issues
show
Bug introduced by
The method getMockBuilder() does not seem to exist on object<EditableSpamProtectionFieldTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
109
            ->disableOriginalConstructor()
110
            ->getMock();
111
112
        $editableFormFieldMock = new EditableSpamProtectionField(array(
113
            'ParentID' => $page->ID,
114
            'Name' => 'MyField',
115
            'CustomErrorMessage' => 'default error message'
116
        ));
117
        $editableFormFieldMock->write();
118
        $editableFormFieldMock->setFormField($formFieldMock);
119
120
        return $editableFormFieldMock;
121
    }
122
123
}
124
125 View Code Duplication
class EditableSpamProtectionFieldTest_Protector implements SpamProtector, TestOnly
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
126
{
127
    public function getFormField($name = null, $title = null, $value = null)
128
    {
129
        return new TextField($name, 'Foo', $value);
130
    }
131
132
    public function setFieldMapping($fieldMapping)
133
    {
134
    }
135
}
136