diff --git a/log/log.txt b/log/log.txt new file mode 100644 index 0000000..f603907 --- /dev/null +++ b/log/log.txt @@ -0,0 +1,15 @@ +(n-a) - [13.10.2018 16:36:29] - 400 NoArguments - 0.41ms - (none) +(n-a) - [13.10.2018 16:36:29] - 400 NoArguments - 0.75ms - (none) +(n-a) - [13.10.2018 16:36:29] - 400 MissingMethod - 1.06ms - (none) +(n-a) - [13.10.2018 16:36:29] - 400 MissingMethod - 0.04ms - (none) +(n-a) - [13.10.2018 16:36:29] - 400 MissingMethod - 0.05ms - (none) +(n-a) - [13.10.2018 16:36:29] - 400 BadMethod - 0.06ms - someNonExistentMockFunction() +(n-a) - [13.10.2018 16:36:29] - 400 BadMethod - 0.06ms - someNonExistentMockFunction() +(n-a) - [13.10.2018 16:36:29] - 400 TooFewArgs - 0.25ms - someMockFunction() +(n-a) - [13.10.2018 16:36:29] - 400 TooFewArgs - 0.16ms - someMockFunction(val) +(n-a) - [13.10.2018 16:36:29] - 400 TooManyArgs - 0.18ms - someMockFunction(val, val, val, val) +(n-a) - [13.10.2018 16:36:29] - 200 - 0.03ms - someMockFunction(val, val) +(n-a) - [13.10.2018 16:36:29] - 200 - 0.02ms - someMockFunction(val, val, val) +(n-a) - [13.10.2018 16:36:29] - 200 - 0.02ms - someMockFunction(val, val, val) +(n-a) - [13.10.2018 16:36:29] - 200 - 0.03ms - someOtherMockFunction(one, two, threeOpt) +(n-a) - [13.10.2018 16:36:29] - 200 - 0.02ms - someOtherMockFunction(two, threeOpt, one) diff --git a/src/Webservice/.WebService.php.swp b/src/Webservice/.WebService.php.swp index 4a0509c..c6db1db 100644 Binary files a/src/Webservice/.WebService.php.swp and b/src/Webservice/.WebService.php.swp differ diff --git a/tests/Webservice/.MockWebserviceSubclass.php.swp b/tests/Webservice/.MockWebserviceSubclass.php.swp index 4245311..566dc8e 100644 Binary files a/tests/Webservice/.MockWebserviceSubclass.php.swp and b/tests/Webservice/.MockWebserviceSubclass.php.swp differ diff --git a/tests/Webservice/.WebserviceTest.php.swp b/tests/Webservice/.WebserviceTest.php.swp index 5a8eca6..f8519d6 100644 Binary files a/tests/Webservice/.WebserviceTest.php.swp and b/tests/Webservice/.WebserviceTest.php.swp differ diff --git a/tests/Webservice/MockWebserviceSubclass.php b/tests/Webservice/MockWebserviceSubclass.php index 06fde41..2a2a714 100644 --- a/tests/Webservice/MockWebserviceSubclass.php +++ b/tests/Webservice/MockWebserviceSubclass.php @@ -7,4 +7,13 @@ class MockWebserviceSubclass extends Webservice { return 'Some Mock Function Return'; } + + public function someOtherMockFunction($one, $two, $threeOpt = null) + { + return array( + 'one' => $one, + 'two' => $two, + 'threeOpt' => $threeOpt + ); + } } diff --git a/tests/Webservice/WebserviceTest.php b/tests/Webservice/WebserviceTest.php index 464334e..31cc2fc 100644 --- a/tests/Webservice/WebserviceTest.php +++ b/tests/Webservice/WebserviceTest.php @@ -45,10 +45,6 @@ class WebserviceTest extends TestCase */ public function testReturnsErrorNoArgumentsWhenNoParamtersInGetPostRequest() { - $this->removeLogs(); - $log = Logger::getLastLogs(); - $this->assertEquals(Logger::NO_LOG_YET_MSG, $log); - $_GET = array(); $_POST = array(); $this->webservice->run(false); @@ -60,10 +56,6 @@ class WebserviceTest extends TestCase public function testDoesNotReturnErrorNoArgumentsWhenParamtersInGetRequest() { - $this->removeLogs(); - $log = Logger::getLastLogs(); - $this->assertEquals(Logger::NO_LOG_YET_MSG, $log); - $_GET = array('a' => 'a1'); $this->webservice->run(false); @@ -75,26 +67,17 @@ class WebserviceTest extends TestCase public function testDoesNotReturnErrorNoArgumentsWhenParamtersInPostRequest() { - $this->removeLogs(); - $log = Logger::getLastLogs(); - $this->assertEquals(Logger::NO_LOG_YET_MSG, $log); - $_POST = array('a' => 'a1'); $this->webservice->run(false); $response = ob_get_clean(); $responseArray = json_decode($response, true); - var_dump($responseArray); $this->assertNotEquals(100, $responseArray['error']['code']); $this->assertNotEquals('NoArguments', $responseArray['error']['name']); } public function testReturnsErrorMissingMethodWhenNoFuncParamInRequest() { - $this->removeLogs(); - $log = Logger::getLastLogs(); - $this->assertEquals(Logger::NO_LOG_YET_MSG, $log); - $_POST = array('a' => 'a1'); $this->webservice->run(false); @@ -106,27 +89,153 @@ class WebserviceTest extends TestCase public function testDoesNotReturnErrorMissingMethodWhenFuncParamInRequest() { - $this->removeLogs(); - $log = Logger::getLastLogs(); - $this->assertEquals(Logger::NO_LOG_YET_MSG, $log); + $_GET = array('func' => 'someNonExistentMockFunction'); $_POST = array(); + $this->webservice->run(false); + $response = ob_get_clean(); + $responseArray = json_decode($response, true); + $this->assertNotEquals(101, $responseArray['error']['code']); + $this->assertNotEquals('MissingMethod', $responseArray['error']['name']); + } + + public function testReturnsErrorBadMethodWhenMethodNotExistsFuncParamInRequest() + { + $_GET = array('func' => 'someNonExistentMockFunction'); $_POST = array(); + + $this->webservice->run(false); + $response = ob_get_clean(); + $responseArray = json_decode($response, true); + $this->assertEquals(102, $responseArray['error']['code']); + $this->assertEquals('BadMethod', $responseArray['error']['name']); + } + + public function testReturnsErrorTooFewArgsWhenMethodNeedsMoreParamsInRequest() + { $_GET = array('func' => 'someMockFunction'); $_POST = array(); $this->webservice->run(false); $response = ob_get_clean(); $responseArray = json_decode($response, true); - $this->assertEquals(100, $responseArray['error']['code']); + $this->assertEquals(103, $responseArray['error']['code']); + $this->assertEquals('TooFewArgs', $responseArray['error']['name']); } - public function testRunThrowsAUsageExceptionWhenFuncParamIsNotCallableOnSubclass() + public function testReturnsErrorTooFewArgsWhenMethodNeedsEvenMoreParamsInRequest() { + $_GET = array( + 'func' => 'someMockFunction', + 'wrongParamName' => 'val', + ); + + $this->webservice->run(false); + $response = ob_get_clean(); + $responseArray = json_decode($response, true); + $this->assertEquals(103, $responseArray['error']['code']); + $this->assertEquals('TooFewArgs', $responseArray['error']['name']); } - public function testRunThrowsAUsageExceptionWhenMissingRequiredParamsInRequest() + public function testReturnsErrorTooManyArgsWhenMethodHasLessParamsThanInRequest() { + $_GET = array( + 'param1' => 'val', + 'param2' => 'val', + 'param3' => 'val', + 'func' => 'someMockFunction', + 'param4' => 'val', + ); + + $this->webservice->run(false); + $response = ob_get_clean(); + $responseArray = json_decode($response, true); + $this->assertEquals(104, $responseArray['error']['code']); + $this->assertEquals('TooManyArgs', $responseArray['error']['name']); + } + + public function testWebserviceSubclassMethodParamNamesDontMatterAsLongThatThereIsTheRightCount() + { + $_GET = array( + 'func' => 'someMockFunction', + 'wrongParamName' => 'val', + 'wrongParamName2' => 'val', + ); + + $this->webservice->run(false); + $response = ob_get_clean(); + $responseArray = json_decode($response, true); + $this->assertEquals(true, isset($responseArray['result'])); + } + + public function testReturnsResultIfProvidedAllParameters() + { + $_GET = array( + 'func' => 'someMockFunction', + 'anyParamName1' => 'val', + 'anyParamName2' => 'val', + 'anyParamName3' => 'val', + ); + + $this->webservice->run(false); + $response = ob_get_clean(); + $responseArray = json_decode($response, true); + $this->assertEquals(true, isset($responseArray['result'])); } - public function testRunThrowsAUsageExceptionWhenTooManyParamsInRequest() + public function testReturnsResultFormattedAsMethodNameAndReturnValue() { + $_GET = array( + 'func' => 'someMockFunction', + 'anyParamName1' => 'val', + 'anyParamName2' => 'val', + 'anyParamName3' => 'val', + ); + + $this->webservice->run(false); + $response = ob_get_clean(); + $responseArray = json_decode($response, true); + $this->assertEquals(array( + 'someMockFunction' => 'Some Mock Function Return' + ), $responseArray['result']); + } + + public function testOrderOfParamatersMatchesArrayOrder() + { + $_GET = array( + 'func' => 'someOtherMockFunction', + 'anyParamName1' => 'one', + 'anyParamName2' => 'two', + 'anyParamName3' => 'threeOpt', + ); + + $this->webservice->run(false); + $response = ob_get_clean(); + $responseArray = json_decode($response, true); + $this->assertEquals(array( + 'someOtherMockFunction' => array( + 'one' => 'one', + 'two' => 'two', + 'threeOpt' => 'threeOpt', + ) + ), $responseArray['result']); + } + + public function testOrderOfParamatersMatchesArrayMixedOrder() + { + $_GET = array( + 'func' => 'someOtherMockFunction', + 'anyParamName2' => 'two', + 'anyParamName3' => 'threeOpt', + 'anyParamName1' => 'one', + ); + + $this->webservice->run(false); + $response = ob_get_clean(); + $responseArray = json_decode($response, true); + $this->assertEquals(array( + 'someOtherMockFunction' => array( + 'one' => 'two', + 'two' => 'threeOpt', + 'threeOpt' => 'one', + ) + ), $responseArray['result']); } }