DefinitionTest.php 7.5 KB

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