Skip to content

Commit e60a1e7

Browse files
committed
Add static constructors + tests
1 parent 22701d5 commit e60a1e7

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/Parser.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,28 @@ public function __construct($headers = null, $options = [])
2424
}
2525
}
2626

27+
/**
28+
* Create a new Parser instance from a user agent string
29+
*
30+
* @param string $agent Required, a string containing the raw User-Agent
31+
* @param array $options Optional, an array with configuration options
32+
*/
33+
public static function fromUserAgent($agent, array $options = [])
34+
{
35+
return new static($agent, $options);
36+
}
37+
38+
/**
39+
* Create a new Parser instance from a user agent string
40+
*
41+
* @param array $headers Required, an array with all of the headers
42+
* @param array $options Optional, an array with configuration options
43+
*/
44+
public static function fromHeaders(array $headers, array $options = [])
45+
{
46+
return new static($headers, $options);
47+
}
48+
2749
/**
2850
* Analyse the provided headers or User-Agent string
2951
*

tests/unit/ParserTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,27 @@ public function testCreatingParserWithoutArgumentsAndCallAnalyse()
4040
{
4141
$parser = new Parser();
4242
$parser->analyse("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; InfoPath.1)");
43-
43+
44+
$this->assertTrue($parser->isBrowser('Internet Explorer', '=', '6.0'));
45+
}
46+
47+
public function testCreatingParserFromUserAgent()
48+
{
49+
$parser = Parser::fromUserAgent('Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; InfoPath.1)');
50+
4451
$this->assertTrue($parser instanceof \WhichBrowser\Parser);
4552

4653
$this->assertTrue($parser->isBrowser('Internet Explorer', '=', '6.0'));
4754
}
4855

56+
public function testCreatingParserFromHeaderArray()
57+
{
58+
$parser = Parser::fromHeaders([
59+
'User-Agent' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; InfoPath.1)'
60+
]);
61+
62+
$this->assertTrue($parser instanceof \WhichBrowser\Parser);
63+
64+
$this->assertTrue($parser->isBrowser('Internet Explorer', '=', '6.0'));
65+
}
4966
}

0 commit comments

Comments
 (0)