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
149 lines (123 loc) · 5.26 KB
/
ExportPackServiceTest.php
File metadata and controls
149 lines (123 loc) · 5.26 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
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Tests\Unit\Services\Packs;
use ZipArchive;
use Mockery as m;
use Tests\TestCase;
use phpmock\phpunit\PHPMock;
use Pterodactyl\Models\Pack;
use Illuminate\Contracts\Filesystem\Factory;
use Pterodactyl\Services\Packs\ExportPackService;
use Pterodactyl\Contracts\Repository\PackRepositoryInterface;
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);
}
}