当我尝试加载配置文件时,没有返回任何内容,如果我没有尝试加载它,我的测试失败,因为它试图访问配置文件中定义的变量。
vendor/phpunit/phpunit/phpunit --bootstrap config/config.php src/Notification.php tests/NotificationTest -v --debug
没有配置
vendor/phpunit/phpunit/phpunit --bootstrap src/Notification.php tests/NotificationTest -v --debug PHPUnit 6.1.1 by Sebastian Bergmann and contributors.
Runtime: PHP 7.0.17
Starting test 'NotificationTest::testEmail'.
E 1 / 1 (100%)
Time: 26 ms, Memory: 4.00MB
There was 1 error:
1) NotificationTest::testEmail
Trying to get property of non-object
src/Notification.php:13
tests/NotificationTest.php:10
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
更新
在我需要加载
的配置文件中查找变量时仍然出错vendor/phpunit/phpunit/phpunit tests/NotificationTest -v --debug
PHPUnit 6.1.1 by Sebastian Bergmann and contributors.
Runtime: PHP 7.0.17
Starting test 'NotificationTest::testEmail'.
E 1 / 1 (100%)
Time: 26 ms, Memory: 4.00MB
There was 1 error:
1) NotificationTest::testEmail
Trying to get property of non-object
src/Notification.php:13
tests/NotificationTest.php:10
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
更新2 - 新错误
vendor/phpunit/phpunit/phpunit --bootstrap config/config.php tests/NotificationTest -v --debug
PHPUnit 6.1.1 by Sebastian Bergmann and contributors.
Runtime: PHP 7.0.17
Starting test 'NotificationTest::testEmail'.
INFO - Message sent
E 1 / 1 (100%)
Time: 128 ms, Memory: 4.00MB
There was 1 error:
1) NotificationTest::testEmail
TypeError: Return value of CCP\Notification::sendEmail() must be an instance of boolean, boolean returned
src/Notification.php:32
tests/NotificationTest.php:11
ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
看起来像严格类型和php7的问题,我做得不对。
源代码
<?php
declare(strict_types=1);
namespace CCP;
use CCP\MyMailer;
class Notification{
private $log;
public function __construct(){
global $CFG;
$this->log = $CFG->log;
}
public function sendEmail(string $from, array $to, string $subject, string $body): \boolean{
$mail = new MyMailer;
$mail->setFrom($from);
foreach($to as $value){
$mail->addAddress($value);
}
$mail->Subject = $subject;
$mail->Body = $body;
if(!$mail->send()) {
$this->log->error(" Message could not be sent for ");
$this->log->error(" Mailer error: ".$mail->ErrorInfo);
return false;
} else {
$this->log->info(" Message sent ");
}
return true;
}
public function emailTest(){
$mail_from = "m.w@mail.com"; # for testing
# code to retrieve the submitter of the subnet
$rcpt_to = ["s.u@mail.com"];
$subject = "CCP :: IAM :: Subnet Request Approved";
$data = "subnet was been approved by the IP Admin";
$this->sendEmail($mail_from, $rcpt_to, $subject, $data);
}
}
?>
修复,它是bool而不是布尔