Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Created March 29, 2018 14:20
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/248c7741273e6be414c1f98c11085fe7 to your computer and use it in GitHub Desktop.
Save tommcfarlin/248c7741273e6be414c1f98c11085fe7 to your computer and use it in GitHub Desktop.
[PHP] Writing Unit Tests with PHPUnit, Part 1: The Set Up
<?php
class AcmeCache
{
private $duration;
public function __construct()
{
$this->duration = 43200;
}
public function setDuration($duration)
{
$this->duration = $duration;
}
public function getDuration()
{
return $this->duration;
}
// More cache code omitted...
}
<?php
use PHPUnit\Framework\TestCase;
class AcmeCacheTest extends TestCase
{
protected $cache;
protected function setUp()
{
$this->cache = new AcmeCache();
}
public function testDefaultDuration()
{
$this->assertTrue($this->cache->getDuration() === 43200);
}
public function testNewDuration()
{
$this->cache->setDuration(1000);
$this->assertFalse($this->cache->getDuration() === 43200);
$this->assertTrue($this->cache->getDuration() === 1000);
}
// More to come...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment