Completed
Push — issue#666 ( 5c565a...94715f )
by Guilherme
03:27
created

TagUriTest::testGetters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 15
rs 9.4285
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
15
class TagUriTest extends \PHPUnit_Framework_TestCase
16
{
17
    public function testValidTagUri()
18
    {
19
        $expected = 'example.com';
20
        $name = $this->getTagUri($expected)->getAuthorityName();
21
22
        $this->assertEquals($expected, $name);
23
    }
24
25
    public function testValidFullDate()
26
    {
27
        $expected = '2017-11-30';
28
29
        $tag = "tag:example.com,{$expected}:my_claim";
30
        $tagUri = TagUri::createFromString($tag);
31
32
        $this->assertEquals($expected, $tagUri->getDate());
33
        $this->assertEquals($tag, $tagUri->__toString());
34
    }
35
36
    public function testInvalidDay()
37
    {
38
        $date = '2017-11-31';
39
        $this->setExpectedException('\InvalidArgumentException', "Invalid date: {$date}");
40
41
        $tag = "tag:example.com,{$date}:my_claim";
42
        TagUri::createFromString($tag);
43
    }
44
45
    public function testInvalidMonth()
46
    {
47
        $date = '2017-13';
48
        $this->setExpectedException('\InvalidArgumentException', "Invalid date: {$date}");
49
50
        $tag = "tag:example.com,{$date}:my_claim";
51
        TagUri::createFromString($tag);
52
    }
53
54
    public function testValidEmailTagUri()
55
    {
56
        $expected = '[email protected]';
57
        $name = $this->getTagUri($expected)->getAuthorityName();
58
59
        $this->assertEquals($expected, $name);
60
    }
61
62
    public function testInvalidTagUri()
63
    {
64
        $this->setExpectedException('\InvalidArgumentException');
65
        $this->getTagUri('example .com');
66
    }
67
68
    public function testInvalidEmailTagUri()
69
    {
70
        $this->setExpectedException('\InvalidArgumentException');
71
        $this->getTagUri('[email protected] test');
72
    }
73
74
    /**
75
     * @param string $authorityName
76
     * @return TagUri
77
     */
78
    private function getTagUri($authorityName)
79
    {
80
        $tagUri = "tag:{$authorityName},2017:my_claim";
81
82
        return TagUri::createFromString($tagUri);
83
    }
84
85
    public function testWithFragment()
86
    {
87
        $tag = $this->getTagUri('example.com');
88
        $expected = $tag->__toString().'#it_works';
89
90
        $this->assertEquals($expected, $tag->withFragment('it_works')->__toString());
91
    }
92
93
    public function testGetters()
94
    {
95
        $tag = TagUri::createFromString('tag:example.com,2017:some-claim#fragment');
96
97
        $this->assertEquals('tag', $tag->getScheme());
98
        $this->assertEquals('2017', $tag->getDate());
99
        $this->assertEquals('some-claim', $tag->getSpecific());
100
        $this->assertEquals('fragment', $tag->getFragment());
101
        $this->assertEquals('example.com', $tag->getHost());
102
103
        $this->assertEquals('', $tag->getUserInfo());
104
        $this->assertEquals('', $tag->getPath());
105
        $this->assertEquals('', $tag->getQuery());
106
        $this->assertNull($tag->getPort());
107
    }
108
109
    public function testWith()
110
    {
111
        $methods = [
112
            'withScheme',
113
            'withUserInfo',
114
            'withHost',
115
            'withPort',
116
            'withPath',
117
            'withQuery',
118
        ];
119
120
        $successCount = 0;
121
        $tag = new TagUri();
122
        foreach ($methods as $method) {
123
            try {
124
                $tag->$method();
125
                $this->fail("Expected \BadMethodCallException when calling {$method}()");
126
            } catch (\BadMethodCallException $e) {
127
                $successCount++;
128
                continue;
129
            } catch (\Exception $e) {
130
                $this->fail("Expected \BadMethodCallException when calling {$method}() but got ".get_class($e));
131
            }
132
        }
133
134
        $this->assertEquals(count($methods), $successCount);
135
    }
136
137
    public function testSetAuthorityNameWithInvalidEmail()
138
    {
139
        $this->setExpectedException('\InvalidArgumentException');
140
        (new TagUri())->setAuthorityName('@invalid');
141
    }
142
}
143