Skip to content

Commit

Permalink
[InMemory] Make timestamp configurable
Browse files Browse the repository at this point in the history
Use an optional 'timestamp' option in the config to set the 'lastModified' attribute when writing or copying a file. This allows using the memory adapter in testing environments for testing time-based behaviors.
  • Loading branch information
chapa committed Apr 2, 2022
1 parent 286a488 commit 8f9cf8f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
12 changes: 10 additions & 2 deletions src/InMemory/InMemoryFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,25 @@ 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
{
return $this->lastModified;
}

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

return $clone;
}

public function read(): string
{
return $this->contents;
Expand Down
6 changes: 4 additions & 2 deletions src/InMemory/InMemoryFilesystemAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,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 @@ -249,7 +249,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 src/InMemory/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

0 comments on commit 8f9cf8f

Please sign in to comment.