2
0

UserAgentTest.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Http_UserAgent
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: JsonTest.php 12081 2008-10-22 19:07:55Z norm2782 $
  21. */
  22. require_once 'Zend/Config.php';
  23. require_once 'Zend/Http/UserAgent.php';
  24. require_once 'Zend/Http/UserAgent/Mobile.php';
  25. require_once 'Zend/Http/UserAgent/Storage/NonPersistent.php';
  26. require_once dirname(__FILE__) . '/TestAsset/TestPluginLoader.php';
  27. require_once dirname(__FILE__) . '/TestAsset/DesktopDevice.php';
  28. require_once dirname(__FILE__) . '/TestAsset/InvalidDevice.php';
  29. require_once dirname(__FILE__) . '/TestAsset/PopulatedStorage.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Http_UserAgent
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Http
  37. * @group Zend_Http_UserAgent
  38. */
  39. class Zend_Http_UserAgentTest extends PHPUnit_Framework_TestCase
  40. {
  41. public function setUp()
  42. {
  43. $this->server = array();
  44. $this->server['os'] = 'Windows_NT';
  45. $this->server['http_accept'] = '*/*';
  46. $this->server['http_accept_language'] = 'fr-FR';
  47. $this->server['http_accept_encoding'] = 'gzip, deflate';
  48. $this->server['http_user_agent'] = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)';
  49. $this->server['http_host'] = 'zfmobile';
  50. $this->server['http_connection'] = 'Keep-Alive';
  51. $this->server['http_cookie'] = 'ZDEDebuggerPresent=php,phtml,php3';
  52. $this->server['server_signature'] = '';
  53. $this->server['server_software'] = 'Apache/2.2.12 (Win32) mod_ssl/2.2.12 OpenSSL/0.9.8k';
  54. $this->server['server_name'] = 'zfmobile';
  55. $this->server['server_addr'] = '127.0.0.1';
  56. $this->server['server_port'] = '80';
  57. $this->server['remote_addr'] = '127.0.0.1';
  58. $this->server['server_protocol'] = 'HTTP/1.1';
  59. $this->config = array(
  60. 'server' => &$this->server,
  61. 'storage' => array(
  62. 'adapter' => 'NonPersistent',
  63. ),
  64. );
  65. }
  66. public function testMatchUserAgentSimple()
  67. {
  68. $config = $this->config;
  69. $config['server']['server_software'] = 'Apache/2';
  70. $userAgent = new Zend_Http_UserAgent($config);
  71. $device = $userAgent->getDevice();
  72. $this->assertEquals('desktop', $userAgent->getBrowserType());
  73. $this->assertEquals('Internet Explorer', $device->getFeature('browser_name'));
  74. $this->assertEquals('7.0', $device->getFeature('browser_version'));
  75. $this->assertEquals('Internet Explorer', $device->getFeature('browser_compatibility'));
  76. $this->assertEquals('MSIE', $device->getFeature('browser_engine'));
  77. $this->assertEquals('Windows XP', $device->getFeature('device_os_name'));
  78. $this->assertEquals('Windows NT 5.1', $device->getFeature('device_os_token'));
  79. $this->assertEquals('apache', $device->getFeature('server_os'));
  80. $this->assertEquals('2', $device->getFeature('server_os_version'));
  81. }
  82. public function testMatchUserAgentServer()
  83. {
  84. $config = $this->config;
  85. $config['server']['os'] = 'Windows_NT';
  86. $config['server']['http_accept'] = '*/*';
  87. $config['server']['http_user_agent'] = 'Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 5.1)';
  88. $config['server']['server_software'] = 'Apache/99';
  89. $config['user_agent'] = $config['server']["http_user_agent"];
  90. $userAgent = new Zend_Http_UserAgent($config);
  91. $device = $userAgent->getDevice();
  92. $this->assertEquals('desktop', $userAgent->getBrowserType());
  93. $this->assertEquals('Internet Explorer', $device->getFeature('browser_name'));
  94. $this->assertEquals('9.0', $device->getFeature('browser_version'));
  95. $this->assertEquals('Internet Explorer', $device->getFeature('browser_compatibility'));
  96. $this->assertEquals('MSIE', $device->getFeature('browser_engine'));
  97. $this->assertEquals('Windows XP', $device->getFeature('device_os_name'));
  98. $this->assertEquals('Windows NT 5.1', $device->getFeature('device_os_token'));
  99. $this->assertEquals('apache', $device->getFeature('server_os'));
  100. $this->assertEquals('99', $device->getFeature('server_os_version'));
  101. }
  102. public function testUserAgentDefineIdentificationSequence()
  103. {
  104. $browscap = ini_get('browscap');
  105. if (empty($browscap)) {
  106. $this->markTestSkipped('Depends on browscap support');
  107. }
  108. $config = $this->config;
  109. $config['user_agent'] = 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A102 Safari/419.3';
  110. $userAgent = new Zend_Http_UserAgent($config);
  111. $device = $userAgent->getDevice();
  112. $this->assertType('Zend_Http_UserAgent_Mobile', $device);
  113. $this->assertEquals('mobile', $userAgent->getBrowserType());
  114. $this->assertEquals('iPhone', $device->getFeature('mobile_browser'));
  115. $this->assertRegexp('/iPhone/', $device->getFeature('device_os'));
  116. }
  117. public function testUserAgentDefineStorage()
  118. {
  119. $config = array(
  120. 'storage' => array('adapter' => 'NonPersistent'),
  121. 'server' => $this->server,
  122. );
  123. $oUserAgent = new Zend_Http_UserAgent($config);
  124. $browser = $oUserAgent->getUserAgent();
  125. $this->assertType('Zend_Http_UserAgent_Storage_NonPersistent', $oUserAgent->getStorage($browser));
  126. }
  127. public function testUserAgentFeatureAdapter()
  128. {
  129. $config = $this->config;
  130. $config['mobile']['features']['path'] = dirname(__FILE__) . '/TestAsset/Device/Browser/Features/Adapter.php';
  131. $config['mobile']['features']['classname'] = 'Device_Browser_Features_Adapter';
  132. $config['user_agent'] = 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleW1ebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A102 Safari/419.3';
  133. $userAgent = new Zend_Http_UserAgent($config);
  134. $config = $userAgent->getConfig();
  135. $this->assertContains('Device/Browser/Features/Adapter.php', $config['mobile']['features']['path']);
  136. }
  137. public function testSetDefaultConfigAlone()
  138. {
  139. $config['server'] = $this->server;
  140. $userAgent = new Zend_Http_UserAgent($config);
  141. $config = $userAgent->getConfig();
  142. $this->assertEquals(Zend_Http_UserAgent::DEFAULT_IDENTIFICATION_SEQUENCE, $config['identification_sequence']);
  143. $this->assertEquals(Zend_Http_UserAgent::DEFAULT_PERSISTENT_STORAGE_ADAPTER, $config['storage']['adapter']);
  144. }
  145. public function testSetDefaultConfigStorage()
  146. {
  147. $config = array('identification_sequence' => 'Test');
  148. $oUserAgent = new Zend_Http_UserAgent($config);
  149. $test = $oUserAgent->getConfig();
  150. $this->assertEquals('Test', $test['identification_sequence']);
  151. $this->assertEquals(Zend_Http_UserAgent::DEFAULT_PERSISTENT_STORAGE_ADAPTER, $test['storage']['adapter']);
  152. }
  153. public function testSetDefaultConfigBoth()
  154. {
  155. $config = array(
  156. 'identification_sequence' => 'Test',
  157. 'storage' => array('adapter' => 'NonPersistent'),
  158. );
  159. $oUserAgent = new Zend_Http_UserAgent($config);
  160. $test = $oUserAgent->getConfig();
  161. $this->assertEquals('Test', $test['identification_sequence']);
  162. $this->assertEquals('NonPersistent', $test['storage']['adapter']);
  163. }
  164. public function testDeviceClassNameMatchesBrowserTypeIfUserAgentMatches()
  165. {
  166. $browscap = ini_get('browscap');
  167. if (empty($browscap)) {
  168. $this->markTestSkipped('Depends on browscap support');
  169. }
  170. $this->config['browser_type'] = 'MoBiLe';
  171. $this->config['user_agent'] = 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleW1ebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/4A102 Safari/419.3';
  172. $userAgent = new Zend_Http_UserAgent($this->config);
  173. $className = get_class($userAgent->getDevice());
  174. $this->assertEquals('Zend_Http_UserAgent_Mobile', $className);
  175. }
  176. public function testDeviceClassNameMatchesDesktopTypeIfUserAgentDoesNotMatch()
  177. {
  178. $config = array(
  179. 'browser_type' => 'MoBiLe',
  180. );
  181. $userAgent = new Zend_Http_UserAgent($config);
  182. $className = get_class($userAgent->getDevice());
  183. $this->assertEquals('Zend_Http_UserAgent_Desktop', $className);
  184. }
  185. public function testUserAgentFromServerSuperglobalWhenNotProvided()
  186. {
  187. $_SERVER['HTTP_USER_AGENT'] = 'UserAgentTest2';
  188. $ua = new Zend_Http_UserAgent();
  189. $this->assertEquals('UserAgentTest2', $ua->getServerValue('http_user_agent'));
  190. $this->assertEquals('UserAgentTest2', $ua->getUserAgent());
  191. }
  192. public function testAllowsPassingUserAgentDirectly()
  193. {
  194. $this->config['user_agent'] = 'UserAgentTest2';
  195. $ua = new Zend_Http_UserAgent($this->config);
  196. $this->assertEquals('UserAgentTest2', $ua->getUserAgent());
  197. }
  198. public function testAllowsSettingUserAgentManually()
  199. {
  200. $ua = new Zend_Http_UserAgent();
  201. $ua->setUserAgent('userAgentTest');
  202. $this->assertEquals('userAgentTest', $ua->getServerValue('HTTP_USER_AGENT'));
  203. $this->assertEquals('userAgentTest', $ua->getUserAgent());
  204. }
  205. public function testUsesHttpAcceptConstantValueByDefault()
  206. {
  207. unset($this->server['http_accept']);
  208. $ua = new Zend_Http_UserAgent();
  209. $this->assertEquals(Zend_Http_UserAgent::DEFAULT_HTTP_ACCEPT, $ua->getHttpAccept());
  210. $this->assertEquals(Zend_Http_UserAgent::DEFAULT_HTTP_ACCEPT, $ua->getServerValue('HTTP_ACCEPT'));
  211. }
  212. public function testUsesServerHttpAcceptValueWhenPresent()
  213. {
  214. $_SERVER['HTTP_ACCEPT'] = 'HttpAcceptTest2';
  215. $ua = new Zend_Http_UserAgent();
  216. $this->assertEquals('HttpAcceptTest2', $ua->getHttpAccept());
  217. $this->assertEquals('HttpAcceptTest2', $ua->getServerValue('HTTP_ACCEPT'));
  218. }
  219. public function testAllowsPassingHttpAcceptValueViaConfiguration()
  220. {
  221. $this->config['http_accept'] = 'HttpAcceptTest';
  222. $ua = new Zend_Http_UserAgent($this->config);
  223. $this->assertEquals('HttpAcceptTest', $ua->getHttpAccept());
  224. $this->assertEquals('HttpAcceptTest', $ua->getServerValue('HTTP_ACCEPT'));
  225. }
  226. public function testAllowsSettingHttpAcceptManually()
  227. {
  228. $ua = new Zend_Http_UserAgent();
  229. $ua->setHttpAccept('httpAcceptTest');
  230. $this->assertEquals('httpAcceptTest', $ua->getHttpAccept());
  231. $this->assertEquals('httpAcceptTest', $ua->getServerValue('HTTP_ACCEPT'));
  232. }
  233. public function testCanSetConfigWithConfigObject()
  234. {
  235. $config = new Zend_Config($this->config);
  236. $ua = new Zend_Http_UserAgent($config);
  237. $test = $ua->getConfig();
  238. $this->assertEquals($config->storage->adapter, $test['storage']['adapter']);
  239. }
  240. public function testCanSetConfigWithTraversableObject()
  241. {
  242. $config = new ArrayObject($this->config);
  243. $ua = new Zend_Http_UserAgent($config);
  244. $test = $ua->getConfig();
  245. $this->assertEquals($config['storage'], $test['storage']);
  246. }
  247. public function invalidConfigs()
  248. {
  249. return array(
  250. array(true),
  251. array(1),
  252. array(1.0),
  253. array(new stdClass),
  254. );
  255. }
  256. /**
  257. * @dataProvider invalidConfigs
  258. */
  259. public function testSettingConfigWithInvalidTypeRaisesException($arg)
  260. {
  261. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'expected array');
  262. $ua = new Zend_Http_UserAgent($arg);
  263. }
  264. public function testAllowsSettingServerWithArrayObject()
  265. {
  266. $server = new ArrayObject($this->server);
  267. $ua = new Zend_Http_UserAgent(array('server' => $server));
  268. $this->assertEquals($server['os'], $ua->getServerValue('os'));
  269. }
  270. public function testAllowsSettingServerWithTraversableObject()
  271. {
  272. $server = new ArrayIterator($this->server);
  273. $ua = new Zend_Http_UserAgent(array('server' => $server));
  274. $this->assertEquals($this->server['os'], $ua->getServerValue('os'));
  275. }
  276. /**
  277. * @dataProvider invalidConfigs
  278. */
  279. public function testSettingServerWithInvalidTypeRaisesException($arg)
  280. {
  281. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'array or object implementing Traversable');
  282. $ua = new Zend_Http_UserAgent(array('server' => $arg));
  283. }
  284. public function testAllowsSettingPluginLoaderUsingClassname()
  285. {
  286. $ua = new Zend_Http_UserAgent();
  287. $ua->setPluginLoader('device', 'Zend_Http_TestAsset_TestPluginLoader');
  288. $loader = $ua->getPluginLoader('device');
  289. $this->assertType('Zend_Http_TestAsset_TestPluginLoader', $loader);
  290. }
  291. public function testSpecifyingInvalidPluginLoaderClassNameRaisesException()
  292. {
  293. $ua = new Zend_Http_UserAgent();
  294. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'extending Zend_Loader_PluginLoader');
  295. $ua->setPluginLoader('device', 'Zend_Http_TestAsset_InvalidPluginLoader');
  296. }
  297. public function invalidLoaders()
  298. {
  299. return array(
  300. array(true),
  301. array(1),
  302. array(1.0),
  303. array(array()),
  304. );
  305. }
  306. /**
  307. * @dataProvider invalidLoaders
  308. */
  309. public function testSpecifyingInvalidTypeToPluginLoaderRaisesException($arg)
  310. {
  311. $ua = new Zend_Http_UserAgent();
  312. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'class or object');
  313. $ua->setPluginLoader('device', $arg);
  314. }
  315. public function testSpecifyingNonPluginLoaderObjectRaisesException()
  316. {
  317. $ua = new Zend_Http_UserAgent();
  318. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'extending Zend_Loader_PluginLoader');
  319. $ua->setPluginLoader('device', $this);
  320. }
  321. public function testSpecifyingInvalidTypeWhenSettingPluginLoaderRaisesException()
  322. {
  323. $ua = new Zend_Http_UserAgent();
  324. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'plugin loader type');
  325. $ua->setPluginLoader('__bogus__', new Zend_Loader_PluginLoader());
  326. }
  327. public function testAllowsSpecifyingPluginLoadersViaConfiguration()
  328. {
  329. $this->config['plugin_loader'] = array(
  330. 'device' => 'Zend_Http_TestAsset_TestPluginLoader',
  331. 'storage' => 'Zend_Http_TestAsset_TestPluginLoader',
  332. );
  333. $ua = new Zend_Http_UserAgent($this->config);
  334. $deviceLoader = $ua->getPluginLoader('device');
  335. $this->assertType('Zend_Http_TestAsset_TestPluginLoader', $deviceLoader);
  336. $storageLoader = $ua->getPluginLoader('storage');
  337. $this->assertType('Zend_Http_TestAsset_TestPluginLoader', $storageLoader);
  338. $this->assertNotSame($deviceLoader, $storageLoader);
  339. }
  340. public function testAllowsSpecifyingCustomDeviceClassesViaConfiguration()
  341. {
  342. $this->config['desktop'] = array(
  343. 'device' => array(
  344. 'classname' => 'Zend_Http_TestAsset_DesktopDevice',
  345. ),
  346. );
  347. $this->config['user_agent'] = 'desktop';
  348. $ua = new Zend_Http_UserAgent($this->config);
  349. $device = $ua->getDevice();
  350. $this->assertType('Zend_Http_TestAsset_DesktopDevice', $device);
  351. }
  352. public function testAllowsSpecifyingCustomDeviceViaPrefixPath()
  353. {
  354. $this->config['desktop'] = array(
  355. 'device' => array(
  356. 'path' => dirname(__FILE__) . '/TestAsset/Device',
  357. 'prefix' => 'Zend_Http_TestAsset_Device',
  358. ),
  359. );
  360. $this->config['user_agent'] = 'desktop';
  361. $ua = new Zend_Http_UserAgent($this->config);
  362. $device = $ua->getDevice();
  363. $this->assertType('Zend_Http_TestAsset_Device_Desktop', $device);
  364. }
  365. public function testShouldRaiseExceptionOnInvalidDeviceClass()
  366. {
  367. $this->config['desktop'] = array(
  368. 'device' => array(
  369. 'classname' => 'Zend_Http_TestAsset_InvalidDevice',
  370. ),
  371. );
  372. $this->config['user_agent'] = 'desktop';
  373. $ua = new Zend_Http_UserAgent($this->config);
  374. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'Zend_Http_UserAgent_Device');
  375. $ua->getDevice();
  376. }
  377. public function testStorageContainsSerializedUserAgent()
  378. {
  379. $this->config['desktop'] = array(
  380. 'device' => array(
  381. 'classname' => 'Zend_Http_TestAsset_DesktopDevice',
  382. ),
  383. );
  384. $this->config['user_agent'] = 'desktop';
  385. $ua = new Zend_Http_UserAgent($this->config);
  386. // prime storage by retrieving device
  387. $device = $ua->getDevice();
  388. $storage = $ua->getStorage();
  389. $serialized = $storage->read();
  390. $test = unserialize($serialized);
  391. $this->assertEquals($ua->getBrowserType(), $test['browser_type']);
  392. $this->assertEquals($ua->getConfig(), $test['config']);
  393. $this->assertEquals('Zend_Http_TestAsset_DesktopDevice', $test['device_class']);
  394. $this->assertEquals($ua->getUserAgent(), $test['user_agent']);
  395. $this->assertEquals($ua->getHttpAccept(), $test['http_accept']);
  396. $test = unserialize($test['device']);
  397. $this->assertEquals($device->getAllFeatures(), $test['_aFeatures']);
  398. $this->assertEquals($device->getBrowser(), $test['_browser']);
  399. $this->assertEquals($device->getBrowserVersion(), $test['_browserVersion']);
  400. $this->assertEquals($device->getUserAgent(), $test['_userAgent']);
  401. $this->assertEquals($device->getImages(), $test['_images']);
  402. }
  403. public function testCanPopulateFromStorage()
  404. {
  405. $this->config['storage']['adapter'] = 'Zend_Http_TestAsset_PopulatedStorage';
  406. $this->config['user_agent'] = 'desktop';
  407. $ua = new Zend_Http_UserAgent($this->config);
  408. $storage = $ua->getStorage();
  409. $this->assertType('Zend_Http_TestAsset_PopulatedStorage', $storage);
  410. $device = $ua->getDevice();
  411. $this->assertType('Zend_Http_TestAsset_DesktopDevice', $device);
  412. }
  413. public function testCanClearStorage()
  414. {
  415. $this->config['desktop'] = array(
  416. 'device' => array(
  417. 'classname' => 'Zend_Http_TestAsset_DesktopDevice',
  418. ),
  419. );
  420. $this->config['user_agent'] = 'desktop';
  421. $ua = new Zend_Http_UserAgent($this->config);
  422. // Prime storage by retrieving device
  423. $device = $ua->getDevice();
  424. $storage = $ua->getStorage();
  425. $this->assertType('Zend_Http_UserAgent_Storage', $storage);
  426. $this->assertFalse($storage->isEmpty());
  427. $ua->clearStorage();
  428. $this->assertTrue($storage->isEmpty());
  429. }
  430. public function testServerIsImmutableOnceDeviceRetrieved()
  431. {
  432. $config = $this->config;
  433. $userAgent = new Zend_Http_UserAgent($config);
  434. $device = $userAgent->getDevice();
  435. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'immutable');
  436. $userAgent->setServerValue('HTTP_ACCEPT', 'application/json');
  437. }
  438. public function testBrowserTypeIsImmutableOnceDeviceRetrieved()
  439. {
  440. $config = $this->config;
  441. $userAgent = new Zend_Http_UserAgent($config);
  442. $device = $userAgent->getDevice();
  443. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'immutable');
  444. $userAgent->setBrowserType('mobile');
  445. }
  446. public function testHttpAcceptIsImmutableOnceDeviceRetrieved()
  447. {
  448. $config = $this->config;
  449. $userAgent = new Zend_Http_UserAgent($config);
  450. $device = $userAgent->getDevice();
  451. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'immutable');
  452. $userAgent->setHttpAccept('application/json');
  453. }
  454. public function testUserAgentIsImmutableOnceDeviceIsRetrieved()
  455. {
  456. $config = $this->config;
  457. $userAgent = new Zend_Http_UserAgent($config);
  458. $device = $userAgent->getDevice();
  459. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'immutable');
  460. $userAgent->setUserAgent('userAgentTest');
  461. }
  462. public function testStorageIsImmutableOnceDeviceIsRetrieved()
  463. {
  464. $config = $this->config;
  465. $userAgent = new Zend_Http_UserAgent($config);
  466. $device = $userAgent->getDevice();
  467. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'immutable');
  468. $userAgent->setStorage(new Zend_Http_UserAgent_Storage_NonPersistent());
  469. }
  470. public function testAllowsPassingStorageConfigurationOptions()
  471. {
  472. $config = $this->config;
  473. $config['storage']['adapter'] = 'Session';
  474. $config['storage']['options'] = array(
  475. 'browser_type' => 'foobar',
  476. 'member' => 'data',
  477. );
  478. $userAgent = new Zend_Http_UserAgent($config);
  479. $storage = $userAgent->getStorage();
  480. $this->assertEquals('.foobar', $storage->getNamespace());
  481. $this->assertEquals('data', $storage->getMember());
  482. }
  483. /**
  484. * @group ZF-10595
  485. */
  486. public function testAGroupDefinedAndSerialized()
  487. {
  488. $config = $this->config;
  489. $userAgent = new Zend_Http_UserAgent($config);
  490. $device = $userAgent->getDevice();
  491. $userAgent = unserialize(serialize($userAgent));
  492. $device = $userAgent->getDevice();
  493. $groups = $device->getAllGroups();
  494. }
  495. /**
  496. * @group ZF-10665
  497. */
  498. public function testDontDieOnSerialization()
  499. {
  500. $config = $this->config;
  501. $userAgent = new Zend_Http_UserAgent($config);
  502. // If this code doesn't throw a fatal error the test passed.
  503. $userAgent->setUserAgent('userAgentTest');
  504. $userAgent->serialize();
  505. }
  506. }