views:

137

answers:

1

I there, I am currently writing a unit test which asserts that a file did not get modified. The test code execution takes less than one second and therefore I would like to know if it is possible to retrieve the file modification time in milliseconds. filemtime() function returns the UNIX timestamp in seconds.

My current solution is using the sleep(1) function which will assure me that 1 second passed before checking if it was modified or not. I don't like that solution since it slows down the test by a great deal.

I cannot assert the content equality via get_file_contents() since the data that can be rewritten would be the same.

I am guessing it is impossible, is it?

+1  A: 

AFAIK UNIX timestamp's precision is seconds, so this may not be a possibility.

BTW, note that PHP caches the return value of filemtime() internally, thus clearstatcache() should be called before.

An alternative method could be to modify (or delete) the contents of the file first so that you can easily identify changes. Since the state of the system should remain the same after each test is executed, it would make sense anyways to restore the original file contents after the unit test has run.

nuqqsa
I like the idea, I prefer manipulating content than sleeping for 1 second, which is way faster. Thanks for the tip.
Steven Rosato