From 9753938a2e2518595791012ec1d793c174a25540 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lucas?= Date: Fri, 21 Feb 2014 08:15:24 +0100 Subject: [PATCH] Prepare the code to test properly sendtomail.php --- sendtomail.php | 39 +++++++++++++++++++++--------- test/config_test.php | 9 ++++++- test/mailTest.php | 56 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 92 insertions(+), 12 deletions(-) create mode 100644 test/mailTest.php diff --git a/sendtomail.php b/sendtomail.php index 7072703..f09315f 100644 --- a/sendtomail.php +++ b/sendtomail.php @@ -4,22 +4,39 @@ require_once ("config.php"); require_once "resources/PHPMailer/class.phpmailer.php"; require_once "book.php"; -if (is_null ($config['cops_mail_configuration']) || - !is_array ($config['cops_mail_configuration']) || - empty ($config['cops_mail_configuration']["smtp.host"]) || - empty ($config['cops_mail_configuration']["address.from"])) { - echo "NOK. bad configuration of $config ['cops_mail_configuration']"; +function checkConfiguration () { + global $config; + + if (is_null ($config['cops_mail_configuration']) || + !is_array ($config['cops_mail_configuration']) || + empty ($config['cops_mail_configuration']["smtp.host"]) || + empty ($config['cops_mail_configuration']["address.from"])) { + return "NOK. bad configuration."; + } + return False; +} + +function checkRequest ($idData, $emailDest) { + if (empty ($idData)) { + return 'No data sent.'; + } + if (empty ($emailDest)) { + return 'No email sent.'; + } + return False; +} + +if (php_sapi_name() === 'cli') { return; } + +if ($error = checkConfiguration ()) { + echo $error; exit; } $idData = $_REQUEST["data"]; -if (empty ($idData)) { - echo 'No data sent.'; - exit; -} $emailDest = $_REQUEST["email"]; -if (empty ($emailDest)) { - echo 'No email sent.'; +if ($error = checkRequest ($idData, $emailDest)) { + echo $error; exit; } diff --git a/test/config_test.php b/test/config_test.php index a2d3174..bf87645 100644 --- a/test/config_test.php +++ b/test/config_test.php @@ -1,4 +1,11 @@ "smtp.free.fr", + "smtp.username" => "", + "smtp.password" => "", + "smtp.secure" => "", + "address.from" => "cops@slucas.fr" + ); diff --git a/test/mailTest.php b/test/mailTest.php new file mode 100644 index 0000000..e6756dd --- /dev/null +++ b/test/mailTest.php @@ -0,0 +1,56 @@ + + */ + +require_once (dirname(__FILE__) . "/config_test.php"); +require_once (dirname(__FILE__) . "/../book.php"); +require_once (dirname(__FILE__) . "/../sendtomail.php"); + +class MailTest extends PHPUnit_Framework_TestCase +{ + public function testCheckConfiguration () { + global $config; + + $this->assertFalse(checkConfiguration ()); + } + + public function testCheckConfigurationNull () { + global $config; + $config['cops_mail_configuration'] = NULL; + + $this->assertStringStartsWith("NOK", checkConfiguration ()); + } + + public function testCheckConfigurationNotArray () { + global $config; + $config['cops_mail_configuration'] = "Test"; + + $this->assertStringStartsWith("NOK", checkConfiguration ()); + } + + public function testCheckConfigurationSmtpEmpty () { + global $config; + $config['cops_mail_configuration']["smtp.host"] = ""; + + $this->assertStringStartsWith("NOK", checkConfiguration ()); + } + + public function testCheckConfigurationEmailEmpty () { + global $config; + $config['cops_mail_configuration']["address.from"] = ""; + + $this->assertStringStartsWith("NOK", checkConfiguration ()); + } + + public function testCheckConfigurationEmailNotValid () { + global $config; + $config['cops_mail_configuration']["address.from"] = "a"; + + $this->markTestIncomplete(); + } + +} \ No newline at end of file