PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Zend/Log/Formatter/SimpleTest.php

https://bitbucket.org/ksekar/campus
PHP | 141 lines | 87 code | 22 blank | 32 comment | 3 complexity | fc765bd441f80ec3bf089098211fc617 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_Log
  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: SimpleTest.php 24594 2012-01-05 21:27:01Z matthew $
  21. */
  22. if (!defined('PHPUnit_MAIN_METHOD')) {
  23. define('PHPUnit_MAIN_METHOD', 'Zend_Log_Formatter_SimpleTest::main');
  24. }
  25. /** Zend_Log_Formatter_Simple */
  26. require_once 'Zend/Log/Formatter/Simple.php';
  27. /**
  28. * @category Zend
  29. * @package Zend_Log
  30. * @subpackage UnitTests
  31. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. * @group Zend_Log
  34. */
  35. class Zend_Log_Formatter_SimpleTest extends PHPUnit_Framework_TestCase
  36. {
  37. public static function main()
  38. {
  39. $suite = new PHPUnit_Framework_TestSuite(__CLASS__);
  40. $result = PHPUnit_TextUI_TestRunner::run($suite);
  41. }
  42. public function testConstructorThrowsOnBadFormatString()
  43. {
  44. try {
  45. new Zend_Log_Formatter_Simple(1);
  46. $this->fail();
  47. } catch (Exception $e) {
  48. $this->assertType('Zend_Log_Exception', $e);
  49. $this->assertRegExp('/must be a string/i', $e->getMessage());
  50. }
  51. }
  52. public function testDefaultFormat()
  53. {
  54. $fields = array('timestamp' => 0,
  55. 'message' => 'foo',
  56. 'priority' => 42,
  57. 'priorityName' => 'bar');
  58. $f = new Zend_Log_Formatter_Simple();
  59. $line = $f->format($fields);
  60. $this->assertContains((string)$fields['timestamp'], $line);
  61. $this->assertContains($fields['message'], $line);
  62. $this->assertContains($fields['priorityName'], $line);
  63. $this->assertContains((string)$fields['priority'], $line);
  64. }
  65. function testComplexValues()
  66. {
  67. $fields = array('timestamp' => 0,
  68. 'priority' => 42,
  69. 'priorityName' => 'bar');
  70. $f = new Zend_Log_Formatter_Simple();
  71. $fields['message'] = 'Foo';
  72. $line = $f->format($fields);
  73. $this->assertContains($fields['message'], $line);
  74. $fields['message'] = 10;
  75. $line = $f->format($fields);
  76. $this->assertContains($fields['message'], $line);
  77. $fields['message'] = 10.5;
  78. $line = $f->format($fields);
  79. $this->assertContains($fields['message'], $line);
  80. $fields['message'] = true;
  81. $line = $f->format($fields);
  82. $this->assertContains('1', $line);
  83. $fields['message'] = fopen('php://stdout', 'w');
  84. $line = $f->format($fields);
  85. $this->assertContains('Resource id ', $line);
  86. fclose($fields['message']);
  87. $fields['message'] = range(1,10);
  88. $line = $f->format($fields);
  89. $this->assertContains('array', $line);
  90. $fields['message'] = new Zend_Log_Formatter_SimpleTest_TestObject1();
  91. $line = $f->format($fields);
  92. $this->assertContains($fields['message']->__toString(), $line);
  93. $fields['message'] = new Zend_Log_Formatter_SimpleTest_TestObject2();
  94. $line = $f->format($fields);
  95. $this->assertContains('object', $line);
  96. }
  97. /**
  98. * @group ZF-9176
  99. */
  100. public function testFactory()
  101. {
  102. $options = array(
  103. 'format' => '%timestamp% [%priority%]: %message% -- %info%'
  104. );
  105. $formatter = Zend_Log_Formatter_Simple::factory($options);
  106. $this->assertType('Zend_Log_Formatter_Simple', $formatter);
  107. }
  108. }
  109. class Zend_Log_Formatter_SimpleTest_TestObject1 {
  110. public function __toString()
  111. {
  112. return 'Hello World';
  113. }
  114. }
  115. class Zend_Log_Formatter_SimpleTest_TestObject2 {
  116. }
  117. if (PHPUnit_MAIN_METHOD == 'Zend_Log_Formatter_SimpleTest::main') {
  118. Zend_Log_Formatter_SimpleTest::main();
  119. }