CallbackHandlerTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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_Stdlib
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2015 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_Stdlib_CallbackHandlerTest::main');
  24. }
  25. require_once 'Zend/Stdlib/CallbackHandler.php';
  26. require_once 'Zend/Stdlib/TestAsset/SignalHandlers/InstanceMethod.php';
  27. require_once 'Zend/Stdlib/TestAsset/SignalHandlers/ObjectCallback.php';
  28. /**
  29. * @todo Remove all closures from tests and refactor as methods or functions
  30. * @category Zend
  31. * @package Zend_Stdlib
  32. * @subpackage UnitTests
  33. * @group Zend_Stdlib
  34. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class Zend_Stdlib_CallbackHandlerTest 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. if (isset($this->args)) {
  47. unset($this->args);
  48. }
  49. $this->error = false;
  50. }
  51. public function testCallbackShouldStoreMetadata()
  52. {
  53. $handler = new Zend_Stdlib_CallbackHandler('rand', array('event' => 'foo'));
  54. $this->assertEquals('foo', $handler->getMetadatum('event'));
  55. $this->assertEquals(array('event' => 'foo'), $handler->getMetadata());
  56. }
  57. public function testCallbackShouldBeStringIfNoHandlerPassedToConstructor()
  58. {
  59. $handler = new Zend_Stdlib_CallbackHandler('rand');
  60. $this->assertSame('rand', $handler->getCallback());
  61. }
  62. public function testCallbackShouldBeArrayIfHandlerPassedToConstructor()
  63. {
  64. $handler = new Zend_Stdlib_CallbackHandler(array('Zend_Stdlib_TestAsset_SignalHandlers_ObjectCallback', 'test'));
  65. $this->assertSame(array('Zend_Stdlib_TestAsset_SignalHandlers_ObjectCallback', 'test'), $handler->getCallback());
  66. }
  67. public function testCallShouldInvokeCallbackWithSuppliedArguments()
  68. {
  69. $handler = new Zend_Stdlib_CallbackHandler(array( $this, 'handleCall' ));
  70. $args = array('foo', 'bar', 'baz');
  71. $handler->call($args);
  72. $this->assertSame($args, $this->args);
  73. }
  74. public function testPassingInvalidCallbackShouldRaiseInvalidCallbackExceptionDuringInstantiation()
  75. {
  76. $this->setExpectedException('Zend_Stdlib_Exception_InvalidCallbackException');
  77. $handler = new Zend_Stdlib_CallbackHandler('boguscallback');
  78. }
  79. public function testCallShouldReturnTheReturnValueOfTheCallback()
  80. {
  81. $handler = new Zend_Stdlib_CallbackHandler(array('Zend_Stdlib_TestAsset_SignalHandlers_ObjectCallback', 'test'));
  82. if (!is_callable(array('Zend_Stdlib_TestAsset_SignalHandlers_ObjectCallback', 'test'))) {
  83. echo "\nClass exists? " . var_export(class_exists('Zend_Stdlib_TestAsset_SignalHandlers_ObjectCallback'), 1) . "\n";
  84. echo "Include path: " . get_include_path() . "\n";
  85. }
  86. $this->assertEquals('bar', $handler->call(array()));
  87. }
  88. public function testStringCallbackResolvingToClassDefiningInvokeNameShouldRaiseException()
  89. {
  90. $this->setExpectedException('Zend_Stdlib_Exception_InvalidCallbackException');
  91. $handler = new Zend_Stdlib_CallbackHandler('Zend_Stdlib_TestAsset_SignalHandlers_Invokable');
  92. }
  93. public function testStringCallbackReferringToClassWithoutDefinedInvokeShouldRaiseException()
  94. {
  95. $this->setExpectedException('Zend_Stdlib_Exception_InvalidCallbackException');
  96. $class = new Zend_Stdlib_TestAsset_SignalHandlers_InstanceMethod();
  97. $handler = new Zend_Stdlib_CallbackHandler($class);
  98. }
  99. public function errorHandler($errno, $errstr)
  100. {
  101. $this->error = true;
  102. }
  103. public function testCallbackConsistingOfStringContextWithNonStaticMethodShouldRaiseException()
  104. {
  105. if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
  106. $this->markTestSkipped('Behavior of is_callable changes between 5.2 and 5.3');
  107. }
  108. $this->setExpectedException('Zend_Stdlib_Exception_InvalidCallbackException');
  109. $handler = new Zend_Stdlib_CallbackHandler(array('Zend_Stdlib_TestAsset_SignalHandlers_InstanceMethod', 'handler'));
  110. }
  111. public function testStringCallbackConsistingOfNonStaticMethodShouldRaiseException()
  112. {
  113. if (version_compare(PHP_VERSION, '5.3.0', '>=')) {
  114. $this->markTestSkipped('Behavior of is_callable changes between 5.2 and 5.3');
  115. }
  116. $this->setExpectedException('Zend_Stdlib_Exception_InvalidCallbackException');
  117. $handler = new Zend_Stdlib_CallbackHandler('Zend_Stdlib_TestAsset_SignalHandlers_InstanceMethod::handler');
  118. }
  119. public function testCallbackToClassImplementingOverloadingButNotInvocableShouldRaiseException()
  120. {
  121. $this->setExpectedException('Zend_Stdlib_Exception_InvalidCallbackException');
  122. $handler = new Zend_Stdlib_CallbackHandler('foo', array( 'Zend_Stdlib_TestAsset_SignalHandlers_Overloadable', 'foo' ));
  123. }
  124. public function handleCall()
  125. {
  126. $this->args = func_get_args();
  127. }
  128. }
  129. if (PHPUnit_MAIN_METHOD == 'Zend_Stdlib_CallbackHandlerTest::main') {
  130. Zend_Stdlib_CallbackHandlerTest::main();
  131. }