From 959513fdc866910855b886051394962ab0ee582e Mon Sep 17 00:00:00 2001 From: tomneedham Date: Thu, 14 Nov 2013 00:40:09 +0000 Subject: [PATCH] Add tests for OC_API::mergeResponses() --- tests/lib/api.php | 159 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) create mode 100644 tests/lib/api.php diff --git a/tests/lib/api.php b/tests/lib/api.php new file mode 100644 index 00000000000..6cff1823d50 --- /dev/null +++ b/tests/lib/api.php @@ -0,0 +1,159 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +class Test_API extends PHPUnit_Framework_TestCase { + + // Helps build a response variable + public function buildResponse($shipped=true, $data=null, $code=100) { + return array( + 'shipped' => $shipped, + 'response' => new OC_OCS_Result($data, $code), + 'app' => uniqid('testapp_', true), + ); + } + + // Validate details of the result + public function checkResult($result, $success=true) { + // Check response is of correct type + $this->assertEquals('OC_OCS_Result', get_class($result)); + // CHeck if it succeeded + $this->assertEquals($success, $result->succeeded()); + } + + // Test the merging of multiple responses + public function testMergeResponses(){ + // Tests that app responses are merged correctly + // Setup some data arrays + $data1 = array( + 'users' => array( + 'tom' => array( + 'key' => 'value', + ), + 'frank' => array( + 'key' => 'value', + ), + )); + + $data2 = array( + 'users' => array( + 'tom' => array( + 'key' => 'newvalue', + ), + 'jan' => array( + 'key' => 'value', + ), + )); + // Test merging one success result + $response = $this->buildResponse(true, $data1); + $result = OC_API::mergeResponses(array($response)); + $this->assertEquals($response['response'], $result); + $this->checkResult($result); + + $response = $this->buildResponse(true, $data1, 101); + $result = OC_API::mergeResponses(array($response)); + $this->assertEquals($response['response'], $result); + $this->checkResult($result); + + $response = $this->buildResponse(true, $data1, 997); + $result = OC_API::mergeResponses(array($response)); + $this->assertEquals($response['response'], $result); + $this->checkResult($result, false); + + // Two shipped success results + $result = OC_API::mergeResponses(array( + $this->buildResponse(true, $data1O), + $this->buildResponse(true, $data2), + )); + $this->checkResult($result); + $resultData = $result->getData(); + $this->assertArrayHasKey('jan', $resultData['users']); + + // Two shipped results, one success and one failure + $result = OC_API::mergeResponses(array( + $this->buildResponse(true, $data1), + $this->buildResponse(true, $data2, 997), + )); + $this->checkResult($result, false); + $resultData = $result->getData(); + $this->assertArrayHasKey('jan', $resultData['users']); + + // Two shipped results, both failure + $result = OC_API::mergeResponses(array( + $this->buildResponse(true, $data1, 997), + $this->buildResponse(true, $data2, 997), + )); + $this->checkResult($result, false); + $resultData = $result->getData(); + $this->assertArrayHasKey('jan', $resultData['users']); + + // Two third party success results + $result = OC_API::mergeResponses(array( + $this->buildResponse(false, $data1), + $this->buildResponse(false, $data2), + )); + $this->checkResult($result); + $resultData = $result->getData(); + $this->assertArrayHasKey('jan', $resultData['users']); + + // Two third party results, one success and one failure + $result = OC_API::mergeResponses(array( + $this->buildResponse(false, $data1), + $this->buildResponse(false, $data2, 997), + )); + $this->checkResult($result, false); + $resultData = $result->getData(); + $this->assertArrayHasKey('jan', $resultData['users']); + + // Two third party results, both failure + $result = OC_API::mergeResponses(array( + $this->buildResponse(false, $data1, 997), + $this->buildResponse(false, $data2, 997), + )); + $this->checkResult($result, false); + $resultData = $result->getData(); + $this->assertArrayHasKey('jan', $resultData['users']); + + // One of each, both success + $result = OC_API::mergeResponses(array( + $this->buildResponse(false, $data1), + $this->buildResponse(true, $data2), + )); + $this->checkResult($result); + $resultData = $result->getData(); + $this->assertArrayHasKey('jan', $resultData['users']); + + // One of each, both failure + $result = OC_API::mergeResponses(array( + $this->buildResponse(false, $data1, 997), + $this->buildResponse(true, $data2, 997), + )); + $this->checkResult($result, false); + $resultData = $result->getData(); + $this->assertArrayHasKey('jan', $resultData['users']); + + // One of each, shipped success + $result = OC_API::mergeResponses(array( + $this->buildResponse(false, $data1, 997), + $this->buildResponse(true, $data2), + )); + $this->checkResult($result); + $resultData = $result->getData(); + $this->assertArrayHasKey('jan', $resultData['users']); + + // One of each, third party success + $result = OC_API::mergeResponses(array( + $this->buildResponse(false, $data1), + $this->buildResponse(true, $data2, 997), + )); + $this->checkResult($result, false); + $resultData = $result->getData(); + $this->assertArrayHasKey('jan', $resultData['users']); + + } + +} -- 2.39.5