RegionLoader: Switch to using named constructors

this makes the code more self-descriptive, and also helps to detect potential bugs.
This commit is contained in:
Dylan K. Taylor
2021-04-15 21:57:23 +01:00
parent e8dd4de5c8
commit 5d83f4670a
4 changed files with 56 additions and 22 deletions

View File

@ -25,11 +25,14 @@ namespace pocketmine\world\format\io\region;
use PHPUnit\Framework\TestCase;
use pocketmine\world\format\ChunkException;
use function bin2hex;
use function clearstatcache;
use function file_exists;
use function random_bytes;
use function str_repeat;
use function sys_get_temp_dir;
use function unlink;
use const DIRECTORY_SEPARATOR;
class RegionLoaderTest extends TestCase{
@ -43,7 +46,7 @@ class RegionLoaderTest extends TestCase{
if(file_exists($this->regionPath)){
unlink($this->regionPath);
}
$this->region = new RegionLoader($this->regionPath);
$this->region = RegionLoader::createNew($this->regionPath);
}
public function tearDown() : void{
@ -63,7 +66,7 @@ class RegionLoaderTest extends TestCase{
$this->region->writeChunk(0, 0, $data);
$this->region->close();
$r = new RegionLoader($this->regionPath);
$r = RegionLoader::loadExisting($this->regionPath);
self::assertSame($data, $r->readChunk(0, 0));
}
@ -119,11 +122,27 @@ class RegionLoaderTest extends TestCase{
*/
public function testRegionHeaderCachedFilesizeRegression() : void{
$this->region->close();
$region = new RegionLoader($this->regionPath); //now we have a region, so the header will be verified, triggering two filesize() calls
$region = RegionLoader::loadExisting($this->regionPath); //now we have a region, so the header will be verified, triggering two filesize() calls
$data = str_repeat("hello", 2000);
$region->writeChunk(0, 0, $data); //add some data to the end of the file, to make the cached filesize invalid
$region->close();
$region = new RegionLoader($this->regionPath);
$region = RegionLoader::loadExisting($this->regionPath);
self::assertSame($data, $region->readChunk(0, 0));
}
public function testCreateNewWithExistingRegion() : void{
$this->region->close();
$this->expectException(\RuntimeException::class);
RegionLoader::createNew($this->regionPath);
}
public function testLoadExistingWithMissingFile() : void{
clearstatcache();
do{
$randfile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . bin2hex(random_bytes(6)) . ".mca";
}while(file_exists($randfile));
$this->expectException(\RuntimeException::class);
RegionLoader::loadExisting($randfile);
}
}