Passed
Push — master ( 2823db...e46f60 )
by Alexander
10:02
created

BaseMessageTest.php$0 ➔ testMagicToString()   A

Complexity

Conditions 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 23
rs 9.552
1
<?php
2
3
namespace Yiisoft\Mailer\Tests;
4
5
class BaseMessageTest extends TestCase
6
{
7
    public function testSend(): void
8
    {
9
        $mailer = $this->getMailer();
10
        $message = $mailer->compose()->setSubject('foo');
11
        $message->send();
12
        $this->assertEquals($message, $mailer->sentMessages[0], 'Unable to send message!');
13
    }
14
15
    public function testToString(): void
16
    {
17
        $mailer = $this->getMailer();
18
        $message = $mailer->compose();
19
        $this->assertEquals($message->toString(), '' . $message);
20
    }
21
22
    public function testMagicToString(): void
23
    {
24
        set_error_handler([$this, 'errorHandler'], E_USER_ERROR);
25
26
        $errstr = 'Unsupported __toString';
27
        $message = new class($errstr) extends TestMessage {
28
            private $errstr;
29
30
            public function __construct($errstr)
31
            {
32
                $this->errstr = $errstr;
33
            }
34
35
            public function toString(): string
36
            {
37
                throw new \Exception($this->errstr);
38
                return '';
39
            }
40
        };
41
        $this->assertEquals('', strval($message));
42
        $this->assertCount(1, $this->errors);
43
        $this->assertEquals(E_USER_ERROR, $this->errors[0]['errno']);
44
        $this->assertStringContainsString($errstr, $this->errors[0]['errstr']);
45
    }
46
47
    private $errors;
48
49
    public function errorHandler(int $errno, string $errstr, string $errfile, int $errline, array $errcontext): bool
50
    {
51
        $this->errors[] = [
52
            'errno' => $errno,
53
            'errstr' => $errstr,
54
        ];
55
56
        return true;
57
    }
58
}
59