*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\HTTP;
use CodeIgniter\Test\CIUnitTestCase;
use Config\App;
/**
* This should be the same as RequestTest,
* except also testing the methods added by CLIRequest
*
* @backupGlobals enabled
*
* @internal
*
* @group Others
*/
final class CLIRequestTest extends CIUnitTestCase
{
private CLIRequest $request;
protected function setUp(): void
{
parent::setUp();
$this->request = new CLIRequest(new App());
$_POST = [];
$_GET = [];
}
public function testParsingSegments()
{
$_SERVER['argv'] = [
'index.php',
'users',
'21',
'profile',
'-foo',
'bar',
];
// reinstantiate it to force parsing
$this->request = new CLIRequest(new App());
$segments = [
'users',
'21',
'profile',
];
$this->assertSame($segments, $this->request->getSegments());
}
public function testParsingSegmentsWithHTMLMetaChars()
{
$_SERVER['argv'] = [
'index.php',
'users',
'21',
'abc < def',
"McDonald's",
'aaa',
];
// reinstantiate it to force parsing
$this->request = new CLIRequest(new App());
$segments = [
'users',
'21',
'abc < def',
"McDonald's",
'aaa',
];
$this->assertSame($segments, $this->request->getSegments());
}
public function testParsingOptions()
{
$_SERVER['argv'] = [
'index.php',
'users',
'21',
'profile',
'--foo',
'bar',
'--foo-bar',
'yes',
];
// reinstantiate it to force parsing
$this->request = new CLIRequest(new App());
$options = [
'foo' => 'bar',
'foo-bar' => 'yes',
];
$this->assertSame($options, $this->request->getOptions());
}
public function testParsingOptionDetails()
{
$_SERVER['argv'] = [
'index.php',
'users',
'21',
'profile',
'--foo',
'bar',
];
// reinstantiate it to force parsing
$this->request = new CLIRequest(new App());
$this->assertSame('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',
];
// reinstantiate it to force parsing
$this->request = new CLIRequest(new App());
$this->assertSame('-foo bar -baz "queue some stuff"', $this->request->getOptionString());
$this->assertSame('--foo bar --baz "queue some stuff"', $this->request->getOptionString(true));
}
public function testParsingNoOptions()
{
$_SERVER['argv'] = [
'index.php',
'users',
'21',
'profile',
];
// reinstantiate it to force parsing
$this->request = new CLIRequest(new App());
$expected = '';
$this->assertSame($expected, $this->request->getOptionString());
}
public function testParsingArgs()
{
$_SERVER['argv'] = [
'spark',
'command',
'param1',
'param2',
'--opt1',
'opt1val',
'--opt-2',
'opt 2 val',
'param3',
];
// reinstantiate it to force parsing
$this->request = new CLIRequest(new App());
$options = [
'command',
'param1',
'param2',
'opt1' => 'opt1val',
'opt-2' => 'opt 2 val',
'param3',
];
$this->assertSame($options, $this->request->getArgs());
}
public function testParsingPath()
{
$_SERVER['argv'] = [
'index.php',
'users',
'21',
'profile',
'--foo',
'bar',
];
// reinstantiate it to force parsing
$this->request = new CLIRequest(new App());
$this->assertSame('users/21/profile', $this->request->getPath());
}
public function testParsingMalformed()
{
$_SERVER['argv'] = [
'index.php',
'users',
'21',
'pro-file',
'--foo',
'bar',
'--baz',
'queue some stuff',
];
// reinstantiate it to force parsing
$this->request = new CLIRequest(new App());
$this->assertSame('-foo bar -baz "queue some stuff"', $this->request->getOptionString());
$this->assertSame('--foo bar --baz "queue some stuff"', $this->request->getOptionString(true));
$this->assertSame('users/21/pro-file', $this->request->getPath());
}
public function testParsingMalformed2()
{
$_SERVER['argv'] = [
'index.php',
'users',
'21',
'profile',
'--foo',
'oops-bar',
'--baz',
'queue some stuff',
];
// reinstantiate it to force parsing
$this->request = new CLIRequest(new App());
$this->assertSame('-foo oops-bar -baz "queue some stuff"', $this->request->getOptionString());
$this->assertSame('--foo oops-bar --baz "queue some stuff"', $this->request->getOptionString(true));
$this->assertSame('users/21/profile', $this->request->getPath());
}
public function testParsingMalformed3()
{
$_SERVER['argv'] = [
'index.php',
'users',
'21',
'profile',
'--foo',
'oops',
'bar',
'--baz',
'queue some stuff',
];
// reinstantiate it to force parsing
$this->request = new CLIRequest(new App());
$this->assertSame('-foo oops -baz "queue some stuff"', $this->request->getOptionString());
$this->assertSame('--foo oops --baz "queue some stuff"', $this->request->getOptionString(true));
$this->assertSame('users/21/profile/bar', $this->request->getPath());
}
public function testFetchGlobalsSingleValue()
{
$_POST['foo'] = 'bar';
$_GET['bar'] = 'baz';
$this->assertSame('bar', $this->request->fetchGlobal('post', 'foo'));
$this->assertSame('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