From ba39896accdc6b49e538feda9ebaa79d4eb6567b Mon Sep 17 00:00:00 2001
From: Benjamin Laugueux <benjamin@yzalis.com>
Date: Fri, 14 Sep 2012 17:53:16 +0200
Subject: [PATCH 1/2] Added new UserAgent provider.

---
 readme.md                             |   9 ++
 src/Faker/Provider/UserAgent.php      | 161 ++++++++++++++++++++++++++
 test/Faker/Provider/UserAgentTest.php |  40 +++++++
 3 files changed, 210 insertions(+)
 create mode 100644 src/Faker/Provider/UserAgent.php
 create mode 100644 test/Faker/Provider/UserAgentTest.php

diff --git a/readme.md b/readme.md
index 317827ba..8cdac67e 100644
--- a/readme.md
+++ b/readme.md
@@ -161,6 +161,15 @@ Each of the generator properties (like `name`, `address`, and `lorem`) are calle
     lexify($string = '????') // 'wgts'  
     bothify($string = '## ??') // '42 jz' 
 
+### `Faker\Provider\UserAgent`
+
+    userAgent              // 'Mozilla/5.0 (Windows CE) AppleWebKit/5350 (KHTML, like Gecko) Chrome/13.0.888.0 Safari/5350'  
+    chrome                 // 'Mozilla/5.0 (Macintosh; PPC Mac OS X 10_6_5) AppleWebKit/5312 (KHTML, like Gecko) Chrome/14.0.894.0 Safari/5312'  
+    firefox                // 'Mozilla/5.0 (X11; Linuxi686; rv:7.0) Gecko/20101231 Firefox/3.6'  
+    safari                 // 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_7_1 rv:3.0; en-US) AppleWebKit/534.11.3 (KHTML, like Gecko) Version/4.0 Safari/534.11.3'  
+    opera                  // 'Opera/8.25 (Windows NT 5.1; en-US) Presto/2.9.188 Version/10.00'  
+    internetExplorer       // 'Mozilla/5.0 (compatible; MSIE 7.0; Windows 98; Win 9x 4.90; Trident/3.0)'  
+
 ## Localization
 
 `Faker\Factory` can take a locale as an argument, to return localized data. If no localized provider is found, the factory fallbacks to the default locale (en_EN).
