diff --git a/Bonder/Collections/Map.php b/Bonder/Collections/Map.php index 4c59b70..075de6f 100644 --- a/Bonder/Collections/Map.php +++ b/Bonder/Collections/Map.php @@ -95,6 +95,17 @@ public function get($key, $default = null) { public function set($key, $value) { $this->values[$key] = $value; } + + /** + * Sets all the values from the $otherMap. + * + * @param Map $otherMap the other map. + */ + public function setAll(Map $otherMap) { + foreach ($otherMap->values as $key => &$value) { + $this->set($key, $value); + } + } /** * Removes the specified key from the map. diff --git a/Bonder/Controller.php b/Bonder/Controller.php index e9301f8..3febd39 100644 --- a/Bonder/Controller.php +++ b/Bonder/Controller.php @@ -17,4 +17,11 @@ interface Controller { */ public function service(\Bonder\Request $request); + /** Returns the Filters configuration. Should return an array + * mapping alias names to filter classes. + * + * @return array An array (alias => filter class name). + */ + public function getFilters(); + } \ No newline at end of file diff --git a/Bonder/Controllers/FixedResponseController.php b/Bonder/Controllers/FixedResponseController.php index a5b1f3c..9af5bd9 100644 --- a/Bonder/Controllers/FixedResponseController.php +++ b/Bonder/Controllers/FixedResponseController.php @@ -26,5 +26,13 @@ public function __construct(\Bonder\Response $response) { public function service(\Bonder\Request $request) { return $this->response; } - + + /** Returns the Filters configuration. Should return an array + * mapping alias names to filter classes. + * + * @return array An array (alias => filter class name). + */ + public function getFilters() { + return array(); + } } \ No newline at end of file diff --git a/Bonder/Controllers/LambdaController.php b/Bonder/Controllers/LambdaController.php index f91c6c8..8de443c 100644 --- a/Bonder/Controllers/LambdaController.php +++ b/Bonder/Controllers/LambdaController.php @@ -31,5 +31,13 @@ public function service(\Bonder\Request $request) { $function = $this->lambda; return $function($request); } - + + /** Returns the Filters configuration. Should return an array + * mapping alias names to filter classes. + * + * @return array An array (alias => filter class name). + */ + public function getFilters() { + return array(); + } } \ No newline at end of file diff --git a/Bonder/Filters/ConfiguredFiltersProvider.php b/Bonder/Filters/ConfiguredFiltersProvider.php index 96b6166..7a65c93 100644 --- a/Bonder/Filters/ConfiguredFiltersProvider.php +++ b/Bonder/Filters/ConfiguredFiltersProvider.php @@ -1,6 +1,8 @@ wrappedProvider = $wrappedProvider; $this->configurator = $configurator; @@ -38,8 +40,8 @@ public function __construct( * (non-PHPdoc) * @see Bonder\Filters.FiltersProvider::getFilters() */ - public function getFilters($uri) { - $result = $this->wrappedProvider->getFilters($uri); + public function getFilters($uri, Controller $controller) { + $result = $this->wrappedProvider->getFilters($uri, $controller); $configurator = $this->configurator; array_walk($result, function($filter) use ($configurator) { $configurator->configure($filter); diff --git a/Bonder/Filters/CrafterFilterChainProvider.php b/Bonder/Filters/CrafterFilterChainProvider.php index ff010e6..2c2268e 100644 --- a/Bonder/Filters/CrafterFilterChainProvider.php +++ b/Bonder/Filters/CrafterFilterChainProvider.php @@ -43,7 +43,7 @@ public function __construct( */ public function get($uri) { $controllerResult = $this->controllerProvider->getResult($uri); - $filters = $this->filtersProvider->getFilters($uri); + $filters = $this->filtersProvider->getFilters($uri, $controllerResult->getController()); return new \Bonder\Filters\FilterChainProviderResult( new \Bonder\Filters\SimpleFilterChain($filters, $controllerResult->getController()), $controllerResult->getUriVariables()); diff --git a/Bonder/Filters/FilterChain.php b/Bonder/Filters/FilterChain.php index 002793d..eaca235 100644 --- a/Bonder/Filters/FilterChain.php +++ b/Bonder/Filters/FilterChain.php @@ -30,6 +30,6 @@ public function getFilters(); * @param \Bonder\Request $request the request. * @return \Bonder\Response the response. */ - public function call(\Bonder\Request $request); + public function execute(\Bonder\Request $request); } \ No newline at end of file diff --git a/Bonder/Filters/FiltersProvider.php b/Bonder/Filters/FiltersProvider.php index 70afa1e..3123433 100644 --- a/Bonder/Filters/FiltersProvider.php +++ b/Bonder/Filters/FiltersProvider.php @@ -1,20 +1,21 @@ filter. */ - public function getFilters($uri); - + public function getFilters($uri, Controller $controller); + } \ No newline at end of file diff --git a/Bonder/Filters/LambdaFilter.php b/Bonder/Filters/LambdaFilter.php index 9137337..0b81aa4 100644 --- a/Bonder/Filters/LambdaFilter.php +++ b/Bonder/Filters/LambdaFilter.php @@ -1,6 +1,8 @@ lambda = $lambda; - if (!is_callable($lambda)) { - throw new \Bonder\Exceptions\Exception("Argument lambda is not callable"); + if (!is_callable($lambda)) { + throw new \Bonder\Exceptions\Exception("Argument lambda is not callable"); } } - public function filter(\Bonder\Request $request, - \Bonder\Filters\NextFilterCaller $next) { + public function filter(Request $request, + NextFilterCaller $next) { $lambda = $this->lambda; return $lambda($request, $next); } diff --git a/Bonder/Filters/NextFilterCaller.php b/Bonder/Filters/NextFilterCaller.php index 6ab2c71..190868d 100644 --- a/Bonder/Filters/NextFilterCaller.php +++ b/Bonder/Filters/NextFilterCaller.php @@ -2,6 +2,8 @@ namespace Bonder\Filters; +use Bonder\Request; + /** * Caller for the next filter in the chain. * @@ -11,11 +13,12 @@ interface NextFilterCaller { /** - * Calls the next filter. - * - * @param \Bonder\Request $request the request. + * Calls the next filter, passing along the processed + * variable values as an array (varname => value). + * + * @param Request $request the request. + * @param array $variables the variables. * @return \Bonder\Response the response. */ - public function call(\Bonder\Request $request); - + public function call(Request $request, Array $variables = array()); } \ No newline at end of file diff --git a/Bonder/Filters/SimpleFilterChain.php b/Bonder/Filters/SimpleFilterChain.php index 4f16ec4..50e374a 100644 --- a/Bonder/Filters/SimpleFilterChain.php +++ b/Bonder/Filters/SimpleFilterChain.php @@ -1,6 +1,9 @@ filters = $filters; $this->controller = $controller; + foreach ($this->filters as $alias => $filter) { + $this->filtersList[] = array($alias, $filter); + } } /** * Returns the controller. * - * @return \Bonder\Controller the controller. + * @return Controller the controller. */ public function getController() { return $this->controller; @@ -54,17 +64,62 @@ public function getFilters() { * Executes the first filter in the chain. If no filters are available, * calls the controller. * - * @param \Bonder\Request $request the request. + * @param Request $request the request. * @return \Bonder\Response the response. */ - public function call(\Bonder\Request $request) { - if (empty($this->filters)) { + public function execute(Request $request) { + return $this->executeStep($request, 0); + } + + /** + * Recursion entry point for next filter callers. Public because PHP lacks private inner classes. + * + * @param Request $request + * @param $step + * @param Map $filterVariables + * @return mixed + */ + public function executeStep(Request $request, $step) { + if ($step < count($this->filtersList)) { + list($alias, $filter) = $this->filtersList[$step]; + return $filter->filter($request, new SimpleFilterChainNextCaller($this, $step + 1, $alias)); + } + if ($step == count($this->filtersList)) { return $this->controller->service($request); } - $nextFilter = reset($this->filters); - $nextFilters = array_slice($this->filters, 1); - return $nextFilter->filter($request, - new SimpleFilterChain($nextFilters, $this->controller)); + throw new \InvalidArgumentException("Step can't be higher than the filters amount + 1"); + } + +} + +/** Utility class to simplify filter chain recursion. */ +final class SimpleFilterChainNextCaller implements NextFilterCaller { + + /** + * @var SimpleFilterChain + */ + private $filterChain; + + /** + * @var int + */ + private $step; + + /** + * @var string + */ + private $alias; + + public function __construct(SimpleFilterChain $filterChain, $step, $alias) { + $this->filterChain = $filterChain; + $this->step = $step; + $this->alias = $alias; + } + + public function call(Request $request, Array $variables = array()) { + if (!is_numeric($this->alias)) { + $request->getFilterVariables()->set($this->alias, Map::fromArray($variables)); + } + return $this->filterChain->executeStep($request, $this->step); } - } \ No newline at end of file diff --git a/Bonder/Filters/ValueFinderFiltersProvider.php b/Bonder/Filters/ValueFinderFiltersProvider.php index a9b2eab..b434a73 100644 --- a/Bonder/Filters/ValueFinderFiltersProvider.php +++ b/Bonder/Filters/ValueFinderFiltersProvider.php @@ -1,6 +1,7 @@ valueFinder->getAllValues($uri); - return array_map(function(\Bonder\Util\ValueFinderResult $r) { + $global = array_map(function(\Bonder\Util\ValueFinderResult $r) { return $r->getValue(); }, $results); + $cFilters = $controller->getFilters(); + $allFilters = array_merge($global, $cFilters); + return $allFilters; } } \ No newline at end of file diff --git a/Bonder/Http/HttpController.php b/Bonder/Http/HttpController.php index c0ab9fd..e6fc039 100644 --- a/Bonder/Http/HttpController.php +++ b/Bonder/Http/HttpController.php @@ -15,6 +15,11 @@ abstract class HttpController implements \Bonder\Controller, \Bonder\ContextAwar */ private $context; + public function getFilters() { + // No filters in the default implementation. + return array(); + } + public function setContext(\Bonder\Context $context) { $this->context = $context; } diff --git a/Bonder/Http/HttpRequestFactory.php b/Bonder/Http/HttpRequestFactory.php index e326f6c..817bad0 100644 --- a/Bonder/Http/HttpRequestFactory.php +++ b/Bonder/Http/HttpRequestFactory.php @@ -1,6 +1,8 @@ get = $get; $this->post = $post; @@ -72,6 +80,7 @@ public function __construct( $this->server = $server; $this->files = $files; $this->uriVariables = $uriVariables; + $this->filterVariables = $filterVariables; } public function getGET() { @@ -130,4 +139,13 @@ public function getHttpProtocol() { } return 'http'; } + + /** + * Returns the filter variables; + * + * @return Map the filter variables; + */ + public function getFilterVariables(){ + return $this->filterVariables; + } } \ No newline at end of file diff --git a/Bonder/Launcher.php b/Bonder/Launcher.php index d5ac394..cd21be8 100644 --- a/Bonder/Launcher.php +++ b/Bonder/Launcher.php @@ -1,6 +1,7 @@ output; + public function getOutputStream() { + return $this->output; } /** @@ -119,14 +120,17 @@ public function getProcessor() { */ public function launch() { $chainProvider = $this->getFilterChainProvider(); - $factory = $this->getConfigurationFactory(); - $uri = $factory->getUriProvider()->getUri(); - $job = new \Bonder\Process\FactoryJob( - $uri, - $factory->getRequestFactory(), - $chainProvider); - $response = $this->processor->process($job); - $response->writeTo($this->getOutputStream()); + $factory = $this->getConfigurationFactory(); + $uri = $factory->getUriProvider()->getUri(); + $job = new \Bonder\Process\FactoryJob( + $uri, + $factory->getRequestFactory(), + $chainProvider); + $response = $this->processor->process($job); + if (!($response instanceof Response)) { + throw new Exception("Processor returned a non-response"); + } + $response->writeTo($this->getOutputStream()); return $response; } diff --git a/Bonder/Process/SimpleProcessor.php b/Bonder/Process/SimpleProcessor.php index d8b6f0d..782051f 100644 --- a/Bonder/Process/SimpleProcessor.php +++ b/Bonder/Process/SimpleProcessor.php @@ -14,7 +14,7 @@ final class SimpleProcessor implements \Bonder\Process\Processor { * @see Bonder\Process.Processor::process() */ public function process(\Bonder\Process\Job $job) { - return $job->getFilterChain()->call($job->getRequest()); + return $job->getFilterChain()->execute($job->getRequest()); } } \ No newline at end of file diff --git a/Bonder/Request.php b/Bonder/Request.php index c26320f..6744993 100644 --- a/Bonder/Request.php +++ b/Bonder/Request.php @@ -1,6 +1,7 @@ getMock("\Bonder\Controller"); + $controller->expects($this->any())->method("getFilters")->willReturn(array()); $builder = new \Bonder\Builders\StandardFiltersProviderBuilder(); $context = $this->getMock("\Bonder\Context"); $f1 = $this->getMock("\Bonder\Filters\Filter"); @@ -28,19 +30,19 @@ public function testSimple() { $this->assertSame($builder, $builder->setFilters($filters)); $this->assertSame($filters, $builder->getFilters()); $fp = $builder->build(); - $key1aFilters = $fp->getFilters("key1a"); + $key1aFilters = $fp->getFilters("key1a", $controller); $this->assertSame($f1, $key1aFilters[0]); $this->assertCount(1, $key1aFilters); - $key2bFilters = $fp->getFilters("key2b"); + $key2bFilters = $fp->getFilters("key2b", $controller); $this->assertCount(1, $key2bFilters); $this->assertSame($f2, $key2bFilters[0]); - $key3endFilters = $fp->getFilters("key3end"); + $key3endFilters = $fp->getFilters("key3end", $controller); $this->assertCount(2, $key3endFilters); $this->assertEquals(array($f3, $f4), $key3endFilters); - $emptyFilters = $fp->getFilters("nothing"); + $emptyFilters = $fp->getFilters("nothing", $controller); $this->assertEmpty($emptyFilters); } diff --git a/BonderTest/Filters/ConfiguredFiltersProviderTest.php b/BonderTest/Filters/ConfiguredFiltersProviderTest.php index 1b7c2cd..5345aad 100644 --- a/BonderTest/Filters/ConfiguredFiltersProviderTest.php +++ b/BonderTest/Filters/ConfiguredFiltersProviderTest.php @@ -8,6 +8,8 @@ final class ConfiguredFiltersProviderTest extends \PHPUnit_Framework_TestCase { public function testSimple() { + $controller = $this->getMock("\Bonder\Controller"); + $controller->expects($this->any())->method("getFilters")->willReturn(array()); $uri = '/boring_uri/nthaoesu/aoeusthaosetu/uaoeu'; $originalFilters = array( $this->getMock("\Bonder\Filter"), @@ -24,10 +26,10 @@ public function testSimple() { }))->will($this->returnArgument(0)); $wrappedProvider = $this->getMock("\Bonder\Filters\FiltersProvider"); $wrappedProvider->expects($this->any())->method("getFilters") - ->with($uri)->willReturn($originalFilters); + ->with($uri, $controller)->willReturn($originalFilters); $fp = new \Bonder\Filters\ConfiguredFiltersProvider( $wrappedProvider, $configurator); - $filters = $fp->getFilters($uri); + $filters = $fp->getFilters($uri, $controller); $this->assertEquals($originalFilters, $configured); $this->assertEquals($originalFilters, $filters); } diff --git a/BonderTest/Filters/FilterChainProviderResultTest.php b/BonderTest/Filters/FilterChainProviderResultTest.php index 26c62a4..63c280a 100644 --- a/BonderTest/Filters/FilterChainProviderResultTest.php +++ b/BonderTest/Filters/FilterChainProviderResultTest.php @@ -9,7 +9,6 @@ final class FilterChainProviderResultTest extends \PHPUnit_Framework_TestCase { public function testCreate() { $uriVariables = new \Bonder\Collections\Map(); - $controller = $this->getMock("\Bonder\Controller"); $filterChain = $this->getMock("\Bonder\Filters\FilterChain"); $fcpr = new \Bonder\Filters\FilterChainProviderResult( $filterChain, $uriVariables); diff --git a/BonderTest/Filters/LambdaFilterTest.php b/BonderTest/Filters/LambdaFilterTest.php index ad0441c..04332aa 100644 --- a/BonderTest/Filters/LambdaFilterTest.php +++ b/BonderTest/Filters/LambdaFilterTest.php @@ -6,16 +6,6 @@ * @author hbandura */ final class LambdaFilterTest extends \PHPUnit_Framework_TestCase { - - public function testThrow() { - try { - $f = new \Bonder\Filters\LambdaFilter(null); - $this->fail("Exception not thrown"); - } catch (\Bonder\Exceptions\Exception $e) { - // success - } - } - public function testSameParameters() { $self = $this; $originalRequest = $this->getMock("\Bonder\Request"); @@ -36,7 +26,7 @@ function(\Bonder\Request $request, public function testReturn() { $response = $this->getMock("\Bonder\Response"); - $request = $this->getMock("\Bonder\Request"); + $request = $this->getMock("\Bonder\Request"); $next = $this->getMock("\Bonder\Filters\NextFilterCaller"); $f = function(\Bonder\Request $request, \Bonder\Filters\NextFilterCaller $next) use ($response) { diff --git a/BonderTest/Filters/SimpleFilterChainTest.php b/BonderTest/Filters/SimpleFilterChainTest.php index c260ec1..eb68abf 100644 --- a/BonderTest/Filters/SimpleFilterChainTest.php +++ b/BonderTest/Filters/SimpleFilterChainTest.php @@ -1,11 +1,18 @@ getMock("\Bonder\Controller"); @@ -15,7 +22,7 @@ public function testGetters() { $this->getMock("\Bonder\Filter"), $this->getMock("\Bonder\Filter") ); - $fc = new \Bonder\Filters\SimpleFilterChain($filters, $controller); + $fc = new SimpleFilterChain($filters, $controller); $this->assertSame($controller, $fc->getController()); $this->assertEquals($filters, $fc->getFilters()); } @@ -24,57 +31,86 @@ public function testEmptyFilters() { $self = $this; $response = $this->getMock("\Bonder\Response"); $originalRequest = $this->getMock("\Bonder\Request"); - $fc = new \Bonder\Filters\SimpleFilterChain(array(), + $fc = new SimpleFilterChain(array(), new \Bonder\Controllers\LambdaController( - function(\Bonder\Request $request) use ($self, $originalRequest, $response) { + function(Request $request) use ($self, $originalRequest, $response) { $self->assertSame($originalRequest, $request); return $response; } ) ); - $this->assertSame($response, $fc->call($originalRequest)); + $this->assertSame($response, $fc->execute($originalRequest)); // Again! - $this->assertSame($response, $fc->call($originalRequest)); + $this->assertSame($response, $fc->execute($originalRequest)); } public function testCallAllFilters() { $response = $this->getMock("\Bonder\Response"); - $request = $this->getMock("\Bonder\Request"); + $request = $this->getRequestMock(new Map()); $filters = array(); $resultFilters = array(); for ($i = 0; $i < 20; $i++) { $filters[] = $this->getFilterLink($resultFilters, $i); } - $fc = new \Bonder\Filters\SimpleFilterChain($filters, - new \Bonder\Controllers\FixedResponseController($response)); - $controllerResponse = $fc->call($request); + $fc = new SimpleFilterChain($filters, + new FixedResponseController($response)); + $controllerResponse = $fc->execute($request); $this->assertSame($response, $controllerResponse); $this->assertEquals(range(0, 19), $resultFilters); + $this->assertTrue($request->getFilterVariables()->isEmpty()); } private function getFilterLink(&$resultFilters, $i) { - return new \Bonder\Filters\LambdaFilter( - function(\Bonder\Request $request, - \Bonder\Filters\NextFilterCaller $next) use (&$resultFilters, $i) { + return new LambdaFilter( + function(Request $request, + NextFilterCaller $next) use (&$resultFilters, $i) { $resultFilters[] = $i; - return $next->call($request); + return $next->call($request, array("var_$i" => "lol$i", "var2_$i" => "lol2$i")); } ); } private function getDropFilter(\Bonder\Response $response) { - return new \Bonder\Filters\LambdaFilter( - function(\Bonder\Request $request, - \Bonder\Filters\NextFilterCaller $next) use ($response) { - return $response; - } + return new LambdaFilter( + function(Request $request, + NextFilterCaller $next) use ($response) { + return $response; + } ); } + + /** Returns the request mock to use in tests. */ + public function getRequestMock(Map $filterVariables) { + $request = $this->getMock("\Bonder\Request"); + $request->expects($this->any())->method("getFilterVariables")->willReturn($filterVariables); + return $request; + } + + public function testFilterVariables() { + $response = $this->getMock("\Bonder\Response"); + $request = $this->getRequestMock(new Map()); + $filters = array(); + $resultFilters = array(); + for ($i = 0; $i < 20; $i++) { + $filters["test_" . $i] = $this->getFilterLink($resultFilters, $i); + } + $vars = new Map(); + $fc = new SimpleFilterChain($filters, + new LambdaController(function(Request $request) use ($vars, $response) { + $vars->setAll($request->getFilterVariables()); + return $response; + })); + $fc->execute($request); + for ($i = 0; $i < 20; $i++) { + $this->assertEquals("lol$i", $vars->get("test_$i")->get("var_$i")); + $this->assertEquals("lol2$i", $vars->get("test_$i")->get("var2_$i")); + } + } public function testDrop() { $controllerResponse = $this->getMock("\Bonder\Response"); $dropFilterResponse = $this->getMock("\Bonder\Response"); - $this->assertNotSame($controllerResponse, $dropFilterResponse); + $this->assertNotSame($controllerResponse, $dropFilterResponse); $request = $this->getMock("\Bonder\Request"); $arr = array(); $filters = array( @@ -83,10 +119,10 @@ public function testDrop() { $this->getDropFilter($dropFilterResponse), $this->getFilterLink($arr, 2) ); - $fc = new \Bonder\Filters\SimpleFilterChain($filters, - new \Bonder\Controllers\FixedResponseController($controllerResponse)); - $response = $fc->call($request); - $this->assertSame($response, $dropFilterResponse); + $fc = new SimpleFilterChain($filters, + new FixedResponseController($controllerResponse)); + $response = $fc->execute($request); + $this->assertSame($response, $dropFilterResponse); $this->assertEquals(range(0, 1), $arr); } diff --git a/BonderTest/Filters/ValueFinderFiltersProviderTest.php b/BonderTest/Filters/ValueFinderFiltersProviderTest.php index f42fb75..a00aa19 100644 --- a/BonderTest/Filters/ValueFinderFiltersProviderTest.php +++ b/BonderTest/Filters/ValueFinderFiltersProviderTest.php @@ -1,6 +1,9 @@ getController(array()); $uri = '/omgomgomg/blahhh/aosentuhsaonethu'; - $valueFinder = $this->getMock("\Bonder\Util\ValueFinder"); + $valueFinder = $this->getMock('\Bonder\Util\ValueFinder'); $valueFinder->expects($this->any())->method("getAllValues") ->with($uri)->willReturn(array()); - $fp = new \Bonder\Filters\ValueFinderFiltersProvider($valueFinder); - $result = $fp->getFilters($uri); + $fp = new ValueFinderFiltersProvider($valueFinder); + $result = $fp->getFilters($uri, $controller); $this->assertTrue(is_array($result)); $this->assertEmpty($result); } public function testGetFilters() { $uri = '/omgomgomg/blahhh/aosentuhsaonethu'; + $globalFilter1 = $this->getMock('\Bonder\Filter'); + $globalFilter2 = $this->getMock('\Bonder\Filter'); + $globalFilter3 = $this->getMock('\Bonder\Filter'); $results = array( - new \Bonder\Util\ValueFinderResult( - 'k', $uri, $this->getMock("\Bonder\Filter"), - new \Bonder\Collections\Map()), - new \Bonder\Util\ValueFinderResult( - 'k', $uri, $this->getMock("\Bonder\Filter"), - new \Bonder\Collections\Map()), - new \Bonder\Util\ValueFinderResult( - 'k', $uri, $this->getMock("\Bonder\Filter"), - new \Bonder\Collections\Map()) - ); - $valueFinder = $this->getMock("\Bonder\Util\ValueFinder"); - $valueFinder->expects($this->any())->method("getAllValues") - ->with($uri)->willReturn($results); - $fp = new \Bonder\Filters\ValueFinderFiltersProvider($valueFinder); - $filters = $fp->getFilters($uri); + new ValueFinderResult( + 'k', $uri, $globalFilter1, + new Map()), + new ValueFinderResult( + 'k', $uri, $globalFilter2, + new Map()), + new ValueFinderResult( + 'k', $uri, $globalFilter3, + new Map()) + ); + $valueFinder = $this->getMock('\Bonder\Util\ValueFinder'); + $valueFinder->expects($this->any())->method("getAllValues") + ->with($uri)->willReturn($results); + $fp = new ValueFinderFiltersProvider($valueFinder); + $cFilters = array( + 'alias1' => $this->getMock('\Bonder\Filter'), + 0 => $this->getMock('\Bonder\Filter'), + 'alias2' => $this->getMock('\Bonder\Filter') + ); + $filters = $fp->getFilters($uri, $this->getController($cFilters)); $this->assertTrue(is_array($filters)); - for ($i = 0; $i < count($results); $i++) { + for ($i = 0; $i < count($results); $i++) { $this->assertEquals($results[$i]->getValue(), $filters[$i]); - } + } + $expectedFilters = array( + $globalFilter1, + $globalFilter2, + $globalFilter3, + 'alias1' => $cFilters['alias1'], + $cFilters[0], + 'alias2' => $cFilters['alias2'] + ); + $this->assertEquals($expectedFilters, $filters); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject + */ + private function getController(Array $filters) { + $controller = $this->getMock('\Bonder\Controller'); + $controller->expects($this->any())->method("getFilters")->willReturn($filters); + return $controller; } } \ No newline at end of file diff --git a/BonderTest/Http/HttpControllerTest.php b/BonderTest/Http/HttpControllerTest.php index 3122645..60fb80e 100644 --- a/BonderTest/Http/HttpControllerTest.php +++ b/BonderTest/Http/HttpControllerTest.php @@ -6,7 +6,12 @@ * @author hbandura */ final class HttpControllerTest extends \PHPUnit_Framework_TestCase { - + + public function testEmptyFilters() { + $controller = $this->getMockForAbstractClass("\Bonder\Http\HttpController"); + $this->assertEmpty($controller->getFilters()); + } + public function testContextGetter() { $controller = $this->getMockForAbstractClass("\Bonder\Http\HttpController"); $context = $this->getMock("\Bonder\Context"); @@ -50,7 +55,7 @@ private function doTestMethod($method) { true, true, array($method) - ); + ); $request = $this->getRequest($method); $response = $this->getMock("\Bonder\Http\HttpResponse"); $controller->expects($this->any())->method($method) @@ -59,13 +64,13 @@ private function doTestMethod($method) { } private function doTestThrowOnMethod($method) { - $controller = $this->getMockForAbstractClass("\Bonder\Http\HttpController"); - $request = $this->getRequest($method); - try { + $controller = $this->getMockForAbstractClass("\Bonder\Http\HttpController"); + $request = $this->getRequest($method); + try { $controller->service($request); - $this->fail("Exception not thrown on $method"); - } catch (\Bonder\Exceptions\NotImplementedException $e) { - // success + $this->fail("Exception not thrown on $method"); + } catch (\Bonder\Exceptions\NotImplementedException $e) { + // success } // try calling directly try { @@ -77,8 +82,8 @@ private function doTestThrowOnMethod($method) { } private function getRequest($method) { - $request = $this->getMock("\Bonder\Http\HttpRequest"); - $request->expects($this->any())->method("getMethod") + $request = $this->getMock("\Bonder\Http\HttpRequest"); + $request->expects($this->any())->method("getMethod") ->willReturn($method); return $request; } diff --git a/BonderTest/Http/MapsHttpRequestTest.php b/BonderTest/Http/MapsHttpRequestTest.php index b9cf89d..2ab7214 100644 --- a/BonderTest/Http/MapsHttpRequestTest.php +++ b/BonderTest/Http/MapsHttpRequestTest.php @@ -1,6 +1,7 @@ assertSame($get, $request->getGET()); $this->assertSame($post, $request->getPOST()); @@ -32,6 +35,7 @@ public function testCreate() { $this->assertSame($server, $request->getServer()); $this->assertSame($files, $request->getFiles()); $this->assertSame($uriVariables, $request->getUriVariables()); + $this->assertSame($filterVariables, $request->getFilterVariables()); } /** @@ -40,13 +44,14 @@ public function testCreate() { */ public function create(Array $serverVars) { return new \Bonder\Http\MapsHttpRequest( - new \Bonder\Collections\Map(), // GET - new \Bonder\Collections\Map(), // POST - new \Bonder\Collections\Map(), // COOKIES - new \Bonder\Collections\Map(), // SESSION - \Bonder\Collections\Map::fromArray($serverVars), - new \Bonder\Collections\Map(), // FILES - new \Bonder\Collections\Map());// URI VARIABLES + new Map(), // GET + new Map(), // POST + new Map(), // COOKIES + new Map(), // SESSION + Map::fromArray($serverVars), + new Map(), // FILES + new Map(), // URI VARIABLES + new Map());// FILTER VARIABLES } public function testURI() { diff --git a/BonderTest/LauncherFactoryTest.php b/BonderTest/LauncherFactoryTest.php index f700e0f..77e0546 100644 --- a/BonderTest/LauncherFactoryTest.php +++ b/BonderTest/LauncherFactoryTest.php @@ -8,18 +8,18 @@ final class LauncherFactoryTest extends \PHPUnit_Framework_TestCase { protected function setUp() { - global $_SERVER; - global $_POST; - global $_GET; - global $_COOKIE; - global $_FILES; - global $_SESSION; - $_SERVER = array(); - $_POST = array(); - $_GET = array(); - $_COOKIE = array(); - $_FILES = array(); - $_SESSION = array(); + global $_SERVER; + global $_POST; + global $_GET; + global $_COOKIE; + global $_FILES; + global $_SESSION; + $_SERVER = array(); + $_POST = array(); + $_GET = array(); + $_COOKIE = array(); + $_FILES = array(); + $_SESSION = array(); $_SERVER['DOCUMENT_URI'] = '/aoeu/aoeu/aoeu'; } @@ -35,7 +35,7 @@ public function testHttp() { $response->expects($this->once())->method("writeTo") ->with($launcher->getOutputStream()); $fc = $this->getMock("\Bonder\Filters\FilterChain"); - $fc->expects($this->once())->method("call") + $fc->expects($this->once())->method("execute") ->willReturn($response); $fcr = new \Bonder\Filters\FilterChainProviderResult($fc, new \Bonder\Collections\Map()); @@ -59,8 +59,8 @@ public function testGetFCPBuilder() { } public function testStandardHttp() { - $resources = array('aoeu' => 'aoeusnthaoeu', 'shou' => 37); - $controllers = array('aoeuaoeu' => $this->getMock("\Bonder\Controller")); + $resources = array('aoeu' => 'aoeusnthaoeu', 'shou' => 37); + $controllers = array('aoeuaoeu' => $this->getMock("\Bonder\Controller")); $filters = array('aoeuuuu' => $this->getMock("\Bonder\Filter")); $launcher = \Bonder\LauncherFactory::getStandardHttp( $resources, $controllers, $filters); @@ -74,18 +74,18 @@ public function testStandardHttp() { } public function testStandardHttpWithDefault() { - $resources = array('aoeu' => 'aoeusnthaoeu', 'shou' => 37); - $controllers = array('aoeuaoeu' => $this->getMock("\Bonder\Controller")); + $resources = array('aoeu' => 'aoeusnthaoeu', 'shou' => 37); + $controllers = array('aoeuaoeu' => $this->getMock("\Bonder\Controller")); $filters = array('aoeuuuu' => $this->getMock("\Bonder\Filter")); - $controller = $this->getMock("\Bonder\Controller"); - $launcher = \Bonder\LauncherFactory::getStandardHttpWithDefault( - $resources, $controllers, $filters, $controller); - $this->assertEquals( - \Bonder\LauncherFactory::getHttp( - \Bonder\LauncherFactory::getStandardFCPBuilder( - $resources, $controllers, $filters - )->setDefaultController($controller)->build() - ) + $controller = $this->getMock("\Bonder\Controller"); + $launcher = \Bonder\LauncherFactory::getStandardHttpWithDefault( + $resources, $controllers, $filters, $controller); + $this->assertEquals( + \Bonder\LauncherFactory::getHttp( + \Bonder\LauncherFactory::getStandardFCPBuilder( + $resources, $controllers, $filters + )->setDefaultController($controller)->build() + ) , $launcher); } } \ No newline at end of file diff --git a/BonderTest/Process/SimpleProcessorTest.php b/BonderTest/Process/SimpleProcessorTest.php index 1ab9b35..20d578d 100644 --- a/BonderTest/Process/SimpleProcessorTest.php +++ b/BonderTest/Process/SimpleProcessorTest.php @@ -17,7 +17,7 @@ public function testProcess() { ->willReturn($fc); $job->expects($this->any())->method("getRequest") ->willReturn($request); - $fc->expects($this->once())->method("call") + $fc->expects($this->once())->method("execute") ->with($request)->willReturn($response); $this->assertSame($response, $processor->process($job)); }