UseragentTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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_Application
  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. */
  21. if (!defined('PHPUnit_MAIN_METHOD')) {
  22. define('PHPUnit_MAIN_METHOD', 'Zend_Application_Resource_UseragentTest::main');
  23. }
  24. /**
  25. * Zend_Loader_Autoloader
  26. */
  27. require_once 'Zend/Loader/Autoloader.php';
  28. require_once 'Zend/Application/Resource/ResourceAbstract.php';
  29. require_once 'Zend/Application/Resource/Useragent.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Application
  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_Application
  37. */
  38. class Zend_Application_Resource_UseragentTest extends PHPUnit_Framework_TestCase
  39. {
  40. public static function main()
  41. {
  42. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  43. $result = PHPUnit_TextUI_TestRunner::run($suite);
  44. }
  45. public function setUp()
  46. {
  47. // Store original autoloaders
  48. $this->loaders = spl_autoload_functions();
  49. if (!is_array($this->loaders)) {
  50. // spl_autoload_functions does not return empty array when no
  51. // autoloaders registered...
  52. $this->loaders = array();
  53. }
  54. Zend_Loader_Autoloader::resetInstance();
  55. $this->autoloader = Zend_Loader_Autoloader::getInstance();
  56. $this->application = new Zend_Application('testing');
  57. require_once dirname(__FILE__) . '/../_files/ZfAppBootstrap.php';
  58. $this->bootstrap = new ZfAppBootstrap($this->application);
  59. Zend_Controller_Action_HelperBroker::resetHelpers();
  60. }
  61. public function tearDown()
  62. {
  63. // Restore original autoloaders
  64. $loaders = spl_autoload_functions();
  65. foreach ($loaders as $loader) {
  66. spl_autoload_unregister($loader);
  67. }
  68. foreach ($this->loaders as $loader) {
  69. spl_autoload_register($loader);
  70. }
  71. // Reset autoloader instance so it doesn't affect other tests
  72. Zend_Loader_Autoloader::resetInstance();
  73. }
  74. public function testInitializationInitializesUserAgentObject()
  75. {
  76. $resource = new Zend_Application_Resource_Useragent(array());
  77. $resource->setBootstrap($this->bootstrap);
  78. $resource->init();
  79. $this->assertTrue($resource->getUserAgent() instanceof Zend_Http_UserAgent);
  80. }
  81. public function testOptionsPassedToResourceAreUsedToSetUserAgentState()
  82. {
  83. $options = array(
  84. 'storage' => array('adapter' => 'NonPersistent'),
  85. );
  86. $resource = new Zend_Application_Resource_Useragent($options);
  87. $resource->setBootstrap($this->bootstrap);
  88. $resource->init();
  89. $ua = $resource->getUserAgent();
  90. $storage = $ua->getStorage();
  91. $this->assertTrue($storage instanceof Zend_Http_UserAgent_Storage_NonPersistent);
  92. }
  93. public function testInjectsUserAgentIntoViewHelperWhenViewResourcePresent()
  94. {
  95. $this->bootstrap->registerPluginResource('view', array());
  96. $resource = new Zend_Application_Resource_Useragent(array());
  97. $resource->setBootstrap($this->bootstrap);
  98. $resource->init();
  99. $view = $this->bootstrap->getResource('view');
  100. $helper = $view->getHelper('userAgent');
  101. $expected = $resource->getUserAgent();
  102. $this->assertSame($expected, $helper->getUserAgent());
  103. }
  104. }
  105. if (PHPUnit_MAIN_METHOD == 'Zend_Application_Resource_UseragentTest::main') {
  106. Zend_Application_Resource_UseragentTest::main();
  107. }