DefinitionTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. // Call Zend_Server_Method_DefinitionTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Server_Method_DefinitionTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  7. /** Zend_Server_Method_Definition */
  8. require_once 'Zend/Server/Method/Definition.php';
  9. /** Zend_Server_Method_Callback */
  10. require_once 'Zend/Server/Method/Callback.php';
  11. /** Zend_Server_Method_Prototype */
  12. require_once 'Zend/Server/Method/Prototype.php';
  13. /**
  14. * Test class for Zend_Server_Method_Definition
  15. */
  16. class Zend_Server_Method_DefinitionTest extends PHPUnit_Framework_TestCase
  17. {
  18. /**
  19. * Runs the test methods of this class.
  20. *
  21. * @return void
  22. */
  23. public static function main()
  24. {
  25. $suite = new PHPUnit_Framework_TestSuite("Zend_Server_Method_DefinitionTest");
  26. $result = PHPUnit_TextUI_TestRunner::run($suite);
  27. }
  28. /**
  29. * Sets up the fixture, for example, open a network connection.
  30. * This method is called before a test is executed.
  31. *
  32. * @return void
  33. */
  34. public function setUp()
  35. {
  36. $this->definition = new Zend_Server_Method_Definition();
  37. }
  38. /**
  39. * Tears down the fixture, for example, close a network connection.
  40. * This method is called after a test is executed.
  41. *
  42. * @return void
  43. */
  44. public function tearDown()
  45. {
  46. }
  47. public function testCallbackShouldBeNullByDefault()
  48. {
  49. $this->assertNull($this->definition->getCallback());
  50. }
  51. public function testSetCallbackShouldAcceptMethodCallback()
  52. {
  53. $callback = new Zend_Server_Method_Callback();
  54. $this->definition->setCallback($callback);
  55. $test = $this->definition->getCallback();
  56. $this->assertSame($callback, $test);
  57. }
  58. public function testSetCallbackShouldAcceptArray()
  59. {
  60. $callback = array(
  61. 'type' => 'function',
  62. 'function' => 'foo',
  63. );
  64. $this->definition->setCallback($callback);
  65. $test = $this->definition->getCallback()->toArray();
  66. $this->assertSame($callback, $test);
  67. }
  68. public function testMethodHelpShouldBeEmptyStringByDefault()
  69. {
  70. $this->assertEquals('', $this->definition->getMethodHelp());
  71. }
  72. public function testMethodHelpShouldBeMutable()
  73. {
  74. $this->assertEquals('', $this->definition->getMethodHelp());
  75. $this->definition->setMethodHelp('foo bar');
  76. $this->assertEquals('foo bar', $this->definition->getMethodHelp());
  77. }
  78. public function testNameShouldBeNullByDefault()
  79. {
  80. $this->assertNull($this->definition->getName());
  81. }
  82. public function testNameShouldBeMutable()
  83. {
  84. $this->assertNull($this->definition->getName());
  85. $this->definition->setName('foo.bar');
  86. $this->assertEquals('foo.bar', $this->definition->getName());
  87. }
  88. public function testObjectShouldBeNullByDefault()
  89. {
  90. $this->assertNull($this->definition->getObject());
  91. }
  92. public function testObjectShouldBeMutable()
  93. {
  94. $this->assertNull($this->definition->getObject());
  95. $object = new stdClass;
  96. $this->definition->setObject($object);
  97. $this->assertEquals($object, $this->definition->getObject());
  98. }
  99. /**
  100. * @expectedException Zend_Server_Exception
  101. */
  102. public function testSettingObjectToNonObjectShouldThrowException()
  103. {
  104. $this->definition->setObject('foo');
  105. }
  106. public function testInvokeArgumentsShouldBeEmptyArrayByDefault()
  107. {
  108. $args = $this->definition->getInvokeArguments();
  109. $this->assertTrue(is_array($args));
  110. $this->assertTrue(empty($args));
  111. }
  112. public function testInvokeArgumentsShouldBeMutable()
  113. {
  114. $this->testInvokeArgumentsShouldBeEmptyArrayByDefault();
  115. $args = array('foo', array('bar', 'baz'), new stdClass);
  116. $this->definition->setInvokeArguments($args);
  117. $this->assertSame($args, $this->definition->getInvokeArguments());
  118. }
  119. public function testPrototypesShouldBeEmptyArrayByDefault()
  120. {
  121. $prototypes = $this->definition->getPrototypes();
  122. $this->assertTrue(is_array($prototypes));
  123. $this->assertTrue(empty($prototypes));
  124. }
  125. public function testDefinitionShouldAllowAddingSinglePrototypes()
  126. {
  127. $this->testPrototypesShouldBeEmptyArrayByDefault();
  128. $prototype1 = new Zend_Server_Method_Prototype;
  129. $this->definition->addPrototype($prototype1);
  130. $test = $this->definition->getPrototypes();
  131. $this->assertSame($prototype1, $test[0]);
  132. $prototype2 = new Zend_Server_Method_Prototype;
  133. $this->definition->addPrototype($prototype2);
  134. $test = $this->definition->getPrototypes();
  135. $this->assertSame($prototype1, $test[0]);
  136. $this->assertSame($prototype2, $test[1]);
  137. }
  138. public function testDefinitionShouldAllowAddingMultiplePrototypes()
  139. {
  140. $prototype1 = new Zend_Server_Method_Prototype;
  141. $prototype2 = new Zend_Server_Method_Prototype;
  142. $prototypes = array($prototype1, $prototype2);
  143. $this->definition->addPrototypes($prototypes);
  144. $this->assertSame($prototypes, $this->definition->getPrototypes());
  145. }
  146. public function testSetPrototypesShouldOverwriteExistingPrototypes()
  147. {
  148. $this->testDefinitionShouldAllowAddingMultiplePrototypes();
  149. $prototype1 = new Zend_Server_Method_Prototype;
  150. $prototype2 = new Zend_Server_Method_Prototype;
  151. $prototypes = array($prototype1, $prototype2);
  152. $this->assertNotSame($prototypes, $this->definition->getPrototypes());
  153. $this->definition->setPrototypes($prototypes);
  154. $this->assertSame($prototypes, $this->definition->getPrototypes());
  155. }
  156. public function testDefintionShouldSerializeToArray()
  157. {
  158. $name = 'foo.bar';
  159. $callback = array('function' => 'foo', 'type' => 'function');
  160. $prototypes = array(array('returnType' => 'struct', 'parameters' => array('string', 'array')));
  161. $methodHelp = 'foo bar';
  162. $object = new stdClass;
  163. $invokeArgs = array('foo', array('bar', 'baz'));
  164. $this->definition->setName($name)
  165. ->setCallback($callback)
  166. ->setPrototypes($prototypes)
  167. ->setMethodHelp($methodHelp)
  168. ->setObject($object)
  169. ->setInvokeArguments($invokeArgs);
  170. $test = $this->definition->toArray();
  171. $this->assertEquals($name, $test['name']);
  172. $this->assertEquals($callback, $test['callback']);
  173. $this->assertEquals($prototypes, $test['prototypes']);
  174. $this->assertEquals($methodHelp, $test['methodHelp']);
  175. $this->assertEquals($object, $test['object']);
  176. $this->assertEquals($invokeArgs, $test['invokeArguments']);
  177. }
  178. public function testPassingOptionsToConstructorShouldSetObjectState()
  179. {
  180. $options = array(
  181. 'name' => 'foo.bar',
  182. 'callback' => array('function' => 'foo', 'type' => 'function'),
  183. 'prototypes' => array(array('returnType' => 'struct', 'parameters' => array('string', 'array'))),
  184. 'methodHelp' => 'foo bar',
  185. 'object' => new stdClass,
  186. 'invokeArguments' => array('foo', array('bar', 'baz')),
  187. );
  188. $definition = new Zend_Server_Method_Definition($options);
  189. $test = $definition->toArray();
  190. $this->assertEquals($options['name'], $test['name']);
  191. $this->assertEquals($options['callback'], $test['callback']);
  192. $this->assertEquals($options['prototypes'], $test['prototypes']);
  193. $this->assertEquals($options['methodHelp'], $test['methodHelp']);
  194. $this->assertEquals($options['object'], $test['object']);
  195. $this->assertEquals($options['invokeArguments'], $test['invokeArguments']);
  196. }
  197. }
  198. // Call Zend_Server_Method_DefinitionTest::main() if this source file is executed directly.
  199. if (PHPUnit_MAIN_METHOD == "Zend_Server_Method_DefinitionTest::main") {
  200. Zend_Server_Method_DefinitionTest::main();
  201. }