Last active
December 25, 2015 19:09
-
-
Save ackintosh/7026140 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
class Something | |
{ | |
public function receiveData($data) | |
{ | |
// do something. | |
mail($to, $subject, $message); | |
// do something. | |
} | |
} |
This file contains hidden or 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 | |
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 | |
} | |
} | |
This file contains hidden or 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 | |
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 | |
} | |
} |
This file contains hidden or 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 | |
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