Skip to content

Instantly share code, notes, and snippets.

@ackintosh
Last active December 25, 2015 19:09
Show Gist options
  • Save ackintosh/7026140 to your computer and use it in GitHub Desktop.
Save ackintosh/7026140 to your computer and use it in GitHub Desktop.
<?php
class Something
{
public function receiveData($data)
{
// do something.
mail($to, $subject, $message);
// do something.
}
}
<?php
class Something
{
public function receiveData($data)
{
// do something.
$this->mail($to, $subject, $message);
// do something.
}
protected function mail($to, $subject, $message)
{
mail($to, $subject, $message);
}
}
class FakeSomething extends Something
{
protected function mail($to, $subject, $message)
{
}
}
class SomethingTest extends PHPUnit_Framework_TestCase
{
public function testReceiveData()
{
$something = new FakeSomething();
// write tests
}
}
<?php
class Something
{
private $mailFunction;
public function __construct()
{
$this->mailFunction = function () {
call_user_func_array('mail', func_get_args());
};
}
public function receiveData($data)
{
// do something.
call_user_func_array($this->mailFunction, array($to, $subject, $message));
// do something.
}
public function setMailFunction(callable $func)
{
$this->mailFunction = $func;
}
}
class SomethingTest extends PHPUnit_Framework_TestCase
{
public function testReceiveData()
{
$something = new Something();
$something->setMailFunction(function () {
// dummy
return;
});
// write tests
}
}
<?php
class SomethingTest extends PHPUnit_Framework_TestCase
{
public function testReceiveData()
{
$something = new Something();
$something->setMailFunction(function ($to, $subject, $message) {
$this->assertSame('[email protected]', $to);
$this->assertSame('sample subject', $subject);
$this->assertSame('hello', $message);
return;
});
$something->receiveData('data');
// write tests
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment