1
0

api.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Tom Needham <tom@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. class Test_API extends \Test\TestCase {
  9. // Helps build a response variable
  10. /**
  11. * @param string $message
  12. */
  13. function buildResponse($shipped, $data, $code, $message=null) {
  14. return array(
  15. 'shipped' => $shipped,
  16. 'response' => new OC_OCS_Result($data, $code, $message),
  17. 'app' => $this->getUniqueID('testapp_'),
  18. );
  19. }
  20. // Validate details of the result
  21. /**
  22. * @param OC_OCS_Result $result
  23. */
  24. function checkResult($result, $success) {
  25. // Check response is of correct type
  26. $this->assertInstanceOf('OC_OCS_Result', $result);
  27. // Check if it succeeded
  28. /** @var $result OC_OCS_Result */
  29. $this->assertEquals($success, $result->succeeded());
  30. }
  31. function dataProviderTestOneResult() {
  32. return array(
  33. array(100, true),
  34. array(101, false),
  35. array(997, false),
  36. );
  37. }
  38. /**
  39. * @dataProvider dataProviderTestOneResult
  40. *
  41. * @param $statusCode
  42. * @param $succeeded
  43. */
  44. public function testOneResult($statusCode, $succeeded) {
  45. // Setup some data arrays
  46. $data1 = array(
  47. 'users' => array(
  48. 'tom' => array(
  49. 'key' => 'value',
  50. ),
  51. 'frank' => array(
  52. 'key' => 'value',
  53. ),
  54. ));
  55. // Test merging one success result
  56. $response = $this->buildResponse(true, $data1, $statusCode);
  57. $result = OC_API::mergeResponses(array($response));
  58. $this->assertEquals($response['response'], $result);
  59. $this->checkResult($result, $succeeded);
  60. }
  61. function dataProviderTestMergeResponses() {
  62. return array(
  63. // Two shipped success results
  64. array(true, 100, true, 100, true),
  65. // Two shipped results, one success and one failure
  66. array(true, 100, true, 998, false),
  67. // Two shipped results, both failure
  68. array(true, 997, true, 998, false),
  69. // Two third party success results
  70. array(false, 100, false, 100, true),
  71. // Two third party results, one success and one failure
  72. array(false, 100, false, 998, false),
  73. // Two third party results, both failure
  74. array(false, 997, false, 998, false),
  75. // One of each, both success
  76. array(false, 100, true, 100, true),
  77. array(true, 100, false, 100, true),
  78. // One of each, both failure
  79. array(false, 997, true, 998, false),
  80. // One of each, shipped success
  81. array(false, 997, true, 100, true),
  82. // One of each, third party success
  83. array(false, 100, true, 998, false),
  84. );
  85. }
  86. /**
  87. * @dataProvider dataProviderTestMergeResponses
  88. *
  89. * Test the merging of multiple responses
  90. * @param $statusCode1
  91. * @param $statusCode2
  92. * @param $succeeded
  93. */
  94. public function testMultipleMergeResponses($shipped1, $statusCode1, $shipped2, $statusCode2, $succeeded){
  95. // Tests that app responses are merged correctly
  96. // Setup some data arrays
  97. $data1 = array(
  98. 'users' => array(
  99. 'tom' => array(
  100. 'key' => 'value',
  101. ),
  102. 'frank' => array(
  103. 'key' => 'value',
  104. ),
  105. ));
  106. $data2 = array(
  107. 'users' => array(
  108. 'tom' => array(
  109. 'key' => 'newvalue',
  110. ),
  111. 'jan' => array(
  112. 'key' => 'value',
  113. ),
  114. ));
  115. // Two shipped success results
  116. $result = OC_API::mergeResponses(array(
  117. $this->buildResponse($shipped1, $data1, $statusCode1, "message1"),
  118. $this->buildResponse($shipped2, $data2, $statusCode2, "message2"),
  119. ));
  120. $this->checkResult($result, $succeeded);
  121. $resultData = $result->getData();
  122. $resultMeta = $result->getMeta();
  123. $resultStatusCode = $result->getStatusCode();
  124. $this->assertArrayHasKey('jan', $resultData['users']);
  125. // check if the returned status message matches the selected status code
  126. if ($resultStatusCode === 997) {
  127. $this->assertEquals('message1', $resultMeta['message']);
  128. } elseif ($resultStatusCode === 998) {
  129. $this->assertEquals('message2', $resultMeta['message']);
  130. } elseif ($resultStatusCode === 100) {
  131. $this->assertEquals(null, $resultMeta['message']);
  132. }
  133. }
  134. }