summaryrefslogtreecommitdiffstats
path: root/tests/CSRFTest.php
blob: 39fda58401664d79ee16f2e225647054846f1e53 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php

use PHPUnit\Framework\TestCase;

class CSRFTest extends TestCase
{
    public function testGetKey()
    {
        $this->temp_key = CSRF::getKey();
        $this->assertIsString($this->temp_key);
        $this->assertEquals(32, strlen($this->temp_key));
    }

    public function testGenerate()
    {
        $token = CSRF::generate("some-action");
        $this->assertIsString($token);
        $this->assertEquals(64, strlen($token));
        
        $this->expectException(InvalidArgumentException::class);
        CSRF::generate();
        CSRF::generate(12);
        CSRF::generate(null);
    }

    public function testVerify()
    {
        $token = CSRF::generate("some-action");
        $this->assertEquals(CSRF::verify($token, "some-action"), true);
        $this->assertEquals(CSRF::verify($token, "other-action"), false);
        $this->assertEquals(CSRF::verify("anything-else", "some-action"), false);
        $this->assertEquals(CSRF::verify(1, "string"), false);
        $this->assertEquals(CSRF::verify("string", 2), false);
        $this->assertEquals(CSRF::verify(null, null), false);
    }
}