2017-09-19 23:55:40 +02:00
|
|
|
<?php
|
|
|
|
|
2018-09-04 18:35:13 +02:00
|
|
|
namespace Engelsystem\Test\Unit\Http;
|
2017-09-19 23:55:40 +02:00
|
|
|
|
2020-04-07 15:08:49 +02:00
|
|
|
use Engelsystem\Config\Config;
|
2017-09-19 23:55:40 +02:00
|
|
|
use Engelsystem\Http\Request;
|
2018-08-22 03:10:08 +02:00
|
|
|
use Engelsystem\Http\UrlGenerator;
|
2020-04-07 15:08:49 +02:00
|
|
|
use Engelsystem\Test\Unit\TestCase;
|
2017-09-19 23:55:40 +02:00
|
|
|
|
|
|
|
class UrlGeneratorTest extends TestCase
|
|
|
|
{
|
2020-04-07 15:08:49 +02:00
|
|
|
public function provideLinksTo(): array
|
2017-09-19 23:55:40 +02:00
|
|
|
{
|
|
|
|
return [
|
|
|
|
['/foo/path', '/foo/path', 'http://foo.bar/foo/path', [], 'http://foo.bar/foo/path'],
|
|
|
|
['foo', '/foo', 'https://foo.bar/foo', [], 'https://foo.bar/foo'],
|
|
|
|
['foo', '/foo', 'http://f.b/foo', ['test' => 'abc', 'bla' => 'foo'], 'http://f.b/foo?test=abc&bla=foo'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider provideLinksTo
|
2018-08-22 03:10:08 +02:00
|
|
|
* @covers \Engelsystem\Http\UrlGenerator::to
|
2020-04-07 15:08:49 +02:00
|
|
|
* @covers \Engelsystem\Http\UrlGenerator::generateUrl
|
2017-09-19 23:55:40 +02:00
|
|
|
*
|
|
|
|
* @param string[] $arguments
|
|
|
|
*/
|
|
|
|
public function testTo(
|
|
|
|
string $urlToPath,
|
|
|
|
string $path,
|
|
|
|
string $willReturn,
|
|
|
|
array $arguments,
|
|
|
|
string $expectedUrl
|
|
|
|
): void {
|
|
|
|
$request = $this->getMockBuilder(Request::class)
|
|
|
|
->getMock();
|
|
|
|
$request->expects($this->once())
|
|
|
|
->method('getUriForPath')
|
|
|
|
->with($path)
|
|
|
|
->willReturn($willReturn);
|
2020-04-07 15:08:49 +02:00
|
|
|
$this->app->instance('request', $request);
|
|
|
|
$this->app->instance('config', new Config());
|
2017-09-19 23:55:40 +02:00
|
|
|
|
2020-04-07 15:08:49 +02:00
|
|
|
$urlGenerator = new UrlGenerator();
|
2017-09-19 23:55:40 +02:00
|
|
|
|
2017-09-20 01:09:11 +02:00
|
|
|
$url = $urlGenerator->to($urlToPath, $arguments);
|
2017-09-19 23:55:40 +02:00
|
|
|
$this->assertEquals($expectedUrl, $url);
|
|
|
|
}
|
2020-04-07 15:08:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\UrlGenerator::to
|
|
|
|
*/
|
|
|
|
public function testToWithValidUrl(): void
|
|
|
|
{
|
|
|
|
$url = new UrlGenerator();
|
|
|
|
$this->app->instance('config', new Config());
|
|
|
|
|
|
|
|
$this->assertEquals('https://foo.bar/batz', $url->to('https://foo.bar/batz'));
|
|
|
|
$this->assertEquals('https://some.url?lorem=ipsum', $url->to('https://some.url', ['lorem' => 'ipsum']));
|
|
|
|
$this->assertEquals('mailto:foo@bar.batz', $url->to('mailto:foo@bar.batz'));
|
|
|
|
$this->assertEquals('#some-anchor', $url->to('#some-anchor'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\UrlGenerator::to
|
|
|
|
* @covers \Engelsystem\Http\UrlGenerator::generateUrl
|
|
|
|
*/
|
|
|
|
public function testToWithApplicationURL(): void
|
|
|
|
{
|
|
|
|
$this->app->instance('config', new Config(['url' => 'https://foo.bar/base/']));
|
|
|
|
|
|
|
|
$url = new UrlGenerator();
|
|
|
|
|
|
|
|
$this->assertEquals('https://foo.bar/base/test', $url->to('test'));
|
|
|
|
$this->assertEquals('https://foo.bar/base/test', $url->to('/test'));
|
|
|
|
$this->assertEquals('https://foo.bar/base/lorem?ipsum=dolor', $url->to('/lorem', ['ipsum' => 'dolor']));
|
|
|
|
|
|
|
|
$this->app->instance('config', new Config(['url' => 'https://foo.bar/base']));
|
|
|
|
$this->assertEquals('https://foo.bar/base/test', $url->to('test'));
|
|
|
|
$this->assertEquals('https://foo.bar/base/test', $url->to('/test'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \Engelsystem\Http\UrlGenerator::isValidUrl
|
|
|
|
*/
|
|
|
|
public function testIsValidUrl(): void
|
|
|
|
{
|
|
|
|
$url = new UrlGenerator();
|
|
|
|
|
|
|
|
$this->assertTrue($url->isValidUrl('https://foo.bar'));
|
|
|
|
$this->assertTrue($url->isValidUrl('#foo-bar'));
|
|
|
|
$this->assertTrue($url->isValidUrl('tel:+123456'));
|
|
|
|
$this->assertTrue($url->isValidUrl('ftp://foo@bar.batz'));
|
|
|
|
|
|
|
|
$this->assertFalse($url->isValidUrl('foo/bar'));
|
|
|
|
$this->assertFalse($url->isValidUrl('foo/uff://bar'));
|
|
|
|
$this->assertFalse($url->isValidUrl('lorem/ipsum#dolor'));
|
|
|
|
}
|
2017-09-19 23:55:40 +02:00
|
|
|
}
|