From: Michael M Slusarz Date: Sat, 11 Sep 2010 04:38:37 +0000 (-0600) Subject: Use PHPUnit mock function, rather than static mock files X-Git-Url: https://git.internetallee.de/?a=commitdiff_plain;h=21d7218dc143d6f728c6d2f833e08c2c5e833cce;p=horde.git Use PHPUnit mock function, rather than static mock files --- diff --git a/imp/lib/tests/Imp/Autoload.php b/imp/lib/tests/Imp/Autoload.php index b44c5f1a9..cf7276a85 100644 --- a/imp/lib/tests/Imp/Autoload.php +++ b/imp/lib/tests/Imp/Autoload.php @@ -36,10 +36,3 @@ if (!spl_autoload_functions()) { /** Catch strict standards */ error_reporting(E_ALL | E_STRICT); - -/** Load stubs */ -require_once 'Stub/Browser.php'; -require_once 'Stub/Identity.php'; -require_once 'Stub/Injector.php'; -require_once 'Stub/Prefs.php'; -require_once 'Stub/Registry.php'; \ No newline at end of file diff --git a/imp/lib/tests/Imp/Stub/Browser.php b/imp/lib/tests/Imp/Stub/Browser.php deleted file mode 100644 index 5357ca8f5..000000000 --- a/imp/lib/tests/Imp/Stub/Browser.php +++ /dev/null @@ -1,41 +0,0 @@ - - * @license http://www.fsf.org/copyleft/lgpl.html LGPL - * @link http://pear.horde.org/index.php?package=Imp - */ - -/** - * Dummy Horde_Browser stub. - * - * Copyright 2010 The Horde Project (http://www.horde.org/) - * - * See the enclosed file COPYING for license information (GPL). If you - * did not receive this file, see http://www.fsf.org/copyleft/gpl.html. - * - * @category Horde - * @package IMP - * @subpackage UnitTests - * @author Gunnar Wrobel - * @license http://www.fsf.org/copyleft/gpl.html GPL - * @link http://pear.horde.org/index.php?package=Imp - */ -class IMP_Stub_Browser -{ - public function hasQuirk($quirk) - { - return false; - } - - public function hasFeature($feature) - { - return false; - } -} \ No newline at end of file diff --git a/imp/lib/tests/Imp/Stub/Identity.php b/imp/lib/tests/Imp/Stub/Identity.php deleted file mode 100644 index ec2e06ebf..000000000 --- a/imp/lib/tests/Imp/Stub/Identity.php +++ /dev/null @@ -1,73 +0,0 @@ - - * @license http://www.fsf.org/copyleft/lgpl.html LGPL - * @link http://pear.horde.org/index.php?package=Imp - */ - -/** - * Dummy IMP_Prefs_Identity stub. - * - * Copyright 2010 The Horde Project (http://www.horde.org/) - * - * See the enclosed file COPYING for license information (GPL). If you - * did not receive this file, see http://www.fsf.org/copyleft/gpl.html. - * - * @category Horde - * @package IMP - * @subpackage UnitTests - * @author Gunnar Wrobel - * @license http://www.fsf.org/copyleft/gpl.html GPL - * @link http://pear.horde.org/index.php?package=Imp - */ -class IMP_Stub_Identity -{ - private $_id = 'default'; - - public function getMatchingIdentity($mail) - { - if ($mail == 'test@example.org') { - return 'test'; - } - } - - public function setDefault($id) - { - if ($id != 'test' && $id != 'other' && $id != 'default') { - throw new Exception("Unexpected default $id!"); - } - $this->_id = $id; - } - - public function getDefault() - { - return $this->_id; - } - - public function getFromAddress() - { - return 'test@example.org'; - } - - public function getValue($value) - { - switch ($value) { - case 'fullname': - return 'Mr. Test'; - case 'replyto_addr': - switch ($this->_id) { - case 'test': - return 'test@example.org'; - case 'other': - return 'reply@example.org'; - } - } - } -} \ No newline at end of file diff --git a/imp/lib/tests/Imp/Stub/Injector.php b/imp/lib/tests/Imp/Stub/Injector.php deleted file mode 100644 index 2d859a053..000000000 --- a/imp/lib/tests/Imp/Stub/Injector.php +++ /dev/null @@ -1,46 +0,0 @@ - - * @license http://www.fsf.org/copyleft/lgpl.html LGPL - * @link http://pear.horde.org/index.php?package=Imp - */ - -/** - * Dummy Horde_Injector stub. - * - * Copyright 2010 The Horde Project (http://www.horde.org/) - * - * See the enclosed file COPYING for license information (GPL). If you - * did not receive this file, see http://www.fsf.org/copyleft/gpl.html. - * - * @category Horde - * @package IMP - * @subpackage UnitTests - * @author Gunnar Wrobel - * @license http://www.fsf.org/copyleft/gpl.html GPL - * @link http://pear.horde.org/index.php?package=Imp - */ -class IMP_Stub_Injector -{ - private $_mail; - - public function getInstance($interface) - { - switch($interface) { - case 'IMP_Identity': - return new IMP_Stub_Identity(); - case 'IMP_Mail': - if (!isset($this->_mail)) { - $this->_mail = new Horde_Mail_Transport_Mock(); - } - return $this->_mail; - } - } -} \ No newline at end of file diff --git a/imp/lib/tests/Imp/Stub/Prefs.php b/imp/lib/tests/Imp/Stub/Prefs.php deleted file mode 100644 index 0fdb53f35..000000000 --- a/imp/lib/tests/Imp/Stub/Prefs.php +++ /dev/null @@ -1,41 +0,0 @@ - - * @license http://www.fsf.org/copyleft/lgpl.html LGPL - * @link http://pear.horde.org/index.php?package=Imp - */ - -/** - * Dummy Horde_Prefs stub. - * - * Copyright 2010 The Horde Project (http://www.horde.org/) - * - * See the enclosed file COPYING for license information (GPL). If you - * did not receive this file, see http://www.fsf.org/copyleft/gpl.html. - * - * @category Horde - * @package IMP - * @subpackage UnitTests - * @author Gunnar Wrobel - * @license http://www.fsf.org/copyleft/gpl.html GPL - * @link http://pear.horde.org/index.php?package=Imp - */ -class IMP_Stub_Prefs -{ - public function getValue($pref) - { - switch($pref) { - case 'date_format': - return '%x'; - case 'twentyFour': - return true; - } - } -} \ No newline at end of file diff --git a/imp/lib/tests/Imp/Stub/Registry.php b/imp/lib/tests/Imp/Stub/Registry.php deleted file mode 100644 index b4f10577d..000000000 --- a/imp/lib/tests/Imp/Stub/Registry.php +++ /dev/null @@ -1,48 +0,0 @@ - - * @license http://www.fsf.org/copyleft/lgpl.html LGPL - * @link http://pear.horde.org/index.php?package=Imp - */ - -/** - * Dummy Horde_Registry stub. - * - * Copyright 2010 The Horde Project (http://www.horde.org/) - * - * See the enclosed file COPYING for license information (GPL). If you - * did not receive this file, see http://www.fsf.org/copyleft/gpl.html. - * - * @category Horde - * @package IMP - * @subpackage UnitTests - * @author Gunnar Wrobel - * @license http://www.fsf.org/copyleft/gpl.html GPL - * @link http://pear.horde.org/index.php?package=Imp - */ -class IMP_Stub_Registry -{ - private $_charset = 'UTF-8'; - - public function getCharset() - { - return $this->_charset; - } - - public function setCharset($charset) - { - $this->_charset = $charset; - } - - public function get($parameter, $app = null) - { - return null; - } -} diff --git a/imp/lib/tests/Imp/Unit/Mime/Viewer/ItipTest.php b/imp/lib/tests/Imp/Unit/Mime/Viewer/ItipTest.php index 92667ac56..e3187f2e4 100644 --- a/imp/lib/tests/Imp/Unit/Mime/Viewer/ItipTest.php +++ b/imp/lib/tests/Imp/Unit/Mime/Viewer/ItipTest.php @@ -35,18 +35,145 @@ require_once dirname(__FILE__) . '/../../../Autoload.php'; class Imp_Unit_Mime_Viewer_ItipTest extends PHPUnit_Framework_TestCase { + private $_identity; + private $_identityId = 'default'; + private $_mail; + private $_registryCharset = 'UTF-8'; + public function setUp() { - $GLOBALS['registry'] = new IMP_Stub_Registry(); - $GLOBALS['browser'] = new IMP_Stub_Browser(); - $GLOBALS['prefs'] = new IMP_Stub_Prefs(); - $GLOBALS['injector'] = new IMP_Stub_Injector(); + $browser = $this->getMock('Horde_Browser'); + $browser->expects($this->any()) + ->method('hasQuirk') + ->will($this->returnValue(false)); + $browser->expects($this->any()) + ->method('hasFeature') + ->will($this->returnValue(false)); + $GLOBALS['browser'] = $browser; + + $injector = $this->getMock('Horde_Injector', array(), array(), '', false); + $injector->expects($this->any()) + ->method('getInstance') + ->will($this->returnCallback(array($this, '_injectorGetInstance'))); + $GLOBALS['injector'] = $injector; + + $prefs = $this->getMock('Horde_Prefs', array(), array(), '' , false); + $prefs->expects($this->any()) + ->method('getValue') + ->will($this->returnCallback(array($this, '_prefsGetValue'))); + $GLOBALS['prefs'] = $prefs; + + $registry = $this->getMock('Horde_Registry', array(), array(), '', false); + $registry->expects($this->any()) + ->method('getCharset') + ->will($this->returnCallback(array($this, '_registryGetCharset'))); + $registry->expects($this->any()) + ->method('setCharset') + ->will($this->returnCallback(array($this, '_registrySetCharset'))); + $GLOBALS['registry'] = $registry; + $GLOBALS['conf']['server']['name'] = 'localhost'; $_GET['identity'] = 'test'; $_SERVER['REMOTE_ADDR'] = 'localhost'; $_SESSION = array('imp' => array('view' => 'imp')); } + public function _injectorGetInstance($interface) + { + switch ($interface) { + case 'IMP_Identity': + if (!isset($this->_identity)) { + $identity = $this->getMock('Horde_Core_Prefs_Identity', array(), array(), '', false); + $identity->expects($this->any()) + ->method('setDefault') + ->will($this->returnCallback(array($this, '_identitySetDefault'))); + $identity->expects($this->any()) + ->method('getDefault') + ->will($this->returnCallback(array($this, '_identityGetDefault'))); + $identity->expects($this->any()) + ->method('getFromAddress') + ->will($this->returnValue('test@example.org')); + $identity->expects($this->any()) + ->method('getValue') + ->will($this->returnCallback(array($this, '_identityGetValue'))); + $identity->expects($this->any()) + ->method('getMatchingIdentity') + ->will($this->returnCallback(array($this, '_identityGetMatchingIdentity'))); + $this->_identity = $identity; + } + return $this->_identity; + + case 'IMP_Mail': + if (!isset($this->_mail)) { + $this->_mail = Horde_Mail::factory('Mock'); + } + return $this->_mail; + } + } + + public function _identityGetMatchingIdentity($mail) + { + if ($mail == 'test@example.org') { + return 'test'; + } + } + + public function _identitySetDefault($id) + { + if (($id != 'test') && + ($id != 'other') && + ($id != 'default')) { + throw new Exception("Unexpected default $id!"); + } + + $this->_identityId = $id; + } + + public function _identityGetDefault() + { + return $this->_identityId; + } + + public function _identityGetValue($value) + { + switch ($value) { + case 'fullname': + return 'Mr. Test'; + + case 'replyto_addr': + switch ($this->_identityId) { + case 'test': + return 'test@example.org'; + + case 'other': + return 'reply@example.org'; + } + } + } + + public function _prefsGetValue($pref) + { + switch ($pref) { + case 'date_format': + return '%x'; + + case 'twentyFour': + return true; + } + } + + public function _registryGetCharset() + { + return $this->_registryCharset; + } + + public function _registrySetCharset($charset) + { + $this->_registryCharset = $charset; + } + + /* Begin tests */ + public function testAcceptingAnInvitationResultsInReplySent() { $_GET['itip_action'] = array(0 => 'accept'); @@ -449,7 +576,7 @@ extends PHPUnit_Framework_TestCase $this->assertEquals('Tentative: Test Invitation', $this->_getMailHeaders()->getValue('Subject')); } public function testResultMimeMessageHeadersContainsReplyToForAlternateIdentity() - { + { $_GET['identity'] = 'other'; $_GET['itip_action'] = array(0 => 'accept'); $viewer = $this->_getViewer($this->_getInvitation()->exportvCalendar()); @@ -516,11 +643,9 @@ extends PHPUnit_Framework_TestCase { $mail = ''; if (isset($GLOBALS['injector']->getInstance('IMP_Mail')->sentMessages[0])) { - $mail .= $GLOBALS['injector']->getInstance('IMP_Mail')->sentMessages[0]['header_text'] . "\n\n"; - $body = $GLOBALS['injector']->getInstance('IMP_Mail')->sentMessages[0]['body']; - while (!feof($body)) { - $mail .= fread($body, 8192); - } + $mail .= $GLOBALS['injector']->getInstance('IMP_Mail')->sentMessages[0]['header_text'] . + "\n\n" . + $GLOBALS['injector']->getInstance('IMP_Mail')->sentMessages[0]['body']; } return $mail; }