PHP Classes

File: tests/util-tests/DirectoryTest.php

Recommend this page to a friend!
  Classes of Maik Greubel   PHP Generics   tests/util-tests/DirectoryTest.php   Download  
File: tests/util-tests/DirectoryTest.php
Role: Unit test script
Content type: text/plain
Description: Unit test for Directory utility class
Class: PHP Generics
Framework for accessing streams, sockets and logs
Author: By
Last change: Update of tests/util-tests/DirectoryTest.php
Date: 6 months ago
Size: 1,720 bytes
 

Contents

Class file image Download
<?php

namespace Generics\Tests;

use
Generics\Util\Directory;
use
Generics\Util\RandomString;
use
Generics\Streams\FileOutputStream;

class
DirectoryTest extends \PHPUnit\Framework\TestCase
{
    public function
testCreateAndRemoveSimple()
    {
       
$tempDirName = RandomString::generate(8, RandomString::ASCII);
       
$dir = new Directory(getcwd() . "/$tempDirName");
       
$dir->create();
       
$dir->isEmpty();
       
$dir->remove();
       
$this->assertFalse($dir->exists());
    }

    public function
testCreateAndRemoveRecursive()
    {
       
$tempDirName = RandomString::generate(8, RandomString::ASCII);
       
$dir = new Directory(getcwd() . $tempDirName . '/subdir1/subdir2');
       
$dir->create(true);

       
$tempFileName = tempnam($dir->getPath(), 'txt');
       
$fd = fopen($tempFileName, "w");
       
fclose($fd);

       
$dir2 = new Directory(getcwd() . $tempDirName);
       
$dir2->remove(true);

       
$this->assertFalse($dir2->exists());
    }

    public function
testFileExists()
    {
       
$fileName = "test.txt";
       
$tempDirName = RandomString::generate(8, RandomString::ASCII);

       
$dir = new Directory(getcwd() . "/$tempDirName");
       
$this->assertFalse($dir->fileExists($fileName));

       
$dir->create();

       
$this->assertFalse($dir->fileExists($fileName));

       
$file = new FileOutputStream($dir->getPath() . '/' . $fileName);
       
$file->write("Rant data");
       
$file->close();

       
$this->assertTrue($dir->fileExists($fileName));

       
$this->assertFalse($dir->isEmpty());
       
$this->assertTrue($dir->isEmpty($fileName));

       
$dir->remove(true);

       
$this->assertFalse($dir->fileExists($fileName));
    }
}