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

TestCase   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
eloc 24
c 2
b 0
f 0
dl 0
loc 83
rs 10
1
<?php
2
3
namespace Yiisoft\Mailer\Tests;
4
5
use PHPUnit\Framework\TestCase as BaseTestCase;
6
use Psr\Container\ContainerInterface;
7
use Yiisoft\Mailer\MailerInterface;
8
use Yiisoft\Mailer\MessageInterface;
9
use Yiisoft\Di\Container;
10
11
abstract class TestCase extends BaseTestCase
12
{
13
    /**
14
     * @var ContainerInterface $container
15
     */
16
    private $container;
17
18
    protected function setUp(): void
19
    {
20
        parent::setUp();
21
22
        $config = require __DIR__ . '/config.php';
23
        $this->container = new Container($config);
24
    }
25
26
    protected function tearDown(): void
27
    {
28
        $this->container = null;
29
30
        parent::tearDown();
31
    }
32
33
    protected function get($id)
34
    {
35
        return $this->container->get($id);
36
    }
37
38
    /**
39
     * @return TestMailer mailer instance.
40
     */
41
    protected function getMailer(): TestMailer
42
    {
43
        return $this->get(MailerInterface::class);
44
    }
45
46
    /**
47
     * Creates a new message instance.
48
     * @return MessageInterface
49
     */
50
    protected function createMessage(string $subject = 'foo', string $from = '[email protected]', string $to = '[email protected]'): MessageInterface
51
    {
52
        return (new TestMessage())
53
            ->setSubject($subject)
54
            ->setFrom($from)
55
            ->setTo($to);
56
    }
57
58
    /**
59
     * Asserting two strings equality ignoring line endings.
60
     * @param string $expected
61
     * @param string $actual
62
     * @param string $message
63
     */
64
    protected function assertEqualsWithoutLE(string $expected, string $actual, string $message = ''): void
65
    {
66
        $expected = str_replace("\r\n", "\n", $expected);
67
        $actual = str_replace("\r\n", "\n", $actual);
68
        $this->assertEquals($expected, $actual, $message);
69
    }
70
71
    /**
72
     * @return string test file path.
73
     */
74
    protected function getTestFilePath(): string
75
    {
76
        return sys_get_temp_dir() . DIRECTORY_SEPARATOR . basename(str_replace('\\', '_', get_class($this))) . '_' . getmypid();
77
    }
78
79
    protected function saveFile(string $filename, string $data): void
80
    {
81
        $path = dirname($filename);
82
        if (!is_dir($path)) {
83
            mkdir($path, 0777, true);
84
        }
85
86
        file_put_contents($filename, $data);
87
    }
88
89
    protected function getObjectPropertyValue($obj, string $name)
90
    {
91
        $property = new \ReflectionProperty(get_class($obj), $name);
92
        $property->setAccessible(true);
93
        return $property->getValue($obj);
94
    }
95
}
96