Skip to content

Commit c2d02f8

Browse files
author
Holger Lösken
committed
Imrpove testing
1 parent 1ba315c commit c2d02f8

File tree

7 files changed

+45
-59
lines changed

7 files changed

+45
-59
lines changed

config/self-update.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
*/
6969

7070
'exclude_folders' => [
71+
'__MACOSX',
7172
'node_modules',
7273
'bootstrap/cache',
7374
'bower',

src/Models/UpdateExecutor.php

Lines changed: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,16 @@ public function run(Release $release): bool
7878

7979
private function moveFiles(string $folder): void
8080
{
81-
$files = File::allFiles($folder, true);
81+
$files = (new Finder())->in($folder)
82+
->exclude(config('self-update.exclude_folders'))
83+
->ignoreDotFiles(false)
84+
->files();
8285

8386
collect($files)->each(function (SplFileInfo $file) {
8487
if ($file->getRealPath()) {
85-
File::copy($file->getRealPath(), $this->targetFile($file));
88+
File::copy(
89+
$file->getRealPath(), Str::finish($this->basePath, DIRECTORY_SEPARATOR) . $file->getFilename()
90+
);
8691
}
8792
});
8893
}
@@ -91,50 +96,20 @@ private function moveFolders(string $folder): void
9196
{
9297
$directories = (new Finder())->in($folder)->exclude(config('self-update.exclude_folders'))->directories();
9398

94-
collect($directories->sort(function (SplFileInfo $a, SplFileInfo $b) {
99+
$sorted = collect($directories->sort(function (SplFileInfo $a, SplFileInfo $b) {
95100
return strlen($b->getRealpath()) - strlen($a->getRealpath());
96-
}))->each(function (SplFileInfo $directory) {
101+
}));
102+
103+
$sorted->each(function (SplFileInfo $directory) {
104+
97105
if (! dirsIntersect(File::directories($directory->getRealPath()), config('self-update.exclude_folders'))) {
98-
File::copyDirectory($directory->getRealPath(), $this->targetFolder($directory));
106+
File::copyDirectory(
107+
$directory->getRealPath(),
108+
Str::finish($this->basePath, DIRECTORY_SEPARATOR) . Str::finish($directory->getRelativePath(), DIRECTORY_SEPARATOR) . $directory->getBasename()
109+
);
99110
}
100111

101112
File::deleteDirectory($directory->getRealPath());
102113
});
103114
}
104-
105-
/**
106-
* Detect if target path should be project root. Probably not if only unit tests are run, because there is no
107-
* project root then.
108-
*
109-
* @param SplFileInfo $file
110-
*
111-
* @return string
112-
*/
113-
private function targetFile(SplFileInfo $file): string
114-
{
115-
if (empty($this->basePath)) {
116-
return base_path($file->getFilename());
117-
}
118-
119-
return $this->basePath.$file->getFilename();
120-
}
121-
122-
/**
123-
* Detect if target path should be project root. Probably not if only unit tests are run, because there is no
124-
* project root then.
125-
*
126-
* @param SplFileInfo $directory
127-
*
128-
* @return string
129-
*/
130-
private function targetFolder(SplFileInfo $directory): string
131-
{
132-
if (empty($this->basePath)) {
133-
return Str::finish(base_path($directory->getRealPath()), DIRECTORY_SEPARATOR).$directory->getBasename();
134-
}
135-
136-
return $this->basePath
137-
.Str::finish($directory->getRealPath(), DIRECTORY_SEPARATOR)
138-
.$directory->getBasename();
139-
}
140115
}

tests/Data/release-1.2.zip

3.84 KB
Binary file not shown.

tests/Models/UpdateExecutorTest.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
use Codedge\Updater\Models\Release;
66
use Codedge\Updater\Models\UpdateExecutor;
7+
use Codedge\Updater\SourceRepositoryTypes\GithubRepositoryTypes\GithubTagType;
78
use Codedge\Updater\Tests\TestCase;
9+
use GuzzleHttp\Client;
810
use Illuminate\Support\Facades\File;
911
use org\bovigo\vfs\vfsStream;
1012

@@ -34,17 +36,22 @@ protected function setUp(): void
3436
public function it_can_run_successfully(): void
3537
{
3638
$dir = (string) config('self-update.repository_types.github.download_path') . '/update-dir';
37-
File::makeDirectory($dir, 0775, false, true);
39+
File::makeDirectory($dir, 0775, true, true);
3840

39-
$this->release->setStoragePath((string) config('self-update.repository_types.github.download_path'))
40-
->setRelease('release-1.0.zip')
41+
$client = $this->getMockedClient([
42+
$this->getResponse200ZipFile(),
43+
]);
44+
45+
$this->release->setVersion('release-1.2')
46+
->setStoragePath((string) config('self-update.repository_types.github.download_path'))
47+
->setRelease('release-1.2.zip')
4148
->updateStoragePath()
42-
->setDownloadUrl('some-local-file')
43-
->download($this->getMockedDownloadZipFileClient());
49+
->setDownloadUrl('some/url/')
50+
->download($client);
4451
$this->release->extract();
4552

46-
$updateExecutor = new UpdateExecutor();
47-
$this->assertTrue($updateExecutor->setBasePath($dir)->run($this->release));
53+
$updateExecutor = (new UpdateExecutor())->setBasePath($dir);
54+
$this->assertTrue($updateExecutor->run($this->release));
4855

4956
}
5057

tests/SourceRepositoryTypes/GithubRepositoryTypeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function it_can_run_update(): void
6060
->setRelease('release-1.0.zip')
6161
->updateStoragePath()
6262
->setDownloadUrl('some-local-file')
63-
->download($this->getMockedDownloadZipFileClient());
63+
->download($this->getMockedClient([ $this->getResponse200ZipFile() ]));
6464
$release->extract();
6565

6666
Event::fake();

tests/SourceRepositoryTypes/HttpRepositoryTypeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function it_can_run_update(): void
2727
->setRelease('release-1.0.zip')
2828
->updateStoragePath()
2929
->setDownloadUrl('some-local-file')
30-
->download($this->getMockedDownloadZipFileClient());
30+
->download($this->getMockedClient([ $this->getResponse200ZipFile() ]));
3131
$release->extract();
3232

3333
$this->assertTrue($http->update($release));

tests/TestCase.php

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,17 @@ protected function getEnvironmentSetUp($app)
6060
'private_access_token' => '',
6161
],
6262
],
63-
'exclude_folders' => [],
63+
'exclude_folders' => [
64+
'__MACOSX',
65+
'node_modules',
66+
'bootstrap/cache',
67+
'bower',
68+
'storage/app',
69+
'storage/framework',
70+
'storage/logs',
71+
'storage/self-update',
72+
'vendor',
73+
],
6474
'log_events' => false,
6575
'mail_to' => [
6676
'address' => '',
@@ -114,13 +124,6 @@ protected function getMockedClient($responses): Client
114124
return new Client(['handler' => $handler]);
115125
}
116126

117-
protected function getMockedDownloadZipFileClient(): Client
118-
{
119-
$handler = HandlerStack::create(new MockHandler([ $this->getResponse200ZipFile() ]));
120-
121-
return new Client(['handler' => $handler]);
122-
}
123-
124127
protected function getResponse200Type(string $type): Response
125128
{
126129
return new Response(
@@ -134,7 +137,7 @@ protected function getResponse200ZipFile(): Response
134137
200,
135138
[
136139
'Content-Type' => 'application/zip',
137-
'Content-Disposition' => 'attachment; filename="zip_file.zip"',
140+
'Content-Disposition' => 'attachment; filename="release-1.2.zip"',
138141
],
139142
fopen(__DIR__ . '/Data/release-1.2.zip', 'r')
140143
);

0 commit comments

Comments
 (0)