2
0

DefinitionTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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-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. // Call Zend_Server_DefinitionTest::main() if this source file is executed directly.
  23. if (!defined("PHPUnit_MAIN_METHOD")) {
  24. define("PHPUnit_MAIN_METHOD", "Zend_Server_DefinitionTest::main");
  25. }
  26. require_once dirname(__FILE__) . '/../../TestHelper.php';
  27. /** Zend_Server_Definition */
  28. require_once 'Zend/Server/Definition.php';
  29. /** Zend_Server_Method_Definition */
  30. require_once 'Zend/Server/Method/Definition.php';
  31. /** Zend_Server_Method_Callback */
  32. require_once 'Zend/Server/Method/Callback.php';
  33. /** Zend_Server_Method_Prototype */
  34. require_once 'Zend/Server/Method/Prototype.php';
  35. /**
  36. * Test class for Zend_Server_Definition
  37. *
  38. * @category Zend
  39. * @package Zend_Server
  40. * @subpackage UnitTests
  41. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  42. * @license http://framework.zend.com/license/new-bsd New BSD License
  43. * @group Zend_Server
  44. */
  45. class Zend_Server_DefinitionTest extends PHPUnit_Framework_TestCase
  46. {
  47. /**
  48. * Runs the test methods of this class.
  49. *
  50. * @return void
  51. */
  52. public static function main()
  53. {
  54. $suite = new PHPUnit_Framework_TestSuite("Zend_Server_DefinitionTest");
  55. $result = PHPUnit_TextUI_TestRunner::run($suite);
  56. }
  57. /**
  58. * Sets up the fixture, for example, open a network connection.
  59. * This method is called before a test is executed.
  60. *
  61. * @return void
  62. */
  63. public function setUp()
  64. {
  65. $this->definition = new Zend_Server_Definition();
  66. }
  67. /**
  68. * Tears down the fixture, for example, close a network connection.
  69. * This method is called after a test is executed.
  70. *
  71. * @return void
  72. */
  73. public function tearDown()
  74. {
  75. }
  76. public function testMethodsShouldBeEmptyArrayByDefault()
  77. {
  78. $methods = $this->definition->getMethods();
  79. $this->assertTrue(is_array($methods));
  80. $this->assertTrue(empty($methods));
  81. }
  82. public function testDefinitionShouldAllowAddingSingleMethods()
  83. {
  84. $method = new Zend_Server_Method_Definition(array('name' => 'foo'));
  85. $this->definition->addMethod($method);
  86. $methods = $this->definition->getMethods();
  87. $this->assertEquals(1, count($methods));
  88. $this->assertSame($method, $methods['foo']);
  89. $this->assertSame($method, $this->definition->getMethod('foo'));
  90. }
  91. public function testDefinitionShouldAllowAddingMultipleMethods()
  92. {
  93. $method1 = new Zend_Server_Method_Definition(array('name' => 'foo'));
  94. $method2 = new Zend_Server_Method_Definition(array('name' => 'bar'));
  95. $this->definition->addMethods(array($method1, $method2));
  96. $methods = $this->definition->getMethods();
  97. $this->assertEquals(2, count($methods));
  98. $this->assertSame($method1, $methods['foo']);
  99. $this->assertSame($method1, $this->definition->getMethod('foo'));
  100. $this->assertSame($method2, $methods['bar']);
  101. $this->assertSame($method2, $this->definition->getMethod('bar'));
  102. }
  103. public function testSetMethodsShouldOverwriteExistingMethods()
  104. {
  105. $this->testDefinitionShouldAllowAddingMultipleMethods();
  106. $method1 = new Zend_Server_Method_Definition(array('name' => 'foo'));
  107. $method2 = new Zend_Server_Method_Definition(array('name' => 'bar'));
  108. $methods = array($method1, $method2);
  109. $this->assertNotEquals($methods, $this->definition->getMethods());
  110. $this->definition->setMethods($methods);
  111. $test = $this->definition->getMethods();
  112. $this->assertEquals(array_values($methods), array_values($test));
  113. }
  114. public function testHasMethodShouldReturnFalseWhenMethodNotRegisteredWithDefinition()
  115. {
  116. $this->assertFalse($this->definition->hasMethod('foo'));
  117. }
  118. public function testHasMethodShouldReturnTrueWhenMethodRegisteredWithDefinition()
  119. {
  120. $this->testDefinitionShouldAllowAddingMultipleMethods();
  121. $this->assertTrue($this->definition->hasMethod('foo'));
  122. }
  123. public function testDefinitionShouldAllowRemovingIndividualMethods()
  124. {
  125. $this->testDefinitionShouldAllowAddingMultipleMethods();
  126. $this->assertTrue($this->definition->hasMethod('foo'));
  127. $this->definition->removeMethod('foo');
  128. $this->assertFalse($this->definition->hasMethod('foo'));
  129. }
  130. public function testDefinitionShouldAllowClearingAllMethods()
  131. {
  132. $this->testDefinitionShouldAllowAddingMultipleMethods();
  133. $this->definition->clearMethods();
  134. $test = $this->definition->getMethods();
  135. $this->assertTrue(empty($test));
  136. }
  137. public function testDefinitionShouldSerializeToArray()
  138. {
  139. $method = array(
  140. 'name' => 'foo.bar',
  141. 'callback' => array(
  142. 'type' => 'function',
  143. 'function' => 'bar',
  144. ),
  145. 'prototypes' => array(
  146. array(
  147. 'returnType' => 'string',
  148. 'parameters' => array('string'),
  149. ),
  150. ),
  151. 'methodHelp' => 'Foo Bar!',
  152. 'invokeArguments' => array('foo'),
  153. );
  154. $definition = new Zend_Server_Definition();
  155. $definition->addMethod($method);
  156. $test = $definition->toArray();
  157. $this->assertEquals(1, count($test));
  158. $test = array_shift($test);
  159. $this->assertEquals($method['name'], $test['name']);
  160. $this->assertEquals($method['methodHelp'], $test['methodHelp']);
  161. $this->assertEquals($method['invokeArguments'], $test['invokeArguments']);
  162. $this->assertEquals($method['prototypes'][0]['returnType'], $test['prototypes'][0]['returnType']);
  163. }
  164. public function testPassingOptionsToConstructorShouldSetObjectState()
  165. {
  166. $method = array(
  167. 'name' => 'foo.bar',
  168. 'callback' => array(
  169. 'type' => 'function',
  170. 'function' => 'bar',
  171. ),
  172. 'prototypes' => array(
  173. array(
  174. 'returnType' => 'string',
  175. 'parameters' => array('string'),
  176. ),
  177. ),
  178. 'methodHelp' => 'Foo Bar!',
  179. 'invokeArguments' => array('foo'),
  180. );
  181. $options = array($method);
  182. $definition = new Zend_Server_Definition($options);
  183. $test = $definition->toArray();
  184. $this->assertEquals(1, count($test));
  185. $test = array_shift($test);
  186. $this->assertEquals($method['name'], $test['name']);
  187. $this->assertEquals($method['methodHelp'], $test['methodHelp']);
  188. $this->assertEquals($method['invokeArguments'], $test['invokeArguments']);
  189. $this->assertEquals($method['prototypes'][0]['returnType'], $test['prototypes'][0]['returnType']);
  190. }
  191. }
  192. // Call Zend_Server_DefinitionTest::main() if this source file is executed directly.
  193. if (PHPUnit_MAIN_METHOD == "Zend_Server_DefinitionTest::main") {
  194. Zend_Server_DefinitionTest::main();
  195. }