Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Microsoft Edge User Agent #464

Merged
merged 9 commits into from
Mar 24, 2022
8 changes: 8 additions & 0 deletions src/Faker/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,10 @@
*
* @method string chrome()
*
* @property string $msedge
*
* @method string msedge()
*
* @property string $firefox
*
* @method string firefox()
Expand All @@ -537,6 +541,10 @@
*
* @method string macPlatformToken()
*
* @property string $iosMobileToken
*
* @method string iosMobileToken()
*
* @property string $linuxPlatformToken
*
* @method string linuxPlatformToken()
Expand Down
36 changes: 34 additions & 2 deletions src/Faker/Provider/UserAgent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class UserAgent extends Base
{
protected static $userAgents = ['firefox', 'chrome', 'internetExplorer', 'opera', 'safari'];
protected static $userAgents = ['firefox', 'chrome', 'internetExplorer', 'opera', 'safari', 'edge'];

protected static $windowsPlatformTokens = [
'Windows NT 6.2', 'Windows NT 6.1', 'Windows NT 6.0', 'Windows NT 5.2', 'Windows NT 5.1',
Expand All @@ -25,7 +25,7 @@ class UserAgent extends Base
/**
* Add as many languages as you like.
*/
protected static $lang = ['en-US', 'sl-SI'];
protected static $lang = ['en-US', 'sl-SI', 'nl-NL'];

/**
* Generate mac processor
Expand Down Expand Up @@ -81,6 +81,28 @@ public static function chrome()
return 'Mozilla/5.0 ' . static::randomElement($platforms);
}

/**
* Generate Edge user agent
*
* @example 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.82 Safari/537.36 Edg/99.0.1150.36'
*
* @return string
*/
public static function msedge()
{
$saf = self::numberBetween(531, 537) . '.' . self::numberBetween(0, 2);
$chrv = self::numberBetween(79, 99) . '.0';

$platforms = [
'(' . static::windowsPlatformToken() . ") AppleWebKit/$saf (KHTML, like Gecko) Chrome/$chrv" . '.' . self::numberBetween(4000, 4844) . '.' . self::numberBetween(10, 99) . " Safari/$saf Edg/$chrv" . self::numberBetween(1000, 1146) . '.' . self::numberBetween(0, 99),
'(' . static::macPlatformToken() . ") AppleWebKit/$saf (KHTML, like Gecko) Chrome/$chrv" . '.' . self::numberBetween(4000, 4844) . '.' . self::numberBetween(10, 99) . " Safari/$saf Edg/$chrv" . self::numberBetween(1000, 1146) . '.' . self::numberBetween(0, 99),
'(' . static::linuxPlatformToken() . ") AppleWebKit/$saf (KHTML, like Gecko) Chrome/$chrv" . '.' . self::numberBetween(4000, 4844) . '.' . self::numberBetween(10, 99) . " Safari/$saf EdgA/$chrv" . self::numberBetween(1000, 1146) . '.' . self::numberBetween(0, 99),
'(' . static::iosMobileToken() . ") AppleWebKit/$saf (KHTML, like Gecko) Version/15.0 EdgiOS/$chrv" . self::numberBetween(1000, 1146) . '.' . self::numberBetween(0, 99) . " Mobile/15E148 Safari/$saf",
];

return 'Mozilla/5.0 ' . static::randomElement($platforms);
}

/**
* Generate Firefox user agent
*
Expand Down Expand Up @@ -177,6 +199,16 @@ public static function macPlatformToken()
return 'Macintosh; ' . static::randomElement(static::$macProcessor) . ' Mac OS X 10_' . self::numberBetween(5, 8) . '_' . self::numberBetween(0, 9);
}

/**
* @return string
*/
public static function iosMobileToken()
{
$iosVer = self::numberBetween(13, 15) . '_' . self::numberBetween(0, 2);

return 'iPhone; CPU iPhone OS ' . $iosVer . ' like Mac OS X';
}

/**
* @return string
*/
Expand Down
5 changes: 5 additions & 0 deletions test/Faker/Provider/UserAgentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ public function testChromeUserAgent()
{
self::assertStringContainsString('(KHTML, like Gecko) Chrome/', UserAgent::chrome());
}

public function testMSEdgeUserAgent()
{
self::assertStringContainsString('Edg', UserAgent::msedge());
}
}