/** * build method returns a Mux object with registered resources. * * @return Pux\Mux */ public function build() { $prefix = $this->options['prefix']; foreach ($this->resources as $resId => $controller) { $resourceMux = $controller->expand(); $path = $prefix . '/' . $resId; $this->mux->mount($path, $resourceMux); } return $this->mux; }
public function testAnnotations() { if (defined('HHVM_VERSION')) { echo "HHVM does not support Reflection to expand controller action methods"; return; } $controller = new ExpandableProductController(); $this->assertTrue(is_array($map = $controller->getActionMethods())); $routes = $controller->getActionRoutes(); $this->assertNotEmpty($routes); $this->assertEquals('', $routes[0][0], 'the path'); $this->assertEquals('indexAction', $routes[0][1], 'the mapping method'); $mux = new Pux\Mux(); // works fine // $submux = $controller->expand(); // $mux->mount('/product', $submux ); // gc scan bug $mux->mount('/product', $controller->expand()); $paths = array('/product/delete' => 'DELETE', '/product/update' => 'PUT', '/product/add' => 'POST', '/product/foo/bar' => null, '/product/item' => 'GET', '/product' => null); foreach ($paths as $path => $method) { if ($method) { $_SERVER['REQUEST_METHOD'] = $method; } else { $_SERVER['REQUEST_METHOD'] = 'GET'; } ok($mux->dispatch($path), $path); } }
public function testMountStrRoutes() { $mux = new \Pux\Mux(); $submux = new \Pux\Mux(); ok($submux); $submux->any('/hello/str', array('HelloController2', 'indexAction')); $mux->mount('/sub', $submux); }
public function testMuxMounting() { $mainMux = new Mux(); $mainMux->expand = false; $pageMux = new Mux(); $pageMux->add('/page1', array('PageController', 'page1')); $pageMux->add('/page2', array('PageController', 'page2')); $mainMux->mount('/sub', $pageMux); $pageMux2 = new Mux(); $pageMux2->add('/bar', array('PageController', 'page1')); $pageMux2->add('/zoo', array('PageController', 'page2')); is(2, $pageMux2->length()); $mainMux->mount('/foo', $pageMux2); is(2, $mainMux->length()); foreach (array('/sub/page1', '/sub/page2', '/foo/bar', '/foo/zoo') as $p) { $r = $mainMux->dispatch($p); ok($r, "Matched route for {$p}"); } return $mainMux; }
public function testIndexAction() { return $this->markTestSkipped('phifty bootstrap script is not fixed yet'); $handler = new EmailTemplateCRUDHandler(); $mux = new Mux(); $mux->mount('/bs/email', $handler); $route = $mux->dispatch('/bs/email'); $this->assertNotNull($route); $response = RouteExecutor::execute($route); $this->assertNotNull($response); }
public function testMuxCustomDispatch() { $mux = new Mux(); $mux->mount('/bs/product', function ($x) { $x = new Mux(); $x->any('/create', ['Product', 'createAction']); $x->any('/edit/:id', ['Product', 'editAction']); return $x; }); $route = $mux->dispatch('/bs/product/create'); $this->assertNotEmpty($route); $this->assertEquals('/create', $route[1]); $route = $mux->dispatch('/bs/product/edit/30'); $this->assertNotEmpty($route); $this->assertEquals('/edit/:id', $route[3]['pattern']); }
public function testMuxMountNoExpandAndDispatchToCallableSubMux() { $mux = new \Pux\Mux(); $mux->expand = false; $mux->mount('/test', function (Mux $submux) { $submux->any('/hello/static', array('HelloController2', 'indexAction')); $submux->any('/hello/:name', array('HelloController2', 'indexAction')); }); ok($mux); ok($routes = $mux->getRoutes()); $r = $mux->dispatch('/test/hello/John'); ok($r); $this->assertPcreRoute($r, '/hello/:name'); $r = $mux->dispatch('/test/hello/static'); ok($r); $this->assertNonPcreRoute($r, '/hello/static'); }
public function testRESTfulDispatch() { $con = new ProductResourceController(); $routes = $con->getActionRoutes(); $this->assertNotEmpty($routes); $this->assertTrue(is_array($routes)); $methods = $con->getActionMethods(); $this->assertNotEmpty($methods); $productMux = $con->expand(); // there is a sorting bug (fixed), this tests it. $this->assertNotEmpty($productMux); $root = new Mux(); $root->mount('/product', $con->expand()); $_SERVER['REQUEST_METHOD'] = 'GET'; $this->assertNotNull($root->dispatch('/product/10')); $_SERVER['REQUEST_METHOD'] = 'DELETE'; $this->assertNotNull($root->dispatch('/product/10')); $_SERVER['REQUEST_METHOD'] = 'POST'; $this->assertNotNull($root->dispatch('/product')); // create $_SERVER['REQUEST_METHOD'] = 'POST'; $this->assertNotNull($root->dispatch('/product/10')); // update }
* Created by PhpStorm. * User: Aztyu * Date: 26/12/2015 * Time: 15:24 */ require_once 'vendor/autoload.php'; require_once 'controller/api/ApiUserController.php'; require_once 'controller/api/ApiFuelController.php'; require_once 'controller/api/SiteController.php'; require_once 'entities/Coordinates.php'; use Pux\Mux; use Pux\Executor; $path = explode("?", $_SERVER['REQUEST_URI'])[0]; $api_mux = new Mux(); $api_user_mux = new Mux(); $api_fuel_mux = new Mux(); $main_mux = new Mux(); $api_user_mux->get('/check', ['ApiUserController', 'checkAction']); $api_user_mux->get('/connect', ['ApiUserController', 'connectAction']); $api_user_mux->get('/create', ['ApiUserController', 'createAction']); $api_mux->mount('/user', $api_user_mux); $api_fuel_mux->get('/find', ['ApiFuelController', 'findAction']); $api_fuel_mux->post('/insert', ['ApiFuelController', 'insertAction']); $api_mux->mount('/fuel', $api_fuel_mux); $main_mux->mount('/api', $api_mux); $main_mux->get('/', ['SiteController', 'indexAction']); $route = $main_mux->dispatch($path); if ($route == null) { $route = $main_mux->dispatch('/'); } echo Executor::execute($route);
/** * @depends testControllerConstructor */ public function testMountNoExpand($controller) { $mainMux = new Mux(); $mainMux->expand = false; $mainMux->mount('/product', $controller); $mainMux->any('/', array('ProductController', 'indexAction')); ok($mainMux->getRoutes()); count_ok(2, $mainMux->getRoutes(), 'route count should be 2'); ok($r = $mainMux->dispatch('/product'), 'matched /product'); // match indexAction $this->assertSame(array('CRUDProductController', 'indexAction'), $r[2]); ok($r = $mainMux->dispatch('/product/add')); $this->assertSame(array('CRUDProductController', 'addAction'), $r[2]); ok($r = $mainMux->dispatch('/product/del')); $this->assertSame(array('CRUDProductController', 'delAction'), $r[2]); ok(null == $mainMux->dispatch('/foo')); ok(null == $mainMux->dispatch('/bar')); }
public function testSubmuxMergeOptions() { $mux = new Mux(); $this->assertEquals(0, $mux->length()); $submux = new Mux(); $submux->any('/foo', array('FooController', 'indexAction'), array('default' => array('suffix' => 'csv'))); $submux->any('/hello/:name', array('HelloController', 'indexAction'), array('default' => array('suffix' => 'json', 'prefix' => 'v1'))); $this->assertEquals(2, $submux->length()); ok($routes = $submux->getRoutes()); $this->assertEquals(2, $submux->length()); $this->assertEquals(0, $mux->length()); // XXX: we don't expand routes now, so we won't have the default // options for the subroutes... /* $mux->mount('/sub', $submux, array( 'default' => array('suffix' => 'xml', 'prefix' => 'v1'), 'require' => array('suffix' => '[a-z]{3,4}') )); */ $mux->mount('/sub', $submux); $this->assertEquals(1, $mux->length()); $r = $mux->dispatch('/sub/hello/John'); $this->assertNotNull($r); $this->assertEquals('json', $r[3]['default']['suffix']); $this->assertEquals('v1', $r[3]['default']['prefix']); $this->assertPcreRoute($r, '/hello/:name'); $r = $mux->dispatch('/sub/foo'); $this->assertNotNull($r); // ok($r[3]['default']['suffix'] == 'csv'); // ok($r[3]['require']['suffix'] == '[a-z]{3,4}'); $this->assertNonPcreRoute($r, '/foo'); }
public function testCallableSubMux() { $mux = new \Pux\Mux(); $mux->mount('/test', function (Mux $submux) { $submux->any('/hello/static', array('HelloController2', 'indexAction')); $submux->any('/hello/:name', array('HelloController2', 'indexAction')); }); ok($routes = $mux->getRoutes()); ok(is_array($routes)); $this->assertCount(1, $routes); $this->assertEquals('/test', $routes[0][1]); $r = $mux->dispatch('/test/hello/John'); $this->assertNotEmpty($r); $this->assertPcreRoute($r, '/hello/:name'); $r = $mux->dispatch('/test/hello/static'); $this->assertNotEmpty($r); $this->assertNonPcreRoute($r, '/hello/static'); }
<?php require '../vendor/autoload.php'; use Pux\Controller\ExpandableController; use Pux\RouteExecutor; use Pux\Mux; use Pux\Router; class MyController extends Controller { public function indexAction() { return 'MyController::indexAction()!'; } public function helloAction() { return 'MyController::helloAction()!'; } /** * @uri /foo */ public function overrideAction() { return 'MyController::overrideAction(), NOT MyController::fooAction()!'; } } $controller = new MyController(); $mux = new Mux(); $mux->mount('/', $controller->expand()); $route = $mux->dispatch($_SERVER['REQUEST_URI']); printf("Response: %s\n", RouteExecutor::execute($route));