Skip to content

Commit

Permalink
Add Microsoft Edge User Agent (#464)
Browse files Browse the repository at this point in the history
* Added en_US Name Justin

* Added Edge UserAgent and Test

* decided to rename edge to msedge to avoid possible collisions of names

* Revert "Added en_US Name Justin"

This reverts commit ef0e1c4.

* User Agent test reversion - change back to msedge

It seems that when the User Agent test reverted to the original name of `edge` and not `msedge`. This change fixes it back.

* white space php-fixer

* phpdoc requirements in Generator.php

* erroneous white space - even after lint?

* Update UserAgent.php

Co-authored-by: Bram <bram@ceulemans.dev>
  • Loading branch information
dutchie027 and bram-pkg authored Mar 24, 2022
1 parent 834cd7c commit acef8f2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
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());
}
}

0 comments on commit acef8f2

Please sign in to comment.