by Joeri Timmermans
Wikipedia:
"Unit testing is a software testing method by which individual units of source code are tested to determine if they are fit for use"
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
backupGlobals = "false" backupStaticAttributes = "false"
colors = "true" convertErrorsToExceptions = "true"
convertNoticesToExceptions = "true" convertWarningsToExceptions = "true"
processIsolation = "false" stopOnFailure = "false"
syntaxCheck = "false" bootstrap = "bootstrap.php.cache" >
<testsuites>
<testsuite name="Project Test Suite">
<directory>../src/*/*Bundle/Tests</directory>
<directory>../src/*/Bundle/*Bundle/Tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>../src</directory>
<exclude>
<directory>../src/*/*Bundle/Resources</directory>
<directory>../src/*/*Bundle/Tests</directory>
<directory>../src/*/Bundle/*Bundle/Resources</directory>
<directory>../src/*/Bundle/*Bundle/Tests</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
AssertTrue/AssertFalse - Check the input to verify it equals true/false
AssertEquals - Check the result against another input for a match
AssertGreaterThan - Check the result to see if it’s larger than a value
(there’s also LessThan, GreaterThanOrEqual, and LessThanOrEqual)
AssertContains - Check that the input contains a certain value
AssertType - Check that a variable is of a certain type
AssertNull - Check that a variable is null
AssertFileExists - Verify that a file exists
AssertRegExp - Check the input against a regular expression
...
<?php
namespace Example;
class HelloWorld
{
public $helloWorld;
public function __construct($string = 'Hello World!')
{
$this->helloWorld = $string;
}
public function sayHello()
{
return $this->helloWorld;
}
}
<?php
namespace Example;
class HelloWorldTest extends \PHPUnit_Framework_TestCase
{
public function testSayHello()
{
$hw = new HelloWorld();
$string = $hw->sayHello();
$this->assertEquals('Hello World!', $string);
}
}
PHPUnit 4.2.5 by Sebastian Bergmann.
Configuration read from /var/www/example/build/phpunit.xml
.
Time: 70 ms, Memory: 3.50Mb
OK (1 test, 1 assertion)
<?php
namespace Example;
class MyListTest extends \PHPUnit_Framework_TestCase
{
protected $myList;
public function setUp()
{
$this->myList = array();
}
public function testMyListEmpty()
{
$this->assertEquals(0, sizeof($this->myList));
}
public function testMyListHasOne()
{
array_push($this->myList, 'myItem');
$this->assertEquals(1, sizeof($this->myList));
}
public function tearDown()
{
unset($this->myList);
}
}
PHPUnit 4.2.5 by Sebastian Bergmann.
Configuration read from /var/www/example/build/phpunit.xml
..
Time: 67 ms, Memory: 3.50Mb
OK (2 tests, 2 assertions)
<?php
namespace Example;
class HelloMailer
{
public $mailer;
public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}
public function sendMessage($message)
{
return $this->mailer->send($message);
}
}
<?php
namespace Example;
class HelloMailerTest extends \PHPUnit_Framework_TestCase
{
protected $mailer;
public function setUp()
{
$mailer = $this->getMockBuilder('Example\Mailer')
->setMockClassName('Mailer')
->disableOriginalConstructor()
->setMethods(array('send'))
->getMock();
$mailer->expects($this->any())
->method('send')
->with($this->equalTo('Hello mailer!'))
->will($this->returnValue(true));
$this->mailer = $mailer;
}
public function testSend()
{
$hm = new HelloMailer($this->mailer);
$response = $hm->sendMessage('Hello mailer!');
$this->assertTrue($response);
}
}
PHPUnit 4.2.5 by Sebastian Bergmann.
Configuration read from /var/www/example/build/phpunit.xml
.
Time: 80 ms, Memory: 4.00Mb
OK (1 test, 1 assertion)