Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
File transport: fixed exceptions, tests
Browse files Browse the repository at this point in the history
- Fixed to use new exception strategy
- Made more easily testable
- Removed underscore prefixes from protected attributes
  • Loading branch information
weierophinney committed Nov 2, 2010
1 parent 4262108 commit bfc0000
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 34 deletions.
42 changes: 32 additions & 10 deletions library/Zend/Mail/Transport/File.php
Expand Up @@ -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
Expand Down Expand Up @@ -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'];
}
}

Expand All @@ -105,29 +105,51 @@ 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()
{
return function($transport) {
return 'ZendMail_' . time() . '_' . mt_rand() . '.tmp';
};
}
}

/**
* 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;
}
}
43 changes: 19 additions & 24 deletions tests/Zend/Mail/FileTest.php
Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -178,4 +173,4 @@ public function testPrependToClosure()
// and default callback part
$this->assertContains('ZendMail', $entry);
}
}
}

0 comments on commit bfc0000

Please sign in to comment.