Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make timestamp configurable #12

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions InMemoryFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ class InMemoryFile
*/
private $visibility;

public function updateContents(string $contents): void
public function updateContents(string $contents, ?int $timestamp): void
{
$this->contents = $contents;
$this->lastModified = time();
$this->lastModified = $timestamp ?: time();
}

public function lastModified(): int
Expand Down Expand Up @@ -75,4 +75,12 @@ public function visibility(): ?string
{
return $this->visibility;
}

public function withLastModified(int $lastModified): self
{
$clone = clone $this;
$clone->lastModified = $lastModified;

return $clone;
}
}
6 changes: 4 additions & 2 deletions InMemoryFilesystemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function write(string $path, string $contents, Config $config): void
{
$path = $this->preparePath($path);
$file = $this->files[$path] = $this->files[$path] ?? new InMemoryFile();
$file->updateContents($contents);
$file->updateContents($contents, $config->get('timestamp'));

$visibility = $config->get(Config::OPTION_VISIBILITY, $this->defaultVisibility);
$file->setVisibility($visibility);
Expand Down Expand Up @@ -231,7 +231,9 @@ public function copy(string $source, string $destination, Config $config): void
throw UnableToCopyFile::fromLocationTo($source, $destination);
}

$this->files[$destination] = clone $this->files[$source];
$lastModified = $config->get('timestamp', time());

$this->files[$destination] = $this->files[$source]->withLastModified($lastModified);
}

private function preparePath(string $path): string
Expand Down
16 changes: 16 additions & 0 deletions InMemoryFilesystemAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,22 @@ public function fetching_unknown_mime_type_of_a_file(): void
parent::fetching_unknown_mime_type_of_a_file();
}

/**
* @test
*/
public function using_custom_timestamp(): void
{
$adapter = $this->adapter();

$now = 100;
$adapter->write('file.txt', 'contents', new Config(['timestamp' => $now]));
$this->assertEquals($now, $adapter->lastModified('file.txt')->lastModified());

$earlier = 50;
$adapter->copy('file.txt', 'new_file.txt', new Config(['timestamp' => $earlier]));
$this->assertEquals($earlier, $adapter->lastModified('new_file.txt')->lastModified());
}

protected static function createFilesystemAdapter(): FilesystemAdapter
{
return new InMemoryFilesystemAdapter();
Expand Down