I
I
Ivan Baturin2020-04-11 23:23:54
PHP
Ivan Baturin, 2020-04-11 23:23:54

How to make a unit test for the method of sending messages to Email?

You need to write a unit test for the mail.php file, which contains a method for sending a message to Email.

header("Location: http://landingtaxi/");	

$name = $_POST['firstname'];
$email = $_POST['email_box'];
$col = $_POST['select_col'];

if(isset($name) && isset($email) && isset($col)){

  switch ($col) {
    case 2:
      $col_name = "ГОСТ";
      break;
    case 3:
      $col_name = "Яндекс";
      break;
    case 4:
      $col_name = "Яндекс + Light Box";
      break;
    default:
      $col_name = "Не выбрана";
      break;
  }

  require_once "SendMailSmtpClass.php"; // подключаем класс
  $mailSMTP = new SendMailSmtpClass('[email protected]', '***', 'ssl://smtp.mail.ru', 465, "UTF-8");
  // $mailSMTP = new SendMailSmtpClass('логин', 'пароль', 'хост', 'порт', 'кодировка письма');

  // от кого
  $from = array(
  "Иван", // Имя отправителя
  "[email protected]" // почта отправителя
  );
  // кому
  $to = '[email protected]';


  $result = $mailSMTP->send($to, 'Заказ на оклейку', "Поступил заказ на оклейку автомобиля. Выбранная опция: " . $col_name . ". Имя заказчика: " . $name . ". Почта заказчика: " . $email, $from);
  // $result = $mailSMTP->send('Кому письмо', 'Тема письма', 'Текст письма', 'Отправитель письма');

  if($result === true){
    echo "Done";
  }else{
    echo "Error: " . $result;
  }

  $to = $email;

  $result = $mailSMTP->send($to, 'Оклейка автомобиля', "Благодарим вас за выбор нашей компании! Вам скоро перезвонят для уточнения деталей. С уважением, Батурин Иван!", $from);
}

exit;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maksim Fedorov, 2020-04-12
@MrLumus

Unit test:
A unit test is not possible in this case, because there is no unit test in the form of a pure function (without side effects). There is a solid side effect here: sending to SMTP, listening to HTTP, that is, you don’t really test with units.
The unit test object is either a function or an object. And you also have no function/procedure even at least.
It will be functional (if you call it from the code) or acceptance (if you pull your application via HTTP) in any case.
How to finally test it:
Such functionality is usually tested like this:

  • to start, the mail sending settings are transferred to the config
  • in a test environment, the settings of their SMTP server are slipped into the config
  • set up a fake mail server, such as Mailhog on Go (very easy to set up)
  • perform the work of the script so that the mail leaves, the mail flies to your spoofed server, and they check through the API that the letter has arrived

If you take Codeception, install Mailhog on the machine where the tests will be run, or a docker container (in general, in a couple of lines, for example, if you run CI in Gitlab), and install the Codeception + Mailhog module. You can easily do everything.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question