CakePHP WTN Goodies
Please Support:
CakePHP WebTechNick Plugin
Info:
- Version: 2.0
- Requirements: CakePHP 1.x/2.x, PHP 4+
- Docs: CakePHP WebTechNick Plugin API
- Article: CakePHP WebTechNick Plugin Discussion and Examples
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. :)
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.
//some_model.php
With this, testing that a model sends an email is just as simple as testing its controller counterpart.
//some_model.test.php
My next release will allow a model to use a template and email element to gain separation of logic and presentation.
Enjoy, more to come soon!
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
- function email($id = null){
- if($id) $this->id = $id;
- $this->Email->to = $this->field('email');
- $this->Email->from = 'awesome_admin@example.com';
- $this->Email->subject = 'Example from Email Behavior';
- $this->Email->send('You rock!');
- }
With this, testing that a model sends an email is just as simple as testing its controller counterpart.
//some_model.test.php
- App::import('Model', 'SomeModel');
- App::import('Component','Email');
- class SomeModelTestCase extends CakeTestCase {
- var $SomeModel = null;
- //fixtures here....
- function startTest() {
- $this->SomeModel =& ClassRegistry::init('SomeModel');
- Mock::generate('EmailComponent');
- $this->SomeModel->Email = new MockEmailComponent();
- }
- function testEmail(){
- $this->SomeModel->Email->expectOnce('send');
- $this->SomeModel->email(1); //email with id 1;
- $result = $this->SomeModel->Email;
- $this->assertEqual('awesome_admin@example.com', $result->from);
- }
- }
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!