From 682e5164b27f4f36d80dfeca8f4a2f87afdd918e Mon Sep 17 00:00:00 2001 From: Ben XO <75862+ben-xo@users.noreply.github.com> Date: Mon, 17 Oct 2022 17:05:16 +0100 Subject: [PATCH] Finally get getopt media-dir tests working --- dir2cast.php | 4 +- test/FakeGetoptTest.php | 4 +- test/SettingsHandlerTest.php | 182 ++++++++++++++++++++++++++++++----- test/bootstrap.php | 18 ++-- test/run.sh | 1 + 5 files changed, 171 insertions(+), 38 deletions(-) diff --git a/dir2cast.php b/dir2cast.php index f1cd7b1..ff2ab03 100644 --- a/dir2cast.php +++ b/dir2cast.php @@ -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); } } @@ -2223,6 +2222,7 @@ function main($args) } catch(ExitException $e) { + print($e->getMessage()."\n"); exit($e->getCode()); } } diff --git a/test/FakeGetoptTest.php b/test/FakeGetoptTest.php index a5bcc80..887ff2c 100644 --- a/test/FakeGetoptTest.php +++ b/test/FakeGetoptTest.php @@ -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() diff --git a/test/SettingsHandlerTest.php b/test/SettingsHandlerTest.php index 8155795..b12ff0b 100644 --- a/test/SettingsHandlerTest.php +++ b/test/SettingsHandlerTest.php @@ -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() { @@ -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']); @@ -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 /** @@ -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); + } + } } } diff --git a/test/bootstrap.php b/test/bootstrap.php index 6ed07a2..461e017 100644 --- a/test/bootstrap.php +++ b/test/bootstrap.php @@ -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', <</dev/null", $output, $result_code); return unserialize($output[0]); } diff --git a/test/run.sh b/test/run.sh index 11f01d9..031418f 100755 --- a/test/run.sh +++ b/test/run.sh @@ -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 \