Skip to content

Commit 2e34762

Browse files
committed
Add test for pack exporting
1 parent 47eec03 commit 2e34762

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed

database/factories/ModelFactory.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,17 @@
131131
$factory->state(Pterodactyl\Models\ServiceVariable::class, 'editable', function () {
132132
return ['user_editable' => 1];
133133
});
134+
135+
$factory->define(Pterodactyl\Models\Pack::class, function (Faker\Generator $faker) {
136+
return [
137+
'id' => $faker->unique()->randomNumber(),
138+
'option_id' => $faker->randomNumber(),
139+
'uuid' => $faker->uuid,
140+
'name' => $faker->word,
141+
'description' => null,
142+
'version' => $faker->randomNumber(),
143+
'selectable' => 1,
144+
'visible' => 1,
145+
'locked' => 0,
146+
];
147+
});
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Tests\Unit\Services\Packs;
26+
27+
use Illuminate\Contracts\Filesystem\Factory;
28+
use Mockery as m;
29+
use phpmock\phpunit\PHPMock;
30+
use Pterodactyl\Contracts\Repository\PackRepositoryInterface;
31+
use Pterodactyl\Models\Pack;
32+
use Pterodactyl\Services\Packs\ExportPackService;
33+
use Tests\TestCase;
34+
use ZipArchive;
35+
36+
class ExportPackServiceTest extends TestCase
37+
{
38+
use PHPMock;
39+
40+
/**
41+
* @var \ZipArchive
42+
*/
43+
protected $archive;
44+
45+
/**
46+
* @var \Pterodactyl\Contracts\Repository\PackRepositoryInterface
47+
*/
48+
protected $repository;
49+
50+
/**
51+
* @var \Pterodactyl\Services\Packs\ExportPackService
52+
*/
53+
protected $service;
54+
55+
/**
56+
* @var \Illuminate\Contracts\Filesystem\Factory
57+
*/
58+
protected $storage;
59+
60+
/**
61+
* Setup tests.
62+
*/
63+
public function setUp()
64+
{
65+
parent::setUp();
66+
67+
$this->archive = m::mock(ZipArchive::class);
68+
$this->repository = m::mock(PackRepositoryInterface::class);
69+
$this->storage = m::mock(Factory::class);
70+
71+
$this->service = new ExportPackService($this->storage, $this->repository, $this->archive);
72+
}
73+
74+
/**
75+
* Provide standard data to all tests.
76+
*/
77+
protected function setupTestData()
78+
{
79+
$this->model = factory(Pack::class)->make();
80+
$this->json = [
81+
'name' => $this->model->name,
82+
'version' => $this->model->version,
83+
'description' => $this->model->description,
84+
'selectable' => $this->model->selectable,
85+
'visible' => $this->model->visible,
86+
'locked' => $this->model->locked,
87+
];
88+
}
89+
90+
/**
91+
* Test that an archive of the entire pack can be exported.
92+
*/
93+
public function testFilesAreBundledIntoZipWhenRequested()
94+
{
95+
$this->setupTestData();
96+
97+
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'tempnam')
98+
->expects($this->once())->willReturn('/tmp/myfile.test');
99+
100+
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'fopen')->expects($this->never());
101+
102+
$this->archive->shouldReceive('open')->with('/tmp/myfile.test', $this->archive::CREATE)->once()->andReturnSelf();
103+
$this->storage->shouldReceive('disk->files')->with('packs/' . $this->model->uuid)->once()->andReturn(['file_one']);
104+
$this->archive->shouldReceive('addFile')->with(storage_path('app/file_one'), 'file_one')->once()->andReturnSelf();
105+
$this->archive->shouldReceive('addFromString')->with('import.json', json_encode($this->json, JSON_PRETTY_PRINT))->once()->andReturnSelf();
106+
$this->archive->shouldReceive('close')->withNoArgs()->once()->andReturnNull();
107+
108+
$response = $this->service->handle($this->model, true);
109+
$this->assertEquals('/tmp/myfile.test', $response);
110+
}
111+
112+
/**
113+
* Test that the pack configuration can be saved as a json file.
114+
*/
115+
public function testPackConfigurationIsSavedAsJsonFile()
116+
{
117+
$this->setupTestData();
118+
119+
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'tempnam')
120+
->expects($this->once())->willReturn('/tmp/myfile.test');
121+
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'fopen')->expects($this->once())->wilLReturn('fp');
122+
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'fwrite')
123+
->expects($this->once())->with('fp', json_encode($this->json, JSON_PRETTY_PRINT))->willReturn(null);
124+
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'fclose')
125+
->expects($this->once())->with('fp')->willReturn(null);
126+
127+
$response = $this->service->handle($this->model);
128+
$this->assertEquals('/tmp/myfile.test', $response);
129+
}
130+
131+
/**
132+
* Test that a model ID can be passed in place of the model itself.
133+
*/
134+
public function testPackIdCanBePassedInPlaceOfModel()
135+
{
136+
$this->setupTestData();
137+
138+
$this->repository->shouldReceive('find')->with($this->model->id)->once()->andReturn($this->model);
139+
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'tempnam')->expects($this->once())->willReturn('/tmp/myfile.test');
140+
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'fopen')->expects($this->once())->wilLReturn(null);
141+
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'fwrite')->expects($this->once())->willReturn(null);
142+
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'fclose')->expects($this->once())->willReturn(null);
143+
144+
$response = $this->service->handle($this->model->id);
145+
$this->assertEquals('/tmp/myfile.test', $response);
146+
}
147+
148+
/**
149+
* Test that an exception is thrown when a ZipArchive cannot be created.
150+
*
151+
* @expectedException \Pterodactyl\Exceptions\Service\Pack\ZipArchiveCreationException
152+
*/
153+
public function testExceptionIsThrownIfZipArchiveCannotBeCreated()
154+
{
155+
$this->setupTestData();
156+
157+
$this->getFunctionMock('\\Pterodactyl\\Services\\Packs', 'tempnam')
158+
->expects($this->once())->willReturn('/tmp/myfile.test');
159+
160+
$this->archive->shouldReceive('open')->once()->andReturn(false);
161+
162+
$this->service->handle($this->model, true);
163+
}
164+
}

0 commit comments

Comments
 (0)