jhartog Posted December 24, 2008 Share Posted December 24, 2008 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. Link to comment https://forums.phpfreaks.com/topic/138334-solved-testing-for-newly-created-ms-word-file/ Share on other sites More sharing options...
MadTechie Posted December 24, 2008 Share Posted December 24, 2008 have you tried $this->assertTrue(file_exists(substr($test_dir, 0, -3) . 'txt')); also break it down a little and test each part.. Link to comment https://forums.phpfreaks.com/topic/138334-solved-testing-for-newly-created-ms-word-file/#findComment-723325 Share on other sites More sharing options...
jhartog Posted December 24, 2008 Author Share Posted December 24, 2008 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. I've been working on this for over 2 hours. Link to comment https://forums.phpfreaks.com/topic/138334-solved-testing-for-newly-created-ms-word-file/#findComment-723332 Share on other sites More sharing options...
MadTechie Posted December 24, 2008 Share Posted December 24, 2008 LOL, Yeah well don't feel too bad i spend ages trying to work out why flush; wasn't working.. opps i mean flush(); it happens to all of us.. Link to comment https://forums.phpfreaks.com/topic/138334-solved-testing-for-newly-created-ms-word-file/#findComment-723337 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.