diff --git a/src/Controller.php b/src/Controller.php index 930f149..7712c4c 100644 --- a/src/Controller.php +++ b/src/Controller.php @@ -20,18 +20,10 @@ class Controller * Endpoints that should not be publicly listed * @var string[] */ - private $hiddenEndpoints = [ + private $hiddenMethods = [ 'getIndexEndpoint' => true, // Value not used ]; - /** - * Controllers that should not be publicly listed - * @var string - */ - private $hiddenControllers = [ - - ]; - /** * The status object that represents an HTTP status * @var Status @@ -78,12 +70,12 @@ protected function setStatusCode($statusCode) * @return $this * @throws Exception */ - protected function hideEndpointMethod($methodName) + protected function hideMethod($methodName) { if (!method_exists($this, $methodName)) { throw new Exception(500, "The method '$methodName' does not exist in ".get_called_class()); } - $this->hiddenEndpoints[$methodName] = true; + $this->hiddenMethods[$methodName] = true; return $this; } @@ -93,13 +85,12 @@ protected function hideEndpointMethod($methodName) * @return bool * @throws Exception */ - public function isEndpointMethodHidden($methodName) + public function isMethodHidden($methodName) { if (!method_exists($this, $methodName)) { throw new Exception(500, "The method '$methodName' does not exist in ".get_called_class()); } - return is_array($this->hiddenEndpoints) - && isset($this->hiddenEndpoints[$methodName]); + return isset($this->hiddenMethods[$methodName]); } /** @@ -108,60 +99,13 @@ public function isEndpointMethodHidden($methodName) * @return $this * @throws Exception */ - protected function showEndpointMethod($methodName) - { - if (!method_exists($this, $methodName)) { - throw new Exception(500, "The method '$methodName' does not exist in ".get_called_class()); - } - if ($this->isEndpointMethodHidden($methodName)) { - unset($this->hiddenEndpoints[$methodName]); - } - return $this; - } - - /** - * Hide a controller - * @param $methodName - * @return $this - * @throws Exception - */ - protected function hideControllerMethod($methodName) - { - if (!method_exists($this, $methodName)) { - throw new Exception(500, "The method '$methodName' does not exist in ".get_called_class()); - } - $this->hiddenControllers[$methodName] = true; - return $this; - } - - /** - * Is a controller currently hidden - * @param $methodName - * @return bool - * @throws Exception - */ - public function isControllerMethodHidden($methodName) - { - if (!method_exists($this, $methodName)) { - throw new Exception(500, "The method '$methodName' does not exist in ".get_called_class()); - } - return is_array($this->hiddenControllers) - && isset($this->hiddenControllers[$methodName]); - } - - /** - * Show a hidden controller - * @param $methodName - * @return $this - * @throws Exception - */ - protected function showControllerMethod($methodName) + protected function showMethod($methodName) { if (!method_exists($this, $methodName)) { throw new Exception(500, "The method '$methodName' does not exist in ".get_called_class()); } - if ($this->isControllerMethodHidden($methodName)) { - unset($this->hiddenControllers[$methodName]); + if ($this->isMethodHidden($methodName)) { + unset($this->hiddenMethods[$methodName]); } return $this; } diff --git a/src/Exception.php b/src/Exception.php index 8807a19..9be4e06 100644 --- a/src/Exception.php +++ b/src/Exception.php @@ -86,7 +86,7 @@ public function jsonSerialize() 'code' => $this->getCode(), ]; $class = __CLASS__; - if($this->getPrevious() instanceof $class) { + if ($this->getPrevious() instanceof $class) { $serialized['previous'] = $this->getPrevious(); } return $serialized; diff --git a/src/Request.php b/src/Request.php index fc93dc6..e225ab6 100644 --- a/src/Request.php +++ b/src/Request.php @@ -136,6 +136,7 @@ protected function getRequestedUri($override = null) */ protected function useActualParameters() { + $this->setParameters($this->urlToParameters()); $this->setParameters($_REQUEST); $this->setParameters($this->parseHeader($_SERVER)); $this->setParameters($this->stringToObject($this->readBody())); @@ -175,6 +176,31 @@ protected function readBody() return @file_get_contents('php://input'); } + /** + * Turns a url string into an array of parameters + * @param string $url + * @return array + * @SuppressWarnings(PHPMD.Superglobals) + */ + public function urlToParameters($url = null) + { + $urlParameters = []; + if (is_null($url)) { + $url = array_key_exists('REQUEST_URI', $_SERVER) + ? $_SERVER['REQUEST_URI'] + : ''; + } + $url = is_null($url) ? parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) : $url; + $urlParts = explode('/', $url); + reset($urlParts); // Note, the first entry will always be blank + $key = next($urlParts); + while (($value = next($urlParts)) !== false) { + $urlParameters[$key] = $value; + $key = $value; + } + return $urlParameters; + } + /** * Tries to turn a string of data into an object. Accepts json, xml or a php serialised object * Failing all else it will return a standard class with the string attached to data @@ -227,9 +253,26 @@ public function getParameter($key, $default = null) if (array_key_exists($key, $this->parameters)) { return $this->parameters[$key]; } + // We can also flatten out the variable names to see if they exist + $flatKey = $this->flatten($key); + foreach ($this->parameters as $index => $value) { + if ($flatKey == $this->flatten($index)) { + return $value; + } + } return $default; } + /** + * Flatten a variable name by removing all non alpha numeric characters and making it lower case + * @param $name + * @return string + */ + protected function flatten($name) + { + return strtolower(preg_replace('/[\W_-]/', '', $name)); + } + /** * Returns all parameters. Does not return header or body parameters, maybe it should * @return array diff --git a/src/Router.php b/src/Router.php index e616886..da2a15f 100644 --- a/src/Router.php +++ b/src/Router.php @@ -105,7 +105,7 @@ public function getEndpoints(Controller $controller) $methods = get_class_methods($controller); foreach ($methods as $classMethod) { if (preg_match('/([a-z]+)([A-Z]\w+)Endpoint$/', $classMethod, $parts)) { - if (!$controller->isEndpointMethodHidden($classMethod)) { + if (!$controller->isMethodHidden($classMethod)) { $method = strtolower($parts[1]); $endpoint = $this->camelcaseToHyphenated($parts[2]); if (!array_key_exists($method, $endpoints)) { @@ -129,7 +129,7 @@ public function getControllers(Controller $controller) $controllers = []; foreach ($methods as $method) { if (preg_match('/(\w+)Controller$/', $method, $parts)) { - if (!$controller->isControllerMethodHidden($method)) { + if (!$controller->isMethodHidden($method)) { $controllers[] = $this->camelcaseToHyphenated($parts[1]); } } @@ -228,12 +228,13 @@ public function getMethodDocumentation(Controller $controller, $method) $parameters = array(); $nMatches = preg_match_all('/@param (\S+) \$?(\S+) ?([\S ]+)?/', $doc, $results); for ($i = 0; $i < $nMatches; $i++) { + $parameterName = $this->camelcaseToHyphenated($results[2][$i]); $parameter = new \stdClass(); $parameter->type = $results[1][$i]; if ($results[3][$i]) { $parameter->description = $results[3][$i]; } - $parameters[$results[2][$i]] = $parameter; + $parameters[$parameterName] = $parameter; } return [ diff --git a/tests/ApiTest.php b/tests/ApiTest.php index 95b157e..7fef3e8 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -86,81 +86,67 @@ public function testQuickStart() $this->assertContains( 'me', - $output->data->controllers, - "Controllers should have contained 'me'" + $output->data->controllers ); $this->assertContains( 'child', - $output->data->controllers, - "Controllers should have contained 'child'" + $output->data->controllers ); $this->assertNotContains( 'hidden-child', - $output->data->controllers, - "Controllers should not have contained 'hidden-child'" + $output->data->controllers ); $this->assertCount( 2, - $output->data->controllers, - "Controllers should have has 2 elements" + $output->data->controllers ); // Endpoints $this->assertObjectHasAttribute( 'information', - $output->data->endpoints->get, - "Get endpoints should have included 'information' it didn't".print_r($output->data, true) + $output->data->endpoints->get ); $this->assertSame( 'Gets some information', - $output->data->endpoints->get->information->description, - "Get Information description was wrong" + $output->data->endpoints->get->information->description ); - $this->assertCount( - 0, - $output->data->endpoints->get->information->parameters, - "Get Information description should not contain any parameters" + $this->assertEmpty( + $output->data->endpoints->get->information->parameters ); $this->assertObjectHasAttribute( 'more-information', - $output->data->endpoints->get, - "Get endpoints should have included more-information it didn't" + $output->data->endpoints->get ); $this->assertSame( 'Get some conditional information', - $output->data->endpoints->get->{'more-information'}->description, - "Get More Information description was wrong" + $output->data->endpoints->get->{'more-information'}->description ); $this->assertSame( 'string', - $output->data->endpoints->get->{'more-information'}->parameters->condition->type, - "Get More Information should take a string called condition" + $output->data->endpoints->get->{'more-information'}->parameters->condition->type ); $this->assertSame( 'The condition for the information', - $output->data->endpoints->get->{'more-information'}->parameters->condition->description, - "Get More Information parameter should be described as 'The condition for the information'" + $output->data->endpoints->get->{'more-information'}->parameters->condition->description ); $this->assertTrue( - count($output->data->endpoints->put) === 1, - "There should have been 1 get endpoints, there were: " . PHP_EOL . count($output->data->endpoints->put) + count($output->data->endpoints->put) === 1 ); $this->assertObjectHasAttribute( 'information', - $output->data->endpoints->put, - "Put endpoints should have included 'information' it didn't" + $output->data->endpoints->put ); } @@ -190,14 +176,12 @@ public function testInvalidEndpoint() $this->assertSame( $output->data, - "Could not find controller or endpoint matching 'not-a-real-endpoint'", - 'Exception should have been caught and returned an appropriate error to the user' + "Could not find controller or endpoint matching 'not-a-real-endpoint'" ); $this->assertSame( $response->getStatus()->getHttpHeader(), - 'HTTP/1.1 404 Not Found', - 'Incorrect header response, got '.$response->getStatus()->getHttpHeader() + 'HTTP/1.1 404 Not Found' ); } @@ -221,10 +205,8 @@ public function testSetRequest() $testRequest = $api->getRequest(); - $this->assertSame( - $testRequest->getParameter('key'), - null, - "Request should not contain a parameter for 'key'" + $this->assertNull( + $testRequest->getParameter('key') ); $api->setRequest($request); @@ -233,8 +215,7 @@ public function testSetRequest() $this->assertSame( $testRequest->getParameter('key'), - 'value', - "Request should contain a parameter for 'key'" + 'value' ); } @@ -255,10 +236,8 @@ public function testSetResponse() $testResponse = $api->getResponse(); - $this->assertSame( - $testResponse->getData(), - null, - "Response should not contain any data" + $this->assertNull( + $testResponse->getData() ); $api->setResponse($response); @@ -267,8 +246,7 @@ public function testSetResponse() $this->assertSame( $testResponse->getData(), - 'test-data', - "Request should contain test data" + 'test-data' ); } @@ -290,18 +268,18 @@ public function testFormatFactory() $testFormatFactory = $api->getFormatFactory(); - $this->assertTrue( - $testFormatFactory->getFormatterFor('xml') instanceof Xml, - "Response should not contain any data" + $this->assertInstanceOf( + 'AyeAye\Formatter\Formats\Xml', + $testFormatFactory->getFormatterFor('xml') ); $api->setFormatFactory($formatFactory); $testFormatFactory = $api->getFormatFactory(); - $this->assertTrue( - $testFormatFactory->getFormatterFor('php') instanceof Php, - "Response should not contain any data" + $this->assertInstanceOf( + 'AyeAye\Formatter\Formats\Php', + $testFormatFactory->getFormatterFor('php') ); } diff --git a/tests/ControllerTest.php b/tests/ControllerTest.php index 113b820..1f4ba26 100644 --- a/tests/ControllerTest.php +++ b/tests/ControllerTest.php @@ -69,32 +69,32 @@ public function testHiddenEndpoints() { $controller = new TestController(); $this->assertTrue( - $controller->isControllerMethodHidden('hiddenChildController') + $controller->isMethodHidden('hiddenChildController') ); $this->assertFalse( - $controller->isControllerMethodHidden('childController') + $controller->isMethodHidden('childController') ); - $hideControllerMethod = $this->getClassMethod($controller, 'hideControllerMethod'); + $hideControllerMethod = $this->getClassMethod($controller, 'hideMethod'); $hideControllerMethod->invoke($controller, 'childController'); - $showControllerMethod = $this->getClassMethod($controller, 'showControllerMethod'); + $showControllerMethod = $this->getClassMethod($controller, 'showMethod'); $showControllerMethod->invoke($controller, 'hiddenChildController'); $this->assertTrue( - $controller->isControllerMethodHidden('childController') + $controller->isMethodHidden('childController') ); $this->assertFalse( - $controller->isControllerMethodHidden('hiddenChildController') + $controller->isMethodHidden('hiddenChildController') ); $controller = new TestBrokenController(); $this->assertFalse( - $controller->isControllerMethodHidden('childController') + $controller->isMethodHidden('childController') ); - $hideControllerMethod = $this->getClassMethod($controller, 'hideControllerMethod'); + $hideControllerMethod = $this->getClassMethod($controller, 'hideMethod'); $hideControllerMethod->invoke($controller, 'childController'); $this->assertTrue( - $controller->isControllerMethodHidden('childController') + $controller->isMethodHidden('childController') ); } @@ -102,34 +102,34 @@ public function testHiddenControllers() { $controller = new TestController(); $this->assertTrue( - $controller->isEndpointMethodHidden('getHiddenEndpoint') + $controller->isMethodHidden('getHiddenEndpoint') ); $this->assertFalse( - $controller->isEndpointMethodHidden('getInformationEndpoint') + $controller->isMethodHidden('getInformationEndpoint') ); - $hideEndpointMethod = $this->getClassMethod($controller, 'hideEndpointMethod'); + $hideEndpointMethod = $this->getClassMethod($controller, 'hideMethod'); $hideEndpointMethod->invoke($controller, 'getInformationEndpoint'); - $showEndpointMethod = $this->getClassMethod($controller, 'showEndpointMethod'); + $showEndpointMethod = $this->getClassMethod($controller, 'showMethod'); $showEndpointMethod->invoke($controller, 'getHiddenEndpoint'); $this->assertTrue( - $controller->isEndpointMethodHidden('getInformationEndpoint') + $controller->isMethodHidden('getInformationEndpoint') ); $this->assertFalse( - $controller->isEndpointMethodHidden('getHiddenEndpoint') + $controller->isMethodHidden('getHiddenEndpoint') ); $controller = new TestBrokenController(); $this->assertFalse( - $controller->isEndpointMethodHidden('getInformationEndpoint') + $controller->isMethodHidden('getInformationEndpoint') ); - $hideEndpointMethod = $this->getClassMethod($controller, 'hideEndpointMethod'); + $hideEndpointMethod = $this->getClassMethod($controller, 'hideMethod'); $hideEndpointMethod->invoke($controller, 'getInformationEndpoint'); $this->assertTrue( - $controller->isEndpointMethodHidden('getInformationEndpoint') + $controller->isMethodHidden('getInformationEndpoint') ); } @@ -141,7 +141,7 @@ public function testHiddenControllers() public function testHideControllerException() { $controller = new TestController(); - $hideControllerMethod = $this->getClassMethod($controller, 'hideControllerMethod'); + $hideControllerMethod = $this->getClassMethod($controller, 'hideMethod'); $hideControllerMethod->invoke($controller, 'fakeController'); } @@ -153,7 +153,7 @@ public function testHideControllerException() public function testHideEndpointException() { $controller = new TestController(); - $hideEndpointMethod = $this->getClassMethod($controller, 'hideEndpointMethod'); + $hideEndpointMethod = $this->getClassMethod($controller, 'hideMethod'); $hideEndpointMethod->invoke($controller, 'fakeEndpoint'); } @@ -165,7 +165,7 @@ public function testHideEndpointException() public function testIsControllerHiddenException() { $controller = new TestController(); - $controller->isControllerMethodHidden('fakeController'); + $controller->isMethodHidden('fakeController'); } /** @@ -176,7 +176,7 @@ public function testIsControllerHiddenException() public function testIsEndpointHiddenException() { $controller = new TestController(); - $controller->isEndpointMethodHidden('fakeEndpoint'); + $controller->isMethodHidden('fakeEndpoint'); } /** @@ -187,7 +187,7 @@ public function testIsEndpointHiddenException() public function testShowControllerException() { $controller = new TestController(); - $showControllerMethod = $this->getClassMethod($controller, 'showControllerMethod'); + $showControllerMethod = $this->getClassMethod($controller, 'showMethod'); $showControllerMethod->invoke($controller, 'fakeController'); } @@ -199,7 +199,7 @@ public function testShowControllerException() public function testShowEndpointException() { $controller = new TestController(); - $showEndpointMethod = $this->getClassMethod($controller, 'showEndpointMethod'); + $showEndpointMethod = $this->getClassMethod($controller, 'showMethod'); $showEndpointMethod->invoke($controller, 'fakeEndpoint'); } } diff --git a/tests/ExceptionTest.php b/tests/ExceptionTest.php index 1a2ec39..743e58a 100644 --- a/tests/ExceptionTest.php +++ b/tests/ExceptionTest.php @@ -39,22 +39,19 @@ public function testPublicMessage() $message = $e->getMessage(); $this->assertSame( $testMessage, - $message, - "Exception messsage was not $testMessage" + $message ); $code = $e->getCode(); $this->assertSame( $testCode, - $code, - "Exception code was not $testCode" + $code ); $publicMessage = $e->getPublicMessage(); $this->assertSame( $testPublicMessage, - $publicMessage, - "Exception public message was not $testPublicMessage" + $publicMessage ); $previous = $e->getPrevious(); @@ -65,8 +62,7 @@ public function testPublicMessage() $this->assertSame( $previousException->getMessage(), - $previous->getMessage(), - "Previous exception not included" + $previous->getMessage() ); } } @@ -80,9 +76,9 @@ public function testDefaultPublicMessage() throw new Exception($testCode); } catch (Exception $e) { $publicMessage = $e->getPublicMessage(); - $this->assertTrue( - $publicMessage == $testPublicMessage, - "Exception public message was not $testPublicMessage: " . PHP_EOL . $publicMessage + $this->assertSame( + $testPublicMessage, + $publicMessage ); } } @@ -95,9 +91,9 @@ public function testCodeStatusMessage() throw new Exception(418); } catch (Exception $e) { $publicMessage = $e->getPublicMessage(); - $this->assertTrue( - $publicMessage == $testPublicMessage, - "Exception public message was not $testPublicMessage: " . PHP_EOL . $publicMessage + $this->assertSame( + $testPublicMessage, + $publicMessage ); } @@ -114,14 +110,12 @@ public function testCodeWithSystemMessage() } catch (Exception $e) { $this->assertSame( $testPublicMessage, - $e->getPublicMessage(), - "Exception public message incorrect" + $e->getPublicMessage() ); $this->assertSame( $systemMessage, - $e->getMessage(), - "Exception system message incorrect" + $e->getMessage() ); $previous = $e->getPrevious(); @@ -132,8 +126,7 @@ public function testCodeWithSystemMessage() $this->assertSame( $previousException->getMessage(), - $previous->getMessage(), - "Previous exception not included" + $previous->getMessage() ); } @@ -152,27 +145,27 @@ public function testJsonSerialization() $object = json_decode($json, true); - $count = count($object); - $this->assertTrue( - $count === 2, - 'There should only be 2 items in the array, there were: ' . PHP_EOL . $count + $this->assertCount( + 2, + $object ); $publicMessage = $object['message']; - $this->assertTrue( - $publicMessage === $testPublicMessage, - "Exception messsage was not $testMessage: " . PHP_EOL . $publicMessage + $this->assertSame( + $testPublicMessage, + $publicMessage ); $code = $object['code']; - $this->assertTrue( - $code === $testCode, - "Exception code was not $testCode: " . PHP_EOL . $code + $this->assertSame( + $testCode, + $code ); } - public function testExceptionChaining() { + public function testExceptionChaining() + { $testMessage = 'Message'; $testCode = 101; $testPublicMessage = 'Public Message'; @@ -184,10 +177,9 @@ public function testExceptionChaining() { $object = json_decode($json, true); - $count = count($object); - $this->assertTrue( - $count === 2, - 'There should only be 2 items in the array, there were: ' . PHP_EOL . $count + $this->assertCount( + 2, + $object ); $newException = new Exception($testPublicMessage, $testCode, $testMessage, $exception); @@ -196,10 +188,9 @@ public function testExceptionChaining() { $object = json_decode($json, true); - $count = count($object); - $this->assertTrue( - $count === 3, - 'There should be 3 items in the array, there were: ' . PHP_EOL . $count + $this->assertCount( + 3, + $object ); } } diff --git a/tests/RequestTest.php b/tests/RequestTest.php index 1b79c9b..d8300d3 100644 --- a/tests/RequestTest.php +++ b/tests/RequestTest.php @@ -33,20 +33,17 @@ public function testDefaultRequest() $method = $request->getMethod(); $this->assertSame( 'GET', - $method, - 'Method is not GET: ' . PHP_EOL . $method + $method ); $this->assertCount( 0, - $request->getParameters(), - 'No Parameters should have been defined' + $request->getParameters() ); $this->assertCount( 0, - $request->getRequestChain(), - 'There shouldn\'t be any elements in the request chain' + $request->getRequestChain() ); } @@ -60,8 +57,7 @@ public function testParseHeader() $headersSize = count($request->parseHeader()); $this->assertEquals( 0, - $headersSize, - 'There shouldn\'t be any headers' + $headersSize ); $_SERVER['CONTENT_TYPE'] = 'application/json'; @@ -71,29 +67,24 @@ public function testParseHeader() $request = new Request(); $headers = $request->parseHeader($_SERVER); - $headersSize = count($headers); - $this->assertEquals( + $this->assertCount( 3, - $headersSize, - 'There should be 3 headers' + $headers ); - $this->assertEquals( + $this->assertSame( $headers['Content-Type'], - $_SERVER['CONTENT_TYPE'], - 'Content-Type should have been set to application/json' + $_SERVER['CONTENT_TYPE'] ); - $this->assertEquals( + $this->assertSame( $headers['Content-Length'], - $_SERVER['CONTENT_LENGTH'], - 'Content-Length should have been set to application/json' + $_SERVER['CONTENT_LENGTH'] ); - $this->assertEquals( + $this->assertSame( $headers['Not-A-Real-Header'], - $_SERVER['HTTP_NOT_A_REAL_HEADER'], - 'Not-A-Real-Header should have been set to "Not a header"' + $_SERVER['HTTP_NOT_A_REAL_HEADER'] ); } @@ -106,26 +97,22 @@ public function testStringToObjectJson() $this->assertCount( 2, - $jsonObject->testArray, - 'testArray should contain 2 elements' + $jsonObject->testArray ); $this->assertSame( 1, - $jsonObject->testArray[0], - 'testArrays first element should be 1' + $jsonObject->testArray[0] ); $this->assertSame( true, - $jsonObject->testArray[1], - 'testArrays second element should be true' + $jsonObject->testArray[1] ); $this->assertSame( 'a string', - $jsonObject->testObject->string, - 'testObject should contain the string "a string"' + $jsonObject->testObject->string ); } @@ -136,14 +123,12 @@ public function testStringToObjectXml() $xmlObject = $request->stringToObject($xml); $this->assertTrue( - is_object($xmlObject), - 'testObject should be an object' + is_object($xmlObject) ); $this->assertEquals( 'a string', - $xmlObject->testObject->string, - 'testObject should contain the string "a string"' + $xmlObject->testObject->string ); } @@ -157,27 +142,89 @@ public function testStringToObjectPhp() $this->assertCount( 2, - $phpObject->testArray, - 'testArray should contain 2 elements' + $phpObject->testArray ); $this->assertSame( 1, - $phpObject->testArray[0], - 'testArrays first element should be 1' + $phpObject->testArray[0] ); $this->assertSame( true, - $phpObject->testArray[1], - 'testArrays second element should be true' + $phpObject->testArray[1] ); $this->assertSame( 'a string', - $phpObject->testObject->string, - 'testObject should contain the string "a string"' + $phpObject->testObject->string + ); + } + + public function testUrlToParameters() + { + $request = new Request(); + + $array = $request->urlToParameters(''); + $this->assertCount( + 0, + $array + ); + + $array = $request->urlToParameters('/'); + $this->assertCount( + 0, + $array + ); + + $array = $request->urlToParameters('/one'); + $this->assertCount( + 0, + $array + ); + + $array = $request->urlToParameters('/one/two'); + $this->assertCount( + 1, + $array + ); + + $array = $request->urlToParameters('/one/two/three'); + $this->assertCount( + 2, + $array + ); + + $array = $request->urlToParameters('/one/two/three/four'); + $this->assertCount( + 3, + $array + ); + $this->assertArrayHasKey( + 'one', + $array + ); + $this->assertSame( + 'two', + $array['one'] ); + $this->assertArrayHasKey( + 'two', + $array + ); + $this->assertSame( + 'three', + $array['two'] + ); + $this->assertArrayHasKey( + 'three', + $array + ); + $this->assertSame( + 'four', + $array['three'] + ); + } public function testStringToStringObject() @@ -185,12 +232,11 @@ public function testStringToStringObject() $string = 'string'; $request = new Request(); - $stringObejct = $request->stringToObject($string); + $stringObject = $request->stringToObject($string); $this->assertSame( $string, - $stringObejct->text, - 'String should have been "string"' + $stringObject->text ); } @@ -199,23 +245,20 @@ public function testGetMethod() $request = new Request(); $this->assertSame( Request::METHOD_GET, - $request->getMethod(), - 'Default method should be GET' + $request->getMethod() ); $_SERVER['REQUEST_METHOD'] = Request::METHOD_DELETE; // Tested later $request = new Request(Request::METHOD_POST); $this->assertSame( Request::METHOD_POST, - $request->getMethod(), - 'Method should be set to POST by constructor' + $request->getMethod() ); $request = new Request(); $this->assertSame( Request::METHOD_DELETE, - $request->getMethod(), - 'Method should be set to DELETE by $_SERVER' + $request->getMethod() ); } @@ -229,32 +272,27 @@ public function testGetParameter() ); $this->assertNull( - $request->getParameter('this-parameter-not-set'), - 'The default value for an unknown value' + $request->getParameter('this-parameter-not-set') ); $this->assertSame( true, - $request->getParameter('true'), - 'Test parameter "true" should be true' + $request->getParameter('true') ); $this->assertSame( false, - $request->getParameter('false'), - 'Test parameter "false" should be false' + $request->getParameter('false') ); $this->assertSame( 'a string', - $request->getParameter('bodyString'), - 'bodyString should be "a string"' + $request->getParameter('bodyString') ); $this->assertNull( - $request->getParameter('this-parameter-not-set'), - 'The default value for an unknown value' + $request->getParameter('this-parameter-not-set') ); } @@ -273,32 +311,27 @@ public function testJsonSerializable() $this->assertSame( Request::METHOD_POST, - $jsonObject->method, - 'Request method should be POST' + $jsonObject->method ); $this->assertSame( '/test/path.xml', - $jsonObject->requestedUri, - 'Requested URI should be /test/path.xml' + $jsonObject->requestedUri ); $this->assertSame( Request::METHOD_POST, - $jsonObject->method, - 'Request method should be POST' + $jsonObject->method ); $this->assertSame( '1', - $jsonObject->parameters->firstParameter, - 'First parameter should be 1' + $jsonObject->parameters->firstParameter ); $this->assertSame( 2, - $jsonObject->parameters->secondParameter, - 'Second parameter should be 2' + $jsonObject->parameters->secondParameter ); // Lets check the server variable is read too @@ -308,8 +341,7 @@ public function testJsonSerializable() $jsonObject = json_decode(json_encode($request)); $this->assertSame( $_SERVER['REQUEST_URI'], - $jsonObject->requestedUri, - "Request URI should be {$_SERVER['REQUEST_URI']}" + $jsonObject->requestedUri ); } @@ -340,27 +372,23 @@ public function testAddParameterFail() { $request = new Request(); $this->assertTrue( - $request->setParameter('name', 'value'), - "Add parameter should have returned true, it didn't" + $request->setParameter('name', 'value') ); $this->assertSame( 'value', - $request->getParameter('name'), - "Parameter should have been 'value'" + $request->getParameter('name') ); } public function testReadBodyDodgily() { - require_once 'TestData/http_get_request_body.php'; $request = new Request(); $this->assertSame( true, - $request->getParameter('hackedJson'), - 'Failed to utilise hacked http_get_request_body... I\'m not sure how I feel about that' + $request->getParameter('hackedJson') ); } @@ -371,22 +399,30 @@ public function testGetFormatFromUri() $uri = '/test/file.php'; $this->assertSame( 'php', - $request->getFormatFromUri($uri), - 'Format should be php' + $request->getFormatFromUri($uri) ); $uri = '/test/file.json'; $this->assertSame( 'json', - $request->getFormatFromUri($uri), - 'Format should be json' + $request->getFormatFromUri($uri) ); $uri = '/test/file.json?parameters=true'; $this->assertSame( 'json', - $request->getFormatFromUri($uri), - 'Format should be json' + $request->getFormatFromUri($uri) + ); + } + + public function testVariableFlattening() + { + $parameters = + [ 'nAmE' => 'string']; + $request = new Request(null, null, $parameters); + $this->assertSame( + 'string', + $request->getParameter('na-me') ); } } diff --git a/tests/ResponseTest.php b/tests/ResponseTest.php index 5df3b07..dade3c0 100644 --- a/tests/ResponseTest.php +++ b/tests/ResponseTest.php @@ -25,8 +25,7 @@ public function testSetData() $this->assertSame( $testData, - $response->getData(), - 'Data did not match test data' + $response->getData() ); } @@ -41,8 +40,7 @@ public function testSetStatus() $this->assertSame( $testStatus->getCode(), - $response->getStatus()->getCode(), - 'Status did not match test status' + $response->getStatus()->getCode() ); } @@ -57,8 +55,7 @@ public function testSetStatusCode() $this->assertSame( $testStatusCode, - $response->getStatus()->getCode(), - 'Status did not match test status' + $response->getStatus()->getCode() ); } @@ -103,20 +100,17 @@ public function testJsonSerializable() $this->assertSame( '418', - $responseObject->status->code, - 'The response object should contain status code 418' + $responseObject->status->code ); $this->assertSame( 'string', - $responseObject->data->string, - 'The response object should contain the string "string"' + $responseObject->data->string ); $this->assertSame( '/test/path', - $responseObject->request->requestedUri, - 'The response object should contain the string "/test/path"' + $responseObject->request->requestedUri ); } @@ -165,8 +159,7 @@ public function testDebugJsonRespond() $this->assertSame( $responseData, - $expectedXml, - "Response data not correct Expected:\n$expectedXml\nGot:\n$responseData" + $expectedXml ); } @@ -224,8 +217,7 @@ public function testDebugXmlRespond() $this->assertSame( $responseData, - $expectedXml, - "Response data not correct Expected:\n$expectedXml\nGot:\n$responseData" + $expectedXml ); } @@ -273,8 +265,7 @@ public function testXmlRespond() $this->assertSame( $responseData, - $expectedXml, - "Response data not correct Expected:\n$expectedXml\nGot:\n$responseData" + $expectedXml ); } @@ -318,8 +309,7 @@ public function testJsonRespond() $this->assertSame( $responseData, - $expectedXml, - "Response data not correct Expected:\n$expectedXml\nGot:\n$responseData" + $expectedXml ); } } diff --git a/tests/RouterTest.php b/tests/RouterTest.php index c579226..e15f067 100644 --- a/tests/RouterTest.php +++ b/tests/RouterTest.php @@ -81,9 +81,9 @@ public function testParseEndpointName() $expectedEndpoint = 'getIndexEndpoint'; $actualEndpoint = $parseEndpointName->invoke($router, $endpoint, $method); - $this->assertTrue( - $actualEndpoint === $expectedEndpoint, - "Expected $expectedEndpoint, is actually: " . PHP_EOL . $actualEndpoint + $this->assertSame( + $expectedEndpoint, + $actualEndpoint ); $endpoint = 'a-longer-name'; @@ -91,9 +91,9 @@ public function testParseEndpointName() $expectedEndpoint = 'postALongerNameEndpoint'; $actualEndpoint = $parseEndpointName->invoke($router, $endpoint, $method); - $this->assertTrue( - $actualEndpoint === $expectedEndpoint, - "Expected $expectedEndpoint, is actually: " . PHP_EOL . $actualEndpoint + $this->assertSame( + $expectedEndpoint, + $actualEndpoint ); } @@ -107,23 +107,24 @@ public function testGetControllers() $router = new Router(); $controllers = $router->getControllers($controller); - $this->assertTrue( - count($controllers) === 2 + $this->assertCount( + 2, + $controllers ); - $this->assertTrue( - in_array('child', $controllers), - 'Controllers should have included child' + $this->assertContains( + 'child', + $controllers ); - $this->assertTrue( - in_array('me', $controllers), - 'Controllers should have included me' + $this->assertContains( + 'me', + $controllers ); - $this->assertFalse( - in_array('hidden-child', $controllers), - 'Controllers should have included me' + $this->assertNotContains( + 'hidden-child', + $controllers ); } @@ -136,85 +137,84 @@ public function testDocumentController() $router = new Router(); $controller = new TestController(); $result = $router->documentController($controller); -//print_r($result); die; + // Controllers - $this->assertTrue( - in_array('me', $result->controllers), - "Controllers should have contained 'me'" + $this->assertContains( + 'me', + $result->controllers ); - $this->assertTrue( - in_array('child', $result->controllers), - "Controllers should have contained 'me'" + $this->assertContains( + 'child', + $result->controllers ); - $this->assertFalse( - in_array('hiddenChild', $result->controllers), - "Controllers should have contained 'me'" + $this->assertNotContains( + 'hiddenChild', + $result->controllers ); - $this->assertTrue( - count($result->controllers) == 2, - "Controllers should have has 2 elements, it had: " . PHP_EOL . count($result->controllers) + $this->assertCount( + 2, + $result->controllers ); // Endpoints - $this->assertTrue( - count($result->endpoints['get']) == 2, - "There should have been 2 get endpoints, there were: " . PHP_EOL . count($result->endpoints['get']) + $this->assertCount( + 2, + $result->endpoints['get'] ); - $this->assertTrue( - array_key_exists('information', $result->endpoints['get']), - "Get endpoints should have included 'information' it didn't" + $this->assertArrayHasKey( + 'information', + $result->endpoints['get'] ); - $this->assertTrue( - $result->endpoints['get']['information']['description'] === 'Gets some information', - "Get Information description was wrong" + $this->assertSame( + 'Gets some information', + $result->endpoints['get']['information']['description'] ); - $this->assertTrue( - count($result->endpoints['get']['information']['parameters']) === 0, - "Get Information description should not contain any parameters" + $this->assertCount( + 0, + $result->endpoints['get']['information']['parameters'] ); - $this->assertTrue( - array_key_exists('more-information', $result->endpoints['get']), - "Get endpoints should have included 'more-information' it didn't" + $this->assertArrayHasKey( + 'more-information', + $result->endpoints['get'] ); - $this->assertTrue( - $result->endpoints['get']['more-information']['description'] === 'Get some conditional information', - "Get More Information description was wrong" + $this->assertSame( + 'Get some conditional information', + $result->endpoints['get']['more-information']['description'] ); - $this->assertTrue( - count($result->endpoints['get']['more-information']['parameters']) === 1, - "Get More Information description should not contain any parameters" + $this->assertCount( + 1, + $result->endpoints['get']['more-information']['parameters'] ); - $this->assertTrue( - $result->endpoints['get']['more-information']['parameters']['condition']->type === 'string', - "Get More Information should take a string called condition" + $this->assertSame( + 'string', + $result->endpoints['get']['more-information']['parameters']['condition']->type ); - $this->assertTrue( + $this->assertSame( + 'The condition for the information', $result->endpoints['get']['more-information']['parameters']['condition']->description - === 'The condition for the information', - "Get More Information parameter should be described as 'The condition for the information'" ); - $this->assertTrue( - count($result->endpoints['put']) === 1, - "There should have been 1 get endpoints, there were: " . PHP_EOL . count($result->endpoints['put']) + $this->assertCount( + 1, + $result->endpoints['put'] ); - $this->assertTrue( - array_key_exists('information', $result->endpoints['put']), - "Put endpoints should have included 'information' it didn't" + $this->assertArrayHasKey( + 'information', + $result->endpoints['put'] ); } @@ -231,9 +231,9 @@ public function testDefaultIndexRoute() $controller = new TestController(); $result = $router->processRequest($request, $controller, []); - $this->assertTrue( - property_exists($result, 'endpoints'), - "Default index endpoint not hit" + $this->assertObjectHasAttribute( + 'endpoints', + $result ); } @@ -267,9 +267,9 @@ public function testAlternativeIndexRoute() $controller = new TestController(); $result = $router->processRequest($request, $controller); - $this->assertTrue( - property_exists($result, 'endpoints'), - "Alternative index endpoint not hit" + $this->assertObjectHasAttribute( + 'endpoints', + $result ); } @@ -311,10 +311,9 @@ public function testKnownRoute() $controller = new TestController(); $result = $router->processRequest($request, $controller); - $this->assertEquals( - $result, + $this->assertSame( 'information', - "Correct endpoint not hit" + $result ); } @@ -334,25 +333,21 @@ public function testParametersFromRequest() $controller = new TestController(); $result = $router->processRequest($request, $controller); - $this->assertEquals( - $result->param1, + $this->assertSame( 'string', - "Data not parsed correctly" + $result->param1 ); - $this->assertEquals( - $result->param2, + $this->assertSame( 9001, - "Data not parsed correctly" + $result->param2 ); - $this->assertEquals( - $result->param3, + $this->assertSame( true, - "Data not parsed correctly" + $result->param3 ); - $this->assertEquals( - $result->param4, + $this->assertSame( false, - "Data not parsed correctly" + $result->param4 ); } } diff --git a/tests/StatusTest.php b/tests/StatusTest.php index 88448a5..857002d 100644 --- a/tests/StatusTest.php +++ b/tests/StatusTest.php @@ -32,14 +32,14 @@ public function testJsonSerialisable() $status = new Status(418); $statusObject = json_decode(json_encode($status)); - $this->assertTrue( - $statusObject->code === 418, - 'Status code should be 418, is actually: ' . PHP_EOL . $statusObject->code + $this->assertSame( + 418, + $statusObject->code ); - $this->assertTrue( - $statusObject->message === 'I\'m a teapot', - 'Status message should be I\'m a teapot, is actually: ' . PHP_EOL . $statusObject->message + $this->assertSame( + 'I\'m a teapot', + $statusObject->message ); } @@ -58,10 +58,9 @@ public function testHttpHeader() $status = new Status(418); $header = $status->getHttpHeader(); - $headers = headers_list(); - $this->assertTrue( - 'HTTP/1.1 418 I\'m a teapot' === $header, - '418 Header not correct' + $this->assertSame( + 'HTTP/1.1 418 I\'m a teapot', + $header ); } } diff --git a/tests/TestData/TestBrokenController.php b/tests/TestData/TestBrokenController.php index 28fb0c3..241e910 100644 --- a/tests/TestData/TestBrokenController.php +++ b/tests/TestData/TestBrokenController.php @@ -13,9 +13,7 @@ class TestBrokenController extends Controller { - protected $hiddenEndpoints; - - protected $hiddenControllers; + protected $hiddenMethods; /** * Gets some information diff --git a/tests/TestData/TestController.php b/tests/TestData/TestController.php index bc553b9..e1a528a 100644 --- a/tests/TestData/TestController.php +++ b/tests/TestData/TestController.php @@ -14,8 +14,8 @@ class TestController extends Controller public function __construct() { - $this->hideControllerMethod('hiddenChildController'); - $this->hideEndpointMethod('getHiddenEndpoint'); + $this->hideMethod('hiddenChildController'); + $this->hideMethod('getHiddenEndpoint'); } /**