@ -13,423 +13,524 @@ use flight\net\Response;
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					class EngineTest extends PHPUnit\Framework\TestCase
 
					 
					 
					 
					class EngineTest extends PHPUnit\Framework\TestCase
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					{
 
					 
					 
					 
					{
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
						public function setUp(): void {
 
					 
					 
					 
					    public function setUp(): void
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$_SERVER = [];
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					        $_SERVER = [];
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function tearDown(): void {
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$_SERVER = [];
 
					 
					 
					 
					    public function tearDown(): void
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testInitBeforeStart() {
 
					 
					 
					 
					        $_SERVER = [];
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new class extends Engine {
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								public function getInitializedVar() {
 
					 
					 
					 
					    public function testInitBeforeStart()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									return $this->initialized;
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								}
 
					 
					 
					 
					        $engine = new class extends Engine {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							};
 
					 
					 
					 
					            public function getInitializedVar()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertTrue($engine->getInitializedVar());
 
					 
					 
					 
					            {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->start();
 
					 
					 
					 
					                return $this->initialized;
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					            }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							// this is necessary cause it doesn't actually send the response correctly
 
					 
					 
					 
					        };
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							ob_end_clean();
 
					 
					 
					 
					        $this->assertTrue($engine->getInitializedVar());
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					        $engine->start();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertFalse($engine->router()->case_sensitive);
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertTrue($engine->response()->content_length);
 
					 
					 
					 
					        // this is necessary cause it doesn't actually send the response correctly
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					        ob_end_clean();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testHandleErrorNoErrorNumber() {
 
					 
					 
					 
					        $this->assertFalse($engine->router()->case_sensitive);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new Engine();
 
					 
					 
					 
					        $this->assertTrue($engine->response()->content_length);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$result = $engine->handleError(0, '', '', 0);
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertFalse($result);
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					    public function testHandleErrorNoErrorNumber()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testHandleErrorWithException() {
 
					 
					 
					 
					        $engine = new Engine();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new Engine();
 
					 
					 
					 
					        $result = $engine->handleError(0, '', '', 0);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->expectException(Exception::class);
 
					 
					 
					 
					        $this->assertFalse($result);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->expectExceptionCode(5);
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->expectExceptionMessage('thrown error message');
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->handleError(5, 'thrown error message', '', 0);
 
					 
					 
					 
					    public function testHandleErrorWithException()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					        $engine = new Engine();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testHandleException() {
 
					 
					 
					 
					        $this->expectException(Exception::class);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new Engine();
 
					 
					 
					 
					        $this->expectExceptionCode(5);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$regex_message = preg_quote('< h1 > 500 Internal Server Error< / h1 > < h3 > thrown exception message (20)< / h3 > ');
 
					 
					 
					 
					        $this->expectExceptionMessage('thrown error message');
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->expectOutputRegex('~'.$regex_message.'~');
 
					 
					 
					 
					        $engine->handleError(5, 'thrown error message', '', 0);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->handleException(new Exception('thrown exception message', 20));
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					    public function testHandleException()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testMapExistingMethod() {
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new Engine();
 
					 
					 
					 
					        $engine = new Engine();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->expectException(Exception::class);
 
					 
					 
					 
					        $regex_message = preg_quote('< h1 > 500 Internal Server Error< / h1 > < h3 > thrown exception message (20)< / h3 > ');
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->expectExceptionMessage('Cannot override an existing framework method.');
 
					 
					 
					 
					        $this->expectOutputRegex('~' . $regex_message . '~');
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->map('_start', function() {});
 
					 
					 
					 
					        $engine->handleException(new Exception('thrown exception message', 20));
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testRegisterExistingMethod() {
 
					 
					 
					 
					    public function testMapExistingMethod()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new Engine();
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->expectException(Exception::class);
 
					 
					 
					 
					        $engine = new Engine();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->expectExceptionMessage('Cannot override an existing framework method.');
 
					 
					 
					 
					        $this->expectException(Exception::class);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->register('_error', 'stdClass');
 
					 
					 
					 
					        $this->expectExceptionMessage('Cannot override an existing framework method.');
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					        $engine->map('_start', function () {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					        });
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testSetArrayOfValues() {
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new Engine();
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->set([ 'key1' => 'value1', 'key2' => 'value2']);
 
					 
					 
					 
					    public function testRegisterExistingMethod()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals('value1', $engine->get('key1'));
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals('value2', $engine->get('key2'));
 
					 
					 
					 
					        $engine = new Engine();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					        $this->expectException(Exception::class);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					        $this->expectExceptionMessage('Cannot override an existing framework method.');
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testStartWithRoute() {
 
					 
					 
					 
					        $engine->register('_error', 'stdClass');
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$_SERVER['REQUEST_METHOD'] = 'GET';
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$_SERVER['REQUEST_URI'] = '/someRoute';
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					    public function testSetArrayOfValues()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new class extends Engine {
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								public function getInitializedVar() {
 
					 
					 
					 
					        $engine = new Engine();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									return $this->initialized;
 
					 
					 
					 
					        $engine->set([ 'key1' => 'value1', 'key2' => 'value2']);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								}
 
					 
					 
					 
					        $this->assertEquals('value1', $engine->get('key1'));
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							};
 
					 
					 
					 
					        $this->assertEquals('value2', $engine->get('key2'));
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->route('/someRoute', function() { echo 'i ran'; }, true);
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->expectOutputString('i ran');
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->start();
 
					 
					 
					 
					    public function testStartWithRoute()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					        $_SERVER['REQUEST_METHOD'] = 'GET';
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						// n0nag0n - I don't know why this does what it does, but it's existing framework functionality 1/1/24
 
					 
					 
					 
					        $_SERVER['REQUEST_URI'] = '/someRoute';
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testStartWithRouteButReturnedValueThrows404() {
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$_SERVER['REQUEST_METHOD'] = 'GET';
 
					 
					 
					 
					        $engine = new class extends Engine {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$_SERVER['REQUEST_URI'] = '/someRoute';
 
					 
					 
					 
					            public function getInitializedVar()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					            {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new class extends Engine {
 
					 
					 
					 
					                return $this->initialized;
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								public function getInitializedVar() {
 
					 
					 
					 
					            }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									return $this->initialized;
 
					 
					 
					 
					        };
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								}
 
					 
					 
					 
					        $engine->route('/someRoute', function () {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							};
 
					 
					 
					 
					            echo 'i ran';
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->route('/someRoute', function() { echo 'i ran'; return true; }, true);
 
					 
					 
					 
					        }, true);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->expectOutputString('< h1 > 404 Not Found< / h1 > < h3 > The page you have requested could not be found.< / h3 >                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 ');
 
					 
					 
					 
					        $this->expectOutputString('i ran');
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->start();
 
					 
					 
					 
					        $engine->start();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testStopWithCode() {
 
					 
					 
					 
					    // n0nag0n - I don't know why this does what it does, but it's existing framework functionality 1/1/24
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new class extends Engine {
 
					 
					 
					 
					    public function testStartWithRouteButReturnedValueThrows404()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								public function getLoader() {
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									return $this->loader;
 
					 
					 
					 
					        $_SERVER['REQUEST_METHOD'] = 'GET';
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								}
 
					 
					 
					 
					        $_SERVER['REQUEST_URI'] = '/someRoute';
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							};
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							// doing this so we can overwrite some parts of the response
 
					 
					 
					 
					        $engine = new class extends Engine {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->getLoader()->register('response', function() {
 
					 
					 
					 
					            public function getInitializedVar()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								return new class extends Response {
 
					 
					 
					 
					            {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									public function setRealHeader(string $header_string, bool $replace = true, int $response_code = 0): self
 
					 
					 
					 
					                return $this->initialized;
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									{
 
					 
					 
					 
					            }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
										return $this;
 
					 
					 
					 
					        };
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									}
 
					 
					 
					 
					        $engine->route('/someRoute', function () {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								};
 
					 
					 
					 
					            echo 'i ran';
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							});
 
					 
					 
					 
					            return true;
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							// need to add another one of these because _stop() stops and gets clean, but $response->send() does too.....
 
					 
					 
					 
					        }, true);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							ob_start();
 
					 
					 
					 
					        $this->expectOutputString('< h1 > 404 Not Found< / h1 > < h3 > The page you have requested could not be found.< / h3 >                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 ');
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->response()->write('I am a teapot');
 
					 
					 
					 
					        $engine->start();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->expectOutputString('I am a teapot');
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->stop(500);
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals(500, $engine->response()->status());
 
					 
					 
					 
					    public function testStopWithCode()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					        $engine = new class extends Engine {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testPostRoute() {
 
					 
					 
					 
					            public function getLoader()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new Engine();
 
					 
					 
					 
					            {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->post('/someRoute', function() { echo 'i ran'; }, true);
 
					 
					 
					 
					                return $this->loader;
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$routes = $engine->router()->getRoutes();
 
					 
					 
					 
					            }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals('POST', $routes[0]->methods[0]);
 
					 
					 
					 
					        };
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals('/someRoute', $routes[0]->pattern);
 
					 
					 
					 
					        // doing this so we can overwrite some parts of the response
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					        $engine->getLoader()->register('response', function () {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					            return new class extends Response {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testPutRoute() {
 
					 
					 
					 
					                public function setRealHeader(string $header_string, bool $replace = true, int $response_code = 0): self
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new Engine();
 
					 
					 
					 
					                {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->put('/someRoute', function() { echo 'i ran'; }, true);
 
					 
					 
					 
					                    return $this;
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$routes = $engine->router()->getRoutes();
 
					 
					 
					 
					                }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals('PUT', $routes[0]->methods[0]);
 
					 
					 
					 
					            };
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals('/someRoute', $routes[0]->pattern);
 
					 
					 
					 
					        });
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					        // need to add another one of these because _stop() stops and gets clean, but $response->send() does too.....
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					        ob_start();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testPatchRoute() {
 
					 
					 
					 
					        $engine->response()->write('I am a teapot');
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new Engine();
 
					 
					 
					 
					        $this->expectOutputString('I am a teapot');
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->patch('/someRoute', function() { echo 'i ran'; }, true);
 
					 
					 
					 
					        $engine->stop(500);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$routes = $engine->router()->getRoutes();
 
					 
					 
					 
					        $this->assertEquals(500, $engine->response()->status());
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals('PATCH', $routes[0]->methods[0]);
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals('/someRoute', $routes[0]->pattern);
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					    public function testPostRoute()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testDeleteRoute() {
 
					 
					 
					 
					        $engine = new Engine();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new Engine();
 
					 
					 
					 
					        $engine->post('/someRoute', function () {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->delete('/someRoute', function() { echo 'i ran'; }, true);
 
					 
					 
					 
					            echo 'i ran';
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$routes = $engine->router()->getRoutes();
 
					 
					 
					 
					        }, true);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals('DELETE', $routes[0]->methods[0]);
 
					 
					 
					 
					        $routes = $engine->router()->getRoutes();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals('/someRoute', $routes[0]->pattern);
 
					 
					 
					 
					        $this->assertEquals('POST', $routes[0]->methods[0]);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					        $this->assertEquals('/someRoute', $routes[0]->pattern);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testHalt() {
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new class extends Engine {
 
					 
					 
					 
					    public function testPutRoute()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								public function getLoader() {
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									return $this->loader;
 
					 
					 
					 
					        $engine = new Engine();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								}
 
					 
					 
					 
					        $engine->put('/someRoute', function () {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							};
 
					 
					 
					 
					            echo 'i ran';
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							// doing this so we can overwrite some parts of the response
 
					 
					 
					 
					        }, true);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->getLoader()->register('response', function() {
 
					 
					 
					 
					        $routes = $engine->router()->getRoutes();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								return new class extends \flight\net\Response {
 
					 
					 
					 
					        $this->assertEquals('PUT', $routes[0]->methods[0]);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									public function __construct() {}
 
					 
					 
					 
					        $this->assertEquals('/someRoute', $routes[0]->pattern);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									public function setRealHeader(string $header_string, bool $replace = true, int $response_code = 0): Response
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									{
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
										return $this;
 
					 
					 
					 
					    public function testPatchRoute()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									}
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								};
 
					 
					 
					 
					        $engine = new Engine();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							});
 
					 
					 
					 
					        $engine->patch('/someRoute', function () {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->expectOutputString('skip---exit');
 
					 
					 
					 
					            echo 'i ran';
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->halt(500, 'skip---exit');
 
					 
					 
					 
					        }, true);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals(500, $engine->response()->status());
 
					 
					 
					 
					        $routes = $engine->router()->getRoutes();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					        $this->assertEquals('PATCH', $routes[0]->methods[0]);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					        $this->assertEquals('/someRoute', $routes[0]->pattern);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testRedirect() {
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new Engine();
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->redirect('https://github.com', 302);
 
					 
					 
					 
					    public function testDeleteRoute()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals('https://github.com', $engine->response()->headers()['Location']);
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals(302, $engine->response()->status());
 
					 
					 
					 
					        $engine = new Engine();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					        $engine->delete('/someRoute', function () {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					            echo 'i ran';
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testRedirectWithBaseUrl() {
 
					 
					 
					 
					        }, true);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new Engine();
 
					 
					 
					 
					        $routes = $engine->router()->getRoutes();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->set('flight.base_url', '/subdirectory');
 
					 
					 
					 
					        $this->assertEquals('DELETE', $routes[0]->methods[0]);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->redirect('/someRoute', 301);
 
					 
					 
					 
					        $this->assertEquals('/someRoute', $routes[0]->pattern);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals('/subdirectory/someRoute', $engine->response()->headers()['Location']);
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals(301, $engine->response()->status());
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					    public function testHalt()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testJson() {
 
					 
					 
					 
					        $engine = new class extends Engine {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new Engine();
 
					 
					 
					 
					            public function getLoader()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->json(['key1' => 'value1', 'key2' => 'value2']);
 
					 
					 
					 
					            {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->expectOutputString('{"key1":"value1","key2":"value2"}');
 
					 
					 
					 
					                return $this->loader;
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals('application/json; charset=utf-8', $engine->response()->headers()['Content-Type']);
 
					 
					 
					 
					            }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals(200, $engine->response()->status());
 
					 
					 
					 
					        };
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					        // doing this so we can overwrite some parts of the response
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					        $engine->getLoader()->register('response', function () {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testJsonP() {
 
					 
					 
					 
					            return new class extends \flight\net\Response {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new Engine();
 
					 
					 
					 
					                public function __construct()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->request()->query['jsonp'] = 'whatever';
 
					 
					 
					 
					                {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->jsonp(['key1' => 'value1', 'key2' => 'value2']);
 
					 
					 
					 
					                }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->expectOutputString('whatever({"key1":"value1","key2":"value2"});');
 
					 
					 
					 
					                public function setRealHeader(string $header_string, bool $replace = true, int $response_code = 0): Response
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals('application/javascript; charset=utf-8', $engine->response()->headers()['Content-Type']);
 
					 
					 
					 
					                {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals(200, $engine->response()->status());
 
					 
					 
					 
					                    return $this;
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					                }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					            };
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testJsonPBadParam() {
 
					 
					 
					 
					        });
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new Engine();
 
					 
					 
					 
					        $this->expectOutputString('skip---exit');
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->jsonp(['key1' => 'value1', 'key2' => 'value2']);
 
					 
					 
					 
					        $engine->halt(500, 'skip---exit');
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->expectOutputString('({"key1":"value1","key2":"value2"});');
 
					 
					 
					 
					        $this->assertEquals(500, $engine->response()->status());
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals('application/javascript; charset=utf-8', $engine->response()->headers()['Content-Type']);
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals(200, $engine->response()->status());
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					    public function testRedirect()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testEtagSimple() {
 
					 
					 
					 
					        $engine = new Engine();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new Engine();
 
					 
					 
					 
					        $engine->redirect('https://github.com', 302);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->etag('etag');
 
					 
					 
					 
					        $this->assertEquals('https://github.com', $engine->response()->headers()['Location']);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals('"etag"', $engine->response()->headers()['ETag']);
 
					 
					 
					 
					        $this->assertEquals(302, $engine->response()->status());
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testEtagWithHttpIfNoneMatch() {
 
					 
					 
					 
					    public function testRedirectWithBaseUrl()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							// just need this not to exit...
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new class extends Engine {
 
					 
					 
					 
					        $engine = new Engine();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								public function _halt(int $code = 200, string $message = ''): void
 
					 
					 
					 
					        $engine->set('flight.base_url', '/subdirectory');
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								{
 
					 
					 
					 
					        $engine->redirect('/someRoute', 301);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									$this->response()->status($code);
 
					 
					 
					 
					        $this->assertEquals('/subdirectory/someRoute', $engine->response()->headers()['Location']);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									$this->response()->write($message);
 
					 
					 
					 
					        $this->assertEquals(301, $engine->response()->status());
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								}
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							};
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$_SERVER['HTTP_IF_NONE_MATCH'] = 'etag';
 
					 
					 
					 
					    public function testJson()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->etag('etag');
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals('"etag"', $engine->response()->headers()['ETag']);
 
					 
					 
					 
					        $engine = new Engine();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals(304, $engine->response()->status());
 
					 
					 
					 
					        $engine->json(['key1' => 'value1', 'key2' => 'value2']);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					        $this->expectOutputString('{"key1":"value1","key2":"value2"}');
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					        $this->assertEquals('application/json; charset=utf-8', $engine->response()->headers()['Content-Type']);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testLastModifiedSimple() {
 
					 
					 
					 
					        $this->assertEquals(200, $engine->response()->status());
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new Engine();
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->lastModified(1234567890);
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals('Fri, 13 Feb 2009 23:31:30 GMT', $engine->response()->headers()['Last-Modified']);
 
					 
					 
					 
					    public function testJsonP()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					        $engine = new Engine();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testLastModifiedWithHttpIfModifiedSince() {
 
					 
					 
					 
					        $engine->request()->query['jsonp'] = 'whatever';
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							// just need this not to exit...
 
					 
					 
					 
					        $engine->jsonp(['key1' => 'value1', 'key2' => 'value2']);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new class extends Engine {
 
					 
					 
					 
					        $this->expectOutputString('whatever({"key1":"value1","key2":"value2"});');
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								public function _halt(int $code = 200, string $message = ''): void
 
					 
					 
					 
					        $this->assertEquals('application/javascript; charset=utf-8', $engine->response()->headers()['Content-Type']);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								{
 
					 
					 
					 
					        $this->assertEquals(200, $engine->response()->status());
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									$this->response()->status($code);
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									$this->response()->write($message);
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								}
 
					 
					 
					 
					    public function testJsonPBadParam()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							};
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$_SERVER['HTTP_IF_MODIFIED_SINCE'] = 'Fri, 13 Feb 2009 23:31:30 GMT';
 
					 
					 
					 
					        $engine = new Engine();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->lastModified(1234567890);
 
					 
					 
					 
					        $engine->jsonp(['key1' => 'value1', 'key2' => 'value2']);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals('Fri, 13 Feb 2009 23:31:30 GMT', $engine->response()->headers()['Last-Modified']);
 
					 
					 
					 
					        $this->expectOutputString('({"key1":"value1","key2":"value2"});');
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals(304, $engine->response()->status());
 
					 
					 
					 
					        $this->assertEquals('application/javascript; charset=utf-8', $engine->response()->headers()['Content-Type']);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					        $this->assertEquals(200, $engine->response()->status());
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testGetUrl() {
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new Engine;
 
					 
					 
					 
					    public function testEtagSimple()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->route('/path1/@param:[0-9]{3}', function() { echo 'I win'; }, false, 'path1');
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$url = $engine->getUrl('path1', [ 'param' => 123 ]);
 
					 
					 
					 
					        $engine = new Engine();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals('/path1/123', $url);
 
					 
					 
					 
					        $engine->etag('etag');
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					        $this->assertEquals('"etag"', $engine->response()->headers()['ETag']);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testMiddlewareCallableFunction() {
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new Engine();
 
					 
					 
					 
					    public function testEtagWithHttpIfNoneMatch()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->route('/path1/@id', function($id) { echo 'OK'.$id; })
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								->addMiddleware(function($params) { echo 'before'.$params['id']; });
 
					 
					 
					 
					        // just need this not to exit...
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->request()->url = '/path1/123';
 
					 
					 
					 
					        $engine = new class extends Engine {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->start();
 
					 
					 
					 
					            public function _halt(int $code = 200, string $message = ''): void
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->expectOutputString('before123OK123');
 
					 
					 
					 
					            {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					                $this->response()->status($code);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					                $this->response()->write($message);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testMiddlewareCallableFunctionReturnFalse() {
 
					 
					 
					 
					            }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new class extends Engine {
 
					 
					 
					 
					        };
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								public function _halt(int $code = 200, string $message = ''): void
 
					 
					 
					 
					        $_SERVER['HTTP_IF_NONE_MATCH'] = 'etag';
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								{
 
					 
					 
					 
					        $engine->etag('etag');
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									$this->response()->status($code);
 
					 
					 
					 
					        $this->assertEquals('"etag"', $engine->response()->headers()['ETag']);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									$this->response()->write($message);
 
					 
					 
					 
					        $this->assertEquals(304, $engine->response()->status());
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								}
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							};
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->route('/path1/@id', function($id) { echo 'OK'.$id; })
 
					 
					 
					 
					    public function testLastModifiedSimple()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								->addMiddleware(function($params) { echo 'before'.$params['id']; return false; });
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->request()->url = '/path1/123';
 
					 
					 
					 
					        $engine = new Engine();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->start();
 
					 
					 
					 
					        $engine->lastModified(1234567890);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->expectOutputString('Forbiddenbefore123');
 
					 
					 
					 
					        $this->assertEquals('Fri, 13 Feb 2009 23:31:30 GMT', $engine->response()->headers()['Last-Modified']);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals(403, $engine->response()->status());
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					    public function testLastModifiedWithHttpIfModifiedSince()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testMiddlewareClassBefore() {
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$middleware = new class {
 
					 
					 
					 
					        // just need this not to exit...
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								public function before($params) {
 
					 
					 
					 
					        $engine = new class extends Engine {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									echo 'before'.$params['id'];
 
					 
					 
					 
					            public function _halt(int $code = 200, string $message = ''): void
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								}
 
					 
					 
					 
					            {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							};
 
					 
					 
					 
					                $this->response()->status($code);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new Engine();
 
					 
					 
					 
					                $this->response()->write($message);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							
 
					 
					 
					 
					            }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->route('/path1/@id',  function($id) { echo 'OK'.$id; })
 
					 
					 
					 
					        };
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								->addMiddleware($middleware);
 
					 
					 
					 
					        $_SERVER['HTTP_IF_MODIFIED_SINCE'] = 'Fri, 13 Feb 2009 23:31:30 GMT';
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->request()->url = '/path1/123';
 
					 
					 
					 
					        $engine->lastModified(1234567890);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->start();
 
					 
					 
					 
					        $this->assertEquals('Fri, 13 Feb 2009 23:31:30 GMT', $engine->response()->headers()['Last-Modified']);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->expectOutputString('before123OK123');
 
					 
					 
					 
					        $this->assertEquals(304, $engine->response()->status());
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testMiddlewareClassBeforeAndAfter() {
 
					 
					 
					 
					    public function testGetUrl()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$middleware = new class {
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								public function before($params) {
 
					 
					 
					 
					        $engine = new Engine();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									echo 'before'.$params['id'];
 
					 
					 
					 
					        $engine->route('/path1/@param:[0-9]{3}', function () {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								}
 
					 
					 
					 
					            echo 'I win';
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								public function after($params) {
 
					 
					 
					 
					        }, false, 'path1');
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									echo 'after'.$params['id'];
 
					 
					 
					 
					        $url = $engine->getUrl('path1', [ 'param' => 123 ]);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								}
 
					 
					 
					 
					        $this->assertEquals('/path1/123', $url);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							};
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new Engine();
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							
 
					 
					 
					 
					    public function testMiddlewareCallableFunction()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->route('/path1/@id',  function($id) { echo 'OK'.$id; })
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								->addMiddleware($middleware);
 
					 
					 
					 
					        $engine = new Engine();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->request()->url = '/path1/123';
 
					 
					 
					 
					        $engine->route('/path1/@id', function ($id) {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->start();
 
					 
					 
					 
					            echo 'OK' . $id;
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->expectOutputString('before123OK123after123');
 
					 
					 
					 
					        })
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					            ->addMiddleware(function ($params) {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					                echo 'before' . $params['id'];
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testMiddlewareClassAfter() {
 
					 
					 
					 
					            });
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$middleware = new class {
 
					 
					 
					 
					        $engine->request()->url = '/path1/123';
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								public function after($params) {
 
					 
					 
					 
					        $engine->start();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									
 
					 
					 
					 
					        $this->expectOutputString('before123OK123');
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									echo 'after'.$params['id'];
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								}
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							};
 
					 
					 
					 
					    public function testMiddlewareCallableFunctionReturnFalse()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new Engine();
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							
 
					 
					 
					 
					        $engine = new class extends Engine {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->route('/path1/@id',  function($id) { echo 'OK'.$id; })
 
					 
					 
					 
					            public function _halt(int $code = 200, string $message = ''): void
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								->addMiddleware($middleware);
 
					 
					 
					 
					            {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->request()->url = '/path1/123';
 
					 
					 
					 
					                $this->response()->status($code);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->start();
 
					 
					 
					 
					                $this->response()->write($message);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->expectOutputString('OK123after123');
 
					 
					 
					 
					            }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					        };
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					        $engine->route('/path1/@id', function ($id) {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testMiddlewareClassAfterFailedCheck() {
 
					 
					 
					 
					            echo 'OK' . $id;
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$middleware = new class {
 
					 
					 
					 
					        })
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								public function after($params) {
 
					 
					 
					 
					            ->addMiddleware(function ($params) {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									echo 'after'.$params['id'];
 
					 
					 
					 
					                echo 'before' . $params['id'];
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									return false;
 
					 
					 
					 
					                return false;
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								}
 
					 
					 
					 
					            });
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							};
 
					 
					 
					 
					        $engine->request()->url = '/path1/123';
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new class extends Engine {
 
					 
					 
					 
					        $engine->start();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								public function _halt(int $code = 200, string $message = ''): void
 
					 
					 
					 
					        $this->expectOutputString('Forbiddenbefore123');
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								{
 
					 
					 
					 
					        $this->assertEquals(403, $engine->response()->status());
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									$this->response()->status($code);
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									$this->response()->write($message);
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								}
 
					 
					 
					 
					    public function testMiddlewareClassBefore()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							};
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							
 
					 
					 
					 
					        $middleware = new class {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->route('/path1/@id',  function($id) { echo 'OK'.$id; })
 
					 
					 
					 
					            public function before($params)
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								->addMiddleware($middleware);
 
					 
					 
					 
					            {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->request()->url = '/path1/123';
 
					 
					 
					 
					                echo 'before' . $params['id'];
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->start();
 
					 
					 
					 
					            }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->assertEquals(403, $engine->response()->status());
 
					 
					 
					 
					        };
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->expectOutputString('ForbiddenOK123after123');
 
					 
					 
					 
					        $engine = new Engine();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					        $engine->route('/path1/@id', function ($id) {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testMiddlewareCallableFunctionMultiple() {
 
					 
					 
					 
					            echo 'OK' . $id;
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new Engine();
 
					 
					 
					 
					        })
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->route('/path1/@id', function($id) { echo 'OK'.$id; })
 
					 
					 
					 
					            ->addMiddleware($middleware);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								->addMiddleware(function($params) { echo 'before1'.$params['id']; })
 
					 
					 
					 
					        $engine->request()->url = '/path1/123';
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								->addMiddleware(function($params) { echo 'before2'.$params['id']; });
 
					 
					 
					 
					        $engine->start();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->request()->url = '/path1/123';
 
					 
					 
					 
					        $this->expectOutputString('before123OK123');
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->start();
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->expectOutputString('before1123before2123OK123');
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					    public function testMiddlewareClassBeforeAndAfter()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						// Pay attention to the order on how the middleware is executed in this test.
 
					 
					 
					 
					        $middleware = new class {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testMiddlewareClassCallableRouteMultiple() {
 
					 
					 
					 
					            public function before($params)
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$middleware = new class {
 
					 
					 
					 
					            {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								public function before($params) {
 
					 
					 
					 
					                echo 'before' . $params['id'];
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									echo 'before'.$params['another_id'];
 
					 
					 
					 
					            }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								}
 
					 
					 
					 
					            public function after($params)
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								public function after($params) {
 
					 
					 
					 
					            {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									echo 'after'.$params['id'];
 
					 
					 
					 
					                echo 'after' . $params['id'];
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								}
 
					 
					 
					 
					            }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							};
 
					 
					 
					 
					        };
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$middleware2 = new class {
 
					 
					 
					 
					        $engine = new Engine();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								public function before($params) {
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									echo 'before'.$params['id'];
 
					 
					 
					 
					        $engine->route('/path1/@id', function ($id) {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								}
 
					 
					 
					 
					            echo 'OK' . $id;
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								public function after($params) {
 
					 
					 
					 
					        })
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									echo 'after'.$params['id'].$params['another_id'];
 
					 
					 
					 
					            ->addMiddleware($middleware);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								}
 
					 
					 
					 
					        $engine->request()->url = '/path1/123';
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							};
 
					 
					 
					 
					        $engine->start();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new Engine();
 
					 
					 
					 
					        $this->expectOutputString('before123OK123after123');
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->route('/path1/@id/subpath1/@another_id',  function() { echo 'OK'; })->addMiddleware([ $middleware, $middleware2 ]);
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->request()->url = '/path1/123/subpath1/456';
 
					 
					 
					 
					    public function testMiddlewareClassAfter()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->start();
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->expectOutputString('before456before123OKafter123456after123');
 
					 
					 
					 
					        $middleware = new class {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					            public function after($params)
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
					
 
					 
					 
					 
					            {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						public function testMiddlewareClassGroupRouteMultipleBooyah() {
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$middleware = new class {
 
					 
					 
					 
					                echo 'after' . $params['id'];
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								public function before($params) {
 
					 
					 
					 
					            }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									echo 'before'.$params['another_id'];
 
					 
					 
					 
					        };
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								}
 
					 
					 
					 
					        $engine = new Engine();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								public function after($params) {
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									echo 'after'.$params['id'];
 
					 
					 
					 
					        $engine->route('/path1/@id', function ($id) {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								}
 
					 
					 
					 
					            echo 'OK' . $id;
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							};
 
					 
					 
					 
					        })
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$middleware2 = new class {
 
					 
					 
					 
					            ->addMiddleware($middleware);
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								public function before($params) {
 
					 
					 
					 
					        $engine->request()->url = '/path1/123';
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									echo 'before'.$params['id'];
 
					 
					 
					 
					        $engine->start();
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								}
 
					 
					 
					 
					        $this->expectOutputString('OK123after123');
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								public function after($params) {
 
					 
					 
					 
					    }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
									echo 'after'.$params['id'].$params['another_id'];
 
					 
					 
					 
					
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								}
 
					 
					 
					 
					    public function testMiddlewareClassAfterFailedCheck()
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							};
 
					 
					 
					 
					    {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine = new Engine();
 
					 
					 
					 
					        $middleware = new class {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->group('/path1/@id', function($router) {
 
					 
					 
					 
					            public function after($params)
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								$router->map('/subpath1/@another_id',  function() { echo 'OK'; });
 
					 
					 
					 
					            {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
								$router->map('/@cool_id',  function() { echo 'OK'; });
 
					 
					 
					 
					                echo 'after' . $params['id'];
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							}, [ $middleware, $middleware2 ]);
 
					 
					 
					 
					                return false;
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							
 
					 
					 
					 
					            }
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->request()->url = '/path1/123/subpath1/456';
 
					 
					 
					 
					        };
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$engine->start();
 
					 
					 
					 
					        $engine = new class extends Engine {
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
							$this->expectOutputString('before456before123OKafter123456after123');
 
					 
					 
					 
					            public function _halt(int $code = 200, string $message = ''): void
 
				
			 
			
				
				
			
		
	
		
		
			
				
					
					 
					 
					 
						}
 
					 
					 
					 
					            {
 
				
			 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					                $this->response()->status($code);
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					                $this->response()->write($message);
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            }
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        };
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        $engine->route('/path1/@id', function ($id) {
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            echo 'OK' . $id;
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        })
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            ->addMiddleware($middleware);
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        $engine->request()->url = '/path1/123';
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        $engine->start();
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        $this->assertEquals(403, $engine->response()->status());
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        $this->expectOutputString('ForbiddenOK123after123');
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    }
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    public function testMiddlewareCallableFunctionMultiple()
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    {
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        $engine = new Engine();
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        $engine->route('/path1/@id', function ($id) {
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            echo 'OK' . $id;
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        })
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            ->addMiddleware(function ($params) {
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					                echo 'before1' . $params['id'];
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            })
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            ->addMiddleware(function ($params) {
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					                echo 'before2' . $params['id'];
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            });
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        $engine->request()->url = '/path1/123';
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        $engine->start();
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        $this->expectOutputString('before1123before2123OK123');
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    }
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    // Pay attention to the order on how the middleware is executed in this test.
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    public function testMiddlewareClassCallableRouteMultiple()
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    {
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        $middleware = new class {
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            public function before($params)
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            {
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					                echo 'before' . $params['another_id'];
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            }
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            public function after($params)
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            {
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					                echo 'after' . $params['id'];
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            }
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        };
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        $middleware2 = new class {
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            public function before($params)
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            {
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					                echo 'before' . $params['id'];
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            }
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            public function after($params)
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            {
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					                echo 'after' . $params['id'] . $params['another_id'];
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            }
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        };
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        $engine = new Engine();
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        $engine->route('/path1/@id/subpath1/@another_id', function () {
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            echo 'OK';
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        })->addMiddleware([ $middleware, $middleware2 ]);
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        $engine->request()->url = '/path1/123/subpath1/456';
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        $engine->start();
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        $this->expectOutputString('before456before123OKafter123456after123');
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    }
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    public function testMiddlewareClassGroupRouteMultipleBooyah()
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    {
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        $middleware = new class {
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            public function before($params)
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            {
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					                echo 'before' . $params['another_id'];
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            }
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            public function after($params)
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            {
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					                echo 'after' . $params['id'];
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            }
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        };
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        $middleware2 = new class {
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            public function before($params)
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            {
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					                echo 'before' . $params['id'];
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            }
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            public function after($params)
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            {
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					                echo 'after' . $params['id'] . $params['another_id'];
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            }
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        };
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        $engine = new Engine();
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        $engine->group('/path1/@id', function ($router) {
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            $router->map('/subpath1/@another_id', function () {
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					                echo 'OK';
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            });
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            $router->map('/@cool_id', function () {
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					                echo 'OK';
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					            });
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        }, [ $middleware, $middleware2 ]);
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        $engine->request()->url = '/path1/123/subpath1/456';
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        $engine->start();
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					        $this->expectOutputString('before456before123OKafter123456after123');
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					 
					 
					 
					 
					    }
 
				
			 
			
		
	
		
		
			
				
					
					 
					 
					 
					}
 
					 
					 
					 
					}