Passed
Pull Request — master (#83)
by Wilmer
28:01 queued 13:07
created

TestCase   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 28
c 0
b 0
f 0
dl 0
loc 122
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\View\Tests;
6
7
use Yiisoft\Composer\Config\Builder;
8
use PHPUnit\Framework\TestCase as BaseTestCase;
9
use Psr\Container\ContainerInterface;
10
use Psr\EventDispatcher\EventDispatcherInterface;
11
use Psr\EventDispatcher\ListenerProviderInterface;
12
use Psr\Log\LoggerInterface;
13
use Yiisoft\Aliases\Aliases;
14
use Yiisoft\Di\Container;
15
use Yiisoft\Files\FileHelper;
16
use Yiisoft\View\Theme;
17
use Yiisoft\View\View;
18
use Yiisoft\View\WebView;
19
20
abstract class TestCase extends BaseTestCase
21
{
22
    /**
23
     * @var Aliases $aliases
24
     */
25
    protected $aliases;
26
27
    /**
28
     * @var ContainerInterface $container
29
     */
30
    private $container;
31
32
    /**
33
     * @var EventDispatcherInterface $eventDispatcher
34
     */
35
    protected $eventDispatcher;
36
37
    /**
38
     * @var LoggerInterface $logger
39
     */
40
    protected $logger;
41
42
    /**
43
     * @var Theme $theme
44
     */
45
    protected $theme;
46
47
    /**
48
     * @var WebView $webView
49
     */
50
    protected $webView;
51
52
    /**
53
     * @var ListenerProviderInterface
54
     */
55
    protected $listenerProvider;
56
57
    /**
58
     * setUp
59
     *
60
     * @return void
61
     */
62
    protected function setUp(): void
63
    {
64
        parent::setUp();
65
66
        $config = require Builder::path('tests');
67
68
        $this->container = new Container($config);
69
70
        $this->aliases = $this->container->get(Aliases::class);
71
        $this->eventDispatcher = $this->container->get(EventDispatcherInterface::class);
72
        $this->listenerProvider = $this->container->get(ListenerProviderInterface::class);
73
        $this->logger = $this->container->get(LoggerInterface::class);
74
        $this->webView = $this->container->get(WebView::class);
75
    }
76
77
    protected function getContainer(): ContainerInterface
78
    {
79
        return $this->container;
80
    }
81
82
    /**
83
     * tearDown
84
     *
85
     * @return void
86
     */
87
    protected function tearDown(): void
88
    {
89
        $this->container = null;
90
        parent::tearDown();
91
    }
92
93
    /**
94
     * Asserting two strings equality ignoring line endings.
95
     * @param string $expected
96
     * @param string $actual
97
     * @param string $message
98
     *
99
     * @return void
100
     */
101
    protected function assertEqualsWithoutLE(string $expected, string $actual, string $message = ''): void
102
    {
103
        $expected = str_replace("\r\n", "\n", $expected);
104
        $actual = str_replace("\r\n", "\n", $actual);
105
106
        $this->assertEquals($expected, $actual, $message);
107
    }
108
109
    /**
110
     * Asserting same ignoring slash.
111
     *
112
     * @param string $expected
113
     * @param string $actual
114
     *
115
     * @return void
116
     */
117
    protected function assertSameIgnoringSlash(string $expected, string $actual): void
118
    {
119
        $expected = str_replace(['/', '\\'], '/', $expected);
120
        $actual = str_replace(['/', '\\'], '/', $actual);
121
        $this->assertSame($expected, $actual);
122
    }
123
124
    /**
125
     * Create view tests.
126
     *
127
     * @param string $basePath
128
     * @param Theme  $theme
129
     *
130
     * @return View
131
     */
132
    protected function createView($basePath, Theme $theme = null): View
133
    {
134
        return new View($basePath, $theme ?: new Theme(), $this->eventDispatcher, $this->logger);
135
    }
136
137
    protected function touch(string $path): void
138
    {
139
        FileHelper::createDirectory(dirname($path));
140
141
        touch($path);
142
    }
143
}
144