PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/tests/unit/testsuite/Mage/Core/Model/LoggerTest.php

https://bitbucket.org/sunil_nextbits/magento2
PHP | 170 lines | 112 code | 14 blank | 44 comment | 0 complexity | 35fdcde1ec5c85910395cf99a6a17d54 MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  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@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @copyright Copyright (c) 2012 X.commerce, Inc. (http://www.magentocommerce.com)
  22. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  23. */
  24. class Mage_Core_Model_LoggerTest extends PHPUnit_Framework_TestCase
  25. {
  26. /**
  27. * @var Mage_Core_Model_Logger|PHPUnit_Framework_MockObject_MockObject
  28. */
  29. protected $_model = null;
  30. /**
  31. * @var ReflectionProperty
  32. */
  33. protected $_loggersProperty = null;
  34. /**
  35. * @var Mage_Core_Model_Config|PHPUnit_Framework_MockObject_MockObject
  36. */
  37. protected $_config = null;
  38. protected function setUp()
  39. {
  40. $this->_config = $this->getMock('Mage_Core_Model_Config', array('getOptions', 'getNode'), array(), '', false);
  41. $options = $this->getMock('StdClass', array('getLogDir'));
  42. $options->expects($this->any())->method('getLogDir')->will($this->returnValue(TESTS_TEMP_DIR));
  43. $this->_config->expects($this->any())->method('getOptions')->will($this->returnValue($options));
  44. $this->_model = new Mage_Core_Model_Logger($this->_config);
  45. $this->_loggersProperty = new ReflectionProperty($this->_model, '_loggers');
  46. $this->_loggersProperty->setAccessible(true);
  47. }
  48. /**
  49. * @param string $key
  50. * @param string $fileOrWrapper
  51. * @dataProvider addStreamLogDataProvider
  52. */
  53. public function testAddStreamLog($key, $fileOrWrapper)
  54. {
  55. $this->_config->expects($this->any())->method('getNode')->will($this->returnValue(''));
  56. $this->assertFalse($this->_model->hasLog($key));
  57. $this->_model->addStreamLog($key, $fileOrWrapper);
  58. $this->assertTrue($this->_model->hasLog($key));
  59. $loggers = $this->_loggersProperty->getValue($this->_model);
  60. $this->assertArrayHasKey($key, $loggers);
  61. $zendLog = $loggers[$key];
  62. $this->assertInstanceOf('Zend_Log', $zendLog);
  63. $writersProperty = new ReflectionProperty($zendLog, '_writers');
  64. $writersProperty->setAccessible(true);
  65. $writers = $writersProperty->getValue($zendLog);
  66. $this->assertArrayHasKey(0, $writers);
  67. $stream = $writers[0];
  68. $this->assertInstanceOf('Zend_Log_Writer_Stream', $writers[0]);
  69. $streamProperty = new ReflectionProperty($stream, '_stream');
  70. $streamProperty->setAccessible(true);
  71. $fileOrWrapper = $streamProperty->getValue($stream);
  72. $this->assertInternalType('resource', $fileOrWrapper);
  73. $this->assertEquals('stream', get_resource_type($fileOrWrapper));
  74. }
  75. /**
  76. * @return array
  77. */
  78. public function addStreamLogDataProvider()
  79. {
  80. return array(
  81. array('test', 'php://output'),
  82. array('test', 'custom_file.log'),
  83. array('test', ''),
  84. );
  85. }
  86. public function testInitForStore()
  87. {
  88. $store = $this->getMock('Mage_Core_Model_Store', array('getConfig'), array(), '', false);
  89. $store->expects($this->at(0))->method('getConfig')->with('dev/log/active')->will($this->returnValue(false));
  90. $store->expects($this->at(1))->method('getConfig')->with('dev/log/active')->will($this->returnValue(true));
  91. $store->expects($this->at(2))->method('getConfig')->with('dev/log/file')->will($this->returnValue(''));
  92. $store->expects($this->at(3))->method('getConfig')->with('dev/log/exception_file')->will(
  93. $this->returnValue('')
  94. );
  95. $this->_model->initForStore($store);
  96. $this->assertFalse($this->_model->hasLog(Mage_Core_Model_Logger::LOGGER_SYSTEM));
  97. $this->assertFalse($this->_model->hasLog(Mage_Core_Model_Logger::LOGGER_EXCEPTION));
  98. $this->_model->initForStore($store);
  99. $this->assertTrue($this->_model->hasLog(Mage_Core_Model_Logger::LOGGER_SYSTEM));
  100. $this->assertTrue($this->_model->hasLog(Mage_Core_Model_Logger::LOGGER_EXCEPTION));
  101. }
  102. /**
  103. * @covers Mage_Core_Model_Logger::hasLog
  104. */
  105. public function testAddStoreLog()
  106. {
  107. $store = $this->getMock('Mage_Core_Model_Store', array('getConfig'), array(), '', false);
  108. $store->expects($this->at(0))->method('getConfig')->with('dev/log/active')->will($this->returnValue(false));
  109. $store->expects($this->at(1))->method('getConfig')->with('dev/log/active')->will($this->returnValue(true));
  110. $key = uniqid();
  111. $this->_model->addStoreLog($key, $store);
  112. $this->assertFalse($this->_model->hasLog($key));
  113. $this->_model->addStoreLog($key, $store);
  114. $this->assertTrue($this->_model->hasLog($key));
  115. }
  116. public function testLog()
  117. {
  118. $messageOne = uniqid();
  119. $messageTwo = uniqid();
  120. $messageThree = uniqid();
  121. $this->expectOutputRegex('/' . 'DEBUG \(7\).+?' . $messageTwo . '.+?' . 'CRIT \(2\).+?' . $messageThree . '/s');
  122. $this->_model->addStreamLog('test', 'php://output');
  123. $this->_model->log($messageOne);
  124. $this->_model->log($messageTwo, Zend_Log::DEBUG, 'test');
  125. $this->_model->log($messageThree, Zend_Log::CRIT, 'test');
  126. }
  127. public function testLogComplex()
  128. {
  129. $this->expectOutputRegex('/Array\s\(\s+\[0\] => 1\s\).+stdClass Object/s');
  130. $this->_model->addStreamLog(Mage_Core_Model_Logger::LOGGER_SYSTEM, 'php://output');
  131. $this->_model->log(array(1));
  132. $this->_model->log(new StdClass);
  133. }
  134. public function testLogDebug()
  135. {
  136. $message = uniqid();
  137. /** @var $model Mage_Core_Model_Logger|PHPUnit_Framework_MockObject_MockObject */
  138. $model = $this->getMock('Mage_Core_Model_Logger', array('log'), array(), '', false);
  139. $model->expects($this->at(0))->method('log')
  140. ->with($message, Zend_Log::DEBUG, Mage_Core_Model_Logger::LOGGER_SYSTEM);
  141. $model->expects($this->at(1))->method('log')
  142. ->with($message, Zend_Log::DEBUG, Mage_Core_Model_Logger::LOGGER_EXCEPTION);
  143. $model->logDebug($message);
  144. $model->logDebug($message, Mage_Core_Model_Logger::LOGGER_EXCEPTION);
  145. }
  146. public function testLogException()
  147. {
  148. $exception = new Exception;
  149. $expected = "\n{$exception}";
  150. /** @var $model Mage_Core_Model_Logger|PHPUnit_Framework_MockObject_MockObject */
  151. $model = $this->getMock('Mage_Core_Model_Logger', array('log'), array(), '', false);
  152. $model->expects($this->at(0))->method('log')
  153. ->with($expected, Zend_Log::ERR, Mage_Core_Model_Logger::LOGGER_EXCEPTION);
  154. $model->logException($exception);
  155. }
  156. }