[PHP] Writing Unit Tests with PHPUnit, Part 3: XML Configuration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<!-- http://phpunit.de/manual/4.1/en/appendixes.configuration.html --> | |
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd" | |
bootstrap="./tests/bootstrap.php" | |
backupGlobals="false" | |
colors="true" | |
convertErrorsToExceptions="true" | |
convertNoticesToExceptions="true" | |
convertWarningsToExceptions="true" | |
> | |
<testsuites> | |
<testsuite name="AcmeTests"> | |
<directory>./tests</directory> | |
</testsuite> | |
</testsuites> | |
<logging> | |
<log type="coverage-text" target="php://stdout" showUncoveredFiles="true"/> | |
</logging> | |
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This array has a single file but could whole the contents of an entire directory. | |
$files = [ | |
dirname(__DIR__).'/AcmeCache.php', | |
]; | |
foreach ($files as $file) { | |
if (file_exists($file)) { | |
require_once $file; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AcmeTests; | |
use PHPUnit\Framework\TestCase; | |
use Acme\AcmeCache; | |
class AcmeCacheTest extends TestCase | |
{ | |
private $cache; | |
public function setUp() | |
{ | |
$this->cache = new AcmeCache(); | |
} | |
public function testCacheExists() | |
{ | |
$this->assertNotNull($this->cache); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme; | |
class AcmeCache | |
{ | |
private $duration; | |
public function __construct() | |
{ | |
$this->duration = 43200; | |
} | |
public function setDuration(int $duration) | |
{ | |
$this->duration = $duration; | |
} | |
public function getDuration(): int | |
{ | |
return $this->duration; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AcmeTests; | |
use PHPUnit\Framework\TestCase; | |
use Acme\AcmeCache; | |
class AcmeCacheTest extends TestCase | |
{ | |
private $cache; | |
public function setUp() | |
{ | |
$this->cache = new AcmeCache(); | |
} | |
public function testCacheExists() | |
{ | |
$this->assertNotNull($this->cache); | |
} | |
public function testDefaultCacheValue() | |
{ | |
$this->assertSame(43200, $this->cache->getDuration()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AcmeTests; | |
use PHPUnit\Framework\TestCase; | |
use Acme\AcmeCache; | |
class AcmeCacheTest extends TestCase | |
{ | |
private $cache; | |
public function setUp() | |
{ | |
$this->cache = new AcmeCache(); | |
} | |
public function testCacheExists() | |
{ | |
$this->assertNotNull($this->cache); | |
} | |
public function testDefaultCacheValue() | |
{ | |
$this->assertSame(43200, $this->cache->getDuration()); | |
} | |
public function testSetCustomDuration() | |
{ | |
$duration = 4200; | |
$this->cache->setDuration($duration); | |
$this->assertSame($duration, $this->cache->getDuration()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment