|
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
|
|
|
|
|
21
|
|
|
abstract class TestCase extends BaseTestCase |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var Aliases $aliases |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $aliases; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var AssetManager $assetManager |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $assetManager; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var ContainerInterface $container |
|
35
|
|
|
*/ |
|
36
|
|
|
private $container; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var EventDispatcherInterface $eventDispatcher |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $eventDispatcher; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var LoggerInterface $logger |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $logger; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var Theme $theme |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $theme; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var WebView $webView |
|
55
|
|
|
*/ |
|
56
|
|
|
protected $webView; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* setUp |
|
60
|
|
|
* |
|
61
|
|
|
* @return void |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function setUp(): void |
|
64
|
|
|
{ |
|
65
|
|
|
parent::setUp(); |
|
66
|
|
|
|
|
67
|
|
|
$config = require Builder::path('tests'); |
|
68
|
|
|
|
|
69
|
|
|
$this->container = new Container($config); |
|
70
|
|
|
|
|
71
|
|
|
$this->aliases = $this->container->get(Aliases::class); |
|
72
|
|
|
$this->assetManager = $this->container->get(AssetManager::class); |
|
73
|
|
|
$this->eventDispatcher = $this->container->get(EventDispatcherInterface::class); |
|
74
|
|
|
$this->logger = $this->container->get(LoggerInterface::class); |
|
75
|
|
|
$this->theme = $this->container->get(Theme::class); |
|
76
|
|
|
$this->webView = $this->createWebView($this->aliases->get('@view')); |
|
77
|
|
|
$this->webView->setAssetManager($this->assetManager); |
|
78
|
|
|
|
|
79
|
|
|
$this->removeAssets('@basePath'); |
|
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
|
|
|
/** |
|
138
|
|
|
* Create webview tests. |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $basePath |
|
141
|
|
|
* @param Theme $theme |
|
142
|
|
|
* |
|
143
|
|
|
* @return View |
|
144
|
|
|
*/ |
|
145
|
|
|
protected function createWebView(string $basePath): WebView |
|
146
|
|
|
{ |
|
147
|
|
|
return new WebView($basePath, $this->theme, $this->eventDispatcher, $this->logger); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function touch(string $path): void |
|
151
|
|
|
{ |
|
152
|
|
|
FileHelper::createDirectory(dirname($path)); |
|
153
|
|
|
|
|
154
|
|
|
touch($path); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
protected function removeAssets(string $basePath): void |
|
158
|
|
|
{ |
|
159
|
|
|
$handle = opendir($dir = $this->aliases->get($basePath)); |
|
160
|
|
|
|
|
161
|
|
|
if ($handle === false) { |
|
162
|
|
|
throw new \Exception("Unable to open directory: $dir"); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
while (($file = readdir($handle)) !== false) { |
|
166
|
|
|
if ($file === '.' || $file === '..' || $file === '.gitignore') { |
|
167
|
|
|
continue; |
|
168
|
|
|
} |
|
169
|
|
|
$path = $dir.DIRECTORY_SEPARATOR.$file; |
|
170
|
|
|
if (is_dir($path)) { |
|
171
|
|
|
FileHelper::removeDirectory($path); |
|
172
|
|
|
} else { |
|
173
|
|
|
FileHelper::unlink($path); |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
closedir($handle); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Verify sources publish files assetbundle. |
|
182
|
|
|
* |
|
183
|
|
|
* @param string $type |
|
184
|
|
|
* @param AssetBundle $bundle |
|
185
|
|
|
* |
|
186
|
|
|
* @return void |
|
187
|
|
|
*/ |
|
188
|
|
|
protected function sourcesPublishVerifyFiles(string $type, AssetBundle $bundle): void |
|
189
|
|
|
{ |
|
190
|
|
|
foreach ($bundle->$type as $filename) { |
|
191
|
|
|
$publishedFile = $bundle->basePath . DIRECTORY_SEPARATOR . $filename; |
|
192
|
|
|
$sourceFile = $this->aliases->get($bundle->sourcePath) . DIRECTORY_SEPARATOR . $filename; |
|
193
|
|
|
|
|
194
|
|
|
$this->assertFileExists($publishedFile); |
|
195
|
|
|
$this->assertFileEquals($publishedFile, $sourceFile); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
$this->assertTrue(is_dir($bundle->basePath . DIRECTORY_SEPARATOR . $type)); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Properly removes symlinked directory under Windows, MacOS and Linux. |
|
203
|
|
|
* |
|
204
|
|
|
* @param string $file path to symlink |
|
205
|
|
|
* |
|
206
|
|
|
* @return bool |
|
207
|
|
|
*/ |
|
208
|
|
|
protected function unlink(string $file): bool |
|
209
|
|
|
{ |
|
210
|
|
|
if (is_dir($file) && DIRECTORY_SEPARATOR === '\\') { |
|
211
|
|
|
return rmdir($file); |
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
return unlink($file); |
|
215
|
|
|
} |
|
216
|
|
|
} |
|
217
|
|
|
|