misc_pterodactyl-panel/tests/Unit/Services/Packs/ExportPackServiceTest.php

151 lines
5.3 KiB
PHP
Raw Normal View History

2017-08-21 01:02:24 +00:00
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
2017-09-26 02:43:01 +00:00
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
2017-08-21 01:02:24 +00:00
*/
namespace Tests\Unit\Services\Packs;
2017-08-27 19:55:25 +00:00
use ZipArchive;
2017-08-21 01:02:24 +00:00
use Mockery as m;
2017-08-27 19:55:25 +00:00
use Tests\TestCase;
2017-08-21 01:02:24 +00:00
use phpmock\phpunit\PHPMock;
use Pterodactyl\Models\Pack;
2017-08-27 19:55:25 +00:00
use Illuminate\Contracts\Filesystem\Factory;
2017-08-21 01:02:24 +00:00
use Pterodactyl\Services\Packs\ExportPackService;
2017-08-27 19:55:25 +00:00
use Pterodactyl\Contracts\Repository\PackRepositoryInterface;
2020-06-25 05:18:48 +00:00
use Pterodactyl\Exceptions\Service\Pack\ZipArchiveCreationException;
2017-08-21 01:02:24 +00:00
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(): void
2017-08-21 01:02:24 +00:00
{
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.
*/
public function testExceptionIsThrownIfZipArchiveCannotBeCreated()
{
2020-06-25 05:18:48 +00:00
$this->expectException(ZipArchiveCreationException::class);
2017-08-21 01:02:24 +00:00
$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);
}
}