DefinitionTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. // Call Zend_Server_DefinitionTest::main() if this source file is executed directly.
  3. if (!defined("PHPUnit_MAIN_METHOD")) {
  4. define("PHPUnit_MAIN_METHOD", "Zend_Server_DefinitionTest::main");
  5. }
  6. require_once dirname(__FILE__) . '/../../TestHelper.php';
  7. /** Zend_Server_Definition */
  8. require_once 'Zend/Server/Definition.php';
  9. /** Zend_Server_Method_Definition */
  10. require_once 'Zend/Server/Method/Definition.php';
  11. /** Zend_Server_Method_Callback */
  12. require_once 'Zend/Server/Method/Callback.php';
  13. /** Zend_Server_Method_Prototype */
  14. require_once 'Zend/Server/Method/Prototype.php';
  15. /**
  16. * Test class for Zend_Server_Definition
  17. */
  18. class Zend_Server_DefinitionTest extends PHPUnit_Framework_TestCase
  19. {
  20. /**
  21. * Runs the test methods of this class.
  22. *
  23. * @return void
  24. */
  25. public static function main()
  26. {
  27. $suite = new PHPUnit_Framework_TestSuite("Zend_Server_DefinitionTest");
  28. $result = PHPUnit_TextUI_TestRunner::run($suite);
  29. }
  30. /**
  31. * Sets up the fixture, for example, open a network connection.
  32. * This method is called before a test is executed.
  33. *
  34. * @return void
  35. */
  36. public function setUp()
  37. {
  38. $this->definition = new Zend_Server_Definition();
  39. }
  40. /**
  41. * Tears down the fixture, for example, close a network connection.
  42. * This method is called after a test is executed.
  43. *
  44. * @return void
  45. */
  46. public function tearDown()
  47. {
  48. }
  49. public function testMethodsShouldBeEmptyArrayByDefault()
  50. {
  51. $methods = $this->definition->getMethods();
  52. $this->assertTrue(is_array($methods));
  53. $this->assertTrue(empty($methods));
  54. }
  55. public function testDefinitionShouldAllowAddingSingleMethods()
  56. {
  57. $method = new Zend_Server_Method_Definition(array('name' => 'foo'));
  58. $this->definition->addMethod($method);
  59. $methods = $this->definition->getMethods();
  60. $this->assertEquals(1, count($methods));
  61. $this->assertSame($method, $methods['foo']);
  62. $this->assertSame($method, $this->definition->getMethod('foo'));
  63. }
  64. public function testDefinitionShouldAllowAddingMultipleMethods()
  65. {
  66. $method1 = new Zend_Server_Method_Definition(array('name' => 'foo'));
  67. $method2 = new Zend_Server_Method_Definition(array('name' => 'bar'));
  68. $this->definition->addMethods(array($method1, $method2));
  69. $methods = $this->definition->getMethods();
  70. $this->assertEquals(2, count($methods));
  71. $this->assertSame($method1, $methods['foo']);
  72. $this->assertSame($method1, $this->definition->getMethod('foo'));
  73. $this->assertSame($method2, $methods['bar']);
  74. $this->assertSame($method2, $this->definition->getMethod('bar'));
  75. }
  76. public function testSetMethodsShouldOverwriteExistingMethods()
  77. {
  78. $this->testDefinitionShouldAllowAddingMultipleMethods();
  79. $method1 = new Zend_Server_Method_Definition(array('name' => 'foo'));
  80. $method2 = new Zend_Server_Method_Definition(array('name' => 'bar'));
  81. $methods = array($method1, $method2);
  82. $this->assertNotEquals($methods, $this->definition->getMethods());
  83. $this->definition->setMethods($methods);
  84. $test = $this->definition->getMethods();
  85. $this->assertEquals(array_values($methods), array_values($test));
  86. }
  87. public function testHasMethodShouldReturnFalseWhenMethodNotRegisteredWithDefinition()
  88. {
  89. $this->assertFalse($this->definition->hasMethod('foo'));
  90. }
  91. public function testHasMethodShouldReturnTrueWhenMethodRegisteredWithDefinition()
  92. {
  93. $this->testDefinitionShouldAllowAddingMultipleMethods();
  94. $this->assertTrue($this->definition->hasMethod('foo'));
  95. }
  96. public function testDefinitionShouldAllowRemovingIndividualMethods()
  97. {
  98. $this->testDefinitionShouldAllowAddingMultipleMethods();
  99. $this->assertTrue($this->definition->hasMethod('foo'));
  100. $this->definition->removeMethod('foo');
  101. $this->assertFalse($this->definition->hasMethod('foo'));
  102. }
  103. public function testDefinitionShouldAllowClearingAllMethods()
  104. {
  105. $this->testDefinitionShouldAllowAddingMultipleMethods();
  106. $this->definition->clearMethods();
  107. $test = $this->definition->getMethods();
  108. $this->assertTrue(empty($test));
  109. }
  110. public function testDefinitionShouldSerializeToArray()
  111. {
  112. $method = array(
  113. 'name' => 'foo.bar',
  114. 'callback' => array(
  115. 'type' => 'function',
  116. 'function' => 'bar',
  117. ),
  118. 'prototypes' => array(
  119. array(
  120. 'returnType' => 'string',
  121. 'parameters' => array('string'),
  122. ),
  123. ),
  124. 'methodHelp' => 'Foo Bar!',
  125. 'invokeArguments' => array('foo'),
  126. );
  127. $definition = new Zend_Server_Definition();
  128. $definition->addMethod($method);
  129. $test = $definition->toArray();
  130. $this->assertEquals(1, count($test));
  131. $test = array_shift($test);
  132. $this->assertEquals($method['name'], $test['name']);
  133. $this->assertEquals($method['methodHelp'], $test['methodHelp']);
  134. $this->assertEquals($method['invokeArguments'], $test['invokeArguments']);
  135. $this->assertEquals($method['prototypes'][0]['returnType'], $test['prototypes'][0]['returnType']);
  136. }
  137. public function testPassingOptionsToConstructorShouldSetObjectState()
  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. $options = array($method);
  155. $definition = new Zend_Server_Definition($options);
  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. }
  165. // Call Zend_Server_DefinitionTest::main() if this source file is executed directly.
  166. if (PHPUnit_MAIN_METHOD == "Zend_Server_DefinitionTest::main") {
  167. Zend_Server_DefinitionTest::main();
  168. }