Now, let's proceed to the next test:
function testCreate() { $this->tearDown(); $this->assertFalse(file_exists($this->file->filename)); clearstatcache();
$this->assertTrue($this->file->create()); $this->assertTrue(file_exists($this->file->filename)); } |
This test will test the create() method and makes sure it's creating the file!
We first delete the file created (automatically) by setUp() by using tearDown() and then check and insure that the file no longer exists by using assertFalse() which will pass only if file_exists() returned false (file does not exists).
We then check to see if the method create() successfully created the file and returned true in the last two lines of the test.
Needless to say that it's more reading friendly to make the test name similar or related to the method/part you are testing.
We'll now see a new assertion type in the next test:
function testReadWrite() { $contents = 'contents'; $this->assertTrue($this->file->putContents($contents)); $this->assertEqual($this->file->getContents(), $contents); } |
This test will check the putContents() and getContents() methods.
We will write some contents to the file using putContents() and make sure everything went ok and then use assertEqual() to make sure that what we'd written in the file is equal to what we get from getContents().
So you think I am paranoid when I check one part twice? It will be useless to just check for putContents() since it might pass the test but without actually doing what expected by just returning true or maybe because we had defined the method incorrectly! So we have to check the contents of this file and insure they are equal.
This is why we wrote two tests to check file existence and creation as well.