Completed
Push — master ( ca2209...0f254b )
by Michael
01:02
created

tests/unit/Forms/HCaptchaFieldTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace X3dgoo\HCaptcha\Tests;
4
5
use PHPUnit_Framework_Error;
6
use SilverStripe\Core\Config\Config;
7
use SilverStripe\Dev\FunctionalTest;
8
use SilverStripe\Forms\RequiredFields;
9
use X3dgoo\HCaptcha\Forms\HCaptchaField;
10
11
class HCaptchaFieldTest extends FunctionalTest
12
{
13
    protected $usesDatabase = true;
14
15
    public function testCreateHCaptchaField()
16
    {
17
        $hCaptchaField = new HCaptchaField('HCaptchaField');
18
19
        $this->assertNotNull($hCaptchaField);
20
    }
21
22
    public function testFieldFailure()
23
    {
24
        $hCaptchaField = new HCaptchaField('HCaptchaField');
25
26
        $this->expectException(PHPUnit_Framework_Error::class);
27
        $this->expectExceptionMessage(
28
            'You must configure HCaptchaField.site_key and HCaptchaField.secret_key. ' .
29
                'You can retrieve these at https://hcaptcha.co'
30
        );
31
32
        $field = $hCaptchaField->Field();
0 ignored issues
show
$field is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
33
    }
34
35
    public function testField()
36
    {
37
        Config::modify()->set(HCaptchaField::class, 'site_key', '{site-key}');
38
        Config::modify()->set(HCaptchaField::class, 'secret_key', '{secret-key}');
39
40
        $hCaptchaField = new HCaptchaField('HCaptchaField');
41
42
        $field = $hCaptchaField->Field();
43
44
        $this->assertNotNull($field);
45
    }
46
47
    public function testValidateFailure()
48
    {
49
        Config::modify()->set(HCaptchaField::class, 'site_key', '{site-key}');
50
        Config::modify()->set(HCaptchaField::class, 'secret_key', '{secret-key}');
51
52
        $hCaptchaField = new HCaptchaField('HCaptchaField');
53
54
        $result = $hCaptchaField->validate(new RequiredFields());
55
56
        $this->assertFalse($result);
57
    }
58
59
    public function testSetSiteKey()
60
    {
61
        $hCaptchaField = new HCaptchaField('HCaptchaField');
62
63
        $hCaptchaField->setSiteKey('{new-site-key}');
64
65
        $this->assertEquals('{new-site-key}', $hCaptchaField->getSiteKey());
66
    }
67
68
    public function testSetSecretKey()
69
    {
70
        $hCaptchaField = new HCaptchaField('HCaptchaField');
71
72
        $hCaptchaField->setSecretKey('{new-secret-key}');
73
    }
74
}
75