Updating code to allow unit test to use try-catch block to catch warning

without stopping code execution inside try
This commit is contained in:
Igor Nadj 2014-11-26 15:27:54 +13:00
parent f823831a63
commit 657606e8c8
2 changed files with 17 additions and 10 deletions

View File

@ -125,20 +125,22 @@ class SSViewerTest extends SapphireTest {
// exception thrown... good
}
// secondly, make sure that requirements combine can handle this and continue safely
// secondly, make sure that requirements combine throws the correct warning, and only that warning
@unlink($combinedTestFilePath);
try{
/*
* Use @ (ignore warning) because a warning would cause php unit to throw an exception and therefore change the execution
* process and mess up the next test.
*/
@Requirements::process_combined_files();
Requirements::process_combined_files();
}catch(PHPUnit_Framework_Error_Warning $e){
if(strstr($e->getMessage(), 'Failed to minify') === false){
$this->fail('Requirements::process_combined_files raised a warning, which is good, but this is not the expected warning ("Failed to minify..."): '.$e);
Requirements::set_backend($oldBackend);
return;
}
}catch(Exception $e){
$this->fail('Requirements::process_combined_files did not catch exception caused by minifying bad js file: '.$e);
Requirements::set_backend($oldBackend);
return;
}
// and make sure the combined content matches the input content, i.e. no loss of functionality
if(!file_exists($combinedTestFilePath)){
$this->fail('No combined file was created at expected path: '.$combinedTestFilePath);
@ -147,7 +149,6 @@ class SSViewerTest extends SapphireTest {
}
$combinedTestFileContents = file_get_contents($combinedTestFilePath);
$this->assertContains($jsFileContents, $combinedTestFileContents);
// reset
Requirements::set_backend($oldBackend);

View File

@ -1129,6 +1129,7 @@ class Requirements_Backend {
if(!$refresh) continue;
$failedToMinify = false;
$combinedData = "";
foreach(array_diff($fileList, $this->blocked) as $file) {
$fileContent = file_get_contents($base . $file);
@ -1136,8 +1137,7 @@ class Requirements_Backend {
try{
$fileContent = $this->minifyFile($file, $fileContent);
}catch(Exception $e){
// failed to minify, use unminified
user_error('Failed to minify '.$file.', exception: '.$e->getMessage(), E_USER_WARNING);
$failedToMinify = true;
}
if ($this->write_header_comment) {
@ -1156,6 +1156,12 @@ class Requirements_Backend {
fclose($fh);
unset($fh);
}
if($failedToMinify){
// Failed to minify, use unminified. This warning is raised at the end to allow code execution
// to complete in case this warning is caught inside a try-catch block.
user_error('Failed to minify '.$file.', exception: '.$e->getMessage(), E_USER_WARNING);
}
// Unsuccessful write - just include the regular JS files, rather than the combined one
if(!$successfulWrite) {