From bfc00000de581c84e998ebbb13ffe7676964a343 Mon Sep 17 00:00:00 2001 From: Matthew Weier O'Phinney Date: Tue, 2 Nov 2010 07:44:18 -0700 Subject: [PATCH] File transport: fixed exceptions, tests - Fixed to use new exception strategy - Made more easily testable - Removed underscore prefixes from protected attributes --- library/Zend/Mail/Transport/File.php | 42 ++++++++++++++++++++------- tests/Zend/Mail/FileTest.php | 43 ++++++++++++---------------- 2 files changed, 51 insertions(+), 34 deletions(-) diff --git a/library/Zend/Mail/Transport/File.php b/library/Zend/Mail/Transport/File.php index 2575098e21a..6980a0cd33e 100644 --- a/library/Zend/Mail/Transport/File.php +++ b/library/Zend/Mail/Transport/File.php @@ -46,14 +46,14 @@ class File extends AbstractTransport * * @var string */ - protected $_path; + protected $path; /** * Callback function generating a file name * * @var string|array|Closure */ - protected $_callback; + protected $callback; /** * Constructor @@ -89,10 +89,10 @@ public function __construct($options = null) public function setOptions(array $options) { if (isset($options['path'])) { - $this->_path = $options['path']; + $this->path = $options['path']; } if (isset($options['callback'])) { - $this->_callback = $options['callback']; + $this->callback = $options['callback']; } } @@ -105,24 +105,26 @@ public function setOptions(array $options) */ protected function _sendMail() { - $file = $this->_path . DIRECTORY_SEPARATOR . call_user_func($this->_callback, $this); + $file = $this->getPath() . DIRECTORY_SEPARATOR . call_user_func($this->getCallback(), $this); if (!is_writable(dirname($file))) { - throw new Exception('Target directory ' . dirname($file) - . ' does not exist or not writable '); + throw new Exception\RuntimeException(sprintf( + 'Target directory "%s" does not exist or is not writable', + dirname($file) + )); } $email = $this->header . $this->EOL . $this->body; if (!file_put_contents($file, $email)) { - throw new Exception('Unable to send mail'); + throw new Exception\RuntimeException('Unable to send mail'); } } /** * Returns the default callback for generating file names * - * @return Closure + * @return callback */ public function getDefaultCallback() { @@ -130,4 +132,24 @@ public function getDefaultCallback() return 'ZendMail_' . time() . '_' . mt_rand() . '.tmp'; }; } -} \ No newline at end of file + + /** + * Retrieve registered path + * + * @return string + */ + public function getPath() + { + return $this->path; + } + + /** + * Get the registered callback for generating file names + * + * @return callback + */ + public function getCallback() + { + return $this->callback; + } +} diff --git a/tests/Zend/Mail/FileTest.php b/tests/Zend/Mail/FileTest.php index e322f43fdf5..d00969cebb2 100644 --- a/tests/Zend/Mail/FileTest.php +++ b/tests/Zend/Mail/FileTest.php @@ -81,20 +81,19 @@ protected function _cleanDir($dir) public function testTransportSetup() { - try { - $transport = new Mail\Transport\File(); - } catch (\Exception $e) { - $this->fail('Exception raised while creating file transport with no params'); - } + $transport = new Mail\Transport\File(); - try { - $transport = new Mail\Transport\File(array( - 'path' => $this->_tmpdir, - 'callback' => function(){return 'test';} - )); - } catch (\Exception $e) { - $this->fail('Exception raised while creating file transport with params'); - } + $callback = function() { + return 'test'; + }; + + $transport = new Mail\Transport\File(array( + 'path' => $this->_tmpdir, + 'callback' => $callback, + )); + + $this->assertEquals($this->_tmpdir, $transport->getPath()); + $this->assertSame($callback, $transport->getCallback()); } protected function _prepareMail() @@ -110,18 +109,14 @@ protected function _prepareMail() public function testNotWritablePathFailure() { - try { - $transport = new Mail\Transport\File(array( - 'path' => $this->_tmpdir . '/not_existing/directory' - )); + $transport = new Mail\Transport\File(array( + 'path' => $this->_tmpdir . '/not_existing/directory' + )); - $mail = $this->_prepareMail(); - $mail->send($transport); + $mail = $this->_prepareMail(); - } catch (Mail\Transport\Exception $e) { - return; // test is ok - } - $this->fail('No exception raised with not writable path set'); + $this->setExpectedException('Zend\Mail\Transport\Exception\RuntimeException', 'not writable'); + $mail->send($transport); } public function testTransportSendMail() @@ -178,4 +173,4 @@ public function testPrependToClosure() // and default callback part $this->assertContains('ZendMail', $entry); } -} \ No newline at end of file +}