-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUrlGeneratorTest.php
83 lines (73 loc) · 3.54 KB
/
UrlGeneratorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace Illuminatech\UrlTrailingSlash\Test;
use Illuminate\Http\Request;
use Illuminate\Routing\RouteCollection;
use Illuminatech\UrlTrailingSlash\Route;
use Illuminatech\UrlTrailingSlash\UrlGenerator;
class UrlGeneratorTest extends TestCase
{
public function testBasicGeneration()
{
$urlGenerator = new UrlGenerator(
new RouteCollection(),
Request::create('http://www.example.com/')
);
$this->assertEquals('http://www.example.com/foo/bar', $urlGenerator->to('foo/bar'));
$this->assertEquals('http://www.example.com/foo/bar/', $urlGenerator->to('foo/bar/'));
$this->assertEquals('http://www.example.com/foo/bar/', $urlGenerator->to('foo/bar//'));
}
public function testBasicRouteGeneration()
{
$urlGenerator = new UrlGenerator(
$routes = new RouteCollection(),
Request::create('http://www.example.com/')
);
$routes->add(new Route(['GET'], '', ['as' => 'home.no.slash']));
$routes->add(new Route(['GET'], '/', ['as' => 'home.with.slash']));
$routes->add(new Route(['GET'], 'foo/bar', ['as' => 'plain.no.slash']));
$routes->add(new Route(['GET'], 'foo/bar/', ['as' => 'plain.with.slash']));
$routes->add(new Route(['GET'], 'foo/bar/{baz}/breeze/{boom}', ['as' => 'param.no.slash']));
$routes->add(new Route(['GET'], 'foo/bar/{baz}/breeze/{boom}/', ['as' => 'param.with.slash']));
$routes->add(new Route(['GET'], 'foo/bar/{option?}', ['as' => 'optional.param.no.slash']));
$routes->add(new Route(['GET'], 'foo/bar/{option?}/', ['as' => 'optional.param.with.slash']));
$this->assertEquals('/', $urlGenerator->route('home.no.slash', [], false));
$this->assertEquals('/', $urlGenerator->route('home.with.slash', [], false));
$this->assertEquals('/foo/bar', $urlGenerator->route('plain.no.slash', [], false));
$this->assertEquals('/foo/bar/', $urlGenerator->route('plain.with.slash', [], false));
$this->assertEquals('/foo/bar/one/breeze/two?extra=three', $urlGenerator->route('param.no.slash', ['one', 'two', 'extra' => 'three'], false));
$this->assertEquals('/foo/bar/one/breeze/two/?extra=three', $urlGenerator->route('param.with.slash', ['one', 'two', 'extra' => 'three'], false));
$this->assertEquals('/foo/bar', $urlGenerator->route('optional.param.no.slash', [], false));
$this->assertEquals('/foo/bar/one', $urlGenerator->route('optional.param.no.slash', ['one'], false));
$this->assertEquals('/foo/bar/', $urlGenerator->route('optional.param.with.slash', [], false));
$this->assertEquals('/foo/bar/one/', $urlGenerator->route('optional.param.with.slash', ['one'], false));
}
/**
* Data provider for {@see testGenerateFull()}
*
* @return array test data.
*/
public static function dataProviderGenerateFull(): array
{
return [
['http://www.example.com/'],
['http://www.example.com/?var=some'],
['http://www.example.com/foo/bar'],
['http://www.example.com/foo/bar/'],
['http://www.example.com/foo/bar?var=some'],
['http://www.example.com/foo/bar/?var=some'],
];
}
/**
* @dataProvider dataProviderGenerateFull
*
* @param string $uri
*/
public function testGenerateFull(string $uri)
{
$urlGenerator = new UrlGenerator(
$routes = new RouteCollection(),
Request::create($uri)
);
$this->assertSame($uri, $urlGenerator->full());
}
}