From 22701d5f4e642bfd758f98471805599c18ddef0d Mon Sep 17 00:00:00 2001 From: Harmen Stoppels Date: Tue, 10 May 2016 13:29:58 +0200 Subject: [PATCH 1/2] Remove redundant tests --- tests/unit/ParserTest.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/tests/unit/ParserTest.php b/tests/unit/ParserTest.php index fcd7767ac..7c37bcdc8 100644 --- a/tests/unit/ParserTest.php +++ b/tests/unit/ParserTest.php @@ -13,8 +13,6 @@ public function testCreatingParserWithString() { $parser = new Parser("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; InfoPath.1)"); - $this->assertTrue($parser instanceof \WhichBrowser\Parser); - $this->assertTrue($parser->isBrowser('Internet Explorer', '=', '6.0')); } @@ -24,8 +22,6 @@ public function testCreatingParserWithHeaders() 'User-Agent' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; InfoPath.1)' ]); - $this->assertTrue($parser instanceof \WhichBrowser\Parser); - $this->assertTrue($parser->isBrowser('Internet Explorer', '=', '6.0')); } @@ -37,8 +33,6 @@ public function testCreatingParserWithOptions() ] ]); - $this->assertTrue($parser instanceof \WhichBrowser\Parser); - $this->assertTrue($parser->isBrowser('Internet Explorer', '=', '6.0')); } From e60a1e732334bcb3e9a6da1dde213f081ddc1172 Mon Sep 17 00:00:00 2001 From: Harmen Stoppels Date: Tue, 10 May 2016 13:30:27 +0200 Subject: [PATCH 2/2] Add static constructors + tests --- src/Parser.php | 22 ++++++++++++++++++++++ tests/unit/ParserTest.php | 19 ++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/Parser.php b/src/Parser.php index b54c41562..234584422 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -24,6 +24,28 @@ public function __construct($headers = null, $options = []) } } + /** + * Create a new Parser instance from a user agent string + * + * @param string $agent Required, a string containing the raw User-Agent + * @param array $options Optional, an array with configuration options + */ + public static function fromUserAgent($agent, array $options = []) + { + return new static($agent, $options); + } + + /** + * Create a new Parser instance from a user agent string + * + * @param array $headers Required, an array with all of the headers + * @param array $options Optional, an array with configuration options + */ + public static function fromHeaders(array $headers, array $options = []) + { + return new static($headers, $options); + } + /** * Analyse the provided headers or User-Agent string * diff --git a/tests/unit/ParserTest.php b/tests/unit/ParserTest.php index 7c37bcdc8..3df451f1b 100644 --- a/tests/unit/ParserTest.php +++ b/tests/unit/ParserTest.php @@ -40,10 +40,27 @@ public function testCreatingParserWithoutArgumentsAndCallAnalyse() { $parser = new Parser(); $parser->analyse("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; InfoPath.1)"); - + + $this->assertTrue($parser->isBrowser('Internet Explorer', '=', '6.0')); + } + + public function testCreatingParserFromUserAgent() + { + $parser = Parser::fromUserAgent('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; InfoPath.1)'); + $this->assertTrue($parser instanceof \WhichBrowser\Parser); $this->assertTrue($parser->isBrowser('Internet Explorer', '=', '6.0')); } + public function testCreatingParserFromHeaderArray() + { + $parser = Parser::fromHeaders([ + 'User-Agent' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; InfoPath.1)' + ]); + + $this->assertTrue($parser instanceof \WhichBrowser\Parser); + + $this->assertTrue($parser->isBrowser('Internet Explorer', '=', '6.0')); + } }