1
1
Fork 0
mirror of https://code.mensbeam.com/MensBeam/Arsse.git synced 2024-12-23 09:02:41 +00:00
Arsse/tests/cases/ImportExport/TestFile.php

132 lines
6.5 KiB
PHP
Raw Normal View History

2019-04-01 20:54:14 +00:00
<?php
/** @license MIT
* Copyright 2017 J. King, Dustin Wilson et al.
* See LICENSE and AUTHORS files for details */
declare(strict_types=1);
namespace JKingWeb\Arsse\TestCase\ImportExport;
2019-06-21 17:55:49 +00:00
use JKingWeb\Arsse\ImportExport\AbstractImportExport;
2019-04-01 20:54:14 +00:00
use JKingWeb\Arsse\ImportExport\Exception;
use org\bovigo\vfs\vfsStream;
/** @covers \JKingWeb\Arsse\ImportExport\AbstractImportExport */
class TestFile extends \JKingWeb\Arsse\Test\AbstractTest {
2019-04-01 20:54:14 +00:00
protected $vfs;
protected $path;
protected $proc;
2019-04-01 20:54:14 +00:00
2019-10-16 18:42:43 +00:00
public function setUp(): void {
2021-02-27 20:24:02 +00:00
parent::setUp();
2019-06-21 17:55:49 +00:00
// create a mock Import/Export processor with stubbed underlying import/export routines
2021-02-27 20:24:02 +00:00
$this->proc = $this->partialMock(AbstractImportExport::class);
$this->proc->export->returns("EXPORT_FILE");
$this->proc->import->returns(true);
2019-04-01 20:54:14 +00:00
$this->vfs = vfsStream::setup("root", null, [
'exportGoodFile' => "",
'exportGoodDir' => [],
'exportBadFile' => "",
'exportBadDir' => [],
'importGoodFile' => "GOOD_FILE",
2019-05-12 20:33:19 +00:00
'importBadFile' => "",
2019-04-01 20:54:14 +00:00
]);
$this->path = $this->vfs->url()."/";
// make the "bad" entries inaccessible
chmod($this->path."exportBadFile", 0000);
chmod($this->path."exportBadDir", 0000);
2019-05-12 20:33:19 +00:00
chmod($this->path."importBadFile", 0000);
2019-04-01 20:54:14 +00:00
}
2019-10-16 18:42:43 +00:00
public function tearDown(): void {
2019-04-01 20:54:14 +00:00
$this->path = null;
$this->vfs = null;
$this->proc = null;
2021-02-27 20:24:02 +00:00
parent::tearDown();
2019-04-01 20:54:14 +00:00
}
/** @dataProvider provideFileExports */
2020-01-20 18:52:48 +00:00
public function testExportToAFile(string $file, string $user, bool $flat, $exp): void {
2019-04-01 20:54:14 +00:00
$path = $this->path.$file;
try {
if ($exp instanceof \JKingWeb\Arsse\AbstractException) {
$this->assertException($exp);
2021-02-27 20:24:02 +00:00
$this->proc->get()->exportFile($path, $user, $flat);
2019-04-01 20:54:14 +00:00
} else {
2021-02-27 20:24:02 +00:00
$this->assertSame($exp, $this->proc->get()->exportFile($path, $user, $flat));
$this->assertSame("EXPORT_FILE", $this->vfs->getChild($file)->getContent());
2019-04-01 20:54:14 +00:00
}
} finally {
2021-02-27 20:24:02 +00:00
$this->proc->export->calledWith($user, $flat);
2019-04-01 20:54:14 +00:00
}
}
2019-10-16 18:42:43 +00:00
public function provideFileExports(): iterable {
2019-04-01 20:54:14 +00:00
$createException = new Exception("fileUncreatable");
$writeException = new Exception("fileUnwritable");
return [
["exportGoodFile", "john.doe@example.com", true, true],
["exportGoodFile", "john.doe@example.com", false, true],
["exportGoodFile", "jane.doe@example.com", true, true],
["exportGoodFile", "jane.doe@example.com", false, true],
["exportGoodDir/file", "john.doe@example.com", true, true],
["exportGoodDir/file", "john.doe@example.com", false, true],
["exportGoodDir/file", "jane.doe@example.com", true, true],
["exportGoodDir/file", "jane.doe@example.com", false, true],
["exportBadFile", "john.doe@example.com", true, $writeException],
["exportBadFile", "john.doe@example.com", false, $writeException],
["exportBadFile", "jane.doe@example.com", true, $writeException],
["exportBadFile", "jane.doe@example.com", false, $writeException],
["exportBadDir/file", "john.doe@example.com", true, $createException],
["exportBadDir/file", "john.doe@example.com", false, $createException],
["exportBadDir/file", "jane.doe@example.com", true, $createException],
["exportBadDir/file", "jane.doe@example.com", false, $createException],
];
}
2019-05-12 20:33:19 +00:00
/** @dataProvider provideFileImports */
2020-01-20 18:52:48 +00:00
public function testImportFromAFile(string $file, string $user, bool $flat, bool $replace, $exp): void {
2019-05-12 20:33:19 +00:00
$path = $this->path.$file;
try {
if ($exp instanceof \JKingWeb\Arsse\AbstractException) {
$this->assertException($exp);
2021-02-27 20:24:02 +00:00
$this->proc->get()->importFile($path, $user, $flat, $replace);
2019-05-12 20:33:19 +00:00
} else {
2021-02-27 20:24:02 +00:00
$this->assertSame($exp, $this->proc->get()->importFile($path, $user, $flat, $replace));
2019-05-12 20:33:19 +00:00
}
} finally {
2021-02-27 20:24:02 +00:00
$this->proc->import->times((int) ($exp === true))->calledWith($user, "GOOD_FILE", $flat, $replace);
2019-05-12 20:33:19 +00:00
}
}
2019-10-16 18:42:43 +00:00
public function provideFileImports(): iterable {
2019-05-12 20:33:19 +00:00
$missingException = new Exception("fileMissing");
$permissionException = new Exception("fileUnreadable");
return [
["importGoodFile", "john.doe@example.com", true, true, true],
["importBadFile", "john.doe@example.com", true, true, $permissionException],
["importNonFile", "john.doe@example.com", true, true, $missingException],
["importGoodFile", "john.doe@example.com", true, false, true],
["importBadFile", "john.doe@example.com", true, false, $permissionException],
["importNonFile", "john.doe@example.com", true, false, $missingException],
["importGoodFile", "john.doe@example.com", false, true, true],
["importBadFile", "john.doe@example.com", false, true, $permissionException],
["importNonFile", "john.doe@example.com", false, true, $missingException],
["importGoodFile", "john.doe@example.com", false, false, true],
["importBadFile", "john.doe@example.com", false, false, $permissionException],
["importNonFile", "john.doe@example.com", false, false, $missingException],
["importGoodFile", "jane.doe@example.com", true, true, true],
["importBadFile", "jane.doe@example.com", true, true, $permissionException],
["importNonFile", "jane.doe@example.com", true, true, $missingException],
["importGoodFile", "jane.doe@example.com", true, false, true],
["importBadFile", "jane.doe@example.com", true, false, $permissionException],
["importNonFile", "jane.doe@example.com", true, false, $missingException],
["importGoodFile", "jane.doe@example.com", false, true, true],
["importBadFile", "jane.doe@example.com", false, true, $permissionException],
["importNonFile", "jane.doe@example.com", false, true, $missingException],
["importGoodFile", "jane.doe@example.com", false, false, true],
["importBadFile", "jane.doe@example.com", false, false, $permissionException],
["importNonFile", "jane.doe@example.com", false, false, $missingException],
];
}
2019-04-01 20:54:14 +00:00
}