Completed
Pull Request — master (#51)
by Robbie
02:02 queued 51s
created

FormSpamProtectionExtensionTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 118
Duplicated Lines 38.98 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 7
dl 46
loc 118
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 16 1
A testEnableSpamProtection() 0 12 1
A testEnableSpamProtectionCustomProtector() 0 8 1
A testEnableSpamProtectionCustomTitle() 9 9 1
A testCustomOptions() 10 10 1
A testConfigurableName() 0 19 1
A testInsertBefore() 13 13 1
A testInsertBeforeMissing() 14 14 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace SilverStripe\SpamProtection\Tests;
4
5
use SilverStripe\Control\Controller;
6
use SilverStripe\Core\Config\Config;
7
use SilverStripe\Dev\SapphireTest;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\Form;
10
use SilverStripe\Forms\TextField;
11
use SilverStripe\SpamProtection\Extension\FormSpamProtectionExtension;
12
use SilverStripe\SpamProtection\Tests\FormSpamProtectionExtensionTest\FooProtector;
13
use SilverStripe\SpamProtection\Tests\FormSpamProtectionExtensionTest\BarProtector;
14
use SilverStripe\SpamProtection\Tests\FormSpamProtectionExtensionTest\BazProtector;
15
16
/**
17
 * @package spamprotection
18
 */
19
class FormSpamProtectionExtensionTest extends SapphireTest
20
{
21
    protected $usesDatabase = false;
22
23
    /**
24
     * @var Form
25
     */
26
    protected $form = null;
27
28
    protected function setUp()
29
    {
30
        parent::setUp();
31
32
        $this->form = new Form(
33
            new Controller,
34
            'Form',
35
            new FieldList(
36
                new TextField('Title'),
37
                new TextField('Comment'),
38
                new TextField('URL')
39
            ),
40
            new FieldList()
41
        );
42
        $this->form->disableSecurityToken();
43
    }
44
45
    public function testEnableSpamProtection()
46
    {
47
        Config::modify()->set(
48
            FormSpamProtectionExtension::class,
49
            'default_spam_protector',
50
            FooProtector::class
51
        );
52
53
        $form = $this->form->enableSpamProtection();
54
55
        $this->assertEquals('Foo', $form->Fields()->fieldByName('Captcha')->Title());
56
    }
57
58
    public function testEnableSpamProtectionCustomProtector()
59
    {
60
        $form = $this->form->enableSpamProtection(array(
61
            'protector' => BarProtector::class
62
        ));
63
64
        $this->assertEquals('Bar', $form->Fields()->fieldByName('Captcha')->Title());
65
    }
66
67 View Code Duplication
    public function testEnableSpamProtectionCustomTitle()
0 ignored issues
show
Duplication introduced by
This method 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...
68
    {
69
        $form = $this->form->enableSpamProtection(array(
70
            'protector' => BarProtector::class,
71
            'title' => 'Baz',
72
        ));
73
74
        $this->assertEquals('Baz', $form->Fields()->fieldByName('Captcha')->Title());
75
    }
76
77 View Code Duplication
    public function testCustomOptions()
0 ignored issues
show
Duplication introduced by
This method 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...
78
    {
79
        $form = $this->form->enableSpamProtection(array(
80
            'protector' => BazProtector::class,
81
            'title' => 'Qux',
82
            'name' => 'Borris'
83
        ));
84
85
        $this->assertEquals('Qux', $form->Fields()->fieldByName('Borris')->Title());
86
    }
87
88
    public function testConfigurableName()
89
    {
90
        $field_name = "test_configurable_name";
91
        Config::modify()->set(
92
            FormSpamProtectionExtension::class,
93
            'default_spam_protector',
94
            FooProtector::class
95
        );
96
        Config::modify()->set(
97
            FormSpamProtectionExtension::class,
98
            'field_name',
99
            $field_name
100
        );
101
        $form = $this->form->enableSpamProtection();
102
        // remove for subsequent tests
103
        Config::modify()->remove(FormSpamProtectionExtension::class, 'field_name');
104
        // field should take up configured name
105
        $this->assertEquals('Foo', $form->Fields()->fieldByName($field_name)->Title());
106
    }
107
108 View Code Duplication
    public function testInsertBefore()
0 ignored issues
show
Duplication introduced by
This method 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...
109
    {
110
        $form = $this->form->enableSpamProtection(array(
111
            'protector' => FooProtector::class,
112
            'insertBefore' => 'URL'
113
        ));
114
115
        $fields = $form->Fields();
116
        $this->assertEquals('Title', $fields[0]->Title());
117
        $this->assertEquals('Comment', $fields[1]->Title());
118
        $this->assertEquals('Foo', $fields[2]->Title());
119
        $this->assertEquals('URL', $fields[3]->Title());
120
    }
121
122 View Code Duplication
    public function testInsertBeforeMissing()
0 ignored issues
show
Duplication introduced by
This method 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...
123
    {
124
        $form = $this->form->enableSpamProtection(array(
125
            'protector' => FooProtector::class,
126
            'insertBefore' => 'NotAField'
127
        ));
128
129
        // field should default to the end instead
130
        $fields = $form->Fields();
131
        $this->assertEquals('Title', $fields[0]->Title());
132
        $this->assertEquals('Comment', $fields[1]->Title());
133
        $this->assertEquals('URL', $fields[2]->Title());
134
        $this->assertEquals('Foo', $fields[3]->Title());
135
    }
136
}
137