1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\RedirectBundle\Tests\Model; |
4
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
6
|
|
|
use Zenstruck\RedirectBundle\Tests\Fixture\Bundle\Entity\DummyNotFound; |
7
|
|
|
use Zenstruck\RedirectBundle\Tests\Fixture\Bundle\Entity\DummyRedirect; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Kevin Bond <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class RedirectTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @dataProvider sourceProvider |
16
|
|
|
* |
17
|
|
|
* @test |
18
|
|
|
*/ |
19
|
|
|
public function set_source($source, $expected) |
20
|
|
|
{ |
21
|
|
|
$redirect = $this->createRedirect($source, '/foo'); |
22
|
|
|
|
23
|
|
|
$this->assertSame($expected, $redirect->getSource()); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @dataProvider destinationProvider |
28
|
|
|
* |
29
|
|
|
* @test |
30
|
|
|
*/ |
31
|
|
|
public function set_destination($destination, $expectedDestination) |
32
|
|
|
{ |
33
|
|
|
$redirect = $this->createRedirect('/', $destination); |
34
|
|
|
|
35
|
|
|
$this->assertSame($expectedDestination, $redirect->getDestination()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @test |
40
|
|
|
*/ |
41
|
|
|
public function get_last_accessed_at() |
42
|
|
|
{ |
43
|
|
|
$redirect = $this->createRedirect('/', '/'); |
44
|
|
|
$this->assertNull($redirect->getLastAccessed()); |
45
|
|
|
|
46
|
|
|
$redirect->updateLastAccessed(); |
47
|
|
|
$this->assertInstanceOf('DateTime', $redirect->getLastAccessed()); |
48
|
|
|
$this->assertEqualsWithDelta(\time(), $redirect->getLastAccessed()->format('U'), 1); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @test |
53
|
|
|
*/ |
54
|
|
|
public function increase_count() |
55
|
|
|
{ |
56
|
|
|
$redirect = $this->createRedirect('/', '/'); |
57
|
|
|
|
58
|
|
|
$this->assertSame(0, $redirect->getCount()); |
59
|
|
|
|
60
|
|
|
$redirect->increaseCount(); |
61
|
|
|
$this->assertSame(1, $redirect->getCount()); |
62
|
|
|
|
63
|
|
|
$redirect->increaseCount(4); |
64
|
|
|
$this->assertSame(5, $redirect->getCount()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @test |
69
|
|
|
*/ |
70
|
|
|
public function create_from_not_found() |
71
|
|
|
{ |
72
|
|
|
$redirect = DummyRedirect::createFromNotFound(new DummyNotFound('/foo', 'https://example.com/foo'), '/baz'); |
73
|
|
|
|
74
|
|
|
$this->assertSame('/foo', $redirect->getSource()); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
View Code Duplication |
public function sourceProvider() |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
return [ |
80
|
|
|
['foo/bar', '/foo/bar'], |
81
|
|
|
['/foo/bar/', '/foo/bar/'], |
82
|
|
|
['foo', '/foo'], |
83
|
|
|
['foo/bar ', '/foo/bar'], |
84
|
|
|
[' foo/bar/', '/foo/bar/'], |
85
|
|
|
[' /foo', '/foo'], |
86
|
|
|
['http://www.example.com/foo', '/foo'], |
87
|
|
|
['http://www.example.com/', '/'], |
88
|
|
|
['http://www.example.com', '/'], |
89
|
|
|
['foo/bar?baz=true', '/foo/bar'], |
90
|
|
|
['http://www.example.com/foo?baz=bar&foo=baz', '/foo'], |
91
|
|
|
['http://www.example.com/foo?baz=bar&foo=baz#baz', '/foo'], |
92
|
|
|
['', null], |
93
|
|
|
[' ', null], |
94
|
|
|
['/', '/'], |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function destinationProvider() |
99
|
|
|
{ |
100
|
|
|
return [ |
101
|
|
|
['/foo', '/foo'], |
102
|
|
|
['foo', '/foo'], |
103
|
|
|
['foo?bar=baz', '/foo?bar=baz'], |
104
|
|
|
[null, null], |
105
|
|
|
['', null], |
106
|
|
|
[' ', null], |
107
|
|
|
[' ', null], |
108
|
|
|
['http://www.example.com/foo', 'http://www.example.com/foo'], |
109
|
|
|
]; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return \Zenstruck\RedirectBundle\Model\Redirect |
114
|
|
|
*/ |
115
|
|
|
private function createRedirect($source, $destination, $permanent = true) |
116
|
|
|
{ |
117
|
|
|
return $this->getMockForAbstractClass( |
118
|
|
|
'Zenstruck\RedirectBundle\Model\Redirect', |
119
|
|
|
[$source, $destination, $permanent] |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
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.