Jump to content

[SOLVED] Testing for newly created MS Word file


jhartog

Recommended Posts

I have written a script that converts Word documents to text using a COM object.  The script works fine.  However, I have tried to write a unit test for the script (using SimpleTest, though I don't think that's important).  The unit test checks for the existence of the newly created text file.  However it always fails.  The unit test is listed below:

 

<?php
function test_msword_files_to_text() {
	chdir(APP_PATH . 'testing');
	if(file_exists(substr(self::TEST_DOC_FILE, 0, -3) . 'txt'))
		throw new Exception('Destination conversion file exists!');
	$test_dir = dirname(realpath(self::TEST_DOC_FILE));
	costdata::msword_files_to_text($test_dir);

	# force the text file to appear
	clearstatcache();

	$this->assertTrue(file_exists(substr(self::TEST_DOC_FILE, 0, -3) . 'txt'));
	if(file_exists(substr(self::TEST_DOC_FILE, 0, -3) . 'txt')){
		if(!unlink(substr(self::TEST_DOC_FILE, 0, -3) . 'txt'))
			echo "Warning: Unable to delete converted Word file!";
	}
}
?>

 

I have tried adding ob_flush() and flush() before clearstatcache(), and I have added sleep() to see if it's a delay thing, all to no avail.  I am also making sure to release the COM object by using $word->Quit(), $word = null; and unset($word);

 

The odd thing is that the test will fail (the $this->assertTrue() line), and then when I run the test a second time without deleting the previously converted file, the first file_exists() will trigger the exception, proving the file is now there.

 

 

have you tried

$this->assertTrue(file_exists(substr($test_dir, 0, -3) . 'txt'));

 

This test would test whether a directory (note that $test_dir contains the directory where the new file should be) with the last part of the directory name changed to '.txt'.  I don't see how that will help.

 

However, it somehow made me think of what the issue was  ::).  In my conversion function costdata::msword_files_to_text(), I was doing a chdir().  Since TEST_DOC_FILE is a relative path, the second file_exists test would fail, since the cwd is different after the Word conversion function is run. Now I feel silly.  :P  I've been working on this for over 2 hours.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.