Skip to content

Instantly share code, notes, and snippets.

@tzmfreedom
Last active June 9, 2019 14:30
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 tzmfreedom/18b784df85559b2ba90ce99a27185f95 to your computer and use it in GitHub Desktop.
Save tzmfreedom/18b784df85559b2ba90ce99a27185f95 to your computer and use it in GitHub Desktop.
<?php
class MailMockStreamWrapper
{
private $content = '';
private $position = 0;
public function stream_open($path)
{
stream_wrapper_restore("file");
$content = file_get_contents($path);
$this->content = str_replace("mail", "Mock::mail", $content);
return true;
}
public function stream_read($count)
{
$ret = substr($this->content, $this->position, $count);
$this->position += strlen($ret);
return $ret;
}
public function stream_stat()
{
return [];
}
public function stream_eof()
{
return $this->position >= strlen($this->content);
}
}
class Mock
{
public static function mail($to, $subject)
{
echo "to => $to, subject => $subject";
}
}
stream_wrapper_unregister("file");
stream_wrapper_register("file", "MailMockStreamWrapper");
require_once 'mail.php'; // => to => hoge, subject => fuga
<?php
mail("hoge", "fuga");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment