[ticket/15671] Add test

PHPBB3-15671
This commit is contained in:
Rubén Calvo 2018-06-08 20:18:50 +02:00
parent a641ea9291
commit 26680763c2
2 changed files with 59 additions and 48 deletions

View file

@ -255,11 +255,11 @@ class local implements adapter_interface, stream_interface
do do
{ {
$parts = explode(DIRECTORY_SEPARATOR, $path); $parts = explode('/', $path);
$parts = array_slice($parts, 0, -1); $parts = array_slice($parts, 0, -1);
$path = implode(DIRECTORY_SEPARATOR, $parts); $path = implode('/', $parts);
} }
while ($path && @rmdir($dirpath . $path)); while ($path && @rmdir($dirpath . '/' . $path));
} }
/** /**

View file

@ -17,28 +17,22 @@
protected $path; protected $path;
protected $filesystem;
public function setUp() public function setUp()
{ {
parent::setUp(); parent::setUp();
$filesystem = new \phpbb\filesystem\filesystem(); $this->filesystem = new \phpbb\filesystem\filesystem();
$phpbb_root_path = getcwd() . DIRECTORY_SEPARATOR; $phpbb_root_path = getcwd() . DIRECTORY_SEPARATOR;
$this->adapter = new \phpbb\storage\adapter\local($filesystem, new \FastImageSize\FastImageSize(), new \phpbb\mimetype\guesser(array(new \phpbb\mimetype\extension_guesser)), $phpbb_root_path); $this->adapter = new \phpbb\storage\adapter\local($this->filesystem, new \FastImageSize\FastImageSize(), new \phpbb\mimetype\guesser(array(new \phpbb\mimetype\extension_guesser)), $phpbb_root_path);
$this->adapter->configure(['path' => 'test_path']); $this->adapter->configure(['path' => 'test_path', 'depth' => 2]);
$this->path = $phpbb_root_path . 'test_path/'; $this->path = $phpbb_root_path . 'test_path/';
mkdir($this->path); mkdir($this->path);
} }
public function data_test_exists()
{
yield [$this->path . '../README.md', true];
yield [$this->path . 'nonexistent_file.php', false];
yield [$this->path . '../phpBB/phpbb', true];
yield [$this->path . 'nonexistent/folder', false];
}
public function tearDown() public function tearDown()
{ {
$this->adapter = null; $this->adapter = null;
@ -48,68 +42,82 @@
public function test_put_contents() public function test_put_contents()
{ {
$this->adapter->put_contents('file.txt', 'abc'); $this->adapter->put_contents('file.txt', 'abc');
$this->assertTrue(file_exists($this->path . 'file.txt')); $this->assertTrue(file_exists($this->path . '3d/8e/file.txt'));
$this->assertEquals(file_get_contents($this->path . 'file.txt'), 'abc'); $this->assertEquals(file_get_contents($this->path . '3d/8e/file.txt'), 'abc');
unlink($this->path . 'file.txt'); unlink($this->path . '3d/8e/file.txt');
rmdir($this->path . '3d/8e');
rmdir($this->path . '3d');
} }
public function test_get_contents() public function test_get_contents()
{ {
file_put_contents($this->path . 'file.txt', 'abc'); mkdir($this->path . '3d/8e', 0777, true);
file_put_contents($this->path . '3d/8e/file.txt', 'abc');
$this->assertEquals($this->adapter->get_contents('file.txt'), 'abc'); $this->assertEquals($this->adapter->get_contents('file.txt'), 'abc');
unlink($this->path . 'file.txt'); unlink($this->path . '3d/8e/file.txt');
rmdir($this->path . '3d/8e');
rmdir($this->path . '3d');
} }
/** public function test_exists()
* @dataProvider data_test_exists
*/
public function test_exists($path, $expected)
{ {
$this->assertSame($expected, $this->adapter->exists($path)); mkdir($this->path . '3d/8e', 0777, true);
touch($this->path . '3d/8e/file.txt');
$this->assertTrue($this->adapter->exists('file.txt'));
$this->assertFalse($this->adapter->exists('3d/8e/file.txt'));
unlink($this->path . '3d/8e/file.txt');
rmdir($this->path . '3d/8e');
rmdir($this->path . '3d');
} }
public function test_delete_file() public function test_delete_file()
{ {
file_put_contents($this->path . 'file.txt', ''); mkdir($this->path . '3d/8e', 0777, true);
$this->assertTrue(file_exists($this->path . 'file.txt')); touch($this->path . '3d/8e/file.txt');
$this->assertTrue(file_exists($this->path . '3d/8e/file.txt'));
$this->adapter->delete('file.txt'); $this->adapter->delete('file.txt');
$this->assertFalse(file_exists($this->path . 'file.txt')); $this->assertFalse(file_exists($this->path . '3d/8e/file.txt'));
} $this->assertFalse(file_exists($this->path . '3d'));
public function test_delete_folder()
{
mkdir($this->path . 'path/to/dir', 0777, true);
$this->assertTrue(file_exists($this->path . 'path/to/dir'));
$this->adapter->delete('path');
$this->assertFalse(file_exists($this->path . 'path/to/dir'));
} }
public function test_rename() public function test_rename()
{ {
file_put_contents($this->path . 'file.txt', ''); mkdir($this->path . '3d/8e', 0777, true);
touch($this->path . '3d/8e/file.txt');
$this->adapter->rename('file.txt', 'file2.txt'); $this->adapter->rename('file.txt', 'file2.txt');
$this->assertFalse(file_exists($this->path . 'file.txt')); $this->assertFalse(file_exists($this->path . '3d/8e/file.txt'));
$this->assertTrue(file_exists($this->path . 'file2.txt')); $this->assertTrue(file_exists($this->path . '27/36/file2.txt'));
unlink($this->path . 'file2.txt'); $this->assertFalse(file_exists($this->path . '3d'));
unlink($this->path . '27/36/file2.txt');
rmdir($this->path . '27/36');
rmdir($this->path . '27');
} }
public function test_copy() public function test_copy()
{ {
file_put_contents($this->path . 'file.txt', 'abc'); mkdir($this->path . '3d/8e', 0777, true);
file_put_contents($this->path . '3d/8e/file.txt', 'abc');
$this->adapter->copy('file.txt', 'file2.txt'); $this->adapter->copy('file.txt', 'file2.txt');
$this->assertEquals(file_get_contents($this->path . 'file.txt'), 'abc'); $this->assertEquals(file_get_contents($this->path . '3d/8e/file.txt'), 'abc');
$this->assertEquals(file_get_contents($this->path . 'file.txt'), 'abc'); $this->assertEquals(file_get_contents($this->path . '27/36/file2.txt'), 'abc');
unlink($this->path . 'file.txt'); unlink($this->path . '3d/8e/file.txt');
unlink($this->path . 'file2.txt'); rmdir($this->path . '3d/8e');
rmdir($this->path . '3d');
unlink($this->path . '27/36/file2.txt');
rmdir($this->path . '27/36');
rmdir($this->path . '27');
} }
public function test_read_stream() public function test_read_stream()
{ {
file_put_contents($this->path . 'file.txt', ''); mkdir($this->path . '3d/8e', 0777, true);
touch($this->path . '3d/8e/file.txt');
$stream = $this->adapter->read_stream('file.txt'); $stream = $this->adapter->read_stream('file.txt');
$this->assertTrue(is_resource($stream)); $this->assertTrue(is_resource($stream));
fclose($stream); fclose($stream);
unlink($this->path . 'file.txt'); unlink($this->path . '3d/8e/file.txt');
rmdir($this->path . '3d/8e');
rmdir($this->path . '3d');
} }
public function test_write_stream() public function test_write_stream()
@ -118,8 +126,11 @@
$stream = fopen($this->path . 'file.txt', 'rb'); $stream = fopen($this->path . 'file.txt', 'rb');
$this->adapter->write_stream('file2.txt', $stream); $this->adapter->write_stream('file2.txt', $stream);
fclose($stream); fclose($stream);
$this->assertEquals(file_get_contents($this->path . 'file2.txt'), 'abc'); $this->assertEquals(file_get_contents($this->path . '27/36/file2.txt'), 'abc');
unlink($this->path . 'file.txt'); unlink($this->path . 'file.txt');
unlink($this->path . 'file2.txt'); unlink($this->path . '27/36/file2.txt');
rmdir($this->path . '27/36');
rmdir($this->path . '27');
} }
} }