Skip to content

Commit

Permalink
Finally get getopt media-dir tests working
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-xo committed Oct 17, 2022
1 parent fbf5464 commit 682e516
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 38 deletions.
4 changes: 2 additions & 2 deletions dir2cast.php
Original file line number Diff line number Diff line change
Expand Up @@ -1642,8 +1642,7 @@ public static function display404($message)
header("HTTP/1.0 404 Not Found");
header("Content-type: text/plain");
}
echo "Not Found: $message\n";
throw new ExitException("", -2);
throw new ExitException("Not Found: $message", -2);
}
}

Expand Down Expand Up @@ -2223,6 +2222,7 @@ function main($args)
}
catch(ExitException $e)
{
print($e->getMessage()."\n");
exit($e->getCode());
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/FakeGetoptTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ public function test_fake_getopt_escaping()
fake_getopt(array('php', "--media-dir= "), '', array('media-dir::')),
array('media-dir' => ' ')
);
print(fake_getopt_command(array('php', '--media-dir=""'), '', array('media-dir::')));
$this->assertEquals(
fake_getopt(array('php', '--media-dir=""'), '', array('media-dir::')),
array('media-dir' => '""')
);
print(fake_getopt(array('php', '--media-dir=\'\''), '', array('media-dir::')));
$this->assertEquals(
fake_getopt(array('php', "--media-dir=''"), '', array('media-dir::')),
array('media-dir' => "''")
false // XXX: seems to be a bug in getopt
);
}
public function test_fake_getopt_both_arg_types()
Expand Down
182 changes: 156 additions & 26 deletions test/SettingsHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class SettingsHandlerTest extends TestCase
'DONT_UNCACHE_IF_OUTPUT_FILE',
'MIN_FILE_AGE',
);

public $temp_file = false;

