testSetDomainAfterInitializeWithServerNameNotLocalhost()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 7
rs 10
1
<?php
2
3
namespace Tests\Enjoys\Cookie;
4
5
use Enjoys\Cookie\Options;
6
use HttpSoft\ServerRequest\ServerRequestCreator;
7
use PHPUnit\Framework\TestCase;
8
use Psr\Http\Message\ServerRequestInterface;
9
10
class OptionsTest extends TestCase
11
{
12
    private  ServerRequestInterface  $request;
13
14
    protected function setUp(): void
15
    {
16
        $this->request = ServerRequestCreator::create();
17
    }
18
19
    public function testSetHttponly()
20
    {
21
        $options = new Options($this->request);
22
        $this->assertSame(false, $options->getOptions()['httponly']);
23
        $options->setHttponly(true);
24
        $this->assertSame(true, $options->getOptions()['httponly']);
25
    }
26
27
28
29
    public function testSetDomain()
30
    {
31
        $options = new Options($this->request);
32
        $options->setDomain('');
33
        $this->assertSame('', $options->getOptions()['domain']);
34
        $options->setDomain('domain.com');
35
        $this->assertSame('domain.com', $options->getOptions()['domain']);
36
    }
37
38
    public function testSetDomainAfterInitializeWithServerNameLocalhost()
39
    {
40
        $request = ServerRequestCreator::createFromGlobals(server: [
41
            'SERVER_NAME' => 'localhost'
42
        ]);
43
        $options = new Options($request);
44
        $this->assertSame('', $options->getOptions()['domain']);
45
    }
46
47
    public function testSetDomainAfterInitializeWithServerNameNotLocalhost()
48
    {
49
        $request = ServerRequestCreator::createFromGlobals(server: [
50
            'SERVER_NAME' => 'Server.localhost'
51
        ]);
52
        $options = new Options($request);
53
        $this->assertSame('server.localhost', $options->getOptions()['domain']);
54
    }
55
56
    public function testSetDomainAfterInitializeWithServerNameIsNull()
57
    {
58
        $request = ServerRequestCreator::createFromGlobals(server: [
59
            'SERVER_NAME' => null
60
        ]);
61
        $options = new Options($request);
62
        $this->assertSame('', $options->getOptions()['domain']);
63
    }
64
65
    public function testSetSameSite()
66
    {
67
        $options = new Options($this->request);
68
        $this->assertArrayNotHasKey('samesite', $options->getOptions());
69
        $options->setSameSite('Lax');
70
        $this->assertSame('Lax', $options->getOptions()['samesite']);
71
    }
72
73
    public function testSetPath()
74
    {
75
        $options = new Options($this->request);
76
        $this->assertSame('', $options->getOptions()['path']);
77
        $options->setPath('/');
78
        $this->assertSame('/', $options->getOptions()['path']);
79
    }
80
81
    public function testSetSecure()
82
    {
83
        $options = new Options($this->request);
84
        $this->assertSame(false, $options->getOptions()['secure']);
85
        $options->setSecure(true);
86
        $this->assertSame(true, $options->getOptions()['secure']);
87
    }
88
89
    public function testSetSecureAfterInitialize()
90
    {
91
        $request = ServerRequestCreator::createFromGlobals(server: [
92
            'HTTPS' => 'on'
93
        ]);
94
        $options = new Options($request);
95
        $this->assertSame(true, $options->getOptions()['secure']);
96
    }
97
98
    public function testSetExpires()
99
    {
100
        $options = new Options($this->request);
101
        $this->assertSame(-1, $options->getOptions()['expires']);
102
        $options->setExpires(100500);
103
        $this->assertSame(100500, $options->getOptions()['expires']);
104
    }
105
106
    public function testGetOptionsWithAddedCustomOptions()
107
    {
108
        $options = new Options($this->request);
109
        $options->setSecure(true);
110
        $this->assertSame(false, $options->getOptions(['secure' => false])['secure']);
111
112
        $options->setPath('/');
113
        $this->assertSame('/path', $options->getOptions(['path' => '/path'])['path']);
114
115
        $options->setHttponly(true);
116
        $this->assertSame(false, $options->getOptions(['httponly' => false])['httponly']);
117
118
        $options->setSameSite('Strict');
119
        $this->assertSame('Lax', $options->getOptions(['samesite' => 'Lax'])['samesite']);
120
121
        $options->setDomain(false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $domain of Enjoys\Cookie\Options::setDomain(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

121
        $options->setDomain(/** @scrutinizer ignore-type */ false);
Loading history...
122
        $this->assertSame('example.com', $options->getOptions(['domain' => 'example.com'])['domain']);
123
124
        $options->setExpires(100500);
125
        $this->assertSame(100500, $options->getOptions(['expires' => 0])['expires']);
126
    }
127
128
}
129