-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathTestCase.php
178 lines (140 loc) · 4.92 KB
/
TestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace Tests;
use Illuminate\Filesystem\Filesystem;
use Statamic\Facades\Path;
use Statamic\Migrator\Concerns\PreparesPathFolder;
use Statamic\Migrator\YAML;
class TestCase extends \Orchestra\Testbench\TestCase
{
use PreparesPathFolder;
protected $siteFixture = 'site';
protected function getPackageProviders($app)
{
return [
\Statamic\Providers\StatamicServiceProvider::class,
\Statamic\Migrator\ServiceProvider::class,
];
}
protected function getPackageAliases($app)
{
return ['Statamic' => 'Statamic\Statamic'];
}
protected function setUp(): void
{
parent::setUp();
$this->files = app(Filesystem::class);
$this->getPaths()->each(function ($path) {
$this->deleteFolder($path);
$this->prepareFolder($path);
});
$this->files->copyDirectory(__DIR__.'/Fixtures/'.$this->siteFixture, base_path('site'));
if (! $this->files->exists(config_path('filesystems-original.php'))) {
$this->files->copy(config_path('filesystems.php'), config_path('filesystems-original.php'));
}
$this->restoreFilesystemConfig();
$this->restoreStatamicConfigs();
}
protected function tearDown(): void
{
$this->getPaths()->each(function ($path) {
$this->deleteFolder($path);
});
$this->restoreFilesystemConfig();
$this->restoreStatamicConfigs();
parent::tearDown();
}
protected function getPaths()
{
if (method_exists($this, 'path')) {
$paths = collect($this->path());
} elseif (method_exists($this, 'paths')) {
$paths = collect($this->paths());
} else {
$paths = collect();
}
$paths->push(base_path('site'));
return $paths->map(function ($path) {
return Path::tidy($path);
});
}
protected function restoreFilesystemConfig()
{
$this->files->copy(config_path('filesystems-original.php'), config_path('filesystems.php'));
}
protected function restoreStatamicConfigs()
{
$this->files->copyDirectory(__DIR__.'/../vendor/statamic/cms/config', config_path('statamic'));
}
protected function assertParsedYamlEquals($expected, $path)
{
return $this->assertEquals($expected, YAML::parse($this->files->get($path)));
}
protected function assertParsedYamlContains($expected, $path)
{
$parsed = collect(YAML::parse($this->files->get($path)));
if (! is_array($expected)) {
return $this->assertContains($expected, $parsed->all());
}
$key = key($expected);
return $this->assertEquals($expected[$key], $parsed[$key]);
}
protected function assertParsedYamlHasKey($key, $path)
{
$parsed = collect(YAML::parse($this->files->get($path)));
return $this->assertTrue($parsed->has($key));
}
protected function assertParsedYamlNotHasKey($key, $path)
{
$parsed = collect(YAML::parse($this->files->get($path)));
return $this->assertFalse($parsed->has($key));
}
protected function assertContainsIgnoringLineEndings($expected, $actual)
{
$actual = str_replace("\r\n", "\n", $actual);
$this->assertStringContainsStringWithNormalizedLineEndings($expected, $actual);
}
protected function assertFileHasContent($expected, $path)
{
$this->assertFileExists($path);
$this->assertStringContainsStringWithNormalizedLineEndings($expected, $this->files->get($path));
}
protected function sitePath($append = null)
{
return collect([base_path('site'), $append])->filter()->implode('/');
}
protected static function normalizeMultilineString($string)
{
return str_replace("\r\n", "\n", $string);
}
/**
* Normalize line endings before performing assertion in windows.
*/
public static function assertStringContainsStringWithNormalizedLineEndings($needle, $haystack, $message = ''): void
{
static::assertStringContainsString(
static::normalizeMultilineString($needle),
static::normalizeMultilineString($haystack),
$message
);
}
/**
* Normalize line endings before performing assertion in windows.
*/
public static function assertSameWithNormalizedLineEndings($needle, $haystack, $message = ''): void
{
static::assertSame(
static::normalizeMultilineString($needle),
static::normalizeMultilineString($haystack),
$message
);
}
/**
* @deprecated
*/
public static function assertFileNotExists(string $filename, string $message = ''): void
{
method_exists(static::class, 'assertFileDoesNotExist')
? static::assertFileDoesNotExist($filename, $message)
: parent::assertFileNotExists($filename, $message);
}
}