public function test_getopt_hook()
{
Expand All @@ -51,17 +53,41 @@ public function test_getopt_hook()
$short_options = '';
$long_options = array('help', 'media-dir::', 'bootstrap');

$cli_options = SettingsHandler::getopt(array(), $short_options, $long_options);
$cli_options = SettingsHandler::getopt(
array(),
$short_options, $long_options
);
$this->assertEquals($cli_options, array());

$cli_options = SettingsHandler::getopt(array('--help'), $short_options, $long_options);
$this->assertEquals($cli_options, array('help' => true));
$cli_options = SettingsHandler::getopt(
array('dir2cast.php'),
$short_options, $long_options
);
$this->assertEquals($cli_options, array());

$cli_options = SettingsHandler::getopt(array('--media-dir='), $short_options, $long_options);
$this->assertEquals($cli_options, array('media-dir' => ''));
$cli_options = SettingsHandler::getopt(
array('dir2cast.php', '--help'),
$short_options, $long_options
);
$this->assertEquals($cli_options, array('help' => false));

$cli_options = SettingsHandler::getopt(
array('dir2cast.php', '--media-dir=test1'),
$short_options, $long_options
);
$this->assertEquals($cli_options, array('media-dir' => 'test1'));

$cli_options = SettingsHandler::getopt(
array('dir2cast.php', '--media-dir=test2', '--bootstrap'),
$short_options, $long_options
);
$this->assertEquals($cli_options, array('media-dir' => 'test2', 'bootstrap' => false));

$cli_options = SettingsHandler::getopt(array('--media-dir=test'), $short_options, $long_options);
$this->assertEquals($cli_options, array('media-dir' => 'test'));
$cli_options = SettingsHandler::getopt(
array('dir2cast.php', '--bootstrap', '--media-dir=test3'),
$short_options, $long_options
);
$this->assertEquals($cli_options, array('media-dir' => 'test3', 'bootstrap' => false));

$this->assertEquals($argv_copy, $GLOBALS['argv']);
$this->assertEquals($argc_copy, $GLOBALS['argc']);
Expand Down Expand Up @@ -184,40 +210,133 @@ public function test_when_SERVER_HTTP_HOST_then_MP3_BASE_defaults_to_same_dir()
*/
public function test_cli_media_404()
{
$temp = tempnam('./', 'test_cli_media_404');
try
{
$this->expectException("ExitException");
$this->expectExceptionCode(-2);
SettingsHandler::bootstrap(array(), array(), array("dir2cast.php", "--media-dir=$temp"));
}
catch(Exception $e)
{
throw $e;
}
finally
{
unlink($temp);
}
$this->temp_file = basename(tempnam('./', 'test_cli_media_404'));
$this->assertFalse(strpos($this->temp_file, '/'));
unlink($this->temp_file);

$this->expectException("ExitException");
$this->expectExceptionMessage("Not Found: {$this->temp_file}");
$this->expectExceptionCode(-2);
SettingsHandler::bootstrap(array(), array(), array("dir2cast.php", "--media-dir={$this->temp_file}"));
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_cli_media_dir_404()
public function test_GET_media_404()
{
$this->temp_file = basename(tempnam('../', 'test_GET_media_404'));
$this->assertFalse(strpos($this->temp_file, '/'));
unlink('../' . $this->temp_file);

$this->expectException("ExitException");
$this->expectExceptionMessage("Not Found: {$this->temp_file}");
$this->expectExceptionCode(-2);
SettingsHandler::bootstrap(array(), array("dir" => $this->temp_file), array());
}


/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_cli_media_not_dir_404()
{
$this->temp_file = basename(tempnam('./', 'test_cli_media_not_dir_404'));

$this->expectException("ExitException");
$this->expectExceptionMessage("Not Found: {$this->temp_file}");
$this->expectExceptionCode(-2);
SettingsHandler::bootstrap(array(), array(), array("dir2cast.php", "--media-dir={$this->temp_file}"));
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_GET_media_not_dir_404()
{
$this->temp_file = basename(tempnam('../', 'test_GET_media_not_dir_404'));

$this->expectException("ExitException");
$this->expectExceptionMessage("Not Found: {$this->temp_file}");
$this->expectExceptionCode(-2);
SettingsHandler::bootstrap(array(), array("dir" => $this->temp_file), array());
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_cli_media_dir_but_no_permissions_404()
{
$this->temp_file = basename(tempnam('./', 'test_cli_media_dir_but_no_permissions_404'));
unlink($this->temp_file);
mkdir($this->temp_file);
chmod($this->temp_file, 0);

$this->expectException("ExitException");
$this->expectExceptionMessage("Not Found: {$this->temp_file}");
$this->expectExceptionCode(-2);
SettingsHandler::bootstrap(array(), array(), array("dir2cast.php", "--media-dir={$this->temp_file}"));
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_GET_media_dir_but_no_permissions_404()
{
$this->temp_file = basename(tempnam('../', 'test_GET_media_dir_but_no_permissions_404'));
unlink('../' . $this->temp_file);
mkdir('../' . $this->temp_file);
chmod('../' . $this->temp_file, 0);

$this->expectException("ExitException");
$this->expectExceptionMessage("Not Found: {$this->temp_file}");
$this->expectExceptionCode(-2);
SettingsHandler::bootstrap(array(), array("dir" => $this->temp_file), array());
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_cli_arg_parsing()
public function test_cli_media_dir_a_ok()
{
$this->temp_file = basename(tempnam('./', 'test_cli_media_dir_a_ok'));
unlink($this->temp_file);
mkdir($this->temp_file);

SettingsHandler::bootstrap(array(), array(), array("dir2cast.php", "--media-dir={$this->temp_file}"));
$this->assertEquals(MP3_BASE, realpath('.'));
$this->assertEquals(MP3_DIR, realpath($this->temp_file));
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function test_GET_media_dir_a_ok()
{
$this->temp_file = basename(tempnam('../', 'test_GET_media_dir_a_ok'));
unlink('../' . $this->temp_file);
mkdir('../' . $this->temp_file);

SettingsHandler::bootstrap(array(), array("dir" => $this->temp_file), array());
$this->assertEquals(MP3_BASE, realpath('..')); // due to bootstrap.php chdir
$this->assertEquals(MP3_DIR, realpath('../' . $this->temp_file));
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
// public function test_cli_arg_parsing()
// {

// }

// TODO: test HTTP_HOST + GET dir

/**
Expand Down Expand Up @@ -396,6 +515,17 @@ public function tearDown(): void
file_exists('itunes_summary.txt') && unlink('itunes_summary.txt');
file_exists('image.jpg') && unlink('image.jpg');
file_exists('itunes_image.jpg') && unlink('itunes_image.jpg');
if($this->temp_file)
{
if(file_exists($this->temp_file)) {
if(is_dir($this->temp_file)) rmdir($this->temp_file);
else unlink($this->temp_file);
}
elseif(file_exists('../'.$this->temp_file)) {
if(is_dir('../'.$this->temp_file)) rmdir('../'.$this->temp_file);
else unlink('../'.$this->temp_file);
}
}
}
}

18 changes: 10 additions & 8 deletions test/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,25 +94,27 @@ function fake_getopt_command($argv_in, $short_options, $long_options)
{
$argv_string = "'" . implode("', '", array_map('escape_single_quoted_string', $argv_in) ). "'";
$argv_count = count($argv_in);
$short_options_string = addslashes($short_options);
$short_options_string = escape_single_quoted_string($short_options);
$long_options_string = "'" . implode("', '", array_map('escape_single_quoted_string', $long_options) ). "'";

$command_parts = array(
'php', '-d', 'register_argc_argv=false', '-r', <<<EOSCRIPT
'php', '-d', 'register_argc_argv=false', '-r', escapeshellarg(<<<EOSCRIPT
\$GLOBALS["argv"]=array($argv_string);
\$GLOBALS["argc"]=$argv_count;
print(serialize((getopt('$short_options_string', array($long_options_string)))));
EOSCRIPT
print(serialize(getopt('$short_options_string', array($long_options_string))));
EOSCRIPT)
);
return implode(" ", array_map('escapeshellarg', $command_parts));
return implode(" ", $command_parts);
}

/**
* Dangerous (due to exec()) and unlikely to work properly outside of testing.
* Needed because getopt() can't have its input mocked without register_argc_argv=false !
*/
function fake_getopt($argv_in, $short_options, $long_options)
{
// $command = php -d 'register_argc_argv=false' -r '$GLOBALS["argv"]=array("php", "--stuff");$GLOBALS["argc"]=1;print(serialize((getopt("", array("stuff")))));'

$command = fake_getopt_command($argv_in, $short_options, $long_options);
exec($command, $output, $result_code);
exec($command . " 2>/dev/null", $output, $result_code);
return unserialize($output[0]);
}

Expand Down
1 change: 1 addition & 0 deletions test/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ if [[ "$PATH_COVERAGE" != '' ]]; then
fi

vendor/bin/phpunit \
--colors=always \
--bootstrap "$SCRIPT_DIR/bootstrap.php" \
--coverage-php /tmp/cov-main \
--coverage-filter ../dir2cast.php \
Expand Down

0 comments on commit 682e516

Please sign in to comment.