Skip to content

Commit

Permalink
Merge pull request #130 from cakephp/1.1-Mailer
Browse files Browse the repository at this point in the history
1.1 mailer
  • Loading branch information
markstory committed Jul 14, 2015
2 parents 7fd20ec + 9b3b3f3 commit 08a38f5
Show file tree
Hide file tree
Showing 5 changed files with 290 additions and 0 deletions.
86 changes: 86 additions & 0 deletions src/Shell/Task/MailerTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 1.1.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Bake\Shell\Task;

use Cake\Utility\Inflector;

/**
* Mailer code generator.
*/
class MailerTask extends SimpleBakeTask
{
/**
* Task name used in path generation.
*
* @var string
*/
public $pathFragment = 'Mailer/';

/**
* {@inheritDoc}
*/
public function name()
{
return 'mailer';
}

/**
* {@inheritDoc}
*/
public function fileName($name)
{
return $name . 'Mailer.php';
}

/**
* {@inheritDoc}
*/
public function template()
{
return 'Mailer/mailer';
}

/**
* Bake the Mailer class and html/text layout files.
*
* @param string $name The name of the mailer to make.
* @return void
*/
public function bake($name)
{
$this->bakeLayouts($name);
return parent::bake($name);
}

/**
* Bake empty layout files for html/text emails.
*
* @param string $name The name of the mailer layouts are needed for.
* @return void
*/
public function bakeLayouts($name)
{
$restore = $this->pathFragment;
$layoutsPath = implode(DS, ['Template', 'Layout', 'Email']);

foreach (['html', 'text'] as $type) {
$this->pathFragment = implode(DS, [$layoutsPath, $type, Inflector::underscore($name) . '.ctp']);
$path = $this->getPath();
$this->createFile($path, '');
}

$this->pathFragment = $restore;
}
}
33 changes: 33 additions & 0 deletions src/Template/Bake/Mailer/mailer.ctp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<%
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 0.1.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
%>
<?php
namespace <%= $namespace %>\Mailer;

use Cake\Mailer\Mailer;

/**
* <%= $name %> mailer.
*/
class <%= $name %>Mailer extends Mailer
{

/**
* Mailer's name.
*
* @var string
*/
static public $name = '<%= $name %>';
}
2 changes: 2 additions & 0 deletions tests/TestCase/Shell/BakeShellTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ public function testMain()
'- fixture',
'- form',
'- helper',
'- mailer',
'- model',
'- plugin',
'- shell',
Expand Down Expand Up @@ -169,6 +170,7 @@ public function testLoadTasksCoreAndApp()
'Bake.Fixture',
'Bake.Form',
'Bake.Helper',
'Bake.Mailer',
'Bake.Model',
'Bake.Plugin',
'Bake.Shell',
Expand Down
151 changes: 151 additions & 0 deletions tests/TestCase/Shell/Task/MailerTaskTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 1.1.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Bake\Test\TestCase\Shell\Task;

use Bake\Shell\Task\BakeTemplateTask;
use Bake\Test\TestCase\TestCase;
use Cake\Core\Configure;
use Cake\Core\Plugin;

/**
* MailerTaskTest class
*/
class MailerTaskTest extends TestCase
{
/**
* setup method
*
* @return void
*/
public function setUp()
{
parent::setUp();
$this->_compareBasePath = Plugin::path('Bake') . 'tests' . DS . 'comparisons' . DS . 'Mailer' . DS;
$io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);

$this->Task = $this->getMock(
'Bake\Shell\Task\MailerTask',
['in', 'err', 'createFile', '_stop'],
[$io]
);
$this->Task->Test = $this->getMock(
'Bake\Shell\Task\TestTask',
[],
[$io]
);
$this->Task->BakeTemplate = new BakeTemplateTask($io);
$this->Task->BakeTemplate->initialize();
$this->Task->BakeTemplate->interactive = false;
}

/**
* Test the excute method.
*
* @return void
*/
public function testMain()
{
$this->Task->Test->expects($this->once())
->method('bake')
->with('mailer', 'Example');

$this->Task->expects($this->at(0))
->method('createFile')
->with(
$this->_normalizePath(APP . 'Template/Layout/Email/html/example.ctp'),
''
);
$this->Task->expects($this->at(1))
->method('createFile')
->with(
$this->_normalizePath(APP . 'Template/Layout/Email/text/example.ctp'),
''
);
$this->Task->expects($this->at(2))
->method('createFile')
->with(
$this->_normalizePath(APP . 'Mailer/ExampleMailer.php'),
$this->stringContains('class ExampleMailer extends Mailer')
);

$this->Task->main('Example');
}

/**
* Test main within a plugin.
*
* @return void
*/
public function testMainPlugin()
{
$this->_loadTestPlugin('TestBake');
$path = Plugin::path('TestBake');

$this->Task->expects($this->at(0))
->method('createFile')
->with(
$this->_normalizePath($path . 'src/Template/Layout/Email/html/example.ctp'),
''
);
$this->Task->expects($this->at(1))
->method('createFile')
->with(
$this->_normalizePath($path . 'src/Template/Layout/Email/text/example.ctp'),
''
);
$this->Task->expects($this->at(2))
->method('createFile')
->with(
$this->_normalizePath($path . 'src/Mailer/ExampleMailer.php'),
$this->stringContains('class ExampleMailer extends Mailer')
);

$this->Task->main('TestBake.Example');
}

/**
* Test baking within a plugin.
*
* @return void
*/
public function testBakePlugin()
{
$this->_loadTestPlugin('TestBake');
$path = Plugin::path('TestBake');

$this->Task->plugin = 'TestBake';
$this->Task->expects($this->at(0))
->method('createFile')
->with(
$this->_normalizePath($path . 'src/Template/Layout/Email/html/example.ctp'),
''
);
$this->Task->expects($this->at(1))
->method('createFile')
->with(
$this->_normalizePath($path . 'src/Template/Layout/Email/text/example.ctp'),
''
);
$this->Task->expects($this->at(2))
->method('createFile')
->with(
$this->_normalizePath($path . 'src/Mailer/ExampleMailer.php'),
$this->stringContains('class ExampleMailer extends Mailer')
);

$result = $this->Task->bake('Example');
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}
}
18 changes: 18 additions & 0 deletions tests/comparisons/Mailer/testBakePlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace TestBake\Mailer;

use Cake\Mailer\Mailer;

/**
* Example mailer.
*/
class ExampleMailer extends Mailer
{

/**
* Mailer's name.
*
* @var string
*/
static public $name = 'Example';
}

0 comments on commit 08a38f5

Please sign in to comment.