diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..5bde552 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,3 @@ +/tests export-ignore +/phpunit.xml export-ignore +/.gitattributes export-ignore \ No newline at end of file diff --git a/composer.json b/composer.json index 5e3e98d..00d5d62 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,8 @@ ], "require": { "php": ">=5.4.0", - "illuminate/support": "~4.0|~5.0" + "illuminate/support": "~4.0|~5.0", + "mockery/mockery": "^0.9.9" }, "require-dev": { "phpunit/phpunit": "~4.0" @@ -19,5 +20,10 @@ "Briedis\\ApiBuilder": "src/" } }, + "autoload-dev": { + "psr-4": { + "Briedis\\ApiBuilder\\Tests\\": "tests/" + } + }, "minimum-stability": "stable" } diff --git a/src/Briedis/ApiBuilder/Exceptions/InvalidParameterTypeException.php b/src/Briedis/ApiBuilder/Exceptions/InvalidParameterTypeException.php index 1739d14..046a132 100644 --- a/src/Briedis/ApiBuilder/Exceptions/InvalidParameterTypeException.php +++ b/src/Briedis/ApiBuilder/Exceptions/InvalidParameterTypeException.php @@ -1,4 +1,5 @@ registrar = $registrar; + } + + /** + * Generate a route for a given method + * @param Method $method + * @param array|Closure|null|string $routeAction + * @return self + */ + public function add(Method $method, $routeAction) + { + $mapping = [ + 'GET' => 'get', + 'POST' => 'post', + 'PUT' => 'put', + 'DELETE' => 'delete', + 'PATCH' => 'patch', + ]; + + if (!isset($mapping[$method::METHOD])) { + throw new \InvalidArgumentException('Unknown method type: ' . $method::METHOD); + } + + $routeMethod = $mapping[$method::METHOD]; + + call_user_func([$this->registrar, $routeMethod], $method::URI, $routeAction); + + return $this; + } +} \ No newline at end of file diff --git a/tests/MultiDepthValidatorTest.php b/tests/MultiDepthValidatorTest.php index 7986bd4..ff31bd2 100644 --- a/tests/MultiDepthValidatorTest.php +++ b/tests/MultiDepthValidatorTest.php @@ -1,10 +1,12 @@ mock = Mockery::mock(Registrar::class); + $this->builder = new RouteBuilder($this->mock); + } + + public function testGetMethod() + { + $method = new GetMethodStub; + $this->mock->shouldReceive('get')->with($method::URI, 'action')->once(); + $this->builder->add($method, 'action'); + } + + public function testPostMethod() + { + $method = new PostMethodStub; + $this->mock->shouldReceive('post')->with($method::URI, 'controller@method')->once(); + $this->builder->add($method, 'controller@method'); + } + + public function testInvalidMethod() + { + $method = new InvalidMethodStub; + self::setExpectedException(\InvalidArgumentException::class); + $this->builder->add($method, 'controller@method'); + } +} \ No newline at end of file diff --git a/tests/SingleDepthValidatorTest.php b/tests/SingleDepthValidatorTest.php index 7160581..e0e6fab 100644 --- a/tests/SingleDepthValidatorTest.php +++ b/tests/SingleDepthValidatorTest.php @@ -1,11 +1,13 @@