Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Unit tests for shell

  • Loading branch information...
commit 5167a173511f42304e38eb10283d6ab9b8dac3c6 1 parent 6d101a4
José Lorenzo Rodríguez lorenzo authored
15 Console/Command/SenderShell.php
View
@@ -45,14 +45,14 @@ public function main() {
$layout = $e['EmailQueue']['layout'] === 'default' ? $this->params['layout'] : $e['EmailQueue']['layout'];
try {
- $email = new CakeEmail($configName);
+ $email = $this->_newEmail($configName);
$sent = $email
->to($e['EmailQueue']['to'])
->template($template, $layout)
->viewVars($e['EmailQueue']['template_vars'])
->send();
- } catch (SocketException $e) {
- $this->err($e->getMessage());
+ } catch (SocketException $exception) {
+ $this->err($exception->getMessage());
$sent = false;
}
@@ -78,4 +78,13 @@ public function clearLocks() {
ClassRegistry::init('EmailQueue.EmailQueue')->clearLocks();
}
+/**
+ * Returns a new instance of CakeEmail
+ *
+ * @return CakeEmail
+ **/
+ protected function _newEmail($config) {
+ return new CakeEmail($config);
+ }
+
}
154 Test/Case/Console/Command/SenderShellTest.php
View
@@ -0,0 +1,154 @@
+<?php
+
+App::uses('SenderShell', 'EmailQueue.Console/Command');
+App::uses('CakeEmail', 'Network/Email');
+
+/**
+ * SenderShell Test Case
+ *
+ */
+class SenderShellTest extends CakeTestCase {
+
+/**
+ * Fixtures
+ *
+ * @var array
+ */
+ public $fixtures = array(
+ 'plugin.email_queue.email_queue'
+ );
+
+/**
+ * setUp method
+ *
+ * @return void
+ */
+ public function setUp() {
+ parent::setUp();
+ $this->out = $this->getMock('ConsoleOutput', array(), array(), '', false);
+ $this->in = $this->getMock('ConsoleInput', array(), array(), '', false);
+
+ $this->Sender = $this->getMock(
+ 'SenderShell',
+ array('in', 'out', 'err', '_stop', '_newEmail'),
+ array($this->out, $this->out, $this->in)
+ );
+ $this->Sender->params = array(
+ 'limit' => 10,
+ 'template' => 'default',
+ 'layout' => 'default',
+ 'config' => 'default'
+ );
+ }
+
+ public function testMainAllFail() {
+ $email = $this->getMock('CakeEmail', array('to', 'template', 'viewVars', 'send'));
+
+ $this->Sender->expects($this->exactly(3))->method('_newEmail')->with('default')->will($this->returnValue($email));
+ $email->expects($this->exactly(3))->method('send')->will($this->returnValue(false));
+ $email->expects($this->exactly(3))->method('to')->will($this->returnSelf());
+ $email->expects($this->exactly(3))->method('template')
+ ->with('default', 'default')
+ ->will($this->returnSelf());
+
+ $email->expects($this->exactly(3))->method('viewVars')
+ ->with(array('a' => 1, 'b' => 2))
+ ->will($this->returnSelf());
+ $this->Sender->main();
+
+ $emails = ClassRegistry::init('EmailQueue.EmailQueue')->find('all', array(
+ 'conditions' => array('id' => array('email-1', 'email-2', 'email-3')
+ )));
+ $this->assertEquals(2, $emails[0]['EmailQueue']['send_tries']);
+ $this->assertEquals(3, $emails[1]['EmailQueue']['send_tries']);
+ $this->assertEquals(4, $emails[2]['EmailQueue']['send_tries']);
+
+ $this->assertFalse($emails[0]['EmailQueue']['locked']);
+ $this->assertFalse($emails[1]['EmailQueue']['locked']);
+ $this->assertFalse($emails[2]['EmailQueue']['locked']);
+
+ $this->assertFalse($emails[0]['EmailQueue']['sent']);
+ $this->assertFalse($emails[1]['EmailQueue']['sent']);
+ $this->assertFalse($emails[2]['EmailQueue']['sent']);
+ }
+
+
+ public function testMainAllWin() {
+ $email = $this->getMock('CakeEmail', array('to', 'template', 'viewVars', 'send'));
+
+ $this->Sender->params['template'] = 'other';
+ $this->Sender->params['layout'] = 'custom';
+ $this->Sender->params['config'] = 'something';
+
+ $this->Sender->expects($this->exactly(3))->method('_newEmail')
+ ->with('something')
+ ->will($this->returnValue($email));
+
+ $email->expects($this->exactly(3))->method('send')->will($this->returnValue(true));
+ $email->expects($this->exactly(3))->method('to')->will($this->returnSelf());
+ $email->expects($this->exactly(3))->method('template')
+ ->with('other', 'custom')
+ ->will($this->returnSelf());
+
+ $email->expects($this->exactly(3))->method('viewVars')
+ ->with(array('a' => 1, 'b' => 2))
+ ->will($this->returnSelf());
+ $this->Sender->main();
+
+ $emails = ClassRegistry::init('EmailQueue.EmailQueue')->find('all', array(
+ 'conditions' => array('id' => array('email-1', 'email-2', 'email-3')
+ )));
+
+ $this->assertEquals(1, $emails[0]['EmailQueue']['send_tries']);
+ $this->assertEquals(2, $emails[1]['EmailQueue']['send_tries']);
+ $this->assertEquals(3, $emails[2]['EmailQueue']['send_tries']);
+
+ $this->assertFalse($emails[0]['EmailQueue']['locked']);
+ $this->assertFalse($emails[1]['EmailQueue']['locked']);
+ $this->assertFalse($emails[2]['EmailQueue']['locked']);
+
+ $this->assertTrue($emails[0]['EmailQueue']['sent']);
+ $this->assertTrue($emails[1]['EmailQueue']['sent']);
+ $this->assertTrue($emails[2]['EmailQueue']['sent']);
+ }
+
+ public function testMainAllFailWithException() {
+ $email = $this->getMock('CakeEmail', array('to', 'template', 'viewVars', 'send'));
+
+ $this->Sender->expects($this->exactly(3))->method('_newEmail')->with('default')->will($this->returnValue($email));
+
+ $email->expects($this->exactly(3))->method('send')->will($this->throwException(new SocketException('fail')));
+
+ $email->expects($this->exactly(3))->method('to')->will($this->returnSelf());
+ $email->expects($this->exactly(3))->method('template')
+ ->with('default', 'default')
+ ->will($this->returnSelf());
+
+ $email->expects($this->exactly(3))->method('viewVars')
+ ->with(array('a' => 1, 'b' => 2))
+ ->will($this->returnSelf());
+ $this->Sender->main();
+
+ $emails = ClassRegistry::init('EmailQueue.EmailQueue')->find('all', array(
+ 'conditions' => array('id' => array('email-1', 'email-2', 'email-3')
+ )));
+ $this->assertEquals(2, $emails[0]['EmailQueue']['send_tries']);
+ $this->assertEquals(3, $emails[1]['EmailQueue']['send_tries']);
+ $this->assertEquals(4, $emails[2]['EmailQueue']['send_tries']);
+
+ $this->assertFalse($emails[0]['EmailQueue']['locked']);
+ $this->assertFalse($emails[1]['EmailQueue']['locked']);
+ $this->assertFalse($emails[2]['EmailQueue']['locked']);
+
+ $this->assertFalse($emails[0]['EmailQueue']['sent']);
+ $this->assertFalse($emails[1]['EmailQueue']['sent']);
+ $this->assertFalse($emails[2]['EmailQueue']['sent']);
+ }
+
+ public function testClearLocks() {
+ ClassRegistry::init('EmailQueue.EmailQueue')->getBatch();
+ $this->Sender->clearLocks();
+ $this->assertEmpty(ClassRegistry::init('EmailQueue.EmailQueue')->findByLocked(true));
+ }
+
+}
4 Test/Fixture/EmailQueueFixture.php
View
@@ -58,7 +58,7 @@ class EmailQueueFixture extends CakeTestFixture {
),
array(
'id' => 'email-2',
- 'to' => 'example@example.com',
+ 'to' => 'example2@example.com',
'config' => 'default',
'template' => 'default',
'layout' => 'default',
@@ -72,7 +72,7 @@ class EmailQueueFixture extends CakeTestFixture {
),
array(
'id' => 'email-3',
- 'to' => 'example@example.com',
+ 'to' => 'example3@example.com',
'config' => 'default',
'template' => 'default',
'layout' => 'default',
Please sign in to comment.
Something went wrong with that request. Please try again.