<?php

/*
 * @copyright NetMonsters <team@netmonsters.ru>
 * @link http://netmonsters.ru
 * @package Majestic
 * @subpackage UnitTests
 * @since 2011-10-..
 * 
 * Unit tests for AutoloadBuilder class
 */

require_once 'vfsStream/vfsStream.php';
require_once dirname(__FILE__) . '/../../util/AutoloadBuilder.php';

/**
 * @TODO: AutoloadBuilder - fix writing to file: construct array first, write to file next - atomic operation
 */
class AutoloadBuilderTest extends PHPUnit_Framework_TestCase
{

    private static $inc_dirs = array();
    private static $file;

    /**
     * @TODO: Load->buildAutoload() - uses two paths - PATH . '/' . APP . '/src' and PATH . '/lib' those are not checked. Can couse error.
     * @expectedException UnexpectedValueException
     * @expectedException PHPUnit_Framework_Error
     */
    public static function setUpBeforeClass()
    {
        if (!defined('PATH')) {
            define('PATH', realpath(dirname(__FILE__) . '/../../../..'));
        }

// default application name
        if (!defined('APP')) {
            define('APP', 'lib/core/tests/face');
        }

        self::$file = PATH . '/' . APP . '/cache/autoload.php';

        // value - if dir exists (to delete it if it was created), default true
        self::$inc_dirs[PATH . '/' . APP . '/src'] = true;
        self::$inc_dirs[PATH . '/' . APP . '/cache'] = true;
        self::$inc_dirs[PATH . '/lib'] = true;

        foreach (self::$inc_dirs as $dir => &$is_exist) {
            if (!file_exists($dir)) {
                $is_exist = false;
                mkdir($dir, 0777, true);
            }
        }
    }
    
    /**
     * @TODO: AutoloadBuilder - check input params: string for filename, array for dirs
     * @expectedException PHPUnit_Framework_Error
     */
    public function testBuildParams()
    {
        $autoload = self::$file;
        $dirs = 'string';
        $builder = new AutoloadBuilder($autoload, $dirs);

        $builder->build();        
    }
    
    public function testBuild()
    {
        $builder = new AutoloadBuilder(self::$file, array(PATH . '/' . APP . '/src', PATH . '/' . APP . '/cache', PATH . '/lib'));

        $builder->build();  
        
        $this->assertFileExists(self::$file);
    }    
        
    public function testAutoloadFileExists()
    {
        $this->assertFileExists(self::$file);
    }
        
    public function testAutoloadArray()
    {
        $this->assertFileExists(self::$file);
        $array = require self::$file;
        $this->assertInternalType('array', $array);
        $this->assertNotEmpty($array);
    }
        
    /** 
     * @expectedException PHPUnit_Framework_Error
     */
    public function testAccessDenied()
    {
        chmod(self::$file, 0400);
        $builder = new AutoloadBuilder(self::$file, array(PATH . '/' . APP . '/src', PATH . '/' . APP . '/cache', PATH . '/lib'));

        $builder->build();  
        chmod(self::$file, 0777);
    }
    
    public static function tearDownAfterClass()
    {
        if (file_exists(self::$file)) {
            unlink(self::$file);
        }

        foreach (self::$inc_dirs as $dir => $is_exist) {
            if (!$is_exist) {
                rmdir($dir);
            }
        }
        rmdir(PATH . '/' . APP);
    }

}