UrlGeneratorTraitTest::tearDownAfterClass()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
namespace FwlibTest\Web;
3
4
use Fwlib\Util\Common\HttpUtil;
5
use Fwlib\Util\UtilContainer;
6
use Fwlib\Web\UrlGeneratorTrait;
7
use Fwolf\Wrapper\PHPUnit\PHPUnitTestCase;
8
use PHPUnit_Framework_MockObject_MockObject as MockObject;
9
10
/**
11
 * @copyright   Copyright 2014-2015 Fwolf
12
 * @license     http://www.gnu.org/licenses/lgpl.html LGPL-3.0+
13
 */
14
class UrlGeneratorTraitTest extends PHPUnitTestCase
15
{
16
    /**
17
     * @var HttpUtil
18
     */
19
    protected static $httpUtilBackup = null;
20
21
    /**
22
     * @var string[]
23
     */
24
    protected static $selfGetParameters = ['foo' => 'bar'];
25
26
    /**
27
     * @var string
28
     */
29
    protected static $selfUrl = 'https://domain.tld/index.php?foo=bar';
30
31
32
    /**
33
     * @return MockObject | UrlGeneratorTrait
34
     */
35
    protected function buildMock()
36
    {
37
        $urlGenerator = $this->getMockBuilder(UrlGeneratorTrait::class)
38
            ->getMockForTrait();
39
40
        return $urlGenerator;
41
    }
42
43
44 View Code Duplication
    public static function setUpBeforeClass()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        $utilContainer = UtilContainer::getInstance();
47
48
        self::$httpUtilBackup = $utilContainer->getHttp();
49
50
        $testCase = new self;
51
        $httpUtil = $testCase->getMock(
52
            HttpUtil::class,
53
            ['getSelfUrl', 'getGets']
54
        );
55
        $httpUtil->expects($testCase->any())
56
            ->method('getSelfUrl')
57
            ->willReturn(self::$selfUrl);
58
        $httpUtil->expects($testCase->any())
59
            ->method('getGets')
60
            ->willReturn(self::$selfGetParameters);
61
62
        $utilContainer->register('Http', $httpUtil);
63
    }
64
65
66
    public static function tearDownAfterClass()
67
    {
68
        $utilContainer = UtilContainer::getInstance();
69
70
        $utilContainer->register('Http', self::$httpUtilBackup);
71
    }
72
73
74
    public function testGetFullUrl()
75
    {
76
        $urlGenerator = $this->buildMock();
77
78
        $this->assertEquals(
79
            self::$selfUrl,
80
            $urlGenerator->getFullUrl()
81
        );
82
83
        // Without tailing '/', original path '/index.php' will kept
84
        $urlGenerator->setBaseUrl('http://net.com:8080');
85
        $this->assertEquals(
86
            'http://net.com:8080/index.php?foo=bar',
87
            $urlGenerator->getFullUrl()
88
        );
89
90
        // With tailing '/', the new path is '/'
91
        $urlGenerator->setBaseUrl('http://net.com/');
92
        $this->assertEquals(
93
            'http://net.com/?foo=bar',
94
            $urlGenerator->getFullUrl()
95
        );
96
97
        $url = 'http://username:password@hostname/path?arg=value#anchor';
98
        $urlGenerator->setFullUrl($url);
99
        $this->assertEquals($url, $urlGenerator->getFullUrl());
100
101
        $urlGenerator->setParameter('foo', 'bar');
102
        $url = 'http://username:password@hostname/path?arg=value&foo=bar#anchor';
103
        $this->assertEquals($url, $urlGenerator->getFullUrl());
104
105
        $urlGenerator->setFullUrl('');
106
        $this->assertEquals('', $urlGenerator->getFullUrl());
107
    }
108
109
110
    public function testGetLink()
111
    {
112
        $urlGenerator = $this->buildMock();
113
114
        $this->assertEquals(
115
            "<a href='?foo=bar' hidden='hidden'>FOO</a>",
116
            $urlGenerator->getLink('FOO', 'hidden=\'hidden\'')
117
        );
118
119
        $this->assertEquals(
120
            "<a href='https://domain.tld/index.php?foo=bar' hidden='hidden'>FOO</a>",
121
            $urlGenerator->getFullLink('FOO', 'hidden=\'hidden\'')
122
        );
123
    }
124
125
126
    public function testGetUrl()
127
    {
128
        $urlGenerator = $this->buildMock();
129
130
        $this->assertEquals(
131
            '?foo=bar',
132
            $urlGenerator->getUrl()
133
        );
134
135
        $urlGenerator->setParameter('f2', 42);
136
        $this->assertEquals(
137
            '?foo=bar&f2=42',
138
            $urlGenerator->getUrl()
139
        );
140
141
        $urlGenerator->setParameters([
142
            'f3' => 420,
143
            'f4' => '4200',
144
        ]);
145
        $this->assertEquals(
146
            '?foo=bar&f2=42&f3=420&f4=4200',
147
            $urlGenerator->getUrl()
148
        );
149
150
        $urlGenerator->unsetParameter('f4');
151
        $this->assertEquals(
152
            '?foo=bar&f2=42&f3=420',
153
            $urlGenerator->getUrl()
154
        );
155
156
        $urlGenerator->unsetParameters(['f2', 'f3']);
157
        $this->assertEquals(
158
            '?foo=bar',
159
            $urlGenerator->getUrl()
160
        );
161
162
        $urlGenerator->unsetAllParameters();
163
        $this->assertEmpty($urlGenerator->getUrl());
164
165
        $urlGenerator->reset(true);
166
        $this->assertEquals(
167
            '?foo=bar',
168
            $urlGenerator->getUrl()
169
        );
170
    }
171
}
172