Files
DomFramework/Tests/FileTest.php

533 lines
14 KiB
PHP

<?php
/**
* DomFramework - Tests
* @package domframework
* @author Dominique Fournier <dominique@fournier38.fr>
* @license BSD
*/
namespace Domframework\Tests;
use Domframework\File;
/**
* Test the domframework File part
*/
class FileTest extends \PHPUnit_Framework_TestCase
{
public function testinit()
{
exec("rm -rf /tmp/testDFFileDir");
}
public function testRealPath01()
{
$file = new File();
$res = $file->realpath("/tmp");
$this->assertSame($res, "/tmp");
}
public function testRealPath02()
{
$file = new File();
$res = $file->realpath("////tmp");
$this->assertSame($res, "/tmp");
}
public function testRealPath03()
{
$file = new File();
$res = $file->realpath("////tmp////");
$this->assertSame($res, "/tmp");
}
public function testRealPath04()
{
$file = new File();
$res = $file->realpath(".////tmp////");
$this->assertSame($res, "./tmp");
}
public function testRealPath05()
{
$file = new File();
$res = $file->realpath("../../../../../tmp/file");
$this->assertSame($res, "/tmp/file");
}
public function testRealPath06()
{
$file = new File();
$res = $file->realpath("../../../../../tmp/file/../../../..");
$this->assertSame($res, "/");
}
public function testRealPath07()
{
$file = new File();
$res = $file->realpath(".././././../tmp/file/../././..");
$this->assertSame($res, "/");
}
public function testRealPath11()
{
$file = new File();
$file->chroot("/tmp");
$res = $file->realpath("/tmp2");
$this->assertSame($res, "/tmp/tmp2");
}
public function testRealPath12()
{
$file = new File();
$file->chroot("/tmp");
$res = $file->realpath("////tmp2");
$this->assertSame($res, "/tmp/tmp2");
}
public function testRealPath13()
{
$file = new File();
$file->chroot("/tmp");
$res = $file->realpath("////tmp2////");
$this->assertSame($res, "/tmp/tmp2");
}
public function testRealPath14()
{
$file = new File();
$file->chroot("/tmp");
$res = $file->realpath(".////tmp2////");
$this->assertSame($res, "/tmp/tmp2");
}
public function testRealPath15()
{
$file = new File();
$file->chroot("/tmp");
$res = $file->realpath("../../../../../tmp2/file");
$this->assertSame($res, "/tmp/tmp2/file");
}
public function testRealPath16()
{
$file = new File();
$file->chroot("/tmp");
$res = $file->realpath("../../../../../tmp2/file/../../../..");
$this->assertSame($res, "/tmp");
}
public function testRealPath17()
{
$file = new File();
$file->chroot("/tmp");
$res = $file->realpath(".././././../tmp2/file/../././..");
$this->assertSame($res, "/tmp");
}
public function testGetcwd1()
{
$file = new File();
$res = $file->getcwd();
$this->assertSame($res, ".");
}
public function testGetcwd2()
{
$file = new File();
$file->chdir("/tmp");
$res = $file->getcwd();
$this->assertSame($res, "/tmp");
}
public function testGetcwd3()
{
$this->setExpectedException("Exception", "Path '/NON EXISTS' not found");
$file = new File();
$file->chdir("/NON EXISTS");
$res = $file->getcwd();
}
public function testGetcwd4()
{
$file = new File();
$file->chdir("/../../../tmp");
$res = $file->getcwd();
$this->assertSame($res, "/tmp");
}
public function testGetcwd5()
{
$file = new File();
$file->chdir("../../../../../../tmp");
$res = $file->getcwd();
$this->assertSame($res, "/tmp");
}
public function testGetcwd6()
{
$this->setExpectedException("Exception", "Path './tmp' not found");
$file = new File();
$file->chdir("./tmp");
$res = $file->getcwd();
}
public function testGetcwd7()
{
$this->setExpectedException(
"Exception",
"Path '/tmp/NON EXISTS' not found"
);
$file = new File();
$file->chdir("/tmp/NON EXISTS");
$res = $file->getcwd();
}
public function testGetcwd8()
{
$this->setExpectedException(
"Exception",
"Parent Path '/NON EXISTS/' not found"
);
$file = new File();
$file->chdir("/NON EXISTS/tmp");
$res = $file->getcwd();
}
public function testchroot1()
{
$this->setExpectedException("Exception");
$file = new File();
$res = $file->chroot("non exists");
}
public function testchroot2()
{
$file = new File();
$res = $file->chroot("/tmp");
$this->assertSame($res, true);
}
// Without chroot
public function testMkdir1()
{
$file = new File();
$res = $file->mkdir("/tmp/testmkdir1-" . time());
$this->assertSame($res, true);
}
// With chroot in tmp
public function testMkdir2()
{
$file = new File();
$res = $file->chroot("/tmp");
$res = $file->mkdir("/testmkdir2-" . time());
$this->assertSame($res, true);
}
// Without chroot and relative
public function testMkdir3()
{
$file = new File();
$res = $file->mkdir("/tmp/testmkdir3-" . time());
$this->assertSame($res, true);
}
// With chroot in tmp and in relative
public function testMkdir4()
{
$file = new File();
$res = $file->chroot("/tmp");
$res = $file->mkdir("testmkdir4-" . time());
$this->assertSame($res, true);
}
// With chroot in tmp
public function testChdir1()
{
if (file_exists("/tmp/testDFFileDir")) {
rmdir("/tmp/testDFFileDir");
}
$file = new File();
$file->chroot("/tmp");
$file->mkdir("/testDFFileDir");
$file->chdir("/testDFFileDir");
$res = $file->getcwd();
$this->assertSame($res, "/testDFFileDir");
}
// With chroot in tmp
public function testChdir2()
{
if (file_exists("/tmp/testDFFileDir")) {
rmdir("/tmp/testDFFileDir");
}
$file = new File();
$file->chroot("/tmp");
$file->mkdir("/testDFFileDir");
$file->chroot("/");
$file->chdir("/testDFFileDir");
$res = $file->getcwd();
$this->assertSame($res, "/testDFFileDir");
}
// With chroot in tmp
public function testChdir3()
{
$this->setExpectedException(
"Exception",
"Path '/tmp/testDFFileDir/NON EXISTS' not found"
);
if (file_exists("/tmp/testDFFileDir")) {
rmdir("/tmp/testDFFileDir");
}
$file = new File();
$file->chroot("/tmp");
$file->mkdir("/testDFFileDir");
$file->chroot("/testDFFileDir");
$file->chdir("/NON EXISTS");
}
// With chroot in tmp
public function testChdir4()
{
$this->setExpectedException(
"Exception",
"Parent Path '/tmp/tmp/' not found"
);
if (file_exists("/tmp/testDFFileDir")) {
rmdir("/tmp/testDFFileDir");
}
$file = new File();
$file->chroot("/tmp");
$file->mkdir("/testDFFileDir");
$file->chroot("..");
$file->chdir("/tmp/testDFFileDir");
}
public function testFileGetContents1()
{
$this->setExpectedException(
"Exception",
"File '/tmp/testDFFileDir' is not a file"
);
if (file_exists("/tmp/testDFFileDir")) {
rmdir("/tmp/testDFFileDir");
}
$file = new File();
$file->chroot("/tmp");
$file->mkdir("/testDFFileDir");
$res = $file->file_get_contents("/testDFFileDir");
}
public function testFileGetContents2()
{
if (file_exists("/tmp/testDFFileDir")) {
rmdir("/tmp/testDFFileDir");
}
$file = new File();
$file->chroot("/tmp");
$file->mkdir("/testDFFileDir");
$file->touch("/testDFFileDir/toto");
$res = $file->file_get_contents("/testDFFileDir/toto");
$this->assertSame($res, "");
}
public function testFilePutContents1()
{
$file = new File();
$file->chroot("/tmp");
$file->file_put_contents("/testDFFileDir/toto", "content");
$res = $file->file_get_contents("/testDFFileDir/toto");
$this->assertSame($res, "content");
}
public function testFilePutContents2()
{
$this->setExpectedException(
"Exception",
"File '/tmp/testDFFileDir' is not a file"
);
$file = new File();
$file->file_put_contents("/tmp/testDFFileDir", "content");
}
public function testFilePutContents3()
{
$file = new File();
$file->chroot("/tmp");
$res = $file->file_put_contents("/testDFFileDir/toto", "content");
$this->assertSame($res, 7);
}
public function testscandir1()
{
$file = new File();
$res = $file->scandir("/tmp/testDFFileDir");
$this->assertSame($res, ["toto"]);
}
public function testscandir2()
{
$file = new File();
$file->chroot("/tmp");
$res = $file->scandir("/testDFFileDir");
$this->assertSame($res, ["toto"]);
}
public function testscandir3()
{
$file = new File();
$file->chroot("/tmp");
$res = $file->scandir("/testDFFileDir/");
$this->assertSame($res, ["toto"]);
}
public function testrmdir1()
{
// Directory not empty and NOT recursive : return false
$file = new File();
$res = $file->rmdir("/tmp/testDFFileDir");
$this->assertSame($res, false);
}
public function testrmdir2()
{
// Directory not empty and NOT recursive : return false
$file = new File();
$file->chroot("/tmp");
$res = $file->rmdir("/testDFFileDir");
$this->assertSame($res, false);
}
public function testrmdir3()
{
// Directory not empty and recursive : return true
$file = new File();
$file->chroot("/tmp");
$res = $file->rmdir("/testDFFileDir", true);
$this->assertSame($res, true);
}
public function testUnlink1()
{
if (file_exists("/tmp/testDFFileDir")) {
rmdir("/tmp/testDFFileDir");
}
$file = new File();
$file->chroot("/tmp");
$file->mkdir("/testDFFileDir");
$file->touch("/testDFFileDir/toto");
$res = $file->unlink("/testDFFileDir/toto");
$this->assertSame($res, true);
}
public function testIsDir1()
{
$file = new File();
$file->chroot("/tmp");
$res = $file->is_dir("//testDFFileDir");
$this->assertSame($res, true);
}
public function testIsDir2()
{
$file = new File();
$file->chroot("/tmp");
$file->touch("/testDFFileDir/toto");
$res = $file->is_dir("//testDFFileDir/toto");
$this->assertSame($res, false);
}
public function testIsFile1()
{
$file = new File();
$file->chroot("/tmp");
$file->touch("/testDFFileDir/toto");
$res = $file->is_file("/testDFFileDir/toto");
$this->assertSame($res, true);
}
public function testIsFile2()
{
$file = new File();
$file->chroot("/tmp");
$file->touch("/testDFFileDir/toto");
$res = $file->is_file("//testDFFileDir");
$this->assertSame($res, false);
}
public function testMkdir5()
{
$file = new File();
$file->chroot("/tmp");
// The parent doesn't exists and not recursive mode : exception
$this->expectException();
$res = $file->mkdir("/testDFFileDir/tptp/titi/poo");
}
public function testMkdir6()
{
$file = new File();
$file->chroot("/tmp");
$res = $file->mkdir("/testDFFileDir/tptp/titi/poo", 0777, true);
$this->assertSame($res, true);
}
public function testGlob1()
{
$file = new File();
$file->chroot("/tmp");
$res = $file->glob("/testDFFileDir/*");
$this->assertSame($res, ["/testDFFileDir/toto",
"/testDFFileDir/tptp"]);
}
public function testGlob2()
{
$file = new File();
$file->chroot("/tmp");
$file->chdir("/testDFFileDir");
$res = $file->glob("*");
$this->assertSame($res, ["toto",
"tptp"]);
}
public function testGlob3()
{
$file = new File();
$file->chroot("/tmp");
$file->chdir("/testDFFileDir");
$res = $file->glob("/testDFFileDir/*");
$this->assertSame($res, ["/testDFFileDir/toto",
"/testDFFileDir/tptp"]);
}
public function testGlob4()
{
$file = new File();
$res = $file->glob("/tmp/testDFFileDir/*");
$this->assertSame($res, ["/tmp/testDFFileDir/toto",
"/tmp/testDFFileDir/tptp"]);
}
public function testGlob5()
{
$file = new File();
$file->chdir("/tmp/testDFFileDir");
$res = $file->glob("*");
$this->assertSame($res, ["toto",
"tptp"]);
}
public function testGlob6()
{
$file = new File();
$file->chdir("/tmp/testDFFileDir");
$res = $file->glob("/tmp/testDFFileDir/*");
$this->assertSame($res, ["/tmp/testDFFileDir/toto",
"/tmp/testDFFileDir/tptp"]);
}
}