Failed Conditions
Pull Request — master (#262)
by Guilherme
10:25 queued 04:26
created

TagUriTest::testWith()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 19
nc 6
nop 0
dl 0
loc 26
rs 8.5806
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\RemoteClaimsBundle\Tests\Model;
12
13
use LoginCidadao\RemoteClaimsBundle\Model\TagUri;
14
use PHPUnit\Framework\TestCase;
15
16
class TagUriTest extends TestCase
17
{
18
    public function testValidTagUri()
19
    {
20
        $expected = 'example.com';
21
        $name = $this->getTagUri($expected)->getAuthorityName();
22
23
        $this->assertEquals($expected, $name);
24
    }
25
26
    public function testValidFullDate()
27
    {
28
        $expected = '2017-11-30';
29
30
        $tag = "tag:example.com,{$expected}:my_claim";
31
        $tagUri = TagUri::createFromString($tag);
32
33
        $this->assertEquals($expected, $tagUri->getDate());
34
        $this->assertEquals($tag, $tagUri->__toString());
35
    }
36
37
    public function testInvalidDay()
38
    {
39
        $date = '2017-11-31';
40
        $this->expectException('\InvalidArgumentException');
41
        $this->expectExceptionMessage("Invalid date: {$date}");
42
43
        $tag = "tag:example.com,{$date}:my_claim";
44
        TagUri::createFromString($tag);
45
    }
46
47
    public function testInvalidMonth()
48
    {
49
        $date = '2017-13';
50
        $this->expectException('\InvalidArgumentException');
51
        $this->expectExceptionMessage("Invalid date: {$date}");
52
53
        $tag = "tag:example.com,{$date}:my_claim";
54
        TagUri::createFromString($tag);
55
    }
56
57
    public function testValidEmailTagUri()
58
    {
59
        $expected = '[email protected]';
60
        $name = $this->getTagUri($expected)->getAuthorityName();
61
62
        $this->assertEquals($expected, $name);
63
    }
64
65
    public function testInvalidTagUri()
66
    {
67
        $this->expectException('\InvalidArgumentException');
68
        $this->getTagUri('example .com');
69
    }
70
71
    public function testInvalidEmailTagUri()
72
    {
73
        $this->expectException('\InvalidArgumentException');
74
        $this->getTagUri('[email protected] test');
75
    }
76
77
    /**
78
     * @param string $authorityName
79
     * @return TagUri
80
     */
81
    private function getTagUri($authorityName)
82
    {
83
        $tagUri = "tag:{$authorityName},2017:my_claim";
84
85
        return TagUri::createFromString($tagUri);
86
    }
87
88
    public function testWithFragment()
89
    {
90
        $tag = $this->getTagUri('example.com');
91
        $expected = $tag->__toString().'#it_works';
92
93
        $this->assertEquals($expected, $tag->withFragment('it_works')->__toString());
94
    }
95
96
    public function testGetters()
97
    {
98
        $tag = TagUri::createFromString('tag:example.com,2017:some-claim#fragment');
99
100
        $this->assertEquals('tag', $tag->getScheme());
101
        $this->assertEquals('2017', $tag->getDate());
102
        $this->assertEquals('some-claim', $tag->getSpecific());
103
        $this->assertEquals('fragment', $tag->getFragment());
104
        $this->assertEquals('example.com', $tag->getHost());
105
106
        $this->assertEquals('', $tag->getUserInfo());
107
        $this->assertEquals('', $tag->getPath());
108
        $this->assertEquals('', $tag->getQuery());
109
        $this->assertNull($tag->getPort());
110
    }
111
112
    public function testWith()
113
    {
114
        $methods = [
115
            'withScheme',
116
            'withUserInfo',
117
            'withHost',
118
            'withPort',
119
            'withPath',
120
            'withQuery',
121
        ];
122
123
        $successCount = 0;
124
        $tag = new TagUri();
125
        foreach ($methods as $method) {
126
            try {
127
                $tag->$method('dummy');
128
                $this->fail("Expected \BadMethodCallException when calling {$method}()");
129
            } catch (\BadMethodCallException $e) {
130
                $successCount++;
131
                continue;
132
            } catch (\Exception $e) {
133
                $this->fail("Expected \BadMethodCallException when calling {$method}() but got ".get_class($e));
134
            }
135
        }
136
137
        $this->assertEquals(count($methods), $successCount);
138
    }
139
140
    public function testSetAuthorityNameWithInvalidEmail()
141
    {
142
        $this->expectException('\InvalidArgumentException');
143
        (new TagUri())->setAuthorityName('@invalid');
144
    }
145
146
    public function testTagCreatedInteractively()
147
    {
148
        $tag = (new TagUri())
149
            ->setAuthorityName('example.com')
150
            ->setDate('2018-01')
151
            ->setSpecific('example');
152
153
        $this->assertEquals('tag:example.com,2018-01:example', $tag->__toString());
154
    }
155
}
156