Reformat code automatically

This commit is contained in:
Meritoo
2022-02-12 14:46:47 +01:00
parent a3af138f0c
commit a3adae50b8
116 changed files with 19152 additions and 19091 deletions

View File

@@ -23,9 +23,174 @@ use Meritoo\Common\Utilities\Uri;
*/
class UriTest extends BaseTestCase
{
public function testConstructor()
/**
* Provides data used to build secured url
*
* @return Generator
*/
public function provideDataForSecuredUrl()
{
static::assertHasNoConstructor(Uri::class);
yield [
'',
'',
'',
'',
];
yield [
'/',
'',
'',
'http://lorem.com/',
];
yield [
'contact',
'',
'',
'http://lorem.com/contact',
];
yield [
'contact',
'john',
'',
'http://lorem.com/contact',
];
yield [
'contact',
'',
'pass123',
'http://lorem.com/contact',
];
yield [
'contact',
'john',
'pass123',
'http://john:pass123@lorem.com/contact',
];
}
public function provideRootUrlAndUrlParts(): ?Generator
{
yield [
'',
'',
];
yield [
'',
'',
'',
];
yield [
'http://my.example',
'http://my.example',
'',
];
yield [
'http://my.example',
'http://my.example',
'',
'',
];
yield [
'http://my.example//test/12/test/',
'http://my.example',
'',
'test',
'12/test',
'',
];
yield [
'http://my.example//test/12/test',
'http://my.example',
'',
'test/',
'12/test/',
];
yield [
'http://my.example/test/12/test/',
'http://my.example',
'/test/',
'/12/test',
'',
];
}
/**
* Provides url to replenish protocol
*
* @return Generator
*/
public function provideUrlToReplenishProtocol()
{
yield [
'http://test',
'test',
'',
];
yield [
'ftp://lorem.ipsum',
'lorem.ipsum',
'ftp',
];
}
/**
* Provides url used to verify if it's external, from another server / domain
*
* @return Generator
*/
public function provideUrlToVerifyIfIsExternal()
{
yield [
'',
false,
];
yield [
'/',
false,
];
yield [
'http://something.different/first-page',
true,
];
yield [
'something.different/first-page',
true,
];
yield [
'http://lorem.com',
false,
];
yield [
'http://lorem.com/contact',
false,
];
yield [
'lorem.com',
false,
];
yield [
'lorem.com/contact',
false,
];
}
public function testAddProtocolToUrl()
@@ -45,61 +210,20 @@ class UriTest extends BaseTestCase
}
/**
* @param mixed $url Empty value, e.g. ""
* @dataProvider provideEmptyValue
*/
public function testReplenishProtocolEmptyUrl($url)
{
self::assertEquals('', Uri::replenishProtocol($url));
}
/**
* @param string $expected Expected result
* @param string $url The url to check and replenish
* @param string $protocol (optional) The protocol which is replenished. If is empty, protocol of current request
* is used.
* @param string $expected
* @param string $rootUrl
* @param string ...$urlParts
*
* @dataProvider provideUrlToReplenishProtocol
* @dataProvider provideRootUrlAndUrlParts
*/
public function testReplenishProtocol($expected, $url, $protocol = '')
public function testBuildUrl(string $expected, string $rootUrl, string ...$urlParts): void
{
/*
* Required to get protocol when it's not provided and to void test failure:
*
* Failed asserting that two strings are identical.
* Expected :'://test'
* Actual :'http://test'
*/
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
self::assertSame($expected, Uri::replenishProtocol($url, $protocol));
static::assertSame($expected, Uri::buildUrl($rootUrl, ...$urlParts));
}
public function testGetServerNameOrIpWithoutProtocol()
public function testConstructor()
{
$_SERVER['HTTP_HOST'] = '';
self::assertEquals('', Uri::getServerNameOrIp());
$host = 'lorem.com';
$_SERVER['HTTP_HOST'] = $host;
self::assertEquals($host, Uri::getServerNameOrIp());
}
public function testGetServerNameOrIpWithProtocol()
{
$_SERVER['HTTP_HOST'] = '';
$_SERVER['SERVER_PROTOCOL'] = '';
self::assertEquals('', Uri::getServerNameOrIp(true));
$host = 'lorem.com';
$protocol = 'HTTP/1.1';
$_SERVER['HTTP_HOST'] = $host;
$_SERVER['SERVER_PROTOCOL'] = $protocol;
self::assertEquals(sprintf('http://%s', $host), Uri::getServerNameOrIp(true));
static::assertHasNoConstructor(Uri::class);
}
public function testGetFullUriWithHost()
@@ -157,6 +281,49 @@ class UriTest extends BaseTestCase
self::assertEquals($refererUrl, Uri::getRefererUri());
}
/**
* @param string $url A path / url to some resource, e.g. page, image, css file
* @param string $user User name used to log in
* @param string $password User password used to log in
* @param string $expectedUrl Expected, secured url
*
* @dataProvider provideDataForSecuredUrl
*/
public function testGetSecuredUrl($url, $user, $password, $expectedUrl)
{
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
$_SERVER['HTTP_HOST'] = 'lorem.com';
self::assertEquals($expectedUrl, Uri::getSecuredUrl($url, $user, $password));
}
public function testGetServerNameOrIpWithProtocol()
{
$_SERVER['HTTP_HOST'] = '';
$_SERVER['SERVER_PROTOCOL'] = '';
self::assertEquals('', Uri::getServerNameOrIp(true));
$host = 'lorem.com';
$protocol = 'HTTP/1.1';
$_SERVER['HTTP_HOST'] = $host;
$_SERVER['SERVER_PROTOCOL'] = $protocol;
self::assertEquals(sprintf('http://%s', $host), Uri::getServerNameOrIp(true));
}
public function testGetServerNameOrIpWithoutProtocol()
{
$_SERVER['HTTP_HOST'] = '';
self::assertEquals('', Uri::getServerNameOrIp());
$host = 'lorem.com';
$_SERVER['HTTP_HOST'] = $host;
self::assertEquals($host, Uri::getServerNameOrIp());
}
public function testGetUserAddressIp()
{
$_SERVER['REMOTE_ADDR'] = '';
@@ -168,58 +335,49 @@ class UriTest extends BaseTestCase
self::assertEquals($userAddressIp, Uri::getUserAddressIp());
}
public function testGetUserOperatingSystemName()
{
$_SERVER['HTTP_USER_AGENT'] = '';
self::assertEquals('', Uri::getUserOperatingSystemName());
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/600.2.5 (KHTML, like'
.' Gecko) Version/8.0.2 Safari/600.2.5';
self::assertEquals('Mac OS', Uri::getUserOperatingSystemName());
}
public function testGetUserWebBrowserInfo()
{
$_SERVER['HTTP_USER_AGENT'] = '';
self::assertEquals('', Uri::getUserWebBrowserInfo());
$browserInfo = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/600.2.5 (KHTML, like Gecko)'
. ' Version/8.0.2 Safari/600.2.5';
.' Version/8.0.2 Safari/600.2.5';
$_SERVER['HTTP_USER_AGENT'] = $browserInfo;
self::assertEquals($browserInfo, Uri::getUserWebBrowserInfo());
}
public function testGetUserWebBrowserNameWithoutVersion()
{
$_SERVER['HTTP_USER_AGENT'] = '';
self::assertEquals('', Uri::getUserWebBrowserName());
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/600.2.5 (KHTML, like'
. ' Gecko) Version/8.0.2 Safari/600.2.5';
self::assertEquals('Apple Safari', Uri::getUserWebBrowserName());
}
public function testGetUserWebBrowserNameWithVersion()
{
$_SERVER['HTTP_USER_AGENT'] = '';
self::assertEquals('', Uri::getUserWebBrowserName(true));
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/600.2.5 (KHTML, like'
. ' Gecko) Version/8.0.2 Safari/600.2.5';
.' Gecko) Version/8.0.2 Safari/600.2.5';
self::assertEquals('Apple Safari 600.2.5', Uri::getUserWebBrowserName(true));
}
public function testGetUserOperatingSystemName()
public function testGetUserWebBrowserNameWithoutVersion()
{
$_SERVER['HTTP_USER_AGENT'] = '';
self::assertEquals('', Uri::getUserOperatingSystemName());
self::assertEquals('', Uri::getUserWebBrowserName());
$_SERVER['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/600.2.5 (KHTML, like'
. ' Gecko) Version/8.0.2 Safari/600.2.5';
.' Gecko) Version/8.0.2 Safari/600.2.5';
self::assertEquals('Mac OS', Uri::getUserOperatingSystemName());
}
public function testIsServerLocalhost()
{
$_SERVER['HTTP_HOST'] = '';
self::assertFalse(Uri::isServerLocalhost());
$_SERVER['HTTP_HOST'] = '127.0.0.1';
self::assertTrue(Uri::isServerLocalhost());
self::assertEquals('Apple Safari', Uri::getUserWebBrowserName());
}
/**
@@ -239,201 +397,43 @@ class UriTest extends BaseTestCase
self::assertEquals($expected, Uri::isExternalUrl($url));
}
/**
* @param string $url A path / url to some resource, e.g. page, image, css file
* @param string $user User name used to log in
* @param string $password User password used to log in
* @param string $expectedUrl Expected, secured url
*
* @dataProvider provideDataForSecuredUrl
*/
public function testGetSecuredUrl($url, $user, $password, $expectedUrl)
public function testIsServerLocalhost()
{
$_SERVER['HTTP_HOST'] = '';
self::assertFalse(Uri::isServerLocalhost());
$_SERVER['HTTP_HOST'] = '127.0.0.1';
self::assertTrue(Uri::isServerLocalhost());
}
/**
* @param string $expected Expected result
* @param string $url The url to check and replenish
* @param string $protocol (optional) The protocol which is replenished. If is empty, protocol of current request
* is used.
*
* @dataProvider provideUrlToReplenishProtocol
*/
public function testReplenishProtocol($expected, $url, $protocol = '')
{
/*
* Required to get protocol when it's not provided and to void test failure:
*
* Failed asserting that two strings are identical.
* Expected :'://test'
* Actual :'http://test'
*/
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1';
$_SERVER['HTTP_HOST'] = 'lorem.com';
self::assertEquals($expectedUrl, Uri::getSecuredUrl($url, $user, $password));
self::assertSame($expected, Uri::replenishProtocol($url, $protocol));
}
/**
* @param string $expected
* @param string $rootUrl
* @param string ...$urlParts
*
* @dataProvider provideRootUrlAndUrlParts
* @param mixed $url Empty value, e.g. ""
* @dataProvider provideEmptyValue
*/
public function testBuildUrl(string $expected, string $rootUrl, string ...$urlParts): void
public function testReplenishProtocolEmptyUrl($url)
{
static::assertSame($expected, Uri::buildUrl($rootUrl, ...$urlParts));
}
/**
* Provides url to replenish protocol
*
* @return Generator
*/
public function provideUrlToReplenishProtocol()
{
yield[
'http://test',
'test',
'',
];
yield[
'ftp://lorem.ipsum',
'lorem.ipsum',
'ftp',
];
}
/**
* Provides url used to verify if it's external, from another server / domain
*
* @return Generator
*/
public function provideUrlToVerifyIfIsExternal()
{
yield[
'',
false,
];
yield[
'/',
false,
];
yield[
'http://something.different/first-page',
true,
];
yield[
'something.different/first-page',
true,
];
yield[
'http://lorem.com',
false,
];
yield[
'http://lorem.com/contact',
false,
];
yield[
'lorem.com',
false,
];
yield[
'lorem.com/contact',
false,
];
}
/**
* Provides data used to build secured url
*
* @return Generator
*/
public function provideDataForSecuredUrl()
{
yield[
'',
'',
'',
'',
];
yield[
'/',
'',
'',
'http://lorem.com/',
];
yield[
'contact',
'',
'',
'http://lorem.com/contact',
];
yield[
'contact',
'john',
'',
'http://lorem.com/contact',
];
yield[
'contact',
'',
'pass123',
'http://lorem.com/contact',
];
yield[
'contact',
'john',
'pass123',
'http://john:pass123@lorem.com/contact',
];
}
public function provideRootUrlAndUrlParts(): ?Generator
{
yield[
'',
'',
];
yield[
'',
'',
'',
];
yield[
'http://my.example',
'http://my.example',
'',
];
yield[
'http://my.example',
'http://my.example',
'',
'',
];
yield[
'http://my.example//test/12/test/',
'http://my.example',
'',
'test',
'12/test',
'',
];
yield[
'http://my.example//test/12/test',
'http://my.example',
'',
'test/',
'12/test/',
];
yield[
'http://my.example/test/12/test/',
'http://my.example',
'/test/',
'/12/test',
'',
];
self::assertEquals('', Uri::replenishProtocol($url));
}
}