Permalink
Browse files

Adding unit tests for EmailQueue model and fixing some minor bugs

  • Loading branch information...
1 parent ab9608f commit 6d101a4dedc026f84898fc7e417ff8d61016dbe5 @lorenzo lorenzo committed Jun 20, 2012
Showing with 302 additions and 1 deletion.
  1. +1 −0 Console/Command/SenderShell.php
  2. +9 −1 Model/EmailQueue.php
  3. +161 −0 Test/Case/Model/EmailQueueTest.php
  4. +131 −0 Test/Fixture/EmailQueueFixture.php
@@ -49,6 +49,7 @@ public function main() {
$sent = $email
->to($e['EmailQueue']['to'])
->template($template, $layout)
+ ->viewVars($e['EmailQueue']['template_vars'])
->send();
} catch (SocketException $e) {
$this->err($e->getMessage());
View
@@ -79,7 +79,12 @@ public function getBatch($size = 10) {
),
'order' => array('EmailQueue.created' => 'ASC')
));
- $this->updateAll(array('locked' => true, array('EmailQueue.id' => Set::extract('{n}.EmailQueue.id', $emails))));
+
+ if (!empty($emails)) {
+ $ids = Set::extract('{n}.EmailQueue.id', $emails);
+ $this->updateAll(array('locked' => true), array('EmailQueue.id' => $ids));
+ }
+
$this->getDataSource()->commit();
return $emails;
}
@@ -149,6 +154,9 @@ public function afterFind($results, $primary) {
}
foreach ($results as &$r) {
+ if (!isset($r[$this->alias]['template_vars'])) {
+ return $results;
+ }
$r[$this->alias]['template_vars'] = json_decode($r[$this->alias]['template_vars'], true);
}
return $results;
@@ -0,0 +1,161 @@
+<?php
+App::uses('EmailQueue', 'EmailQueue.Model');
+
+/**
+ * EmailQueue Test Case
+ *
+ */
+class EmailQueueTest extends CakeTestCase {
+
+/**
+ * Fixtures
+ *
+ * @var array
+ */
+ public $fixtures = array(
+ 'plugin.email_queue.email_queue'
+ );
+
+/**
+ * setUp method
+ *
+ * @return void
+ */
+ public function setUp() {
+ parent::setUp();
+ $this->EmailQueue = ClassRegistry::init('EmailQueue.EmailQueue');
+ }
+
+/**
+ * tearDown method
+ *
+ * @return void
+ */
+ public function tearDown() {
+ unset($this->EmailQueue);
+ parent::tearDown();
+ }
+
+/**
+ * testEnqueue method
+ *
+ * @return void
+ */
+ public function testEnqueue() {
+ $count = $this->EmailQueue->find('count');
+ $this->EmailQueue->enqueue('someone@domain.com', array('a' => 'variable', 'some' => 'thing'));
+ $id = $this->EmailQueue->id;
+ $this->assertEquals(++$count, $this->EmailQueue->find('count'));
+
+ $result = $this->EmailQueue->read(null, $id);
+ $expected = array(
+ 'to' => 'someone@domain.com',
+ 'template' => 'default',
+ 'layout' => 'default',
+ 'template_vars' => array('a' => 'variable', 'some' => 'thing'),
+ 'sent' => false,
+ 'locked' => false,
+ 'send_tries' => '0',
+ 'config' => 'default'
+ );
+ $result = $result['EmailQueue'];
+ $sendAt = new DateTime($result['send_at']);
+ unset($result['id'], $result['created'], $result['modified'], $result['send_at']);
+ $this->assertEquals($expected, $result);
+ $this->assertEquals(gmdate('Y-m-d H'),$sendAt->format('Y-m-d H'));
+
+ $date = gmdate('Y-m-d H:i:s');
+ $this->EmailQueue->enqueue(array('a@example.com', 'b@example.com'), array('a' => 'b'), array('send_at' => $date));
+ $this->assertEquals($count + 2, $this->EmailQueue->find('count'));
+
+ $email = $this->EmailQueue->find('first', array(
+ 'conditions' => array('to' => 'a@example.com')
+ ));
+ $this->assertEquals(array('a' => 'b'), $email['EmailQueue']['template_vars']);
+ $this->assertEquals($date, $email['EmailQueue']['send_at']);
+
+ $email = $this->EmailQueue->find('first', array(
+ 'conditions' => array('to' => 'b@example.com')
+ ));
+ $this->assertEquals(array('a' => 'b'), $email['EmailQueue']['template_vars']);
+ $this->assertEquals($date, $email['EmailQueue']['send_at']);
+
+ $this->EmailQueue->enqueue('c@example.com', array('a' => 'c'), array('send_at' => $date, 'config' => 'other', 'template' => 'custom', 'layout' => 'email'));
+ $email = $this->EmailQueue->read();
+ $this->assertEquals(array('a' => 'c'), $email['EmailQueue']['template_vars']);
+ $this->assertEquals($date, $email['EmailQueue']['send_at']);
+ $this->assertEquals('other', $email['EmailQueue']['config']);
+ $this->assertEquals('custom', $email['EmailQueue']['template']);
+ $this->assertEquals('email', $email['EmailQueue']['layout']);
+ }
+
+/**
+ * testGetBatch method
+ *
+ * @return void
+ */
+ public function testGetBatch() {
+ $batch = $this->EmailQueue->getBatch();
+ $this->assertEquals(array('email-1', 'email-2', 'email-3'), Set::extract('{n}.EmailQueue.id', $batch));
+
+ //At this point previous batch should be locked and next call should return an empty set
+ $batch = $this->EmailQueue->getBatch();
+ $this->assertEmpty($batch);
+
+ //Let's change send_at date for email-6 to get it on a batch
+ $this->EmailQueue->save(array('id' => 'email-6', 'send_at' => '2011-01-01 00:00'));
+ $batch = $this->EmailQueue->getBatch();
+ $this->assertEquals(array('email-6'), Set::extract('{n}.EmailQueue.id', $batch));
+ }
+
+/**
+ * testReleaseLocks method
+ *
+ * @return void
+ */
+ public function testReleaseLocks() {
+ $batch = $this->EmailQueue->getBatch();
+ $this->assertNotEmpty($batch);
+ $this->assertEmpty($this->EmailQueue->getBatch());
+ $this->EmailQueue->releaseLocks(Set::extract('{n}.EmailQueue.id', $batch));
+ $this->assertEquals($batch, $this->EmailQueue->getBatch());
+ }
+
+/**
+ * testClearLocks method
+ *
+ * @return void
+ */
+ public function testClearLocks() {
+ $batch = $this->EmailQueue->getBatch();
+ $this->assertNotEmpty($batch);
+ $this->assertEmpty($this->EmailQueue->getBatch());
+ $this->EmailQueue->clearLocks();
+ $batch = $this->EmailQueue->getBatch();
+ $this->assertEquals(array('email-1', 'email-2', 'email-3', 'email-5'), Set::extract('{n}.EmailQueue.id', $batch));
+ }
+
+/**
+ * testSuccess method
+ *
+ * @return void
+ */
+ public function testSuccess() {
+ $this->EmailQueue->success('email-1');
+ $this->assertEquals(1, $this->EmailQueue->field('sent', array('id' => 'email-1')));
+ }
+
+/**
+ * testFail method
+ *
+ * @return void
+ */
+ public function testFail() {
+ $this->EmailQueue->fail('email-1');
+ $this->assertEquals(2, $this->EmailQueue->field('send_tries', array('id' => 'email-1')));
+
+ $this->EmailQueue->fail('email-1');
+ $this->assertEquals(3, $this->EmailQueue->field('send_tries', array('id' => 'email-1')));
+ }
+
+}
@@ -0,0 +1,131 @@
+<?php
+/**
+ * EmailQueueFixture
+ *
+ */
+class EmailQueueFixture extends CakeTestFixture {
+
+/**
+ * Table name
+ *
+ * @var string
+ */
+ public $table = 'email_queue';
+
+/**
+ * Fields
+ *
+ * @var array
+ */
+ public $fields = array(
+ 'id' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 36, 'key' => 'primary', 'collate' => 'ascii_general_ci', 'charset' => 'ascii'),
+ 'to' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 100, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
+ 'config' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 30, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
+ 'template' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 50, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
+ 'layout' => array('type' => 'string', 'null' => false, 'default' => null, 'length' => 50, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
+ 'template_vars' => array('type' => 'text', 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
+ 'sent' => array('type' => 'boolean', 'null' => false, 'default' => null),
+ 'locked' => array('type' => 'boolean', 'null' => false, 'default' => 0),
+ 'send_tries' => array('type' => 'integer', 'null' => false, 'default' => null, 'length' => 2),
+ 'send_at' => array('type' => 'datetime', 'null' => true, 'default' => null),
+ 'created' => array('type' => 'datetime', 'null' => false, 'default' => null),
+ 'modified' => array('type' => 'datetime', 'null' => false, 'default' => null),
+ 'indexes' => array(
+ 'PRIMARY' => array('column' => 'id', 'unique' => 1)
+ ),
+ 'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_general_ci', 'engine' => 'InnoDB')
+ );
+
+/**
+ * Records
+ *
+ * @var array
+ */
+ public $records = array(
+ array(
+ 'id' => 'email-1',
+ 'to' => 'example@example.com',
+ 'config' => 'default',
+ 'template' => 'default',
+ 'layout' => 'default',
+ 'template_vars' => '{"a":1,"b":2}',
+ 'sent' => 0,
+ 'locked' => 0,
+ 'send_tries' => 1,
+ 'send_at' => '2011-06-20 13:50:48',
+ 'created' => '2011-06-20 13:50:48',
+ 'modified' => '2011-06-20 13:50:48'
+ ),
+ array(
+ 'id' => 'email-2',
+ 'to' => 'example@example.com',
+ 'config' => 'default',
+ 'template' => 'default',
+ 'layout' => 'default',
+ 'template_vars' => '{"a":1,"b":2}',
+ 'sent' => 0,
+ 'locked' => 0,
+ 'send_tries' => 2,
+ 'send_at' => '2011-06-20 13:50:48',
+ 'created' => '2011-06-20 13:50:48',
+ 'modified' => '2011-06-20 13:50:48'
+ ),
+ array(
+ 'id' => 'email-3',
+ 'to' => 'example@example.com',
+ 'config' => 'default',
+ 'template' => 'default',
+ 'layout' => 'default',
+ 'template_vars' => '{"a":1,"b":2}',
+ 'sent' => 0,
+ 'locked' => 0,
+ 'send_tries' => 3,
+ 'send_at' => '2011-06-20 13:50:48',
+ 'created' => '2011-06-20 13:50:48',
+ 'modified' => '2011-06-20 13:50:48'
+ ),
+ array(
+ 'id' => 'email-4',
+ 'to' => 'example@example.com',
+ 'config' => 'default',
+ 'template' => 'default',
+ 'layout' => 'default',
+ 'template_vars' => '{"a":1,"b":2}',
+ 'sent' => 1,
+ 'locked' => 0,
+ 'send_tries' => 0,
+ 'send_at' => '2011-06-20 13:50:48',
+ 'created' => '2011-06-20 13:50:48',
+ 'modified' => '2011-06-20 13:50:48'
+ ),
+ array(
+ 'id' => 'email-5',
+ 'to' => 'example@example.com',
+ 'config' => 'default',
+ 'template' => 'default',
+ 'layout' => 'default',
+ 'template_vars' => '{"a":1,"b":2}',
+ 'sent' => 0,
+ 'locked' => 1,
+ 'send_tries' => 0,
+ 'send_at' => '2011-06-20 13:50:48',
+ 'created' => '2011-06-20 13:50:48',
+ 'modified' => '2011-06-20 13:50:48'
+ ),
+ array(
+ 'id' => 'email-6',
+ 'to' => 'example@example.com',
+ 'config' => 'default',
+ 'template' => 'default',
+ 'layout' => 'default',
+ 'template_vars' => '{"a":1,"b":2}',
+ 'sent' => 0,
+ 'locked' => 0,
+ 'send_tries' => 0,
+ 'send_at' => '2015-06-20 13:50:48',
+ 'created' => '2011-06-20 13:50:48',
+ 'modified' => '2011-06-20 13:50:48'
+ ),
+ );
+
+}

0 comments on commit 6d101a4

Please sign in to comment.