request = new CLIRequest(new App()); $_POST = []; $_GET = []; } //-------------------------------------------------------------------- public function testParsingSegments() { $_SERVER['argv'] = [ 'index.php', 'users', '21', 'profile', '-foo', 'bar', ]; $_SERVER['argc'] = 6; // reinstantiate it to force parsing $this->request = new CLIRequest(new App()); $segments = [ 'users', '21', 'profile', ]; $this->assertEquals($segments, $this->request->getSegments()); } public function testParsingOptions() { $_SERVER['argv'] = [ 'index.php', 'users', '21', 'profile', '-foo', 'bar', ]; $_SERVER['argc'] = 6; // reinstantiate it to force parsing $this->request = new CLIRequest(new App()); $options = ['foo' => 'bar']; $this->assertEquals($options, $this->request->getOptions()); } public function testParsingOptionDetails() { $_SERVER['argv'] = [ 'index.php', 'users', '21', 'profile', '-foo', 'bar', ]; $_SERVER['argc'] = 6; // reinstantiate it to force parsing $this->request = new CLIRequest(new App()); $this->assertEquals('bar', $this->request->getOption('foo')); $this->assertNull($this->request->getOption('notthere')); } public function testParsingOptionString() { $_SERVER['argv'] = [ 'index.php', 'users', '21', 'profile', '-foo', 'bar', '-baz', 'queue some stuff', ]; $_SERVER['argc'] = 8; // reinstantiate it to force parsing $this->request = new CLIRequest(new App()); $expected = '-foo bar -baz "queue some stuff"'; $this->assertEquals($expected, $this->request->getOptionString()); } public function testParsingNoOptions() { $_SERVER['argv'] = [ 'index.php', 'users', '21', 'profile', ]; $_SERVER['argc'] = 4; // reinstantiate it to force parsing $this->request = new CLIRequest(new App()); $expected = ''; $this->assertEquals($expected, $this->request->getOptionString()); } public function testParsingPath() { $_SERVER['argv'] = [ 'index.php', 'users', '21', 'profile', '-foo', 'bar', ]; $_SERVER['argc'] = 6; // reinstantiate it to force parsing $this->request = new CLIRequest(new App()); $expected = 'users/21/profile'; $this->assertEquals($expected, $this->request->getPath()); } public function testParsingMalformed() { $_SERVER['argv'] = [ 'index.php', 'users', '21', 'pro-file', '-foo', 'bar', '-baz', 'queue some stuff', ]; $_SERVER['argc'] = 8; // reinstantiate it to force parsing $this->request = new CLIRequest(new App()); $expectedOptions = '-foo bar -baz "queue some stuff"'; $expectedPath = 'users/21'; $this->assertEquals($expectedOptions, $this->request->getOptionString()); $this->assertEquals($expectedPath, $this->request->getPath()); } public function testParsingMalformed2() { $_SERVER['argv'] = [ 'index.php', 'users', '21', 'profile', '-foo', 'oops-bar', '-baz', 'queue some stuff', ]; $_SERVER['argc'] = 8; // reinstantiate it to force parsing $this->request = new CLIRequest(new App()); $expectedOptions = '-foo oops-bar -baz "queue some stuff"'; $expectedPath = 'users/21/profile'; $this->assertEquals($expectedOptions, $this->request->getOptionString()); $this->assertEquals($expectedPath, $this->request->getPath()); } public function testParsingMalformed3() { $_SERVER['argv'] = [ 'index.php', 'users', '21', 'profile', '-foo', 'oops', 'bar', '-baz', 'queue some stuff', ]; $_SERVER['argc'] = 9; // reinstantiate it to force parsing $this->request = new CLIRequest(new App()); $expectedOptions = '-foo oops -baz "queue some stuff"'; $expectedPath = 'users/21/profile'; $this->assertEquals($expectedOptions, $this->request->getOptionString()); $this->assertEquals($expectedPath, $this->request->getPath()); } //-------------------------------------------------------------------- public function testFetchGlobalsSingleValue() { $_POST['foo'] = 'bar'; $_GET['bar'] = 'baz'; $this->assertEquals('bar', $this->request->fetchGlobal('post', 'foo')); $this->assertEquals('baz', $this->request->fetchGlobal('get', 'bar')); } public function testFetchGlobalsReturnsNullWhenNotFound() { $this->assertNull($this->request->fetchGlobal('post', 'foo')); } public function testFetchGlobalsFiltersValues() { $this->request->setGlobal('post', [ 'foo' => 'bar