Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created April 18, 2018 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/ca4f83191255e661a84e3563b27263de to your computer and use it in GitHub Desktop.
Save tommcfarlin/ca4f83191255e661a84e3563b27263de to your computer and use it in GitHub Desktop.
[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