# Testing file writings fast and furious in PHPUnit

I came across the need the test for a file writing in a PHPUnit test and spent at least 5 minutes trying to figure out a safe place for the writing to happen.
Running tests on Mac and Windows machine something as trivial as the directory separator char might break havoc in tests and handling absolute paths makes it worse.
If the need is for a quick writing test that could happen anywhere then something like this will work solving any path related issue

protected $file; protected function setUp() { // in the same folder as the test file$this->file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'someFile.txt';
}

public function testWriting()
{
$this->subjectUnderTest->write($this->file);

$this->assertFileExists($this->file);
}

and then make care to remove each written file after the test in the tearDown method

protected tearDown()
{
if(file_exists($this->file)) { unlink($this->file);
}
}

I know it’s an anti-pattern but it solves the problem.