From a1e0e27b4ef277856579e5bf5b46b9bf11794b84 Mon Sep 17 00:00:00 2001 From: Synchro Date: Tue, 8 Sep 2015 18:27:09 +0200 Subject: [PATCH] Initial import --- .gitignore | 3 + README.md | 33 ++++++++ composer.json | 24 ++++++ composer.lock | 170 ++++++++++++++++++++++++++++++++++++++++ phpunit.xml.dist | 23 ++++++ src/PhpMailer.php | 60 ++++++++++++++ tests/PhpMailerTest.php | 97 +++++++++++++++++++++++ tests/bootstrap.php | 21 +++++ 8 files changed, 431 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 composer.json create mode 100644 composer.lock create mode 100644 phpunit.xml.dist create mode 100644 src/PhpMailer.php create mode 100644 tests/PhpMailerTest.php create mode 100644 tests/bootstrap.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..549fa8b --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +composer.phar +vendor/ + diff --git a/README.md b/README.md new file mode 100644 index 0000000..083badd --- /dev/null +++ b/README.md @@ -0,0 +1,33 @@ +#PHPMailer logger for Apix Log + +An extension for the [Apix/Log](https://github.com/frqnck/apix-log) PSR-3 logger that sends log messages via email using [PHPMailer](https://github.com/PHPMailer/PHPMailer). + +##Usage + +Create an Apix PhpMailer Log instance, providing a pre-configured PHPMailer instance to the constructor. +This instance will be used for all subsequent messages. + +By default the logger sends an email for each individual log message received, which can be quite inefficient, so call `$logger->setDeferred(true)` to save up the log messages and send them all in one message on `__destruct`. + +We suggest you enable exceptions in your PHPMailer instance otherwise you may not be told about problems sending your log messages. + +##Example + +```php +// Create a PHPMailer instance with exceptions enabled +$mailer = new \PHPMailer(true); +$mailer->addAddress('logs@example.com', 'Log Mailbox'); +$mailer->setFrom('myapp@example.com', 'My App'); +$mailer->isSMTP(); +$mailer->SMTPAuth = true; +$mailer->Host = 'tls://mail.example.com:587'; +$mailer->Username = 'user'; +$mailer->Password = 'pass'; +$mailer->isHTML(false); +$mailer->Subject = 'Error log'; + +$logger = new Apix\Logger\PhpMailer($mailer); +$logger->setDeferred(true); +$logger->info('Log me!'); +$logger->error('Log me too!'); +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..665d67e --- /dev/null +++ b/composer.json @@ -0,0 +1,24 @@ +{ + "name": "phpmailer/apix-log-phpmailer", + "description": "A PHPMailer logger for Apix log", + "type": "library", + "minimum-stability": "stable", + "license": "BSD-3-Clause", + "authors": [ + { + "name": "Marcus Bointon", + "email": "phpmailer@synchromedia.co.uk" + } + ], + "require": { + "php": ">=5.3", + "apix/log": "^1.1", + "phpmailer/phpmailer": "^5.2" + }, + "autoload": { + "psr-4": { + "Apix\\Log\\Logger\\": "src/", + "Apix\\Log\\Logger\\tests\\": "tests/" + } + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..a5ad204 --- /dev/null +++ b/composer.lock @@ -0,0 +1,170 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "hash": "53ae92b07afae09d59db9baa02ac77d4", + "packages": [ + { + "name": "apix/log", + "version": "1.1.4", + "source": { + "type": "git", + "url": "https://github.com/frqnck/apix-log.git", + "reference": "b92e3be6243fc34157209ceb5043010ebf7c8f24" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/frqnck/apix-log/zipball/b92e3be6243fc34157209ceb5043010ebf7c8f24", + "reference": "b92e3be6243fc34157209ceb5043010ebf7c8f24", + "shasum": "" + }, + "require": { + "php": ">=5.3", + "psr/log": "~1.0" + }, + "provide": { + "psr/log-implementation": "1.0.0" + }, + "require-dev": { + "phpunit/phpunit": "4.8.*" + }, + "type": "library", + "autoload": { + "psr-4": { + "Apix\\Log\\": "src/", + "Apix\\Log\\tests\\": "tests/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Franck Cassedanne", + "email": "franck@ouarz.net" + } + ], + "description": "Minimalist, thin and fast PSR-3 compliant (multi-bucket) logger.", + "homepage": "https://github.com/frqnck/apix-log", + "keywords": [ + "apix", + "log", + "logger", + "psr", + "psr-3" + ], + "time": "2015-09-11 10:38:39" + }, + { + "name": "phpmailer/phpmailer", + "version": "v5.2.12", + "source": { + "type": "git", + "url": "https://github.com/PHPMailer/PHPMailer.git", + "reference": "5dd754112d07ab7480824c2916689ce06f83a842" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/PHPMailer/PHPMailer/zipball/5dd754112d07ab7480824c2916689ce06f83a842", + "reference": "5dd754112d07ab7480824c2916689ce06f83a842", + "shasum": "" + }, + "require": { + "php": ">=5.0.0" + }, + "require-dev": { + "phpdocumentor/phpdocumentor": "*", + "phpunit/phpunit": "4.3.*" + }, + "suggest": { + "league/oauth2-client": "Needed for Gmail's XOAUTH2 authentication system" + }, + "type": "library", + "autoload": { + "classmap": [ + "class.phpmailer.php", + "class.phpmaileroauth.php", + "class.oauth.php", + "class.smtp.php", + "class.pop3.php", + "extras/EasyPeasyICS.php", + "extras/ntlm_sasl_client.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "LGPL-2.1" + ], + "authors": [ + { + "name": "Jim Jagielski", + "email": "jimjag@gmail.com" + }, + { + "name": "Marcus Bointon", + "email": "phpmailer@synchromedia.co.uk" + }, + { + "name": "Andy Prevost", + "email": "codeworxtech@users.sourceforge.net" + }, + { + "name": "Brent R. Matzelle" + } + ], + "description": "PHPMailer is a full-featured email creation and transfer class for PHP", + "time": "2015-09-02 10:40:37" + }, + { + "name": "psr/log", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/log.git", + "reference": "fe0936ee26643249e916849d48e3a51d5f5e278b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/log/zipball/fe0936ee26643249e916849d48e3a51d5f5e278b", + "reference": "fe0936ee26643249e916849d48e3a51d5f5e278b", + "shasum": "" + }, + "type": "library", + "autoload": { + "psr-0": { + "Psr\\Log\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for logging libraries", + "keywords": [ + "log", + "psr", + "psr-3" + ], + "time": "2012-12-21 11:40:51" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": { + "php": ">=5.3" + }, + "platform-dev": [] +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..4c12068 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,23 @@ + + + + + + tests + + + + + + \ No newline at end of file diff --git a/src/PhpMailer.php b/src/PhpMailer.php new file mode 100644 index 0000000..ce0bbbf --- /dev/null +++ b/src/PhpMailer.php @@ -0,0 +1,60 @@ + + */ + +namespace Apix\Log\Logger; + +use Apix\Log\Exception; +use Psr\Log\InvalidArgumentException; + +/** + * Apix logger for sending logs via PHPMailer. + * + * @author Marcus Bointon + */ +class PhpMailer extends AbstractLogger implements LoggerInterface +{ + /** + * A PHPMailer instance to use for sending. + * @type \PHPMailer + */ + protected $phpmailer; + + /** + * Constructor. + * Note PHPMailer is *NOT* namespaced. + * @param \PHPMailer $phpmailer A preconfigured PHPMailer instance to use for sending. + */ + public function __construct($phpmailer) + { + if (!$phpmailer instanceof \PHPMailer) { + throw new InvalidArgumentException( + 'Must be an instance of \PHPMailer', 1 + ); + } + + if (count($phpmailer->getToAddresses()) < 1) { + throw new InvalidArgumentException( + 'No valid email address set in \PHPMailer' + ); + } + + $this->phpmailer = $phpmailer; + } + + /** + * {@inheritDoc} + */ + public function write(array $log) + { + $this->phpmailer->Body = $log['msg']; + try { + return $this->phpmailer->send(); + } catch (\phpmailerException $e) { + throw new Exception($e->getMessage(), $e->getCode(), $e); + } + } +} diff --git a/tests/PhpMailerTest.php b/tests/PhpMailerTest.php new file mode 100644 index 0000000..5c16fd2 --- /dev/null +++ b/tests/PhpMailerTest.php @@ -0,0 +1,97 @@ + + * + * @license http://opensource.org/licenses/BSD-3-Clause New BSD License + * + */ + +namespace Apix\Log\Logger; + +class PhpMailerTest extends \PHPUnit_Framework_TestCase +{ + + protected $mailer; + + protected function setUp() + { + $mail = new \PHPMailer(true); + $mail->addAddress('logs@example.com', 'Log Mailbox'); + $mail->setFrom('myapp@example.com', 'My App'); + $mail->isSMTP(); + $mail->SMTPAuth = true; + $mail->Host = 'tls://mail.example.com:587'; + $mail->Username = 'user'; + $mail->Password = 'pass'; + $mail->isHTML(false); + $mail->Subject = 'Error log'; + + $this->mailer = $mail; + } + + protected function tearDown() + { + unset($this->mailer); + } + + /** + * @expectedException Psr\Log\InvalidArgumentException + * @expectedExceptionMessage Must be an instance of \PHPMailer + */ + public function testThrowsInvalidArgumentExceptionWhenNull() + { + new PhpMailer(null); + } + + /** + * @expectedException Psr\Log\InvalidArgumentException + * @expectedExceptionMessage Must be an instance of \PHPMailer + */ + public function testThrowsInvalidArgumentExceptionWhenWrongInstance() + { + new PhpMailer(new \stdClass()); + } + + /** + * @expectedException Psr\Log\InvalidArgumentException + * @expectedExceptionMessage No valid email address set in \PHPMailer + */ + public function testThrowsInvalidArgumentExceptionWhenNoValidEmail() + { + new PhpMailer(new \PHPMailer); + } + + public function testConstructor() + { + new PhpMailer($this->mailer); + } + + /** + * @expectedException Apix\Log\Exception + * This test relies on the mailer not being usable - for example that it uses an unreachable host + */ + public function testThrowsExceptionOnPhpMailerError() + { + $logger = new PhpMailer($this->mailer); + $logger->info("Log me!"); + } + + + public function testWriteIsCalled() + { + $mock = $this->getMockBuilder('Apix\Log\Logger\PHPMailer') + ->disableOriginalConstructor() + ->setMethods(array('write')) + ->getMock(); + + $mock->expects($this->exactly(2))->method('write'); + + $mock->info('Log me!'); + $mock->error('Log me too!'); + } + +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 0000000..6bbc1c5 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,21 @@ + + * + * @license http://opensource.org/licenses/BSD-3-Clause New BSD License + * + */ + +namespace Apix; + +// Set the default timezone +date_default_timezone_set('UTC'); + +define('APP_VENDOR', realpath(__DIR__ . '/../vendor')); + +// Composer +$loader = require APP_VENDOR . '/autoload.php';