api.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 PHPUnit_Framework_TestCase {
  9. // Helps build a response variable
  10. function buildResponse($shipped, $data, $code) {
  11. return array(
  12. 'shipped' => $shipped,
  13. 'response' => new OC_OCS_Result($data, $code),
  14. 'app' => uniqid('testapp_', true),
  15. );
  16. }
  17. // Validate details of the result
  18. function checkResult($result, $success) {
  19. // Check response is of correct type
  20. $this->assertInstanceOf('OC_OCS_Result', $result);
  21. // Check if it succeeded
  22. /** @var $result OC_OCS_Result */
  23. $this->assertEquals($success, $result->succeeded());
  24. }
  25. function dataProviderTestOneResult() {
  26. return array(
  27. array(100, true),
  28. array(101, true),
  29. array(997, false),
  30. );
  31. }
  32. /**
  33. * @dataProvider dataProviderTestOneResult
  34. *
  35. * @param $statusCode
  36. * @param $succeeded
  37. */
  38. public function testOneResult($statusCode, $succeeded) {
  39. // Setup some data arrays
  40. $data1 = array(
  41. 'users' => array(
  42. 'tom' => array(
  43. 'key' => 'value',
  44. ),
  45. 'frank' => array(
  46. 'key' => 'value',
  47. ),
  48. ));
  49. // Test merging one success result
  50. $response = $this->buildResponse(true, $data1, $statusCode);
  51. $result = OC_API::mergeResponses(array($response));
  52. $this->assertEquals($response['response'], $result);
  53. $this->checkResult($result, $succeeded);
  54. }
  55. function dataProviderTestMergeResponses() {
  56. return array(
  57. // Two shipped success results
  58. array(true, 100, true, 100, true),
  59. // Two shipped results, one success and one failure
  60. array(true, 100, true, 997, false),
  61. // Two shipped results, both failure
  62. array(true, 997, true, 997, false),
  63. // Two third party success results
  64. array(false, 100, false, 100, true),
  65. // Two third party results, one success and one failure
  66. array(false, 100, false, 997, false),
  67. // Two third party results, both failure
  68. array(false, 997, false, 997, false),
  69. // One of each, both success
  70. array(false, 100, true, 100, true),
  71. array(true, 100, false, 100, true),
  72. // One of each, both failure
  73. array(false, 997, true, 997, false),
  74. // One of each, shipped success
  75. array(false, 997, true, 100, true),
  76. // One of each, third party success
  77. array(false, 100, true, 997, false),
  78. );
  79. }
  80. /**
  81. * @dataProvider dataProviderTestMergeResponses
  82. *
  83. * Test the merging of multiple responses
  84. * @param $statusCode1
  85. * @param $statusCode2
  86. * @param $succeeded
  87. */
  88. public function testMultipleMergeResponses($shipped1, $statusCode1, $shipped2, $statusCode2, $succeeded){
  89. // Tests that app responses are merged correctly
  90. // Setup some data arrays
  91. $data1 = array(
  92. 'users' => array(
  93. 'tom' => array(
  94. 'key' => 'value',
  95. ),
  96. 'frank' => array(
  97. 'key' => 'value',
  98. ),
  99. ));
  100. $data2 = array(
  101. 'users' => array(
  102. 'tom' => array(
  103. 'key' => 'newvalue',
  104. ),
  105. 'jan' => array(
  106. 'key' => 'value',
  107. ),
  108. ));
  109. // Two shipped success results
  110. $result = OC_API::mergeResponses(array(
  111. $this->buildResponse($shipped1, $data1, $statusCode1),
  112. $this->buildResponse($shipped2, $data2, $statusCode2),
  113. ));
  114. $this->checkResult($result, $succeeded);
  115. $resultData = $result->getData();
  116. $this->assertArrayHasKey('jan', $resultData['users']);
  117. }
  118. }