DefinitionTest.php 9.0 KB

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