1919use Brain \Cortex \Router \Router ;
2020use Brain \Cortex \Tests \TestCase ;
2121use Brain \Cortex \Uri \UriInterface ;
22+ use Brain \Monkey \Functions ;
2223use FastRoute \Dispatcher ;
2324use FastRoute \RouteCollector ;
2425
@@ -222,7 +223,7 @@ public function testMatchMatchingExactMatch()
222223 $ uri ->shouldReceive ('scheme ' )->andReturn ('http ' );
223224 $ uri ->shouldReceive ('host ' )->andReturn ('example.com ' );
224225 $ uri ->shouldReceive ('path ' )->andReturn ('foo ' );
225- $ uri ->shouldReceive ('vars ' )->once ()->andReturn (['c ' => 'C ' ]);
226+ $ uri ->shouldReceive ('vars ' )->atLeast ()-> once ()->andReturn (['c ' => 'C ' ]);
226227 $ uri ->shouldReceive ('chunks ' )->andReturn (['foo ' ]);
227228
228229 $ collector = \Mockery::mock (RouteCollector::class);
@@ -282,7 +283,7 @@ public function testMatchDynamicMatch()
282283 $ uri ->shouldReceive ('scheme ' )->andReturn ('http ' );
283284 $ uri ->shouldReceive ('host ' )->andReturn ('example.com ' );
284285 $ uri ->shouldReceive ('path ' )->andReturn ('foo/i-am-bar ' );
285- $ uri ->shouldReceive ('vars ' )->once ()->andReturn (['c ' => 'C ' ]);
286+ $ uri ->shouldReceive ('vars ' )->atLeast ()-> once ()->andReturn (['c ' => 'C ' ]);
286287 $ uri ->shouldReceive ('chunks ' )->andReturn (['foo ' , 'meh ' ]);
287288
288289 $ collector = \Mockery::mock (RouteCollector::class);
@@ -352,6 +353,7 @@ public function testMatchMatchingExactMatchNoQueryVars()
352353 $ uri ->shouldReceive ('host ' )->andReturn ('example.com ' );
353354 $ uri ->shouldReceive ('path ' )->andReturn ('foo ' );
354355 $ uri ->shouldReceive ('chunks ' )->andReturn (['foo ' ]);
356+ $ uri ->shouldReceive ('vars ' )->andReturn (['foo ' => 'no-way ' ]);
355357
356358 $ collector = \Mockery::mock (RouteCollector::class);
357359 $ collector ->shouldReceive ('addRoute ' )->never ();
@@ -386,6 +388,69 @@ public function testMatchMatchingExactMatchNoQueryVars()
386388 assertSame ($ expected , $ data );
387389 }
388390
391+ public function testMatchMatchingNoQueryVarsMaintainPreviewVar ()
392+ {
393+ Functions::when ('is_user_logged_in ' )->justReturn (true );
394+
395+ $ handler = function () {
396+ return func_get_args ();
397+ };
398+
399+ $ route = new Route ([
400+ 'id ' => 'r1 ' ,
401+ 'path ' => '/foo ' ,
402+ 'handler ' => $ handler ,
403+ 'vars ' => ['d ' => 'D ' ],
404+ 'method ' => 'POST ' ,
405+ 'merge_query_string ' => false ,
406+ ]);
407+ $ routes = new PriorityRouteCollection ();
408+
409+ $ routes ->addRoute ($ route );
410+
411+ $ groups = \Mockery::mock (GroupCollectionInterface::class);
412+ $ groups ->shouldReceive ('mergeGroup ' )->once ()->with ($ route )->andReturn ($ route );
413+
414+ $ uri = \Mockery::mock (UriInterface::class);
415+ $ uri ->shouldReceive ('scheme ' )->andReturn ('http ' );
416+ $ uri ->shouldReceive ('host ' )->andReturn ('example.com ' );
417+ $ uri ->shouldReceive ('path ' )->andReturn ('foo ' );
418+ $ uri ->shouldReceive ('chunks ' )->andReturn (['foo ' ]);
419+ $ uri ->shouldReceive ('vars ' )->andReturn (['foo ' => 'no-way ' , 'preview ' => 1 ]);
420+
421+ $ collector = \Mockery::mock (RouteCollector::class);
422+ $ collector ->shouldReceive ('addRoute ' )->never ();
423+
424+ $ dispatcher = \Mockery::mock (Dispatcher::class);
425+ $ dispatcher ->shouldReceive ('dispatch ' )->never ();
426+
427+ $ factory = function (array $ args ) use ($ dispatcher ) {
428+ assertSame ($ args , ['foo ' => 'bar ' ]);
429+
430+ return $ dispatcher ;
431+ };
432+
433+ $ router = new Router ($ routes , $ groups , $ collector , $ factory );
434+ $ result = $ router ->match ($ uri , 'POST ' );
435+
436+ $ expected = [
437+ 'route ' => 'r1 ' ,
438+ 'path ' => '/foo ' ,
439+ 'vars ' => ['preview ' => 1 , 'd ' => 'D ' ],
440+ 'handler ' => $ handler ,
441+ 'before ' => null ,
442+ 'after ' => null ,
443+ 'template ' => null ,
444+ ];
445+
446+ $ proxy = new Proxy ($ result );
447+ /** @noinspection PhpUndefinedFieldInspection */
448+ $ data = $ proxy ->data ;
449+
450+ assertTrue ($ result ->matched ());
451+ assertSame ($ expected , $ data );
452+ }
453+
389454 public function testMatchMatchingExactMatchCallableVars ()
390455 {
391456 $ handler = function () {
@@ -412,7 +477,7 @@ public function testMatchMatchingExactMatchCallableVars()
412477 $ uri ->shouldReceive ('scheme ' )->andReturn ('http ' );
413478 $ uri ->shouldReceive ('host ' )->andReturn ('example.com ' );
414479 $ uri ->shouldReceive ('path ' )->andReturn ('foo ' );
415- $ uri ->shouldReceive ('vars ' )->once ()->andReturn (['c ' => 'C ' ]);
480+ $ uri ->shouldReceive ('vars ' )->atLeast ()-> once ()->andReturn (['c ' => 'C ' ]);
416481 $ uri ->shouldReceive ('chunks ' )->andReturn (['foo ' ]);
417482
418483 $ collector = \Mockery::mock (RouteCollector::class);
0 commit comments