forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExportPackServiceTest.php
More file actions
164 lines (138 loc) · 6.2 KB
/
ExportPackServiceTest.php
File metadata and controls
164 lines (138 loc) · 6.2 KB
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
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
namespace Tests\Unit\Services\Packs;
use Illuminate\Contracts\Filesystem\Factory;
use Mockery as m;
use phpmock\phpunit\PHPMock;
use Pterodactyl\Contracts\Repository\PackRepositoryInterface;
use Pterodactyl\Models\Pack;
use Pterodactyl\Services\Packs\ExportPackService;
use Tests\TestCase;
use ZipArchive;
class ExportPackServiceTest extends TestCase
{
use PHPMock;
/**
* @var \ZipArchive
*/
protected $archive;
/**
* @var \Pterodactyl\Contracts\Repository\PackRepositoryInterface
*/
protected $repository;
/**
* @var \Pterodactyl\Services\Packs\ExportPackService
*/
protected $service;
/**
* @var \Illuminate\Contracts\Filesystem\Factory
*/
protected $storage;
/**
* Setup tests.
*/
public function setUp()
{
parent::setUp();
$this->archive = m::mock(ZipArchive::class);
$this->repository = m::mock(PackRepositoryInterface::class);
$this->storage = m::mock(Factory::class);
$this->service = new ExportPackService($this->storage, $this->repository, $this->archive);
}
/**
* Provide standard data to all tests.
*/
protected function setupTestData()
{
$this->model = factory(Pack::class)->make();
$this->json = [
'name' => $this->model->name,
'version' => $this->model->version,
'description' => $this->model->description,
'selectable' => $this->model->selectable,
'visible' => $this->model->visible,
'locked' => $this->model->locked,
];
}
/**
* Test that an archive of the entire pack can be exported.
*/
public function testFilesAreBundledIntoZipWhenRequested()
{
$this->setupTestData();
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'tempnam')
->expects($this->once())->willReturn('/tmp/myfile.test');
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'fopen')->expects($this->never());
$this->archive->shouldReceive('open')->with('/tmp/myfile.test', $this->archive::CREATE)->once()->andReturnSelf();
$this->storage->shouldReceive('disk->files')->with('packs/' . $this->model->uuid)->once()->andReturn(['file_one']);
$this->archive->shouldReceive('addFile')->with(storage_path('app/file_one'), 'file_one')->once()->andReturnSelf();
$this->archive->shouldReceive('addFromString')->with('import.json', json_encode($this->json, JSON_PRETTY_PRINT))->once()->andReturnSelf();
$this->archive->shouldReceive('close')->withNoArgs()->once()->andReturnNull();
$response = $this->service->handle($this->model, true);
$this->assertEquals('/tmp/myfile.test', $response);
}
/**
* Test that the pack configuration can be saved as a json file.
*/
public function testPackConfigurationIsSavedAsJsonFile()
{
$this->setupTestData();
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'tempnam')
->expects($this->once())->willReturn('/tmp/myfile.test');
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'fopen')->expects($this->once())->wilLReturn('fp');
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'fwrite')
->expects($this->once())->with('fp', json_encode($this->json, JSON_PRETTY_PRINT))->willReturn(null);
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'fclose')
->expects($this->once())->with('fp')->willReturn(null);
$response = $this->service->handle($this->model);
$this->assertEquals('/tmp/myfile.test', $response);
}
/**
* Test that a model ID can be passed in place of the model itself.
*/
public function testPackIdCanBePassedInPlaceOfModel()
{
$this->setupTestData();
$this->repository->shouldReceive('find')->with($this->model->id)->once()->andReturn($this->model);
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'tempnam')->expects($this->once())->willReturn('/tmp/myfile.test');
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'fopen')->expects($this->once())->wilLReturn(null);
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'fwrite')->expects($this->once())->willReturn(null);
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'fclose')->expects($this->once())->willReturn(null);
$response = $this->service->handle($this->model->id);
$this->assertEquals('/tmp/myfile.test', $response);
}
/**
* Test that an exception is thrown when a ZipArchive cannot be created.
*
* @expectedException \Pterodactyl\Exceptions\Service\Pack\ZipArchiveCreationException
*/
public function testExceptionIsThrownIfZipArchiveCannotBeCreated()
{
$this->setupTestData();
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'tempnam')
->expects($this->once())->willReturn('/tmp/myfile.test');
$this->archive->shouldReceive('open')->once()->andReturn(false);
$this->service->handle($this->model, true);
}
}