Downloads

Google Ad

CakePHP WTN Goodies

Date: Sat, Jan 30th 2010, 14:49 Author: nick Views: 7215 Comments share

CakePHP WebTechNick Plugin

Info:
Watch:
Get it:
  • Download Now
  • git clone git://github.com/webtechnick/CakePHP-WebTechNick-Plugin WebTechNick
I've been putting off this post for a while; I wanted to finish a few more of my little side projects into a nice little goodies plugin for cakePHP. However, with school, and other commitments I've decided to release what I have so far and then later edit/add-to this post as I release more tricks in my bag-o-goodies. :)

Putting the M back in Email...

This may be a hard pill to swallow for anyone who believes full-heartily that email belongs in the C part of MVC. I would agree, most of the time, due to an email being so closely linked to the V of MVC. However, I often find myself needing to shoot off a quick one or two liner email based on just the model. Be it an email to verify a new user account, a quick thank you for a donation, or verification that a payment has been received, it just needs to be short, simple, sweet, and most importantly, reusable.

Whatever the case, I normally don't need a formal view to go with my email as it's rather simple and just plain text. As such, I've found it much more convenient to put all the email logic within my model. Putting the email logic within the M of MVC allows me to write extremely simple cake shells to fire off batch emails via a cronjob (extremely useful IMHO).

Until recently, I've just instantiated the Email Component within a model function and called it accordingly, sending a simple text message (bypassing the view feature of the email component). A major drawbacks to this approach was testing -- I've grown fond of mocking the Email Component and testing that send() function was called when I expect an email to be sent, but by creating the instance within the method my email tests would actually send me emails... ick.

Enter the Email Behavior.....

The first release of my goodies is an email behavior that allows a model to send quick one or two liner messages without a view. The concept is very simple, the behavior takes advantage of the Email Component available in the cakePHP core and just overrides the send method to force a template-less send (since we have no native controller to attach to.

//some_model.php
  1. var $actsAs = array('WebTechNick.Email');
  2.  
  3. function email($id = null){
  4.   if($id) $this->id = $id;
  5.   $this->Email->to = $this->field('email');
  6.   $this->Email->from = 'awesome_admin@example.com';
  7.   $this->Email->subject = 'Example from Email Behavior';
  8.   $this->Email->send('You rock!');
  9. }

With this, testing that a model sends an email is just as simple as testing its controller counterpart.

//some_model.test.php
  1. App::import('Model', 'SomeModel');
  2. App::import('Component','Email');
  3. class SomeModelTestCase extends CakeTestCase {
  4.   var $SomeModel = null;
  5.   //fixtures here....
  6.  
  7.   function startTest() {
  8.     $this->SomeModel =& ClassRegistry::init('SomeModel');
  9.     Mock::generate('EmailComponent');
  10.     $this->SomeModel->Email = new MockEmailComponent();
  11.   }
  12.  
  13.   function testEmail(){
  14.     $this->SomeModel->Email->expectOnce('send');
  15.     $this->SomeModel->email(1); //email with id 1;
  16.     $result = $this->SomeModel->Email;
  17.     $this->assertEqual('awesome_admin@example.com', $result->from);
  18.   }
  19. }

My next release will allow a model to use a template and email element to gain separation of logic and presentation.


Track Auth Login Attempts

Not released yet..


Enjoy, more to come soon!