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
|
|
|
* @var ListenerProviderInterface |
66
|
|
|
*/ |
67
|
|
|
protected $listenerProvider; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* setUp |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
*/ |
74
|
|
|
protected function setUp(): void |
75
|
|
|
{ |
76
|
|
|
parent::setUp(); |
77
|
|
|
|
78
|
|
|
$config = require Builder::path('tests'); |
79
|
|
|
|
80
|
|
|
$this->container = new Container($config); |
81
|
|
|
|
82
|
|
|
$this->aliases = $this->container->get(Aliases::class); |
83
|
|
|
$this->assetManager = $this->container->get(AssetManager::class); |
84
|
|
|
$this->eventDispatcher = $this->container->get(EventDispatcherInterface::class); |
85
|
|
|
$this->listenerProvider = $this->container->get(ListenerProviderInterface::class); |
86
|
|
|
$this->logger = $this->container->get(LoggerInterface::class); |
87
|
|
|
$this->theme = $this->container->get(Theme::class); |
88
|
|
|
$this->webView = $this->container->get(WebView::class); |
89
|
|
|
$this->webView->setAssetManager($this->assetManager); |
90
|
|
|
$this->widget = $this->container->get(Widget::class); |
91
|
|
|
|
92
|
|
|
$this->removeAssets('@basePath'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* tearDown |
97
|
|
|
* |
98
|
|
|
* @return void |
99
|
|
|
*/ |
100
|
|
|
protected function tearDown(): void |
101
|
|
|
{ |
102
|
|
|
$this->container = null; |
103
|
|
|
parent::tearDown(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Asserting two strings equality ignoring line endings. |
108
|
|
|
* @param string $expected |
109
|
|
|
* @param string $actual |
110
|
|
|
* @param string $message |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
protected function assertEqualsWithoutLE(string $expected, string $actual, string $message = ''): void |
115
|
|
|
{ |
116
|
|
|
$expected = str_replace("\r\n", "\n", $expected); |
117
|
|
|
$actual = str_replace("\r\n", "\n", $actual); |
118
|
|
|
|
119
|
|
|
$this->assertEquals($expected, $actual, $message); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Asserting same ignoring slash. |
124
|
|
|
* |
125
|
|
|
* @param string $expected |
126
|
|
|
* @param string $actual |
127
|
|
|
* |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
|
|
protected function assertSameIgnoringSlash(string $expected, string $actual): void |
131
|
|
|
{ |
132
|
|
|
$expected = str_replace(['/', '\\'], '/', $expected); |
133
|
|
|
$actual = str_replace(['/', '\\'], '/', $actual); |
134
|
|
|
$this->assertSame($expected, $actual); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Create view tests. |
139
|
|
|
* |
140
|
|
|
* @param string $basePath |
141
|
|
|
* @param Theme $theme |
142
|
|
|
* |
143
|
|
|
* @return View |
144
|
|
|
*/ |
145
|
|
|
protected function createView($basePath, Theme $theme = null): View |
146
|
|
|
{ |
147
|
|
|
return new View($basePath, $theme ?: new 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->assertDirectoryExists($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
|
|
|
return FileHelper::unlink($file); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
|