Passed
Pull Request — master (#50)
by Alexander
02:52
created

TestCase   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
eloc 50
c 1
b 0
f 0
dl 0
loc 190
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A assertSameIgnoringSlash() 0 5 1
A createView() 0 3 2
A sourcesPublishVerifyFiles() 0 11 2
A assertEqualsWithoutLE() 0 6 1
A touch() 0 5 1
A setUp() 0 17 1
A createWebView() 0 3 1
A unlink() 0 3 1
A tearDown() 0 4 1
B removeAssets() 0 21 7
1
<?php
2
declare(strict_types = 1);
3
4
namespace Yiisoft\Tests;
5
6
use hiqdev\composer\config\Builder;
7
use PHPUnit\Framework\TestCase as BaseTestCase;
8
use Psr\Container\ContainerInterface;
9
use Psr\EventDispatcher\EventDispatcherInterface;
10
use Psr\Log\LoggerInterface;
11
use Yiisoft\Aliases\Aliases;
12
use Yiisoft\Asset\AssetBundle;
13
use Yiisoft\Asset\AssetManager;
14
use Yiisoft\Files\FileHelper;
15
use Yiisoft\Di\Container;
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 AssetManager $assetManager
29
     */
30
    protected $assetManager;
31
32
    /**
33
     * @var ContainerInterface $container
34
     */
35
    private $container;
36
37
    /**
38
     * @var EventDispatcherInterface $eventDispatcher
39
     */
40
    protected $eventDispatcher;
41
42
    /**
43
     * @var LoggerInterface $logger
44
     */
45
    protected $logger;
46
47
    /**
48
     * @var Theme $theme
49
     */
50
    protected $theme;
51
52
    /**
53
     * @var WebView $webView
54
     */
55
    protected $webView;
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->assetManager = $this->container->get(AssetManager::class);
72
        $this->eventDispatcher = $this->container->get(EventDispatcherInterface::class);
73
        $this->logger = $this->container->get(LoggerInterface::class);
74
        $this->theme = $this->container->get(Theme::class);
75
        $this->webView = $this->createWebView($this->aliases->get('@view'));
76
        $this->webView->setAssetManager($this->assetManager);
77
78
        $this->removeAssets('@basePath');
79
    }
80
81
    /**
82
     * tearDown
83
     *
84
     * @return void
85
     */
86
    protected function tearDown(): void
87
    {
88
        $this->container = null;
89
        parent::tearDown();
90
    }
91
92
    /**
93
     * Asserting two strings equality ignoring line endings.
94
     * @param string $expected
95
     * @param string $actual
96
     * @param string $message
97
     *
98
     * @return void
99
     */
100
    protected function assertEqualsWithoutLE(string $expected, string $actual, string $message = ''): void
101
    {
102
        $expected = str_replace("\r\n", "\n", $expected);
103
        $actual = str_replace("\r\n", "\n", $actual);
104
105
        $this->assertEquals($expected, $actual, $message);
106
    }
107
108
    /**
109
     * Asserting same ignoring slash.
110
     *
111
     * @param string $expected
112
     * @param string $actual
113
     *
114
     * @return void
115
     */
116
    protected function assertSameIgnoringSlash(string $expected, string $actual): void
117
    {
118
        $expected = str_replace(['/', '\\'], '/', $expected);
119
        $actual = str_replace(['/', '\\'], '/', $actual);
120
        $this->assertSame($expected, $actual);
121
    }
122
123
    /**
124
     * Create view tests.
125
     *
126
     * @param string $basePath
127
     * @param Theme  $theme
128
     *
129
     * @return View
130
     */
131
    protected function createView($basePath, Theme $theme = null): View
132
    {
133
        return new View($basePath, $theme ?: new Theme(), $this->eventDispatcher, $this->logger);
134
    }
135
136
    /**
137
     * Create webview tests.
138
     *
139
     * @param string $basePath
140
     * @param Theme  $theme
141
     *
142
     * @return View
143
     */
144
    protected function createWebView(string $basePath): WebView
145
    {
146
        return new WebView($basePath, $this->theme, $this->eventDispatcher, $this->logger);
147
    }
148
149
    public function touch(string $path): void
150
    {
151
        FileHelper::createDirectory(dirname($path));
152
153
        touch($path);
154
    }
155
156
    protected function removeAssets(string $basePath): void
157
    {
158
        $handle = opendir($dir = $this->aliases->get($basePath));
159
160
        if ($handle === false) {
161
            throw new \Exception("Unable to open directory: $dir");
162
        }
163
164
        while (($file = readdir($handle)) !== false) {
165
            if ($file === '.' || $file === '..' || $file === '.gitignore') {
166
                continue;
167
            }
168
            $path = $dir.DIRECTORY_SEPARATOR.$file;
169
            if (is_dir($path)) {
170
                FileHelper::removeDirectory($path);
171
            } else {
172
                FileHelper::unlink($path);
173
            }
174
        }
175
176
        closedir($handle);
177
    }
178
179
    /**
180
     * Verify sources publish files assetbundle.
181
     *
182
     * @param string $type
183
     * @param AssetBundle $bundle
184
     *
185
     * @return void
186
     */
187
    protected function sourcesPublishVerifyFiles(string $type, AssetBundle $bundle): void
188
    {
189
        foreach ($bundle->$type as $filename) {
190
            $publishedFile = $bundle->basePath . DIRECTORY_SEPARATOR . $filename;
191
            $sourceFile = $this->aliases->get($bundle->sourcePath) . DIRECTORY_SEPARATOR . $filename;
192
193
            $this->assertFileExists($publishedFile);
194
            $this->assertFileEquals($publishedFile, $sourceFile);
195
        }
196
197
        $this->assertDirectoryExists($bundle->basePath . DIRECTORY_SEPARATOR . $type);
198
    }
199
200
    /**
201
     * Properly removes symlinked directory under Windows, MacOS and Linux.
202
     *
203
     * @param string $file path to symlink
204
     *
205
     * @return bool
206
     */
207
    protected function unlink(string $file): bool
208
    {
209
        return FileHelper::unlink($file);
210
    }
211
}
212