Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[PHP] Writing Unit Tests with PHPUnit, Part 2: The Tear Down
<?php
namespace Pressware\Acme\Tests\API;
use PHPUnit\Framework\TestCase;
class AcmeFileTest extends TestCase
{
private $filename;
private $content;
public function setUp()
{
$this->filename = 'testFile.txt';
$this->content = 'This is a string of data that is meant to be written to the file.';
}
// More to come...
}
<?php
namespace Pressware\Acme\Tests\API;
use PHPUnit\Framework\TestCase;
class AcmeFileTest extends TestCase
{
private $filename;
private $content;
public function setUp()
{
$this->filename = 'testFile.txt';
$this->content = 'This is a string of data that is meant to be written to the file.';
}
public function testWriteReadData()
{
// Writes the content to the file with the given filename.
$fileHandle = fopen($this->filename, 'w');
fwrite($fileHandle, $this->content);
fclose($fileHandle);
// Reads the contents of the file that was just written
$fileHandle = fopen($this->filename, 'r');
$contents = fread($fileHandle, filesize($this->filename));
fclose($fileHandle);
$this->assertSame($this->content, $contents);
}
// More to come..
}
<?php
namespace Pressware\Acme\Tests\API;
use PHPUnit\Framework\TestCase;
class AcmeFileTest extends TestCase
{
private $filename;
private $content;
public function setUp()
{
$this->filename = 'testFile.txt';
$this->content = 'This is a string of data that is meant to be written to the file.';
}
public function testWriteReadData()
{
// Writes the content to the file with the given filename.
$fileHandle = fopen($this->filename, 'w');
fwrite($fileHandle, $this->content);
fclose($fileHandle);
// Reads the contents of the file that was just written
$fileHandle = fopen($this->filename, 'r');
$contents = fread($fileHandle, filesize($this->filename));
fclose($fileHandle);
$this->assertSame($this->content, $contents);
}
public function tearDown()
{
if (file_exists($this->filename)) {
unlink($this->filename);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.