Tornado Tornado - 1 month ago 7
PHP Question

phpunit: pass when single test is run, fails when all tests are run

I have the problem that a specific test runs when I execute only this test. When I run all the tests together this test fails. Running from within NetBeans or from the command line has the same result.

The thing which is unique about this test compared to other tests in my suite is that it takes a file from a resources folder and then copies it to a test folder (which is created in the the beginning of the test and destroyed at the end).

I thought it was a problem with relative/absolute paths, but when I dump the source and target folders they are the same.

This is the problematic code (fails on the copy-statement):

public function testRemoveRequirementFileValid()
{
$dbName = 'TRHUtilTest';
$reqID = 1;
$userID = 14;
$trhVersionID = 3;

//create the database
DBCreator::CreateProjectDatabaseAndTables($dbName);
DB::setConnectionInfo($dbName);

$fileName = 'example.png';
$sourceFolder = dirname(__FILE__).'\TRHUtilResources\\';
$targetLink = '../'.FolderDefine::GetFolder('TRH_RequirementLink', $dbName).$trhVersionID.'/'.$reqID.'/';
FileUtil::MKDirs($targetLink);
$targetFolder = FolderDefine::GetFolder('TRH_RequirementFile', $dbName).$trhVersionID.'\\'.$reqID.'\\';

//2 debugging statements
print PHP_EOL.'Source Folder: '.$sourceFolder.PHP_EOL;
print PHP_EOL.'Target Folder: '.$targetFolder.PHP_EOL;
copy($sourceFolder.$fileName, $targetFolder.$fileName);

$file = new File();
$file->setName($fileName);
$file->setLocation($targetLink);
$file->save();
$fileID = $file->getID();

//create the link between a requirement and a file
TRHUtil::RequirementFileLink($reqID, $fileID, $userID);

//assert that there is one record, then check the structure and then the contents
$this->assertEquals(1, DB::getRecordCount($dbName.'.files_reqs'));
$this->assertEquals(1, TRHUtil::RequirementFileLinkPresent($reqID, $dbName));
$this->assertEquals(1, TRHUtil::RequirementFileLinkCheck($reqID, $fileID, $dbName));
$this->assertEquals(1, count(TRHUtil::GetRequirementFileLinkID($reqID, $dbName)));

TRHUtil::RemoveRequirementFile($reqID, $dbName, $trhVersionID);

//assert that the records about this file are removed and that the folder where the file was staying is also removed
$this->assertEquals(0, DB::getRecordCount($dbName.'.files_reqs'));
$this->assertEquals(0, TRHUtil::RequirementFileLinkPresent($reqID, $dbName));
$this->assertEquals(0, TRHUtil::RequirementFileLinkCheck($reqID, $fileID, $dbName));
$this->assertEquals(0, TRHUtil::GetRequirementFileLinkID($reqID, $dbName));
$this->assertEquals(0, is_dir($targetFolder));

//remove the project folder
$projectFolder = FolderDefine::GetFolder('ProjectRoot', $dbName);
FileUtil::RRMDir($projectFolder);
}


The debugging statements then give:

Source Folder: D:\Inetpub\wwwroot\DTS\tests\util\TRHUtilResources\
Target Folder: D:\Inetpub\wwwroot\DTSfiles\TRHUtilTest\trh\reqs\3\1\


In the output window of NetBeans I also see the following:
copy(D:\Inetpub\wwwroot\DTSfiles\TRHUtilTest\trh\reqs\3\1\example.png): failed to open stream: No such file or directory


However this folder is created before. Worth noting here is that the error about the copy-statement only shows the second parameter and not there are no quotes around this parameter.

Answer

I found the solution, thanks to some additional debugging statements I added after the latest comment of users @AD7six and @Lilshieste.

To create the target folder I used the commands:

$targetLink = '../'.FolderDefine::GetFolder('TRH_RequirementLink', $dbName).$trhVersionID.'/'.$reqID.'/';
FileUtil::MKDirs($targetLink);

This means the following folder was created: ../../../DTSfiles/TRHUtilTest/trh/reqs/3/1/

However, it should be:

$targetFolder = FolderDefine::GetFolder('TRH_RequirementFile', $dbName).$trhVersionID.'\\'.$reqID.'\\';
FileUtil::MKDirs($targetFolder);

Which corresponds to the folder: D:\Inetpub\wwwroot\DTSfiles\TRHUtilTest\trh\reqs\3\1\

Since the start location of running this one unit test is different as the root folder where all the tests are residing is different (the root-test-folder is two directory levels deeper) the target folder was indeed created, but at a wrong level.

Thanks to all for the support.