From 9617f86ecd1159f68ca295b266076ff166031837 Mon Sep 17 00:00:00 2001 From: Martin Keckeis Date: Tue, 26 Apr 2016 13:06:32 +0200 Subject: [PATCH] update to latest deps (#85) * update to latest deps * switch zsxsoft UA * wurfl update to 1.7 * fixing wurfl tests * improve version * change deps --- .php_cs | 1 - README.md | 2 +- composer.json | 13 +- src/Model/Version.php | 8 +- .../Provider/Http/DeviceAtlasComTest.php | 2 +- .../Provider/JenssegersAgentTest.php | 4 +- tests/integration/Provider/WurflTest.php | 71 +++-- tests/integration/Provider/ZsxsoftTest.php | 74 ++--- tests/resources/wurfl/wurfl.xml | 277 +++--------------- tests/unit/Model/VersionTest.php | 21 ++ tests/unit/Provider/AbstractBrowscapTest.php | 29 +- 11 files changed, 193 insertions(+), 309 deletions(-) diff --git a/.php_cs b/.php_cs index 6ed47b4..ffbbfa9 100644 --- a/.php_cs +++ b/.php_cs @@ -14,7 +14,6 @@ $config->finder($finder); $config->level(Symfony\CS\FixerInterface::PSR2_LEVEL); $config->fixers([ //symfony - 'concat_without_spaces', 'double_arrow_multiline_whitespaces', 'duplicate_semicolon', 'empty_return', diff --git a/README.md b/README.md index 12e823a..6854682 100644 --- a/README.md +++ b/README.md @@ -163,7 +163,7 @@ But they require (always) a working internet connection. Here is a comparison matrix, with many analyzed UserAgent strings, to help you device which provider fits your needs. Every provider has it's strengh and weakness, so it will depend on your need, which one you should use. -[Go to the matrix](https://github.com/ThaDafinser/UserAgentParserMatrix) +[Go to the comparison](http://thadafinser.github.io/UserAgentParserComparison/) ### Overview diff --git a/composer.json b/composer.json index 179ca48..dc6a4e1 100644 --- a/composer.json +++ b/composer.json @@ -12,6 +12,7 @@ "UserAgent", "UserAgentParser", + "Sniffing", "Parser", "Detection", @@ -106,17 +107,15 @@ "browscap/browscap-php": "dev-master", "donatj/phpuseragentparser": "^0.5.0", "jenssegers/agent": "^2.3", - "mobiledetect/mobiledetectlib": ">2.7.5,<3", + "mobiledetect/mobiledetectlib": "^2.7.5", "piwik/device-detector": "^3.6", "sinergi/browser-detector": "^6.0", - "ua-parser/uap-php": ">=3.4.3,<4.0", - "whichbrowser/parser": ">=2.0.10,<3.0", + "ua-parser/uap-php": "^3.4.3", + "whichbrowser/parser": "^2.0.10", "woothee/woothee": "^1.2", - "mimmi20/wurfl": "^1.6.4", - "mimmi20/wurfl-storage": "^1.0.3", - "mimmi20/wurfl-virtual-capability": "^1.6.4", + "mimmi20/wurfl": "^1.7.1.1", - "zsxsoft/php-useragent": ">=1.1,<1.2" + "zsxsoft/php-useragent": ">=1.2,<1.4" } } diff --git a/src/Model/Version.php b/src/Model/Version.php index bbd2a54..f12a1e7 100644 --- a/src/Model/Version.php +++ b/src/Model/Version.php @@ -152,7 +152,7 @@ public function setComplete($complete) { // check if the version has only 0 -> so no real result // maybe move this out to the Providers itself? - $left = preg_replace('/[0.]/', '', $complete); + $left = preg_replace('/[0._]/', '', $complete); if ($left === '') { $complete = null; } @@ -240,9 +240,9 @@ private function getCompleteParts($complete) ]; // only digits - preg_match("/\d+(?:\.*\d*)*/", $complete, $result); + preg_match("/\d+(?:[._]*\d*)*/", $complete, $result); if (count($result) > 0) { - $parts = explode('.', $result[0]); + $parts = preg_split("/[._]/", $result[0]); if (isset($parts[0]) && $parts[0] != '') { $versionParts['major'] = (int) $parts[0]; @@ -256,7 +256,7 @@ private function getCompleteParts($complete) } // grab alias - $result = preg_split("/\d+(?:\.*\d*)*/", $complete); + $result = preg_split("/\d+(?:[._]*\d*)*/", $complete); foreach ($result as $row) { $row = trim($row); diff --git a/tests/integration/Provider/Http/DeviceAtlasComTest.php b/tests/integration/Provider/Http/DeviceAtlasComTest.php index 9901d35..941dfbd 100644 --- a/tests/integration/Provider/Http/DeviceAtlasComTest.php +++ b/tests/integration/Provider/Http/DeviceAtlasComTest.php @@ -74,7 +74,7 @@ public function testRealResultDevice() 'minor' => 0, 'patch' => null, - 'alias' => '_', + 'alias' => null, 'complete' => '5_0', ], diff --git a/tests/integration/Provider/JenssegersAgentTest.php b/tests/integration/Provider/JenssegersAgentTest.php index 8a363f2..1315e35 100644 --- a/tests/integration/Provider/JenssegersAgentTest.php +++ b/tests/integration/Provider/JenssegersAgentTest.php @@ -133,10 +133,10 @@ public function testRealResultDevice() 'name' => 'iOS', 'version' => [ 'major' => 5, - 'minor' => null, + 'minor' => 0, 'patch' => null, - 'alias' => '_', + 'alias' => null, 'complete' => '5_0', ], diff --git a/tests/integration/Provider/WurflTest.php b/tests/integration/Provider/WurflTest.php index b93db3c..d4e9c77 100644 --- a/tests/integration/Provider/WurflTest.php +++ b/tests/integration/Provider/WurflTest.php @@ -2,13 +2,16 @@ namespace UserAgentParserTest\Integration\Provider; use UserAgentParser\Provider\Wurfl; +use Wurfl\Handlers; +use Wurfl\Handlers\Chain\UserAgentHandlerChain; +use Wurfl\Handlers\Normalizer\Generic; +use Wurfl\Handlers\Normalizer\UserAgentNormalizer; /** - * * * @author Martin Keckeis * @license MIT - * + * * @coversNothing */ class WurflTest extends AbstractProviderTestCase @@ -29,7 +32,39 @@ private function getWurfl() // cache $cacheStorage = \Wurfl\Storage\Factory::create($wurflConfig->cache); - return new \Wurfl\Manager($wurflConfig, $persistenceStorage, $cacheStorage); + // chain + $genericNormalizers = new UserAgentNormalizer([ + new Generic\UCWEB(), + new Generic\UPLink(), + new Generic\SerialNumbers(), + new Generic\LocaleRemover(), + new Generic\CFNetwork(), + new Generic\BlackBerry(), + new Generic\Android(), + new Generic\TransferEncoding(), + ]); + + $userAgentHandlerChain = new UserAgentHandlerChain(); + $userAgentHandlerChain->addUserAgentHandler(new Handlers\XboxHandler($genericNormalizers)); + $userAgentHandlerChain->addUserAgentHandler(new Handlers\BotCrawlerTranscoderHandler($genericNormalizers)); + // $userAgentHandlerChain->addUserAgentHandler(new Handlers\CatchAllMozillaHandler($genericNormalizers)); + $userAgentHandlerChain->addUserAgentHandler(new Handlers\CatchAllRisHandler($genericNormalizers)); + + $userAgentHandlerChain->setLogger($wurflConfig->getLogger()); + foreach ($userAgentHandlerChain->getHandlers() as $handler) { + /* @var $handler \Wurfl\Handlers\AbstractHandler */ + $handler->setLogger($wurflConfig->getLogger()) + ->setPersistenceProvider($persistenceStorage); + } + + $manager = new \Wurfl\Manager($wurflConfig, $persistenceStorage, $cacheStorage); + + $reflection = new \ReflectionClass($manager); + $property = $reflection->getProperty('userAgentHandlerChain'); + $property->setAccessible(true); + $property->setValue($manager, $userAgentHandlerChain); + + return $manager; } public function testMethodParse() @@ -59,7 +94,7 @@ public function testMethodsResult() $parser = $provider->getParser(); /* @var $result \Wurfl\CustomDevice */ - $result = $parser->getDeviceForUserAgent('A real user agent...'); + $result = $parser->getDeviceForUserAgent('A real user agent...maybe we need the complete file?'); $this->assertInstanceOf('Wurfl\CustomDevice', $result); @@ -155,13 +190,13 @@ public function testRealResultBot() $this->assertArrayHasKey('all', $rawResult); $virtual = $rawResult['virtual']; - $this->assertGreaterThan(21, $virtual); + $this->assertCount(22, $virtual); $this->assertArrayHasKey('is_robot', $virtual); $this->assertArrayHasKey('is_smartphone', $virtual); $this->assertArrayHasKey('complete_device_name', $virtual); $all = $rawResult['all']; - $this->assertCount(530, $all); + $this->assertCount(511, $all); $this->assertArrayHasKey('is_wireless_device', $all); $this->assertArrayHasKey('table_support', $all); $this->assertArrayHasKey('resolution_width', $all); @@ -171,18 +206,18 @@ public function testRealResultDevice() { $provider = new Wurfl($this->getWurfl()); - $result = $provider->parse('Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A538a Safari/419.3'); + $result = $provider->parse('Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; Xbox)'); $this->assertEquals([ 'browser' => [ - 'name' => 'Mobile Safari', + 'name' => 'IE', 'version' => [ - 'major' => 3, + 'major' => 9, 'minor' => 0, 'patch' => null, 'alias' => null, - 'complete' => '3.0', + 'complete' => '9.0', ], ], 'renderingEngine' => [ @@ -198,24 +233,24 @@ public function testRealResultDevice() ], ], 'operatingSystem' => [ - 'name' => 'iOS', + 'name' => 'Windows', 'version' => [ - 'major' => null, + 'major' => 7, 'minor' => null, 'patch' => null, 'alias' => null, - 'complete' => null, + 'complete' => '7', ], ], 'device' => [ - 'model' => 'iPhone', - 'brand' => 'Apple', - 'type' => 'Feature Phone', + 'model' => 'Xbox 360', + 'brand' => 'Microsoft', + 'type' => 'Smart-TV', - 'isMobile' => true, - 'isTouch' => true, + 'isMobile' => null, + 'isTouch' => null, ], 'bot' => [ 'isBot' => null, diff --git a/tests/integration/Provider/ZsxsoftTest.php b/tests/integration/Provider/ZsxsoftTest.php index c590eff..4fb0775 100644 --- a/tests/integration/Provider/ZsxsoftTest.php +++ b/tests/integration/Provider/ZsxsoftTest.php @@ -27,18 +27,18 @@ public function testRealResultDevice() { $provider = new Zsxsoft(); - $result = $provider->parse('Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3'); + $result = $provider->parse('Mozilla/5.0 (Linux; Android 5.0.1; Nexus 7 Build/LRX22C) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.59 Safari/537.36 OPR/26.0.1656.87080'); $this->assertEquals([ 'browser' => [ - 'name' => 'Safari', + 'name' => 'Opera', 'version' => [ - 'major' => 5, - 'minor' => 1, - 'patch' => null, + 'major' => 26, + 'minor' => 0, + 'patch' => 1656, 'alias' => null, - 'complete' => '5.1', + 'complete' => '26.0.1656.87080', ], ], 'renderingEngine' => [ @@ -54,20 +54,20 @@ public function testRealResultDevice() ], ], 'operatingSystem' => [ - 'name' => 'Mac OS X', + 'name' => 'Android', 'version' => [ - 'major' => null, - 'minor' => null, - 'patch' => null, + 'major' => 5, + 'minor' => 0, + 'patch' => 1, 'alias' => null, - 'complete' => null, + 'complete' => '5.0.1', ], ], 'device' => [ - 'model' => 'iPhone iOS 5.0', - 'brand' => 'Apple', + 'model' => 'Nexus 7', + 'brand' => 'Google', 'type' => null, 'isMobile' => null, @@ -86,43 +86,43 @@ public function testRealResultDevice() $rawResult = $result->getProviderResultRaw(); $this->assertEquals([ 'browser' => [ - 'link' => 'http://www.apple.com/safari/', - 'title' => 'Safari 5.1', - 'name' => 'Safari', - 'version' => '5.1', - 'code' => 'safari', - 'image' => 'img/16/browser/safari.png', + 'link' => 'http://www.opera.com/', + 'title' => 'Opera 26.0.1656.87080', + 'name' => 'Opera', + 'version' => '26.0.1656.87080', + 'code' => 'opera-1', + 'image' => 'img/16/browser/opera-1.png', ], 'os' => [ - 'link' => 'http://www.apple.com/macosx/', - 'name' => 'Mac OS X', - 'version' => '', - 'code' => 'mac-3', + 'link' => 'http://www.android.com/', + 'name' => 'Android', + 'version' => '5.0.1', + 'code' => 'android', 'x64' => false, - 'title' => 'Mac OS X', + 'title' => 'Android 5.0.1', 'type' => 'os', 'dir' => 'os', - 'image' => 'img/16/os/mac-3.png', + 'image' => 'img/16/os/android.png', ], 'device' => [ - 'link' => 'http://www.apple.com/iphone', - 'title' => 'Apple iPhone iOS 5.0', - 'model' => 'iPhone iOS 5.0', - 'brand' => 'Apple', - 'code' => 'iphone', + 'link' => 'https://www.google.com/nexus/', + 'title' => 'Google Nexus 7', + 'model' => 'Nexus 7', + 'brand' => 'Google', + 'code' => 'google-nexusone', 'dir' => 'device', 'type' => 'device', - 'image' => 'img/16/device/iphone.png', + 'image' => 'img/16/device/google-nexusone.png', ], 'platform' => [ - 'link' => 'http://www.apple.com/iphone', - 'title' => 'Apple iPhone iOS 5.0', - 'model' => 'iPhone iOS 5.0', - 'brand' => 'Apple', - 'code' => 'iphone', + 'link' => 'https://www.google.com/nexus/', + 'title' => 'Google Nexus 7', + 'model' => 'Nexus 7', + 'brand' => 'Google', + 'code' => 'google-nexusone', 'dir' => 'device', 'type' => 'device', - 'image' => 'img/16/device/iphone.png', + 'image' => 'img/16/device/google-nexusone.png', ], ], $rawResult); } diff --git a/tests/resources/wurfl/wurfl.xml b/tests/resources/wurfl/wurfl.xml index 48c893c..a3566d2 100644 --- a/tests/resources/wurfl/wurfl.xml +++ b/tests/resources/wurfl/wurfl.xml @@ -612,6 +612,9 @@ + + + @@ -1058,6 +1061,7 @@ + @@ -1070,6 +1074,8 @@ + + @@ -1081,8 +1087,7 @@ - - + @@ -1102,255 +1107,59 @@ - - - - - - - - - - - - - - - - + + + + + + - - - - + + - - - - - - + + - - - + + + + - - + - - + + + + - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + - - + - - - - - - - - - - - - - - - - - - - - - - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + diff --git a/tests/unit/Model/VersionTest.php b/tests/unit/Model/VersionTest.php index feb90d2..ed90ed6 100644 --- a/tests/unit/Model/VersionTest.php +++ b/tests/unit/Model/VersionTest.php @@ -66,9 +66,17 @@ public function testCompleteFilterZero() { $version = new Version(); + // 0 gets filtered + $version->setComplete('0'); + $this->assertNull($version->getComplete()); + // 0.0 gets filtered $version->setComplete('0.0'); $this->assertNull($version->getComplete()); + + // 0_0 gets filtered + $version->setComplete('0_0'); + $this->assertNull($version->getComplete()); } public function testCompleteOnlyAlias() @@ -123,6 +131,19 @@ public function testCompleteWithNotAllowedAlpha() $this->assertNull($version->getAlias()); } + public function testCompleteWithUnderscore() + { + $version = new Version(); + + $version->setComplete('6_5_4'); + + $this->assertEquals('6_5_4', $version->getComplete()); + $this->assertEquals(6, $version->getMajor()); + $this->assertEquals(5, $version->getMinor()); + $this->assertEquals(4, $version->getPatch()); + $this->assertNull($version->getAlias()); + } + public function testToArray() { $version = new Version(); diff --git a/tests/unit/Provider/AbstractBrowscapTest.php b/tests/unit/Provider/AbstractBrowscapTest.php index 5fc6e7e..20ed2c1 100644 --- a/tests/unit/Provider/AbstractBrowscapTest.php +++ b/tests/unit/Provider/AbstractBrowscapTest.php @@ -4,12 +4,11 @@ use UserAgentParser\Provider\BrowscapPhp; /** - * * * @author Martin Keckeis * @license MIT - * - * @covers UserAgentParser\Provider\AbstractBrowscap + * + * @covers UserAgentParser\Provider\AbstractBrowscap */ class AbstractBrowscapTest extends AbstractProviderTestCase implements RequiredProviderTestInterface { @@ -46,9 +45,31 @@ private function getParser(\stdClass $result = null, $date = null) } /** - * Provider no result + * Warm cache is missing! + * + * @expectedException \UserAgentParser\Exception\InvalidArgumentException + * @expectedExceptionMessage You need to warm-up the cache first to use this provider + */ + public function testConstructExceptionNOWarmCache() + { + $cache = $this->getMock('BrowscapPHP\Cache\BrowscapCache', [], [], '', false); + + $parser = $this->getMock('BrowscapPHP\Browscap'); + $parser->expects($this->any()) + ->method('getCache') + ->will($this->returnValue($cache)); + + $provider = $this->getMockForAbstractClass('UserAgentParser\Provider\AbstractBrowscap', [ + $parser, + 'anotherExceptedType', + ]); + } + + /** + * Different type * * @expectedException \UserAgentParser\Exception\InvalidArgumentException + * @expectedExceptionMessage Expected the "anotherExceptedType" data file. Instead got the "" data file */ public function testConstructException() {