<?php
 
/**
 
 * $Id: Dir.class.php 562 2006-09-28 16:38:53Z matthieu $
 
 */
 
if (!class_exists('Test_Files_DirectoryListing')) {
 
    if (!defined('__CLASS_PATH__')) {
 
        define('__CLASS_PATH__', realpath(dirname(__FILE__) . '/../../'));
 
    }
 
    if (!defined('__SIMPLETEST_PATH__')) {
 
        define('__SIMPLETEST_PATH__', realpath(__CLASS_PATH__ . '/simpletest/'));
 
    }
 
    require_once __SIMPLETEST_PATH__ . '/shell_tester.php';
 
    require_once __SIMPLETEST_PATH__ . '/reporter.php';
 
    require_once __CLASS_PATH__ . '/Autoload.php';
 
    /**
 
     * unit test case for Files_DirectoryListing
 
     * @author matthieu <[email protected]>
 
     * @package files
 
     * @subpackage unit_test_case
 
     */
 
    class Test_Files_DirectoryListing extends ShellTestCase {
 
        /**
 
         * @var array $references references values
 
         * @access private
 
         */
 
        private $references = array ();
 
        /**
 
         * @var mixed $dir_obj : the directory object
 
         * @access private
 
         */
 
        private $dir_obj = null;
 
        /**
 
         * constructor
 
         * @access public
 
         * @return void
 
         */
 
        public function __construct() {
 
            parent :: __construct();
 
            // use the class path reference as default
 
            $this->references = $this->getFileListFromFolder(__CLASS_PATH__, true);
 
            sort($this->references);
 
            $this->dir_obj = new Files_DirectoryListing(__CLASS_PATH__);
 
        }
 
        /**
 
         * return a list of file in a folder
 
         * @access private
 
         * @return array
 
         * @param string $folder : folder to analyse
 
         * @param boolean $throw_exception : does we need to throw exception if we can open the folder?
 
         */
 
        private function getFileListFromFolder($folder, $throw_exception = false) {
 
            $filelist = array ();
 
            $dir = opendir($folder);
 
            if (!$dir) {
 
                if ($throw_exception) {
 
                    throw Exception('Cannot browse the reference folder. Abort');
 
                }
 
            }
 
            else {
 
                $full_path = '';
 
                while (($file = readdir($dir)) !== false) {
 
                    if (!in_array($file, array (
 
                            '.',
 
                            '..'
 
                        ))) {
 
                        $full_path = $folder . '/' . $file;
 
                        if (is_dir($full_path)) {
 
                            $filelist = array_merge($filelist, $this->getFileListFromFolder($full_path));
 
                        }
 
                        $filelist[] = $full_path;
 
                    }
 
                }
 
                closedir($dir);
 
            }
 
            return $filelist;
 
        }
 
        /**
 
         * test the LIST_files method
 
         * @access public
 
         * @return void
 
         */
 
        public function test_LIST_files_with_subfolders() {
 
            $files = $this->dir_obj->LIST_files('--s');
 
            $expected_results = array ();
 
            foreach ($this->references as $element) {
 
                if (is_file($element)) {
 
                    $expected_results[] = $element;
 
                }
 
            }
 
            $this->_testAnswer($files, $expected_results);
 
        }
 
        /**
 
         * test the LIST_files method for subfolders and extension query
 
         * @access public
 
         * @return void
 
         */
 
        public function test_LIST_files_with_subfolders_and_extensions() {
 
            $files = $this->dir_obj->LIST_files('--s --e php,php5,inc');
 
            $expected_results = array ();
 
            foreach ($this->references as $element) {
 
                if (is_file($element)) {
 
                    $extension = substr($element, strrpos($element, '.') + 1);
 
                    if (in_array($extension, array (
 
                            'php',
 
                            'php5',
 
                            'inc'
 
                        ))) {
 
                        $expected_results[] = $element;
 
                    }
 
                }
 
            }
 
            $this->_testAnswer($files, $expected_results);
 
        }
 
        /**
 
         * test the LIST_files method
 
         * @access public
 
         * @return void
 
         */
 
        public function test_LIST_dir_with_subfolders() {
 
            $files = $this->dir_obj->LIST_dir('--s');
 
            $expected_results = array ();
 
            foreach ($this->references as $element) {
 
                if (is_dir($element)) {
 
                    $expected_results[] = $element;
 
                }
 
            }
 
            $this->_testAnswer($files, $expected_results);
 
        }
 
        /**
 
         * test the LIST_files method
 
         * @access public
 
         * @return void
 
         * @throws Exception
 
         */
 
        public function test_LIST_dir_with_creation_date() {
 
            $create_dir = dirname(__FILE__) . '/temp';
 
            if (mkdir($create_dir)) {
 
                $this->references[] = $create_dir;
 
                $current_date = date("dmY");
 
                $files = $this->dir_obj->LIST_dir('--s --dc = ' . $current_date);
 
                $expected_results = array ();
 
                foreach ($this->references as $element) {
 
                    if (is_dir($element) && (date("dmY", filectime($element)) == $current_date)) {
 
                        $expected_results[] = $element;
 
                    }
 
                }
 
                $this->_testAnswer($files, $expected_results);
 
                $this->assertTrue(in_array($create_dir, $files), "We cannot found with pattern --s --dc = " . $current_date . " the folder $create_dir make today by this test class");
 
                if (!rmdir($create_dir)) {
 
                    throw new Exception('test folder ' . $create_dir . ' cannot be deleted. You must do it self');
 
                }
 
                else {
 
                    array_pop($this->references);
 
                }
 
            }
 
            else {
 
                throw new Exception('We cannot create the folder ' . $create_dir . ' this, test is aborded');
 
            }
 
        }
 
        /**
 
         * @param array $answer : the list of object found
 
         * @acces private
 
         * @return void
 
         */
 
        private function _testAnswer($list, $expected_number_of_results = array ()) {
 
            $this->assertTrue(is_array($list), 'Answer is not an array');
 
            if (count($expected_number_of_results) > 0) {
 
                $this->assertTrue((count($list) > 0), 'File list must not be empty');
 
            }
 
            $this->assertTrue((count($list) == count($expected_number_of_results)), 'List doesnt return the expected element. set the __DEBUG_MODE__ on true to analyse answers');
 
        }
 
    }
 
}
 
 |