Skip to content

Commit 556d7b9

Browse files
authored
Merge pull request #653 from cakephp/issue-558
Make DebugKitTransport a better proxy
2 parents b6a75d4 + a227dc8 commit 556d7b9

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

phpcs.xml.dist

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="CakePHP Core">
3+
<rule ref="./vendor/cakephp/cakephp-codesniffer/CakePHP/ruleset.xml">
4+
<!-- Exclude unwanted sniffs -->
5+
<exclude name="Generic.Commenting.Todo.TaskFound"/> <!-- Excluded during 3.next development -->
6+
</rule>
7+
8+
<!-- Necessary for class aliases used for backwards compat -->
9+
<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
10+
<severity>0</severity>
11+
</rule>
12+
</ruleset>

src/Mailer/Transport/DebugKitTransport.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,61 @@ public function send(Email $email)
8181

8282
return $result;
8383
}
84+
85+
/**
86+
* Proxy unknown methods to the wrapped object
87+
*
88+
* @param string $method The method to call
89+
* @param array $args The args to call $method with.
90+
* @return mixed
91+
*/
92+
public function __call($method, array $args)
93+
{
94+
return call_user_func_array([$this->originalTransport, $method], $args);
95+
}
96+
97+
/**
98+
* Proxy property reads to the wrapped object
99+
*
100+
* @param string $name The property to read.
101+
* @return mixed
102+
*/
103+
public function __get($name)
104+
{
105+
return $this->originalTransport->{$name};
106+
}
107+
108+
/**
109+
* Proxy property changes to the wrapped object
110+
*
111+
* @param string $name The property to read.
112+
* @param mixed $value The property value.
113+
* @return mixed
114+
*/
115+
public function __set($name, $value)
116+
{
117+
return $this->originalTransport->{$name} = $value;
118+
}
119+
120+
/**
121+
* Proxy property changes to the wrapped object
122+
*
123+
* @param string $name The property to read.
124+
* @return bool
125+
*/
126+
public function __isset($name)
127+
{
128+
return isset($this->originalTransport->{$name});
129+
}
130+
131+
/**
132+
* Proxy property changes to the wrapped object
133+
*
134+
* @param string $name The property to delete.
135+
* @return void
136+
*/
137+
public function __unset($name)
138+
{
139+
unset($this->originalTransport->{$name});
140+
}
84141
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4+
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5+
*
6+
* Licensed under The MIT License
7+
* Redistributions of files must retain the above copyright notice.
8+
*
9+
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
10+
* @link http://cakephp.org CakePHP(tm) Project
11+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
12+
*/
13+
namespace DebugKit\Test\TestCase\Mailer\Transport;
14+
15+
use Cake\Mailer\AbstractTransport;
16+
use Cake\Mailer\Email;
17+
use Cake\TestSuite\TestCase;
18+
use DebugKit\Mailer\Transport\DebugKitTransport;
19+
20+
class DebugKitTransportTest extends TestCase
21+
{
22+
public function setUp()
23+
{
24+
$this->log = new \ArrayObject();
25+
$this->wrapped = $this->getMockBuilder(AbstractTransport::class)
26+
->setMethods(['send', 'customMethod'])
27+
->getMock();
28+
$this->transport = new DebugKitTransport(
29+
['debugKitLog' => $this->log],
30+
$this->wrapped
31+
);
32+
}
33+
34+
public function testPropertyProxies()
35+
{
36+
$this->wrapped->property = 'value';
37+
$this->assertTrue(isset($this->transport->property));
38+
$this->assertSame('value', $this->transport->property);
39+
40+
$this->transport->property = 'new value';
41+
$this->assertSame('new value', $this->wrapped->property);
42+
unset($this->transport->property);
43+
$this->assertFalse(isset($this->wrapped->property));
44+
}
45+
46+
public function testMethodProxy()
47+
{
48+
$this->wrapped->method('customMethod')
49+
->will($this->returnValue('bloop'));
50+
$this->assertSame('bloop', $this->transport->customMethod());
51+
}
52+
53+
public function testEmailCapture()
54+
{
55+
$email = new Email();
56+
$email->setSubject('Testing 123')
57+
->setFrom('[email protected]')
58+
->setTo('[email protected]');
59+
$this->transport->send($email);
60+
$this->assertCount(1, $this->log);
61+
62+
$result = $this->log[0];
63+
$this->assertArrayHasKey('headers', $result);
64+
$this->assertArrayHasKey('message', $result);
65+
}
66+
}

0 commit comments

Comments
 (0)