Skip to content

Commit

Permalink
Fixed accessing not initialized values in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
norberttech committed Jul 3, 2024
1 parent ff32a09 commit 34d38d0
Show file tree
Hide file tree
Showing 10 changed files with 45 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,6 @@ public function test_scan() : void

protected function streams() : FilesystemStreams
{
return new FilesystemStreams($this->fstab);
return new FilesystemStreams($this->fstab());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function test_open_stream_for_existing_file() : void
$appendFileStream->append('new content');
$streams->closeWriters($file);

$files = \iterator_to_array($this->fs->list(new Path($file->parentDirectory()->path() . '/*')));
$files = \iterator_to_array($this->fs()->list(new Path($file->parentDirectory()->path() . '/*')));

self::assertCount(2, $files);

Expand All @@ -55,15 +55,15 @@ public function test_open_stream_for_non_existing_file() : void
$appendFileStream->append('new content');
$streams->closeWriters($file);

$files = \iterator_to_array($this->fs->list(new Path($file->parentDirectory()->path() . '/*')));
$files = \iterator_to_array($this->fs()->list(new Path($file->parentDirectory()->path() . '/*')));

self::assertCount(1, $files);
self::assertSame('non-existing-file.txt', $files[0]->path->basename());
}

protected function streams() : FilesystemStreams
{
$streams = new FilesystemStreams($this->fstab);
$streams = new FilesystemStreams($this->fstab());
$streams->setSaveMode(append());

return $streams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function test_open_stream_for_non_existing_file() : void

protected function streams() : FilesystemStreams
{
$streams = new FilesystemStreams($this->fstab);
$streams = new FilesystemStreams($this->fstab());
$streams->setSaveMode(exception_if_exists());

return $streams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function test_open_stream_for_non_existing_file() : void

protected function streams() : FilesystemStreams
{
$streams = new FilesystemStreams($this->fstab);
$streams = new FilesystemStreams($this->fstab());
$streams->setSaveMode(ignore());

return $streams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function test_open_stream_for_existing_file() : void

self::assertSame('some other content', \file_get_contents($path->path()));

self::assertCount(1, $files = \iterator_to_array($this->fs->list(new Path($path->parentDirectory()->path() . '/*'))));
self::assertCount(1, $files = \iterator_to_array($this->fs()->list(new Path($path->parentDirectory()->path() . '/*'))));
self::assertSame('existing-file.txt', $files[0]->path->basename());
}

Expand All @@ -55,7 +55,7 @@ public function test_open_stream_for_non_existing_file() : void

protected function streams() : FilesystemStreams
{
$streams = new FilesystemStreams($this->fstab);
$streams = new FilesystemStreams($this->fstab());
$streams->setSaveMode(overwrite());

return $streams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function test_open_stream_for_existing_partition_with_existing_file() : v
$fileStream->append('appended content');
$streams->closeWriters($file);

$files = \iterator_to_array($this->fs->list(new Path($file->parentDirectory()->path() . '/**/*.txt')));
$files = \iterator_to_array($this->fs()->list(new Path($file->parentDirectory()->path() . '/**/*.txt')));

self::assertCount(2, $files);

Expand All @@ -60,7 +60,7 @@ public function test_open_stream_for_existing_partition_without_existing_file()
$fileStream->append('appended content');
$streams->closeWriters($file);

$files = \iterator_to_array($this->fs->list(new Path($file->parentDirectory()->path() . '/**/*.txt')));
$files = \iterator_to_array($this->fs()->list(new Path($file->parentDirectory()->path() . '/**/*.txt')));

self::assertCount(1, $files);

Expand All @@ -80,7 +80,7 @@ public function test_open_stream_for_non_existing_partition() : void
$appendedFile = $streams->writeTo($file, partitions: [new Partition('partition', 'value')]);
$appendedFile->append('appended content');
$streams->closeWriters($file);
$files = \iterator_to_array($this->fs->list(new Path($file->parentDirectory()->path() . '/partition=value/*')));
$files = \iterator_to_array($this->fs()->list(new Path($file->parentDirectory()->path() . '/partition=value/*')));

self::assertCount(1, $files);

Expand All @@ -90,7 +90,7 @@ public function test_open_stream_for_non_existing_partition() : void

protected function streams() : FilesystemStreams
{
$streams = new FilesystemStreams($this->fstab);
$streams = new FilesystemStreams($this->fstab());
$streams->setSaveMode(append());

return $streams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function test_open_stream_for_existing_partition_without_existing_file()
$fileStream->append('file content');
$streams->closeWriters($file);

$files = \iterator_to_array($this->fs->list(new Path($file->parentDirectory()->path() . '/**/*.txt')));
$files = \iterator_to_array($this->fs()->list(new Path($file->parentDirectory()->path() . '/**/*.txt')));

self::assertCount(1, $files);

Expand All @@ -69,7 +69,7 @@ public function test_open_stream_for_non_existing_partition() : void
$appendedFile = $streams->writeTo($file, partitions: [new Partition('partition', 'value')]);
$appendedFile->append('file content');
$streams->closeWriters($file);
$files = \iterator_to_array($this->fs->list(new Path($file->parentDirectory()->path() . '/partition=value/*')));
$files = \iterator_to_array($this->fs()->list(new Path($file->parentDirectory()->path() . '/partition=value/*')));

self::assertCount(1, $files);

Expand All @@ -79,7 +79,7 @@ public function test_open_stream_for_non_existing_partition() : void

protected function streams() : FilesystemStreams
{
$streams = new FilesystemStreams($this->fstab);
$streams = new FilesystemStreams($this->fstab());
$streams->setSaveMode(exception_if_exists());

return $streams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function test_open_stream_for_existing_partition_with_existing_file() : v
$fileStream->append('new content');
$streams->closeWriters($file);

$files = \iterator_to_array($this->fs->list(new Path($file->parentDirectory()->path() . '/**/*.txt')));
$files = \iterator_to_array($this->fs()->list(new Path($file->parentDirectory()->path() . '/**/*.txt')));

self::assertCount(1, $files);

Expand All @@ -57,7 +57,7 @@ public function test_open_stream_for_existing_partition_without_existing_file()
$fileStream->append('appended content');
$streams->closeWriters($file);

$files = \iterator_to_array($this->fs->list(new Path($file->parentDirectory()->path() . '/**/*.txt')));
$files = \iterator_to_array($this->fs()->list(new Path($file->parentDirectory()->path() . '/**/*.txt')));

self::assertCount(1, $files);

Expand All @@ -77,7 +77,7 @@ public function test_open_stream_for_non_existing_partition() : void
$appendedFile = $streams->writeTo($file, partitions: [new Partition('partition', 'value')]);
$appendedFile->append('appended content');
$streams->closeWriters($file);
$files = \iterator_to_array($this->fs->list(new Path($file->parentDirectory()->path() . '/partition=value/*')));
$files = \iterator_to_array($this->fs()->list(new Path($file->parentDirectory()->path() . '/partition=value/*')));

self::assertCount(1, $files);

Expand All @@ -87,7 +87,7 @@ public function test_open_stream_for_non_existing_partition() : void

protected function streams() : FilesystemStreams
{
$streams = new FilesystemStreams($this->fstab);
$streams = new FilesystemStreams($this->fstab());
$streams->setSaveMode(ignore());

return $streams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function test_open_stream_for_existing_partition_with_existing_file() : v
$fileStream->append('new content');
$streams->closeWriters($file);

$files = \iterator_to_array($this->fs->list(new Path($file->parentDirectory()->path() . '/**/*.txt')));
$files = \iterator_to_array($this->fs()->list(new Path($file->parentDirectory()->path() . '/**/*.txt')));

self::assertCount(1, $files);

Expand All @@ -57,7 +57,7 @@ public function test_open_stream_for_existing_partition_without_existing_file()
$fileStream->append('new content');
$streams->closeWriters($file);

$files = \iterator_to_array($this->fs->list(new Path($file->parentDirectory()->path() . '/**/*.txt')));
$files = \iterator_to_array($this->fs()->list(new Path($file->parentDirectory()->path() . '/**/*.txt')));

self::assertCount(1, $files);

Expand All @@ -77,7 +77,7 @@ public function test_open_stream_for_non_existing_partition() : void
$appendedFile = $streams->writeTo($file, partitions: [new Partition('partition', 'value')]);
$appendedFile->append('new content');
$streams->closeWriters($file);
$files = \iterator_to_array($this->fs->list(new Path($file->parentDirectory()->path() . '/partition=value/*')));
$files = \iterator_to_array($this->fs()->list(new Path($file->parentDirectory()->path() . '/partition=value/*')));

self::assertCount(1, $files);

Expand All @@ -87,7 +87,7 @@ public function test_open_stream_for_non_existing_partition() : void

protected function streams() : FilesystemStreams
{
$streams = new FilesystemStreams($this->fstab);
$streams = new FilesystemStreams($this->fstab());
$streams->setSaveMode(overwrite());

return $streams;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@ protected function setUp() : void
$this->baseMemoryLimit = \ini_get('memory_limit');
$this->cacheDir = Path::realpath(\getenv(Config::CACHE_DIR_ENV))->path();

$this->fs = new NativeLocalFilesystem();
$this->fstab = new FilesystemTable($this->fs);

$this->cleanupCacheDir($this->cacheDir);

if (!$this->fs->status(Path::realpath($this->cacheDir))?->isDirectory()) {
if (!$this->fs()->status(Path::realpath($this->cacheDir))?->isDirectory()) {
\mkdir($this->cacheDir, recursive: true);
}
}
Expand All @@ -50,7 +47,7 @@ protected function cleanFiles() : void
continue;
}

$this->fs->rm(Path::realpath($this->filesDirectory() . DIRECTORY_SEPARATOR . $file));
$this->fs()->rm(Path::realpath($this->filesDirectory() . DIRECTORY_SEPARATOR . $file));
}
}

Expand All @@ -59,6 +56,24 @@ protected function filesDirectory() : string
throw new \RuntimeException('You need to implement filesDirectory method to point to your test files directory.');
}

protected function fs() : Filesystem
{
if ($this->fs === null) {
$this->fs = new NativeLocalFilesystem();
}

return $this->fs;
}

protected function fstab() : FilesystemTable
{
if ($this->fstab === null) {
$this->fstab = new FilesystemTable($this->fs());
}

return $this->fstab;
}

protected function getPath(string $relativePath) : Path
{
return new Path($this->filesDirectory() . DIRECTORY_SEPARATOR . $relativePath);
Expand Down Expand Up @@ -91,8 +106,8 @@ protected function setupFiles(array $datasets, $path = '') : void

private function cleanupCacheDir(string $directory) : void
{
if ($this->fs->status($path = Path::realpath($directory))?->isDirectory()) {
$this->fs->rm($path);
if ($this->fs()->status($path = Path::realpath($directory))?->isDirectory()) {
$this->fs()->rm($path);
}
}
}

0 comments on commit 34d38d0

Please sign in to comment.