Completed
Pull Request — master (#52)
by Wilmer
01:37
created

TestCase::createWebView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
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\EventDispatcher\ListenerProviderInterface;
11
use Psr\Log\LoggerInterface;
12
use Yiisoft\Aliases\Aliases;
13
use Yiisoft\Asset\AssetBundle;
14
use Yiisoft\Asset\AssetManager;
15
use Yiisoft\Files\FileHelper;
16
use Yiisoft\Di\Container;
17
use Yiisoft\View\Theme;
18
use Yiisoft\View\View;
19
use Yiisoft\View\WebView;
20
use Yiisoft\Widget\Widget;
21
22
abstract class TestCase extends BaseTestCase
23
{
24
    /**
25
     * @var Aliases $aliases
26
     */
27
    protected $aliases;
28
29
    /**
30
     * @var AssetManager $assetManager
31
     */
32
    protected $assetManager;
33
34
    /**
35
     * @var ContainerInterface $container
36
     */
37
    private $container;
38
39
    /**
40
     * @var EventDispatcherInterface $eventDispatcher
41
     */
42
    protected $eventDispatcher;
43
44
    /**
45
     * @var LoggerInterface $logger
46
     */
47
    protected $logger;
48
49
    /**
50
     * @var Theme $theme
51
     */
52
    protected $theme;
53
54
    /**
55
     * @var WebView $webView
56
     */
57
    protected $webView;
58
59
    /**
60
     * @var Widget $widget
61
     */
62
    protected $widget;
63
64
    /**
65
     * setUp
66
     *
67
     * @return void
68
     */
69
    protected function setUp(): void
70
    {
71
        parent::setUp();
72
73
        $config = require Builder::path('tests');
74
75
        $this->container = new Container($config);
76
77
        $this->aliases = $this->container->get(Aliases::class);
78
        $this->assetManager = $this->container->get(AssetManager::class);
79
        $this->eventDispatcher = $this->container->get(EventDispatcherInterface::class);
80
        $this->listenerProvider = $this->container->get(ListenerProviderInterface::class);
0 ignored issues
show
Bug Best Practice introduced by
The property listenerProvider does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
81
        $this->logger = $this->container->get(LoggerInterface::class);
82
        $this->theme = $this->container->get(Theme::class);
83
        $this->webView = $this->container->get(WebView::class);
84
        $this->webView->setAssetManager($this->assetManager);
85
        $this->widget = $this->container->get(Widget::class);
86
87
        $this->removeAssets('@basePath');
88
    }
89
90
    /**
91
     * tearDown
92
     *
93
     * @return void
94
     */
95
    protected function tearDown(): void
96
    {
97
        $this->container = null;
98
        parent::tearDown();
99
    }
100
101
    /**
102
     * Asserting two strings equality ignoring line endings.
103
     * @param string $expected
104
     * @param string $actual
105
     * @param string $message
106
     *
107
     * @return void
108
     */
109
    protected function assertEqualsWithoutLE(string $expected, string $actual, string $message = ''): void
110
    {
111
        $expected = str_replace("\r\n", "\n", $expected);
112
        $actual = str_replace("\r\n", "\n", $actual);
113
114
        $this->assertEquals($expected, $actual, $message);
115
    }
116
117
    /**
118
     * Asserting same ignoring slash.
119
     *
120
     * @param string $expected
121
     * @param string $actual
122
     *
123
     * @return void
124
     */
125
    protected function assertSameIgnoringSlash(string $expected, string $actual): void
126
    {
127
        $expected = str_replace(['/', '\\'], '/', $expected);
128
        $actual = str_replace(['/', '\\'], '/', $actual);
129
        $this->assertSame($expected, $actual);
130
    }
131
132
    /**
133
     * Create view tests.
134
     *
135
     * @param string $basePath
136
     * @param Theme  $theme
137
     *
138
     * @return View
139
     */
140
    protected function createView($basePath, Theme $theme = null): View
141
    {
142
        return new View($basePath, $theme ?: new Theme(), $this->eventDispatcher, $this->logger);
143
    }
144
145
    public function touch(string $path): void
146
    {
147
        FileHelper::createDirectory(dirname($path));
148
149
        touch($path);
150
    }
151
152
    protected function removeAssets(string $basePath): void
153
    {
154
        $handle = opendir($dir = $this->aliases->get($basePath));
155
156
        if ($handle === false) {
157
            throw new \Exception("Unable to open directory: $dir");
158
        }
159
160
        while (($file = readdir($handle)) !== false) {
161
            if ($file === '.' || $file === '..' || $file === '.gitignore') {
162
                continue;
163
            }
164
            $path = $dir.DIRECTORY_SEPARATOR.$file;
165
            if (is_dir($path)) {
166
                FileHelper::removeDirectory($path);
167
            } else {
168
                FileHelper::unlink($path);
169
            }
170
        }
171
172
        closedir($handle);
173
    }
174
175
    /**
176
     * Verify sources publish files assetbundle.
177
     *
178
     * @param string $type
179
     * @param AssetBundle $bundle
180
     *
181
     * @return void
182
     */
183
    protected function sourcesPublishVerifyFiles(string $type, AssetBundle $bundle): void
184
    {
185
        foreach ($bundle->$type as $filename) {
186
            $publishedFile = $bundle->basePath . DIRECTORY_SEPARATOR . $filename;
187
            $sourceFile = $this->aliases->get($bundle->sourcePath) . DIRECTORY_SEPARATOR . $filename;
188
189
            $this->assertFileExists($publishedFile);
190
            $this->assertFileEquals($publishedFile, $sourceFile);
191
        }
192
193
        $this->assertDirectoryExists($bundle->basePath . DIRECTORY_SEPARATOR . $type);
194
    }
195
196
    /**
197
     * Properly removes symlinked directory under Windows, MacOS and Linux.
198
     *
199
     * @param string $file path to symlink
200
     *
201
     * @return bool
202
     */
203
    protected function unlink(string $file): bool
204
    {
205
        return FileHelper::unlink($file);
206
    }
207
}
208