diff --git a/src/Faker/Provider/UserAgent.php b/src/Faker/Provider/UserAgent.php
new file mode 100644
index 00000000..e0bc5a91
--- /dev/null
+++ b/src/Faker/Provider/UserAgent.php
@@ -0,0 +1,161 @@
+<?php
+
+namespace Faker\Provider;
+
+class UserAgent extends \Faker\Provider\Base
+{
+    protected static $userAgents = array('firefox', 'chrome', 'internetExplorer', 'opera', 'safari');
+
+    protected static $windowsPlatformTokens = array('Windows NT 6.2', 'Windows NT 6.1', 'Windows NT 6.0', 'Windows NT 5.2', 'Windows NT 5.1', 'Windows NT 5.01', 'Windows NT 5.0', 'Windows NT 4.0', 'Windows 98; Win 9x 4.90', 'Windows 98', 'Windows 95', 'Windows CE');
+
+    /**
+     * Possible processors on Linux
+     */
+    protected static $linuxProcessor = array('i686', 'x86_64');
+
+    /**
+     * Mac processors (it also added U;)
+     */
+    protected static $macProcessor = array('Intel', 'PPC', 'U; Intel', 'U; PPC');
+
+    /**
+     * Add as many languages as you like.
+     */
+    protected static $lang = array('en-US', 'sl-SI');
+
+    /**
+     * Generate mac processor
+     *
+     * @return string
+     */
+    public static function macProcessor()
+    {
+        return static::randomElement(static::$macProcessor);
+    }
+
+    /**
+     * Generrate linux processor
+     *
+     * @return string
+     */
+    public static function linuxProcessor()
+    {
+        return static::randomElement(static::$linuxProcessor);
+    }
+
+    /**
+     * Generate a random user agent
+     *
+     * @example 'Mozilla/5.0 (Windows CE) AppleWebKit/5350 (KHTML, like Gecko) Chrome/13.0.888.0 Safari/5350'
+     */
+    public static function userAgent()
+    {
+        $userAgentName = static::randomElement(static::$userAgents);
+
+        return static::$userAgentName();
+    }
+
+    /**
+     * Generate Chrome user agent
+     *
+     * @example 'Mozilla/5.0 (Macintosh; PPC Mac OS X 10_6_5) AppleWebKit/5312 (KHTML, like Gecko) Chrome/14.0.894.0 Safari/5312'
+     */
+    public static function chrome()
+    {
+        $saf = mt_rand(531, 536) . mt_rand(0, 2);
+
+        $platforms = array(
+            '(' . static::linuxPlatformToken() . ") AppleWebKit/$saf (KHTML, like Gecko) Chrome/" . mt_rand(13, 15) . '.0.' . mt_rand(800, 899) . ".0 Safari/$saf",
+            '(' . static::windowsPlatformToken() . ") AppleWebKit/$saf (KHTML, like Gecko) Chrome/" . mt_rand(13, 15) . '.0.' . mt_rand(800, 899) . ".0 Safari/$saf",
+            '(' . static::macPlatformToken() . ") AppleWebKit/$saf (KHTML, like Gecko) Chrome/" . mt_rand(13, 15) . '.0.' . mt_rand(800, 899) . ".0 Safari/$saf"
+        );
+
+        return 'Mozilla/5.0 ' . static::randomElement($platforms);
+    }
+
+    /**
+     * Generate Firefox user agent
+     *
+     * @example 'Mozilla/5.0 (X11; Linuxi686; rv:7.0) Gecko/20101231 Firefox/3.6'
+     */
+    public static function firefox()
+    {
+        $ver = array(
+            'Gecko/' . date('Ymd', mt_rand(strtotime('2011-1-1'), time())) . ' Firefox/' . mt_rand(4, 15) . '.0',
+            'Gecko/' . date('Ymd', mt_rand(strtotime('2010-1-1'), time())) . ' Firefox/3.6.' . mt_rand(1, 20),
+            'Gecko/' . date('Ymd', mt_rand(strtotime('2010-1-1'), time())) . ' Firefox/3.8'
+        );
+
+        $platforms = array(
+            '(' . static::windowsPlatformToken() . '; ' . static::randomElement(static::$lang) . '; rv:1.9.' . mt_rand(0, 2) . '.20) ' . $ver[array_rand($ver, 1)],
+            '(' . static::linuxPlatformToken() . '; rv:' . mt_rand(5, 7) . '.0) ' . $ver[array_rand($ver, 1)],
+            '(' . static::macPlatformToken() . ' rv:' . mt_rand(2, 6) . '.0) ' . $ver[array_rand($ver, 1)]
+        );
+
+        return "Mozilla/5.0 " . static::randomElement($platforms);
+    }
+
+    /**
+     * Generate Safari user agent
+     *
+     * @example 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_7_1 rv:3.0; en-US) AppleWebKit/534.11.3 (KHTML, like Gecko) Version/4.0 Safari/534.11.3'
+     */
+    public static function safari()
+    {
+        $saf = mt_rand(531, 535) . '.' . mt_rand(1, 50) . '.' . mt_rand(1, 7);
+        if (mt_rand(0, 1) == 0) {
+            $ver = mt_rand(4, 5) . '.' . mt_rand(0, 1);
+        } else {
+            $ver = mt_rand(4, 5) . '.0.' . mt_rand(1, 5);
+        }
+
+        $platforms = array(
+            '(Windows; U; ' . static::windowsPlatformToken() . ") AppleWebKit/$saf (KHTML, like Gecko) Version/$ver Safari/$saf",
+            '(' . static::macPlatformToken() . ' rv:' . mt_rand(2, 6) . '.0; ' . static::randomElement(static::$lang) . ") AppleWebKit/$saf (KHTML, like Gecko) Version/$ver Safari/$saf",
+            '(iPod; U; CPU iPhone OS ' . mt_rand(3, 4) . '_' . mt_rand(0, 3) . ' like Mac OS X; ' . static::randomElement(static::$lang) . ") AppleWebKit/$saf (KHTML, like Gecko) Version/" . mt_rand(3, 4) . ".0.5 Mobile/8B" . mt_rand(111, 119) . " Safari/6$saf",
+        );
+
+        return "Mozilla/5.0 " . static::randomElement($platforms);
+    }
+
+    /**
+     * Generate Opera user agent
+     *
+     * @example 'Opera/8.25 (Windows NT 5.1; en-US) Presto/2.9.188 Version/10.00'
+     */
+    public static function opera()
+    {
+        $platforms = array(
+            '(' . static::linuxPlatformToken() . '; ' . static::randomElement(static::$lang) . ') Presto/2.9.' . mt_rand(160, 190) . ' Version/' . mt_rand(10, 12) . '.00',
+            '(' . static::windowsPlatformToken() . '; ' . static::randomElement(static::$lang) . ') Presto/2.9.' . mt_rand(160, 190) . ' Version/' . mt_rand(10, 12) . '.00'
+        );
+
+        return "Opera/" . mt_rand(8, 9) . '.' . mt_rand(10, 99) . ' ' . static::randomElement($platforms);
+    }
+
+    /**
+     * Generate Internet Explorer user agent
+     *
+     * @example 'Mozilla/5.0 (compatible; MSIE 7.0; Windows 98; Win 9x 4.90; Trident/3.0)'
+     */
+    public static function internetExplorer()
+    {
+        return 'Mozilla/5.0 (compatible; MSIE ' . mt_rand(5, 9) . '.0; ' . static::windowsPlatformToken() . '; Trident/' . mt_rand(3, 5) . '.' . mt_rand(0, 1) . ')';
+
+    }
+
+    public static function windowsPlatformToken()
+    {
+        return static::randomElement(static::$windowsPlatformTokens);
+    }
+
+    public static function macPlatformToken()
+    {
+        return 'Macintosh; ' . static::randomElement(static::$macProcessor) . ' Mac OS X 10_' . mt_rand(5, 8) . '_' . mt_rand(0, 9);
+    }
+
+    public static function linuxPlatformToken()
+    {
+        return 'X11; Linux' . static::randomElement(static::$linuxProcessor);
+    }
+}
diff --git a/test/Faker/Provider/UserAgentTest.php b/test/Faker/Provider/UserAgentTest.php
new file mode 100644
index 00000000..381936bf
--- /dev/null
+++ b/test/Faker/Provider/UserAgentTest.php
@@ -0,0 +1,40 @@
+<?php
+
+namespace Faker\Test\Provider;
+
+use Faker\Provider\UserAgent;
+use Faker\Generator;
+
+class UserAgentTest extends \PHPUnit_Framework_TestCase
+{
+    public function testRandomUserAgent()
+    {
+        echo UserAgent::userAgent();
+        $this->assertNotNull(UserAgent::userAgent());
+    }
+
+    public function testFirefoxUserAgent()
+    {
+        $this->stringContains(' Firefox/', UserAgent::firefox());
+    }
+
+    public function testSafariUserAgent()
+    {
+        $this->stringContains('Safari/', UserAgent::safari());
+    }
+
+    public function testInternetExplorerUserAgent()
+    {
+        $this->assertStringStartsWith('Mozilla/5.0 (compatible; MSIE ', UserAgent::internetExplorer());
+    }
+
+    public function testOperaUserAgent()
+    {
+        $this->assertStringStartsWith('Opera/', UserAgent::opera());
+    }
+
+    public function testChromeUserAgent()
+    {
+        $this->stringContains('(KHTML, like Gecko) Chrome/', UserAgent::chrome());
+    }
+}
\ No newline at end of file

From 707cf049add79eeaf1983419b3431a118714cd37 Mon Sep 17 00:00:00 2001
From: Benjamin Laugueux <benjamin@yzalis.com>
Date: Fri, 14 Sep 2012 17:57:58 +0200
Subject: [PATCH 2/2] Removed echo in UserAgentTest.

---
 test/Faker/Provider/UserAgentTest.php | 1 -
 1 file changed, 1 deletion(-)

diff --git a/test/Faker/Provider/UserAgentTest.php b/test/Faker/Provider/UserAgentTest.php
index 381936bf..6098dc74 100644
--- a/test/Faker/Provider/UserAgentTest.php
+++ b/test/Faker/Provider/UserAgentTest.php
@@ -9,7 +9,6 @@ class UserAgentTest extends \PHPUnit_Framework_TestCase
 {
     public function testRandomUserAgent()
     {
-        echo UserAgent::userAgent();
         $this->assertNotNull(UserAgent::userAgent());
     }