PageRenderTime 57ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Application/Resource/LogTest.php

https://bitbucket.org/ksekar/campus
PHP | 190 lines | 126 code | 22 blank | 42 comment | 4 complexity | e9776e33551b33750e68ed00e3002cce MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  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. * @version $Id$
  21. */
  22. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Application_Resource_LogTest::main');
  24. }
  25. /**
  26. * Zend_Loader_Autoloader
  27. */
  28. require_once 'Zend/Loader/Autoloader.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_Application
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. * @group Zend_Application
  36. */
  37. class Zend_Application_Resource_LogTest extends PHPUnit_Framework_TestCase
  38. {
  39. public static function main()
  40. {
  41. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  42. $result = PHPUnit_TextUI_TestRunner::run($suite);
  43. }
  44. public function setUp()
  45. {
  46. // Store original autoloaders
  47. $this->loaders = spl_autoload_functions();
  48. if (!is_array($this->loaders)) {
  49. // spl_autoload_functions does not return empty array when no
  50. // autoloaders registered...
  51. $this->loaders = array();
  52. }
  53. Zend_Loader_Autoloader::resetInstance();
  54. $this->autoloader = Zend_Loader_Autoloader::getInstance();
  55. $this->application = new Zend_Application('testing');
  56. $this->bootstrap = new Zend_Application_Bootstrap_Bootstrap($this->application);
  57. Zend_Controller_Front::getInstance()->resetInstance();
  58. }
  59. public function tearDown()
  60. {
  61. // Restore original autoloaders
  62. $loaders = spl_autoload_functions();
  63. foreach ($loaders as $loader) {
  64. spl_autoload_unregister($loader);
  65. }
  66. foreach ($this->loaders as $loader) {
  67. spl_autoload_register($loader);
  68. }
  69. // Reset autoloader instance so it doesn't affect other tests
  70. Zend_Loader_Autoloader::resetInstance();
  71. }
  72. public function testInitializationInitializesLogObject()
  73. {
  74. $resource = new Zend_Application_Resource_Log(array());
  75. $resource->setBootstrap($this->bootstrap);
  76. $resource->setOptions(array(
  77. 'Mock' => array('writerName' => 'Mock'),
  78. ));
  79. $resource->init();
  80. $this->assertTrue($resource->getLog() instanceof Zend_Log);
  81. }
  82. public function testInitializationReturnsLogObject()
  83. {
  84. $resource = new Zend_Application_Resource_Log(array());
  85. $resource->setBootstrap($this->bootstrap);
  86. $resource->setOptions(array(
  87. 'Mock' => array('writerName' => 'Mock'),
  88. ));
  89. $test = $resource->init();
  90. $this->assertTrue($test instanceof Zend_Log);
  91. }
  92. public function testOptionsPassedToResourceAreUsedToInitializeLog()
  93. {
  94. $stream = fopen('php://memory', 'w+', false);
  95. $options = array('memory' => array(
  96. 'writerName' => 'Stream',
  97. 'writerParams' => array(
  98. 'stream' => $stream,
  99. )
  100. ));
  101. $resource = new Zend_Application_Resource_Log($options);
  102. $resource->setBootstrap($this->bootstrap);
  103. $resource->init();
  104. $log = $resource->getLog();
  105. $this->assertTrue($log instanceof Zend_Log);
  106. $log->log($message = 'logged-message', Zend_Log::INFO);
  107. rewind($stream);
  108. $this->assertContains($message, stream_get_contents($stream));
  109. }
  110. /**
  111. * @group ZF-8602
  112. */
  113. public function testNumericLogStreamFilterParamsPriorityDoesNotFail()
  114. {
  115. $options = array(
  116. 'stream' => array(
  117. 'writerName' => 'Stream',
  118. 'writerParams' => array(
  119. 'stream' => "php://memory",
  120. 'mode' => 'a'
  121. ),
  122. 'filterName' => 'Priority',
  123. 'filterParams' => array(
  124. 'priority' => '4'
  125. ),
  126. ),
  127. );
  128. $resource = new Zend_Application_Resource_Log($options);
  129. $resource->setBootstrap($this->bootstrap);
  130. $resource->init();
  131. }
  132. /**
  133. * @group ZF-9790
  134. */
  135. public function testInitializationWithFilterAndFormatter()
  136. {
  137. $stream = fopen('php://memory', 'w+');
  138. $options = array(
  139. 'memory' => array(
  140. 'writerName' => 'Stream',
  141. 'writerParams' => array(
  142. 'stream' => $stream,
  143. ),
  144. 'filterName' => 'Priority',
  145. 'filterParams' => array(
  146. 'priority' => Zend_Log::INFO,
  147. ),
  148. 'formatterName' => 'Simple',
  149. 'formatterParams' => array(
  150. 'format' => '%timestamp%: %message%',
  151. )
  152. )
  153. );
  154. $message = 'tottakai';
  155. $resource = new Zend_Application_Resource_Log($options);
  156. $resource->setBootstrap($this->bootstrap);
  157. $log = $resource->init();
  158. $this->assertType('Zend_Log', $log);
  159. $log->log($message, Zend_Log::INFO);
  160. rewind($stream);
  161. $contents = stream_get_contents($stream);
  162. $this->assertStringEndsWith($message, $contents);
  163. $this->assertRegexp('/\d\d:\d\d:\d\d/', $contents);
  164. }
  165. }
  166. if (PHPUnit_MAIN_METHOD == 'Zend_Application_Resource_LogTest::main') {
  167. Zend_Application_Resource_LogTest::main();
  168. }