You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

127 lines
2.6 KiB

  1. <?php
  2. /**
  3. * PhpUnderControl_PhalApi_Test
  4. *
  5. * 针对 ../PhalApi.php PhalApi 类的PHPUnit单元测试
  6. *
  7. * @author: dogstar 20150209
  8. */
  9. require_once dirname(__FILE__) . '/test_env.php';
  10. if (!class_exists('PhalApi')) {
  11. require dirname(__FILE__) . '/../PhalApi.php';
  12. }
  13. class PhpUnderControl_PhalApi_Test extends PHPUnit_Framework_TestCase
  14. {
  15. public $phalApi;
  16. protected function setUp()
  17. {
  18. parent::setUp();
  19. $data = array(
  20. 'service' => 'AnotherImpl.doSth',
  21. );
  22. DI()->request = new PhalApi_Request($data);
  23. $this->phalApi = new PhalApi();
  24. }
  25. protected function tearDown()
  26. {
  27. DI()->response = 'PhalApi_Response_Json';
  28. }
  29. /**
  30. * @group testResponse
  31. */
  32. public function testResponseWithJsonMock()
  33. {
  34. DI()->response = 'PhalApi_Response_Json_Mock';
  35. $rs = $this->phalApi->response();
  36. $rs->output();
  37. $this->expectOutputString('{"ret":200,"data":"hello wolrd!","msg":""}');
  38. }
  39. /**
  40. * @group testResponse
  41. */
  42. public function testResponseWithJsonPMock()
  43. {
  44. DI()->response = new PhalApi_Response_JsonP_Mock('test');
  45. $rs = $this->phalApi->response();
  46. $rs->output();
  47. $this->expectOutputString('test({"ret":200,"data":"hello wolrd!","msg":""})');
  48. }
  49. /**
  50. * @group testResponse
  51. */
  52. public function testResponseWithExplorer()
  53. {
  54. DI()->response = 'PhalApi_Response_Explorer';
  55. $rs = $this->phalApi->response();
  56. $rs->output();
  57. $expRs = array (
  58. 'ret' => 200,
  59. 'data' => 'hello wolrd!',
  60. 'msg' => '',
  61. );
  62. $this->assertEquals($expRs, $rs->getResult());
  63. }
  64. public function testResponseWithBadRequest() {
  65. $data = array(
  66. 'service' => 'AnotherImpl',
  67. );
  68. DI()->request = new PhalApi_Request($data);
  69. DI()->response = 'PhalApi_Response_Json_Mock';
  70. $phalApi = new PhalApi();
  71. $rs = $phalApi->response();
  72. $rs->output();
  73. $this->expectOutputRegex('/"ret":400/');
  74. }
  75. /**
  76. * @expectedException Exception
  77. */
  78. public function testResponseWithException() {
  79. $data = array(
  80. 'service' => 'AnotherImpl.MakeSomeTrouble',
  81. );
  82. DI()->request = new PhalApi_Request($data);
  83. $rs = $this->phalApi->response();
  84. }
  85. }
  86. class Api_AnotherImpl extends PhalApi_Api {
  87. public function doSth() {
  88. return 'hello wolrd!';
  89. }
  90. public function makeSomeTrouble() {
  91. throw new Exception('as u can see, i mean to make some trouble');
  92. }
  93. }