UserAgentTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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-2015 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-2015 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->assertTrue($device instanceof Zend_Http_UserAgent_Mobile);
  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->assertTrue(
  126. $oUserAgent->getStorage($browser) instanceof Zend_Http_UserAgent_Storage_NonPersistent
  127. );
  128. }
  129. public function testUserAgentFeatureAdapter()
  130. {
  131. $config = $this->config;
  132. $config['mobile']['features']['path'] = dirname(__FILE__) . '/TestAsset/Device/Browser/Features/Adapter.php';
  133. $config['mobile']['features']['classname'] = 'Device_Browser_Features_Adapter';
  134. $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';
  135. $userAgent = new Zend_Http_UserAgent($config);
  136. $config = $userAgent->getConfig();
  137. $this->assertContains('Device/Browser/Features/Adapter.php', $config['mobile']['features']['path']);
  138. }
  139. public function testSetDefaultConfigAlone()
  140. {
  141. $config['server'] = $this->server;
  142. $userAgent = new Zend_Http_UserAgent($config);
  143. $config = $userAgent->getConfig();
  144. $this->assertEquals(Zend_Http_UserAgent::DEFAULT_IDENTIFICATION_SEQUENCE, $config['identification_sequence']);
  145. $this->assertEquals(Zend_Http_UserAgent::DEFAULT_PERSISTENT_STORAGE_ADAPTER, $config['storage']['adapter']);
  146. }
  147. public function testSetDefaultConfigStorage()
  148. {
  149. $config = array('identification_sequence' => 'Test');
  150. $oUserAgent = new Zend_Http_UserAgent($config);
  151. $test = $oUserAgent->getConfig();
  152. $this->assertEquals('Test', $test['identification_sequence']);
  153. $this->assertEquals(Zend_Http_UserAgent::DEFAULT_PERSISTENT_STORAGE_ADAPTER, $test['storage']['adapter']);
  154. }
  155. public function testSetDefaultConfigBoth()
  156. {
  157. $config = array(
  158. 'identification_sequence' => 'Test',
  159. 'storage' => array('adapter' => 'NonPersistent'),
  160. );
  161. $oUserAgent = new Zend_Http_UserAgent($config);
  162. $test = $oUserAgent->getConfig();
  163. $this->assertEquals('Test', $test['identification_sequence']);
  164. $this->assertEquals('NonPersistent', $test['storage']['adapter']);
  165. }
  166. public function testDeviceClassNameMatchesBrowserTypeIfUserAgentMatches()
  167. {
  168. $browscap = ini_get('browscap');
  169. if (empty($browscap)) {
  170. $this->markTestSkipped('Depends on browscap support');
  171. }
  172. $this->config['browser_type'] = 'MoBiLe';
  173. $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';
  174. $userAgent = new Zend_Http_UserAgent($this->config);
  175. $className = get_class($userAgent->getDevice());
  176. $this->assertEquals('Zend_Http_UserAgent_Mobile', $className);
  177. }
  178. public function testDeviceClassNameMatchesDesktopTypeIfUserAgentDoesNotMatch()
  179. {
  180. $config = array(
  181. 'browser_type' => 'MoBiLe',
  182. );
  183. $userAgent = new Zend_Http_UserAgent($config);
  184. $className = get_class($userAgent->getDevice());
  185. $this->assertEquals('Zend_Http_UserAgent_Desktop', $className);
  186. }
  187. public function testUserAgentFromServerSuperglobalWhenNotProvided()
  188. {
  189. $_SERVER['HTTP_USER_AGENT'] = 'UserAgentTest2';
  190. $ua = new Zend_Http_UserAgent();
  191. $this->assertEquals('UserAgentTest2', $ua->getServerValue('http_user_agent'));
  192. $this->assertEquals('UserAgentTest2', $ua->getUserAgent());
  193. }
  194. public function testAllowsPassingUserAgentDirectly()
  195. {
  196. $this->config['user_agent'] = 'UserAgentTest2';
  197. $ua = new Zend_Http_UserAgent($this->config);
  198. $this->assertEquals('UserAgentTest2', $ua->getUserAgent());
  199. }
  200. public function testAllowsSettingUserAgentManually()
  201. {
  202. $ua = new Zend_Http_UserAgent();
  203. $ua->setUserAgent('userAgentTest');
  204. $this->assertEquals('userAgentTest', $ua->getServerValue('HTTP_USER_AGENT'));
  205. $this->assertEquals('userAgentTest', $ua->getUserAgent());
  206. }
  207. public function testUsesHttpAcceptConstantValueByDefault()
  208. {
  209. unset($this->server['http_accept']);
  210. $ua = new Zend_Http_UserAgent();
  211. $this->assertEquals(Zend_Http_UserAgent::DEFAULT_HTTP_ACCEPT, $ua->getHttpAccept());
  212. $this->assertEquals(Zend_Http_UserAgent::DEFAULT_HTTP_ACCEPT, $ua->getServerValue('HTTP_ACCEPT'));
  213. }
  214. public function testUsesServerHttpAcceptValueWhenPresent()
  215. {
  216. $_SERVER['HTTP_ACCEPT'] = 'HttpAcceptTest2';
  217. $ua = new Zend_Http_UserAgent();
  218. $this->assertEquals('HttpAcceptTest2', $ua->getHttpAccept());
  219. $this->assertEquals('HttpAcceptTest2', $ua->getServerValue('HTTP_ACCEPT'));
  220. }
  221. public function testAllowsPassingHttpAcceptValueViaConfiguration()
  222. {
  223. $this->config['http_accept'] = 'HttpAcceptTest';
  224. $ua = new Zend_Http_UserAgent($this->config);
  225. $this->assertEquals('HttpAcceptTest', $ua->getHttpAccept());
  226. $this->assertEquals('HttpAcceptTest', $ua->getServerValue('HTTP_ACCEPT'));
  227. }
  228. public function testAllowsSettingHttpAcceptManually()
  229. {
  230. $ua = new Zend_Http_UserAgent();
  231. $ua->setHttpAccept('httpAcceptTest');
  232. $this->assertEquals('httpAcceptTest', $ua->getHttpAccept());
  233. $this->assertEquals('httpAcceptTest', $ua->getServerValue('HTTP_ACCEPT'));
  234. }
  235. public function testCanSetConfigWithConfigObject()
  236. {
  237. $config = new Zend_Config($this->config);
  238. $ua = new Zend_Http_UserAgent($config);
  239. $test = $ua->getConfig();
  240. $this->assertEquals($config->storage->adapter, $test['storage']['adapter']);
  241. }
  242. public function testCanSetConfigWithTraversableObject()
  243. {
  244. $config = new ArrayObject($this->config);
  245. $ua = new Zend_Http_UserAgent($config);
  246. $test = $ua->getConfig();
  247. $this->assertEquals($config['storage'], $test['storage']);
  248. }
  249. public function invalidConfigs()
  250. {
  251. return array(
  252. array(true),
  253. array(1),
  254. array(1.0),
  255. array(new stdClass),
  256. );
  257. }
  258. /**
  259. * @dataProvider invalidConfigs
  260. */
  261. public function testSettingConfigWithInvalidTypeRaisesException($arg)
  262. {
  263. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'expected array');
  264. $ua = new Zend_Http_UserAgent($arg);
  265. }
  266. public function testAllowsSettingServerWithArrayObject()
  267. {
  268. $server = new ArrayObject($this->server);
  269. $ua = new Zend_Http_UserAgent(array('server' => $server));
  270. $this->assertEquals($server['os'], $ua->getServerValue('os'));
  271. }
  272. public function testAllowsSettingServerWithTraversableObject()
  273. {
  274. $server = new ArrayIterator($this->server);
  275. $ua = new Zend_Http_UserAgent(array('server' => $server));
  276. $this->assertEquals($this->server['os'], $ua->getServerValue('os'));
  277. }
  278. /**
  279. * @dataProvider invalidConfigs
  280. */
  281. public function testSettingServerWithInvalidTypeRaisesException($arg)
  282. {
  283. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'array or object implementing Traversable');
  284. $ua = new Zend_Http_UserAgent(array('server' => $arg));
  285. }
  286. public function testAllowsSettingPluginLoaderUsingClassname()
  287. {
  288. $ua = new Zend_Http_UserAgent();
  289. $ua->setPluginLoader('device', 'Zend_Http_TestAsset_TestPluginLoader');
  290. $loader = $ua->getPluginLoader('device');
  291. $this->assertTrue(
  292. $loader instanceof Zend_Http_TestAsset_TestPluginLoader
  293. );
  294. }
  295. public function testSpecifyingInvalidPluginLoaderClassNameRaisesException()
  296. {
  297. $ua = new Zend_Http_UserAgent();
  298. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'extending Zend_Loader_PluginLoader');
  299. $ua->setPluginLoader('device', 'Zend_Http_TestAsset_InvalidPluginLoader');
  300. }
  301. public function invalidLoaders()
  302. {
  303. return array(
  304. array(true),
  305. array(1),
  306. array(1.0),
  307. array(array()),
  308. );
  309. }
  310. /**
  311. * @dataProvider invalidLoaders
  312. */
  313. public function testSpecifyingInvalidTypeToPluginLoaderRaisesException($arg)
  314. {
  315. $ua = new Zend_Http_UserAgent();
  316. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'class or object');
  317. $ua->setPluginLoader('device', $arg);
  318. }
  319. public function testSpecifyingNonPluginLoaderObjectRaisesException()
  320. {
  321. $ua = new Zend_Http_UserAgent();
  322. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'extending Zend_Loader_PluginLoader');
  323. $ua->setPluginLoader('device', $this);
  324. }
  325. public function testSpecifyingInvalidTypeWhenSettingPluginLoaderRaisesException()
  326. {
  327. $ua = new Zend_Http_UserAgent();
  328. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'plugin loader type');
  329. $ua->setPluginLoader('__bogus__', new Zend_Loader_PluginLoader());
  330. }
  331. public function testAllowsSpecifyingPluginLoadersViaConfiguration()
  332. {
  333. $this->config['plugin_loader'] = array(
  334. 'device' => 'Zend_Http_TestAsset_TestPluginLoader',
  335. 'storage' => 'Zend_Http_TestAsset_TestPluginLoader',
  336. );
  337. $ua = new Zend_Http_UserAgent($this->config);
  338. $deviceLoader = $ua->getPluginLoader('device');
  339. $this->assertTrue(
  340. $deviceLoader instanceof Zend_Http_TestAsset_TestPluginLoader
  341. );
  342. $storageLoader = $ua->getPluginLoader('storage');
  343. $this->assertTrue(
  344. $storageLoader instanceof Zend_Http_TestAsset_TestPluginLoader
  345. );
  346. $this->assertNotSame($deviceLoader, $storageLoader);
  347. }
  348. public function testAllowsSpecifyingCustomDeviceClassesViaConfiguration()
  349. {
  350. $this->config['desktop'] = array(
  351. 'device' => array(
  352. 'classname' => 'Zend_Http_TestAsset_DesktopDevice',
  353. ),
  354. );
  355. $this->config['user_agent'] = 'desktop';
  356. $ua = new Zend_Http_UserAgent($this->config);
  357. $device = $ua->getDevice();
  358. $this->assertTrue($device instanceof Zend_Http_TestAsset_DesktopDevice);
  359. }
  360. public function testAllowsSpecifyingCustomDeviceViaPrefixPath()
  361. {
  362. $this->config['desktop'] = array(
  363. 'device' => array(
  364. 'path' => dirname(__FILE__) . '/TestAsset/Device',
  365. 'prefix' => 'Zend_Http_TestAsset_Device',
  366. ),
  367. );
  368. $this->config['user_agent'] = 'desktop';
  369. $ua = new Zend_Http_UserAgent($this->config);
  370. $device = $ua->getDevice();
  371. $this->assertTrue($device instanceof Zend_Http_TestAsset_Device_Desktop);
  372. }
  373. public function testShouldRaiseExceptionOnInvalidDeviceClass()
  374. {
  375. $this->config['desktop'] = array(
  376. 'device' => array(
  377. 'classname' => 'Zend_Http_TestAsset_InvalidDevice',
  378. ),
  379. );
  380. $this->config['user_agent'] = 'desktop';
  381. $ua = new Zend_Http_UserAgent($this->config);
  382. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'Zend_Http_UserAgent_Device');
  383. $ua->getDevice();
  384. }
  385. public function testStorageContainsSerializedUserAgent()
  386. {
  387. $this->config['desktop'] = array(
  388. 'device' => array(
  389. 'classname' => 'Zend_Http_TestAsset_DesktopDevice',
  390. ),
  391. );
  392. $this->config['user_agent'] = 'desktop';
  393. $ua = new Zend_Http_UserAgent($this->config);
  394. // prime storage by retrieving device
  395. $device = $ua->getDevice();
  396. $storage = $ua->getStorage();
  397. $serialized = $storage->read();
  398. $test = unserialize($serialized);
  399. $this->assertEquals($ua->getBrowserType(), $test['browser_type']);
  400. $this->assertEquals($ua->getConfig(), $test['config']);
  401. $this->assertEquals('Zend_Http_TestAsset_DesktopDevice', $test['device_class']);
  402. $this->assertEquals($ua->getUserAgent(), $test['user_agent']);
  403. $this->assertEquals($ua->getHttpAccept(), $test['http_accept']);
  404. $test = unserialize($test['device']);
  405. $this->assertEquals($device->getAllFeatures(), $test['_aFeatures']);
  406. $this->assertEquals($device->getBrowser(), $test['_browser']);
  407. $this->assertEquals($device->getBrowserVersion(), $test['_browserVersion']);
  408. $this->assertEquals($device->getUserAgent(), $test['_userAgent']);
  409. $this->assertEquals($device->getImages(), $test['_images']);
  410. }
  411. public function testCanPopulateFromStorage()
  412. {
  413. $this->config['storage']['adapter'] = 'Zend_Http_TestAsset_PopulatedStorage';
  414. $this->config['user_agent'] = 'desktop';
  415. $ua = new Zend_Http_UserAgent($this->config);
  416. $storage = $ua->getStorage();
  417. $this->assertTrue($storage instanceof Zend_Http_TestAsset_PopulatedStorage);
  418. $device = $ua->getDevice();
  419. $this->assertTrue($device instanceof Zend_Http_TestAsset_DesktopDevice);
  420. }
  421. public function testCanClearStorage()
  422. {
  423. $this->config['desktop'] = array(
  424. 'device' => array(
  425. 'classname' => 'Zend_Http_TestAsset_DesktopDevice',
  426. ),
  427. );
  428. $this->config['user_agent'] = 'desktop';
  429. $ua = new Zend_Http_UserAgent($this->config);
  430. // Prime storage by retrieving device
  431. $device = $ua->getDevice();
  432. $storage = $ua->getStorage();
  433. $this->assertTrue($storage instanceof Zend_Http_UserAgent_Storage);
  434. $this->assertFalse($storage->isEmpty());
  435. $ua->clearStorage();
  436. $this->assertTrue($storage->isEmpty());
  437. }
  438. public function testServerIsImmutableOnceDeviceRetrieved()
  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->setServerValue('HTTP_ACCEPT', 'application/json');
  445. }
  446. public function testBrowserTypeIsImmutableOnceDeviceRetrieved()
  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->setBrowserType('mobile');
  453. }
  454. public function testHttpAcceptIsImmutableOnceDeviceRetrieved()
  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->setHttpAccept('application/json');
  461. }
  462. public function testUserAgentIsImmutableOnceDeviceIsRetrieved()
  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->setUserAgent('userAgentTest');
  469. }
  470. public function testStorageIsImmutableOnceDeviceIsRetrieved()
  471. {
  472. $config = $this->config;
  473. $userAgent = new Zend_Http_UserAgent($config);
  474. $device = $userAgent->getDevice();
  475. $this->setExpectedException('Zend_Http_UserAgent_Exception', 'immutable');
  476. $userAgent->setStorage(new Zend_Http_UserAgent_Storage_NonPersistent());
  477. }
  478. public function testAllowsPassingStorageConfigurationOptions()
  479. {
  480. $config = $this->config;
  481. $config['storage']['adapter'] = 'Session';
  482. $config['storage']['options'] = array(
  483. 'browser_type' => 'foobar',
  484. 'member' => 'data',
  485. );
  486. $userAgent = new Zend_Http_UserAgent($config);
  487. $storage = $userAgent->getStorage();
  488. $this->assertEquals('.foobar', $storage->getNamespace());
  489. $this->assertEquals('data', $storage->getMember());
  490. }
  491. /**
  492. * @group ZF-10595
  493. */
  494. public function testAGroupDefinedAndSerialized()
  495. {
  496. $config = $this->config;
  497. $userAgent = new Zend_Http_UserAgent($config);
  498. $device = $userAgent->getDevice();
  499. $userAgent = unserialize(serialize($userAgent));
  500. $device = $userAgent->getDevice();
  501. $groups = $device->getAllGroups();
  502. }
  503. /**
  504. * @group ZF-10665
  505. */
  506. public function testDontDieOnSerialization()
  507. {
  508. $config = $this->config;
  509. $userAgent = new Zend_Http_UserAgent($config);
  510. // If this code doesn't throw a fatal error the test passed.
  511. $userAgent->setUserAgent('userAgentTest');
  512. $userAgent->serialize();
  513. }
  514. }