Skip to content

Commit

Permalink
more generics + better test
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaDafinser committed Apr 4, 2016
1 parent 85015c1 commit 6fa8fc6
Show file tree
Hide file tree
Showing 32 changed files with 812 additions and 874 deletions.
1 change: 1 addition & 0 deletions src/Provider/AbstractBrowscap.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ abstract class AbstractBrowscap extends AbstractProvider
'bot' => [
'name' => [
'/^General Crawlers/i',
'/^Generic/i',
],
],
];
Expand Down
4 changes: 2 additions & 2 deletions src/Provider/Http/DeviceAtlasCom.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,11 @@ protected function getResult($userAgent, array $headers)
*/
private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw)
{
if (isset($resultRaw->browserName) && $this->isRealResult($resultRaw->browserName) === true) {
if (isset($resultRaw->browserName) && $this->isRealResult($resultRaw->browserName, 'browser', 'name') === true) {
$browser->setName($resultRaw->browserName);
}

if (isset($resultRaw->browserVersion) && $this->isRealResult($resultRaw->browserVersion) === true) {
if (isset($resultRaw->browserVersion) && $this->isRealResult($resultRaw->browserVersion, 'browser', 'version') === true) {
$browser->getVersion()->setComplete($resultRaw->browserVersion);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Provider/Http/NeutrinoApiCom.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ class NeutrinoApiCom extends AbstractHttpProvider

'brand' => [
'/^Generic$/i',
'/^generic web browser$/i',
],

'model' => [
'/^Android/i',
'/^SmartTV$/i',
'/^Windows Phone/i',
'/^Windows Mobile/i',
'/^Firefox/i',
Expand Down Expand Up @@ -240,7 +240,7 @@ private function hydrateBot(Model\Bot $bot, stdClass $resultRaw)
*/
private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw)
{
if (isset($resultRaw->browser_name) && $this->isRealResult($resultRaw->browser_name) === true) {
if (isset($resultRaw->browser_name) && $this->isRealResult($resultRaw->browser_name, 'browser', 'name') === true) {
$browser->setName($resultRaw->browser_name);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Provider/Http/UdgerCom.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private function hydrateBot(Model\Bot $bot, stdClass $resultRaw)
*/
private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw)
{
if (isset($resultRaw->ua_family) && $this->isRealResult($resultRaw->ua_family) === true) {
if (isset($resultRaw->ua_family) && $this->isRealResult($resultRaw->ua_family, 'browser', 'name') === true) {
$browser->setName($resultRaw->ua_family);
}

Expand Down
11 changes: 9 additions & 2 deletions src/Provider/Http/WhatIsMyBrowserCom.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ class WhatIsMyBrowserCom extends AbstractHttpProvider
'/^Unknown Mobile Browser$/i',
'/^Unknown browser$/i',
'/^Webkit based browser$/i',
'/^a UNIX based OS$/i',
],
],

'operatingSystem' => [
'name' => [
'/^Smart TV$/i',
],
],

Expand Down Expand Up @@ -195,7 +202,7 @@ private function hasResult(stdClass $resultRaw)
return true;
}

if (isset($resultRaw->operating_system_name) && $this->isRealResult($resultRaw->operating_system_name) === true) {
if (isset($resultRaw->operating_system_name) && $this->isRealResult($resultRaw->operating_system_name, 'operatingSystem', 'name') === true) {
return true;
}

Expand Down Expand Up @@ -249,7 +256,7 @@ private function hydrateRenderingEngine(Model\RenderingEngine $engine, stdClass
*/
private function hydrateOperatingSystem(Model\OperatingSystem $os, stdClass $resultRaw)
{
if (isset($resultRaw->operating_system_name) && $this->isRealResult($resultRaw->operating_system_name) === true) {
if (isset($resultRaw->operating_system_name) && $this->isRealResult($resultRaw->operating_system_name, 'operatingSystem', 'name') === true) {
$os->setName($resultRaw->operating_system_name);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Provider/WhichBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private function hydrateBot(Model\Bot $bot, \WhichBrowser\Model\Browser $browser
*/
private function hydrateBrowser(Model\Browser $browser, \WhichBrowser\Model\Browser $browserRaw)
{
if ($this->isRealResult($browserRaw->getName()) === true) {
if ($this->isRealResult($browserRaw->getName(), 'browser', 'name') === true) {
$browser->setName($browserRaw->getName());

if ($this->isRealResult($browserRaw->getVersion()) === true) {
Expand Down
10 changes: 8 additions & 2 deletions src/Provider/Woothee.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ class Woothee extends AbstractProvider
'/^UNKNOWN$/i',
],

'device' => [
'type' => [
'/^misc$/i',
],
],

'bot' => [
'name' => [
'/^misc crawler$/i',
Expand Down Expand Up @@ -118,7 +124,7 @@ public function getParser()
*/
private function hasResult(array $resultRaw)
{
if (isset($resultRaw['category']) && $this->isRealResult($resultRaw['category'])) {
if (isset($resultRaw['category']) && $this->isRealResult($resultRaw['category'], 'device', 'type')) {
return true;
}

Expand Down Expand Up @@ -180,7 +186,7 @@ private function hydrateBrowser(Model\Browser $browser, array $resultRaw)
*/
private function hydrateDevice(Model\Device $device, array $resultRaw)
{
if (isset($resultRaw['category']) && $this->isRealResult($resultRaw['category']) === true) {
if (isset($resultRaw['category']) && $this->isRealResult($resultRaw['category'], 'device', 'type') === true) {
$device->setType($resultRaw['category']);
}
}
Expand Down
6 changes: 5 additions & 1 deletion src/Provider/Wurfl.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ class Wurfl extends AbstractProvider
'operatingSystem' => [
'name' => [
'/^Unknown$/i',
'/^en$/i',
'/^en_US$/i',
'/^Desktop$/i',
'/^Mobile$/i',
'/^Randomized by FreeSafeIP.com$/i',
],
],

Expand All @@ -86,7 +91,6 @@ class Wurfl extends AbstractProvider

'model' => [
'/^Android/i',
'/^SmartTV$/i',
'/^Windows Phone/i',
'/^Windows Mobile/i',
'/^Firefox/i',
Expand Down
10 changes: 8 additions & 2 deletions src/Provider/Zsxsoft.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ class Zsxsoft extends AbstractProvider
'/^Unknown$/i',
],

'browser' => [
'name' => [
'/^Mozilla Compatible$/i',
],
],

'device' => [
'model' => [
'/^Browser$/i',
Expand Down Expand Up @@ -123,7 +129,7 @@ public function getParser()
*/
private function hasResult(array $browser, array $os, array $device)
{
if (isset($browser['name']) && $this->isRealResult($browser['name'])) {
if (isset($browser['name']) && $this->isRealResult($browser['name'], 'browser', 'name')) {
return true;
}

Expand All @@ -149,7 +155,7 @@ private function hasResult(array $browser, array $os, array $device)
*/
private function hydrateBrowser(Model\Browser $browser, array $browserRaw)
{
if (isset($browserRaw['name']) && $this->isRealResult($browserRaw['name']) === true) {
if (isset($browserRaw['name']) && $this->isRealResult($browserRaw['name'], 'browser', 'name') === true) {
$browser->setName($browserRaw['name']);
}

Expand Down
130 changes: 41 additions & 89 deletions tests/unit/Provider/AbstractBrowscapTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
* @covers UserAgentParser\Provider\AbstractBrowscap
*/
class AbstractBrowscapTest extends AbstractProviderTestCase
class AbstractBrowscapTest extends AbstractProviderTestCase implements RequiredProviderTestInterface
{
/**
*
Expand Down Expand Up @@ -58,7 +58,7 @@ public function testConstructException()
]);
}

public function testName()
public function testGetName()
{
$provider = $this->getMockForAbstractClass('UserAgentParser\Provider\AbstractBrowscap', [
$this->getParser(),
Expand Down Expand Up @@ -151,6 +151,45 @@ public function testDetectionCapabilities()
], $provider->getDetectionCapabilities());
}

public function testIsRealResult()
{
$parser = $this->getParser();

$provider = new BrowscapPhp($parser);

/*
* general
*/
$this->assertIsRealResult($provider, false, 'unknown');
$this->assertIsRealResult($provider, true, 'unknown something');
$this->assertIsRealResult($provider, true, 'something unknown');

/*
* browser name
*/
$this->assertIsRealResult($provider, false, 'Default Browser', 'browser', 'name');
$this->assertIsRealResult($provider, true, 'Default Browser something', 'browser', 'name');
$this->assertIsRealResult($provider, true, 'something Default Browser', 'browser', 'name');

/*
* device model
*/
$this->assertIsRealResult($provider, false, 'general', 'device', 'model');
$this->assertIsRealResult($provider, false, 'general something', 'device', 'model');
$this->assertIsRealResult($provider, true, 'something general', 'device', 'model');

/*
* bot name
*/
$this->assertIsRealResult($provider, false, 'General Crawlers', 'bot', 'name');
$this->assertIsRealResult($provider, false, 'General Crawlers something', 'bot', 'name');
$this->assertIsRealResult($provider, true, 'something General Crawlers', 'bot', 'name');

$this->assertIsRealResult($provider, false, 'Generic', 'bot', 'name');
$this->assertIsRealResult($provider, false, 'Generic something', 'bot', 'name');
$this->assertIsRealResult($provider, true, 'something Generic', 'bot', 'name');
}

public function testParser()
{
$parser = $this->getParser();
Expand Down Expand Up @@ -532,91 +571,4 @@ public function testParseDeviceModelDefault2()

$this->assertProviderResult($result, $expectedResult);
}

/**
* @dataProvider isRealResult
*/
public function testRealResult($value, $group, $part, $expectedResult)
{
$class = new \ReflectionClass('UserAgentParser\Provider\AbstractBrowscap');
$method = $class->getMethod('isRealResult');
$method->setAccessible(true);

$result = new \stdClass();

$provider = $this->getMockForAbstractClass('UserAgentParser\Provider\AbstractBrowscap', [
$this->getParser($result),
]);

$actualResult = $method->invokeArgs($provider, [
$value,
$group,
$part,
]);

$this->assertEquals($expectedResult, $actualResult);
}

public function isRealResult()
{
return [
/*
* general
*/
[
'unknown',
null,
null,
false,
],

/*
* browserName
*/
[
'Default Browser',
'browser',
'name',
false,
],

/*
* deviceModel
*/
[
'general',
'device',
'model',
false,
],
[
'general Mobile Device',
'device',
'model',
false,
],
[
'Linux Desktop',
'device',
'model',
false,
],
[
'Windows Desktop',
'device',
'model',
false,
],

/*
* botName
*/
[
'General Crawlers',
'bot',
'name',
false,
],
];
}
}
6 changes: 3 additions & 3 deletions tests/unit/Provider/AbstractProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
class AbstractProviderTest extends AbstractProviderTestCase
{
public function testName()
public function testGetName()
{
$provider = $this->getMockForAbstractClass('UserAgentParser\Provider\AbstractProvider');

Expand All @@ -25,7 +25,7 @@ public function testName()
$this->assertEquals('MyName', $provider->getName());
}

public function testHomepage()
public function testGetHomepage()
{
$provider = $this->getMockForAbstractClass('UserAgentParser\Provider\AbstractProvider');

Expand All @@ -39,7 +39,7 @@ public function testHomepage()
$this->assertEquals('https://github.com/vendor/package', $provider->getHomepage());
}

public function testPackageName()
public function testGetPackageName()
{
$provider = $this->getMockForAbstractClass('UserAgentParser\Provider\AbstractProvider');

Expand Down
13 changes: 11 additions & 2 deletions tests/unit/Provider/AbstractProviderTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
use GuzzleHttp\HandlerStack;
use PHPUnit_Framework_TestCase;
use UserAgentParser\Model\UserAgent;
use UserAgentParser\Provider\AbstractProvider;

/**
*
*
* @author Martin Keckeis <[email protected]>
* @license MIT
*/
abstract class AbstractProviderTestCase extends PHPUnit_Framework_TestCase
{
public function assertProviderResult($result, array $expectedResult)
protected function assertProviderResult($result, array $expectedResult)
{
$this->assertInstanceOf('UserAgentParser\Model\UserAgent', $result);

Expand All @@ -25,6 +25,15 @@ public function assertProviderResult($result, array $expectedResult)
$this->assertEquals($result->toArray(), $expectedResult);
}

protected function assertIsRealResult(AbstractProvider $provider, $expected, $value, $group = null, $part = null)
{
$reflection = new \ReflectionClass($provider);
$method = $reflection->getMethod('isRealResult');
$method->setAccessible(true);

$this->assertSame($expected, $method->invoke($provider, $value, $group, $part), $value);
}

/**
*
* @return Client
Expand Down
Loading

0 comments on commit 6fa8fc6

Please sign in to comment.