Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.