SimpleTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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-2010 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. /** PHPUnit_Framework_TestCase */
  23. require_once 'PHPUnit/Framework/TestCase.php';
  24. /** Zend_Log_Formatter_Simple */
  25. require_once 'Zend/Log/Formatter/Simple.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_Log
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_Log
  33. */
  34. class Zend_Log_Formatter_SimpleTest extends PHPUnit_Framework_TestCase
  35. {
  36. public function testConstructorThrowsOnBadFormatString()
  37. {
  38. try {
  39. new Zend_Log_Formatter_Simple(1);
  40. $this->fail();
  41. } catch (Exception $e) {
  42. $this->assertType('Zend_Log_Exception', $e);
  43. $this->assertRegExp('/must be a string/i', $e->getMessage());
  44. }
  45. }
  46. public function testDefaultFormat()
  47. {
  48. $fields = array('timestamp' => 0,
  49. 'message' => 'foo',
  50. 'priority' => 42,
  51. 'priorityName' => 'bar');
  52. $f = new Zend_Log_Formatter_Simple();
  53. $line = $f->format($fields);
  54. $this->assertContains((string)$fields['timestamp'], $line);
  55. $this->assertContains($fields['message'], $line);
  56. $this->assertContains($fields['priorityName'], $line);
  57. $this->assertContains((string)$fields['priority'], $line);
  58. }
  59. function testComplexValues()
  60. {
  61. $fields = array('timestamp' => 0,
  62. 'priority' => 42,
  63. 'priorityName' => 'bar');
  64. $f = new Zend_Log_Formatter_Simple();
  65. $fields['message'] = 'Foo';
  66. $line = $f->format($fields);
  67. $this->assertContains($fields['message'], $line);
  68. $fields['message'] = 10;
  69. $line = $f->format($fields);
  70. $this->assertContains($fields['message'], $line);
  71. $fields['message'] = 10.5;
  72. $line = $f->format($fields);
  73. $this->assertContains($fields['message'], $line);
  74. $fields['message'] = true;
  75. $line = $f->format($fields);
  76. $this->assertContains('1', $line);
  77. $fields['message'] = fopen('php://stdout', 'w');
  78. $line = $f->format($fields);
  79. $this->assertContains('Resource id ', $line);
  80. fclose($fields['message']);
  81. $fields['message'] = range(1,10);
  82. $line = $f->format($fields);
  83. $this->assertContains('array', $line);
  84. $fields['message'] = new Zend_Log_Formatter_SimpleTest_TestObject1();
  85. $line = $f->format($fields);
  86. $this->assertContains($fields['message']->__toString(), $line);
  87. $fields['message'] = new Zend_Log_Formatter_SimpleTest_TestObject2();
  88. $line = $f->format($fields);
  89. $this->assertContains('object', $line);
  90. }
  91. }
  92. class Zend_Log_Formatter_SimpleTest_TestObject1 {
  93. public function __toString()
  94. {
  95. return 'Hello World';
  96. }
  97. }
  98. class Zend_Log_Formatter_SimpleTest_TestObject2 {
  99. }