Skip to content

Commit 96254d8

Browse files
authored
Merge pull request #6 from holtkamp/allow-to-set-timestamp
Allow to indicate specific timestamp when writing a file or folder
2 parents a2f8aa4 + ce69e9c commit 96254d8

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

Diff for: src/PDOAdapter.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function write($path, $contents, Config $config)
5252
$size = strlen($contents);
5353
$type = 'file';
5454
$mimetype = Util::guessMimeType($path, $contents);
55-
$timestamp = time();
55+
$timestamp = $config->get('timestamp', time());
5656

5757
$statement->bindParam(':path', $path, PDO::PARAM_STR);
5858
$statement->bindParam(':contents', $contents, PDO::PARAM_LOB);
@@ -81,7 +81,7 @@ public function update($path, $contents, Config $config)
8181

8282
$size = strlen($contents);
8383
$mimetype = Util::guessMimeType($path, $contents);
84-
$timestamp = time();
84+
$timestamp = $config->get('timestamp', time());
8585

8686
$statement->bindParam(':size', $size, PDO::PARAM_INT);
8787
$statement->bindParam(':mimetype', $mimetype, PDO::PARAM_STR);
@@ -211,9 +211,11 @@ public function createDir($dirname, Config $config)
211211
{
212212
$statement = $this->pdo->prepare("INSERT INTO {$this->table} (path, type, timestamp) VALUES(:path, :type, :timestamp)");
213213

214+
$timestamp = $config->get('timestamp', time());
215+
214216
$statement->bindParam(':path', $dirname, PDO::PARAM_STR);
215217
$statement->bindValue(':type', 'dir', PDO::PARAM_STR);
216-
$statement->bindValue(':timestamp', time(), PDO::PARAM_STR);
218+
$statement->bindValue(':timestamp', $timestamp, PDO::PARAM_STR);
217219

218220
return $statement->execute();
219221
}

Diff for: tests/PDOAdapterTest.php

+26-5
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,26 @@ public function setUp()
5050
$this->filesystem= new Filesystem($this->adapter);
5151
}
5252

53-
protected function getTableContents()
53+
protected function getTableContents($stripTimestampFromReturnedRows = true)
5454
{
5555
$statement = $this->pdo->prepare("SELECT * FROM files");
5656
$statement->execute();
5757
$result = $statement->fetchAll(PDO::FETCH_ASSOC);
5858

59-
return array_map(function($v) {
59+
return array_map(function($v) use($stripTimestampFromReturnedRows) {
6060
unset($v['id']);
61-
unset($v['timestamp']);
61+
if($stripTimestampFromReturnedRows) {
62+
unset($v['timestamp']);
63+
}
6264
$v['size'] = (int) $v['size'];
6365

6466
return $v;
6567
}, $result);
6668
}
6769

68-
protected function assertTableContains($expected)
70+
protected function assertTableContains($expected, $stripTimestampFromReturnedRows = true)
6971
{
70-
$this->assertEquals($expected, $this->getTableContents());
72+
$this->assertEquals($expected, $this->getTableContents($stripTimestampFromReturnedRows));
7173
}
7274

7375
protected function filterContents($contents)
@@ -124,6 +126,25 @@ public function testBasicWrite()
124126
]);
125127
}
126128

129+
public function testWriteWithSpecificTimestamp()
130+
{
131+
$timestamp = \mktime(0, 0, 0, 1, 1, 2000);
132+
$config = array('timestamp' => $timestamp);
133+
134+
$this->assertTrue($this->filesystem->createDir('foo', $config));
135+
136+
$path1 = 'foo/bar.txt';
137+
$contents1 = 'ala ma kota';
138+
$this->assertTrue($this->filesystem->write($path1, $contents1, $config));
139+
140+
$this->assertTableContains([
141+
['path' => 'foo', 'contents' => null, 'type' => 'dir', 'size' => 0, 'mimetype' => null, 'timestamp' => $timestamp],
142+
['path' => $path1, 'contents' => $contents1, 'type' => 'file', 'size' => strlen($contents1), 'mimetype' => 'text/plain', 'timestamp' => $timestamp]
143+
],
144+
false
145+
);
146+
}
147+
127148
public function testListContents()
128149
{
129150
$this->testBasicWrite();

0 commit comments

Comments
 (0)