DefinitionTest.php 9.0 KB

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