Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactoring and formatting tweaks for great justice.

  • Loading branch information...
commit 4f785a0ed76fdff93a7bebd12e840d8aacb910c5 1 parent ef5e63d
@nateabele nateabele authored
View
55 action/Mailer.php
@@ -15,6 +15,7 @@
* @see li3_mailer\action\Mailer::deliver()
*/
class Mailer extends \lithium\core\StaticObject {
+
/**
* Holds extra configurations per message (and a default
* for every message at key `0` if set). See `Mailer::_options()`.
@@ -73,7 +74,7 @@ public static function message(array $options = array()) {
* @see li3_mailer\net\mail\Message
* @see li3_mailer\net\mail\Media::render()
* @see li3_mailer\net\mail\Transport::deliver()
- * @param string $message_name Name of the message to send.
+ * @param string $messageName Name of the message to send.
* @param array $options Options may be:
* - options for creating the message (see `message()`),
* - options for rendering the message (see `Media::render()`),
@@ -86,33 +87,36 @@ public static function message(array $options = array()) {
* the message).
* @return mixed Return value of the transport adapter's deliver method.
*/
- public static function deliver($message_name, array $options = array()) {
- $options = static::_options($message_name, $options);
+ public static function deliver($messageName, array $options = array()) {
+ $options = static::_options($messageName, $options);
$delivery = static::$_classes['delivery'];
- $delivery_name = isset($options['delivery']) ? $options['delivery'] : 'default';
+ $deliveryName = isset($options['delivery']) ? $options['delivery'] : 'default';
unset($options['delivery']);
- $message_options = $options + $delivery::config($delivery_name);
- $message = static::message($message_options);
- $transport = $delivery::adapter($delivery_name);
- $transport_options = isset($options['transport']) ? (array) $options['transport'] : array();
+
+ $messageOptions = $options + $delivery::config($deliveryName);
+ $message = static::message($messageOptions);
+ $transport = $delivery::adapter($deliveryName);
+
+ $transportOptions = isset($options['transport']) ? (array) $options['transport'] : array();
unset($options['transport']);
$data = isset($options['data']) ? $options['data'] : array();
unset($options['data']);
$class = get_called_class();
$name = preg_replace('/Mailer$/', '', substr($class, strrpos($class, "\\") + 1));
+
$options += array(
'mailer' => ($name == '' ? null : Inflector::underscore($name)),
- 'template' => $message_name
+ 'template' => $messageName
);
-
$media = static::$_classes['media'];
- $params = compact('options', 'data', 'message', 'transport', 'transport_options');
+ $params = compact('options', 'data', 'message', 'transport', 'transportOptions');
+
return static::_filter(__FUNCTION__, $params, function($self, $params) use ($media) {
extract($params);
$media::render($message, $data, $options);
- return $transport->deliver($message, $transport_options);
+ return $transport->deliver($message, $transportOptions);
});
}
@@ -130,25 +134,25 @@ public static function deliver($message_name, array $options = array()) {
*/
public static function __callStatic($method, $params) {
$found = preg_match('/^deliver(?P<message>\w+)With(?P<delivery>\w+)$/', $method, $args);
+
if (!$found) {
preg_match('/^deliver(?P<message>\w+)$/', $method, $args);
}
- if ($args) {
- $message = Inflector::underscore($args['message']);
- if (isset($params[0]) && is_array($params[0])) {
- $params = $params[0];
- }
- if (isset($args['delivery'])) {
- $params['delivery'] = Inflector::underscore($args['delivery']);
- }
- return static::deliver($message, $params);
- } else {
+ if (!$args) {
$class = get_called_class();
$class = substr($class, strrpos($class, "\\") + 1);
- throw new BadMethodCallException(
- "Method `{$method}` not defined or handled in class `{$class}`."
- );
+ $message = "Method `{$method}` not defined or handled in class `{$class}`.";
+ throw new BadMethodCallException($message);
+ }
+ $message = Inflector::underscore($args['message']);
+
+ if (isset($params[0]) && is_array($params[0])) {
+ $params = $params[0];
+ }
+ if (isset($args['delivery'])) {
+ $params['delivery'] = Inflector::underscore($args['delivery']);
}
+ return static::deliver($message, $params);
}
/**
@@ -173,6 +177,7 @@ protected static function _options($message, array $options = array()) {
if (isset($options[0])) {
$to = $options[0];
unset($options[0]);
+
if (isset($options['data'])) {
$options += compact('to');
} else {
View
1  template/Mail.php
@@ -12,6 +12,7 @@
* @see lithium\template\View
*/
class Mail extends \lithium\template\View {
+
/**
* Holds a reference to the `Message` object that will be delivered. Allows headers
* and other message attributes to be assigned in the templating layer.
View
3  tests/cases/action/MailerTest.php
@@ -16,6 +16,7 @@
TestMailer::applyFilter('deliver', $filter);
class MailerTest extends \lithium\test\Unit {
+
public function testCreateMessage() {
$to = 'foo@bar';
$message = Mailer::message(compact('to'));
@@ -41,7 +42,7 @@ public function testDeliver() {
$this->assertEqual('fake rendered message', $message->body('html'));
$this->assertEqual('fake rendered message', $message->body('text'));
$this->assertFalse(is_null($transport));
- $this->assertEqual(array('extra' => 'data'), $transport_options);
+ $this->assertEqual(array('extra' => 'data'), $transportOptions);
}
public function testSetsMailer() {
View
1  tests/cases/net/mail/DeliveryTest.php
@@ -6,6 +6,7 @@
use li3_mailer\net\mail\Transport;
class DeliveryTest extends \lithium\test\Unit {
+
public function testUsesGoodAdapters() {
$params = array(array('adapter' => 'Simple'), DeliveryWithPath::_adaptersPath());
$class = DeliveryWithPath::invokeMethod('_class', $params);
View
1  tests/cases/net/mail/GrammarTest.php
@@ -5,6 +5,7 @@
use li3_mailer\net\mail\Grammar;
class GrammarTest extends \lithium\test\Unit {
+
public function testToken() {
$grammar = array('foo' => 'bar', 'baz' => 'qux');
$g = new Grammar(compact('grammar'));
View
18 tests/cases/net/mail/MediaTest.php
@@ -8,6 +8,7 @@
use lithium\core\Libraries;
class MediaTest extends \lithium\test\Unit {
+
public function testType() {
$this->assertEqual('text/plain', Media::type('text'));
$this->assertEqual('text/html', Media::type('html'));
@@ -89,10 +90,12 @@ public function testTemplatePaths() {
$this->assertTrue($view instanceof Mail);
$loader = $view->loader();
$template = $loader->template('template', $options);
- $this->assertEqual(array(
+
+ $expected = array(
LITHIUM_APP_PATH . '/mails/bar/foo.text.php',
LITHIUM_APP_PATH . '/mails/foo.text.php'
- ), $template);
+ );
+ $this->assertEqual($expected, $template);
Media::type('foo', false);
}
@@ -128,18 +131,17 @@ public function testRequest() {
'https://foo.local' => array('HTTP_HOST' => 'foo.local', 'HTTPS' => true),
'http://foo.local/base' => array('HTTP_HOST' => 'foo.local', 'base' => '/base')
);
+
foreach ($tests as $base_url => $expect) {
$message = new Message(compact('base_url'));
$request = Media::invokeMethod('_request', array($message));
$expect += array('HTTPS' => false, 'base' => '');
+
foreach ($expect as $key => $expected) {
$result = $request->env($key);
- $this->assertEqual(
- $expected,
- $result,
- "`{$key}` failed for {$base_url} ({$message->base_url}),"
- . " expected: `{$expected}`, result: `{$result}`"
- );
+ $msg = "`{$key}` failed for {$base_url} ({$message->base_url}),";
+ $msg .= " expected: `{$expected}`, result: `{$result}`";
+ $this->assertEqual($expected, $result, $msg);
}
}
}
View
12 tests/cases/net/mail/MessageTest.php
@@ -5,13 +5,15 @@
use li3_mailer\net\mail\Message;
class MessageTest extends \lithium\test\Unit {
+
public function testConstruct() {
- $config = array('subject', 'date', 'return_path', 'sender', 'from', 'reply_to',
- 'to', 'cc', 'bcc', 'types', 'charset', 'headers', 'body');
- $config = array_combine($config, array_map(function($name) {
- return "test {$name}";
- }, $config));
+ $config = array(
+ 'subject', 'date', 'return_path', 'sender', 'from', 'reply_to', 'to',
+ 'cc', 'bcc', 'types', 'charset', 'headers', 'body'
+ );
+ $config = array_combine($config, array_map(function($n) { return "test {$n}"; }, $config));
$message = new Message($config);
+
foreach ($config as $prop => $expected) {
$this->assertEqual($expected, $message->$prop);
}
View
1  tests/mocks/net/mail/Delivery.php
@@ -5,6 +5,7 @@
use li3_mailer\tests\mocks\net\mail\Transport;
class Delivery extends \li3_mailer\net\mail\Delivery {
+
protected static $_configurations = array('test' => array('from' => 'adapter@config'));
public static function adapter($name = null) {
View
1  tests/mocks/net/mail/DeliveryWithPath.php
@@ -3,6 +3,7 @@
namespace li3_mailer\tests\mocks\net\mail;
class DeliveryWithPath extends \li3_mailer\net\mail\Delivery {
+
public static function _adaptersPath() {
return static::$_adapters;
}
View
1  tests/mocks/net/mail/Transport.php
@@ -3,6 +3,7 @@
namespace li3_mailer\tests\mocks\net\mail;
class Transport extends \li3_mailer\net\mail\Transport {
+
public function deliver($message, array $options = array()) {
return true;
}
View
1  tests/mocks/net/mail/transport/adapter/Swift.php
@@ -5,6 +5,7 @@
use li3_mailer\tests\mocks\net\mail\transport\adapter\SwiftTransport;
class Swift extends \li3_mailer\net\mail\transport\adapter\Swift {
+
protected function transport(array $options = array()) {
return new SwiftTransport();
}
View
1  tests/mocks/net/mail/transport/adapter/SwiftTransport.php
@@ -3,6 +3,7 @@
namespace li3_mailer\tests\mocks\net\mail\transport\adapter;
class SwiftTransport {
+
public $delivered = array();
public function send($message) {
View
1  tests/mocks/template/Mail.php
@@ -5,6 +5,7 @@
use li3_mailer\tests\mocks\template\mail\adapter\FileLoader;
class Mail extends \li3_mailer\template\Mail {
+
public function renderer() {
return $this->_renderer;
}
View
1  tests/mocks/template/MailWithoutRender.php
@@ -3,6 +3,7 @@
namespace li3_mailer\tests\mocks\template;
class MailWithoutRender extends \li3_mailer\template\Mail {
+
public function render($process, array $data = array(), array $options = array()) {
return compact('process', 'data', 'options');
}
View
1  tests/mocks/template/mail/Compiler.php
@@ -3,6 +3,7 @@
namespace li3_mailer\tests\mocks\template\mail;
class Compiler extends \li3_mailer\template\mail\Compiler {
+
public function renderer() {
return $this->_renderer;
}
View
1  tests/mocks/template/mail/adapter/FileLoader.php
@@ -6,6 +6,7 @@
use lithium\template\TemplateException;
class FileLoader extends \lithium\template\view\adapter\File {
+
protected function _paths($type, array $params) {
if (!isset($this->_paths[$type])) {
throw new TemplateException("Invalid template type '{$type}'.");
Please sign in to comment.
Something went wrong with that request. Please try again.