<?php

/*
 * Copyright (c) 2015, Leif Lindbäck
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are 
 * met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright 
 *    notice, this list of conditions and the following disclaimer in the 
 *    documentation and/or other materials provided with the distribution.
 * 
 * 3. Neither the name of the copyright holder nor the names of its 
 *    contributors may be used to endorse or promote products derived from 
 *    this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

namespace Id1354fw\Util;

/**
 * @author Leif Lindback, leifl@kth.se
 */
class ClassesTest extends \PHPUnit_Framework_TestCase {

    public function testGetClassPathRoot() {
        $this->assertEquals(Strings::removeTrailingString(Classes::getClassPathRoot(), 'classes'),
                                                          Strings::removeTrailingString(\getcwd(),
                                                                                        'tests'));
    }

    public function testFileNameOfClass() {
        $this->assertEquals('a/b/c.php', $this->classPart(Classes::fileNameOfClass('a\\b\\c')));
        $this->assertEquals('b/c.php', $this->classPart(Classes::fileNameOfClass('b\\c')));
        $this->assertEquals('c.php', $this->classPart(Classes::fileNameOfClass('c')));
        $this->illegalClassName('\\a\\b\\c');
        $this->illegalClassName('');
        $this->illegalClassName(NULL);
        $this->illegalClassName(23);
        $this->illegalClassName(TRUE);
        $this->illegalClassName(FALSE);
    }

    private function illegalClassName($className) {
        try {
            Classes::fileNameOfClass($className);
            $this->fail('Invalid class name accepted, ' . $className);
        } catch (\InvalidArgumentException $iae) {
            return;
        }
    }

    private function classPart($fileName) {
        return Strings::removeLeadingString($fileName,
                                            Classes::getClassPathRoot() . DIRECTORY_SEPARATOR);
    }

    public function testNameOfClassInFile() {
        $this->assertEquals('a\\b\\c', Classes::nameOfClassInFile($this->complFileName('a/b/c.php')));
        $this->assertEquals('b\\c', Classes::nameOfClassInFile($this->complFileName('b/c.php')));
        $this->assertEquals('c', Classes::nameOfClassInFile($this->complFileName('c.php')));
        $this->illegalFileName($this->complFileName('a/b/c'));
        $this->illegalFileName('a/b/c.php');
        $this->illegalFileName('');
        $this->illegalFileName(NULL);
        $this->illegalFileName(23);
        $this->illegalFileName(TRUE);
        $this->illegalFileName(FALSE);
    }

    public function testNamespaceOfClass() {
        $this->assertEquals('/a/b', Classes::namespaceOfClass('a\\b\\c'));
        $this->assertEquals('/b', Classes::namespaceOfClass('b\\c'));
        $this->assertEquals('', Classes::namespaceOfClass('c'));
        $this->illegalClassName('\\a\\b\\c');
        $this->illegalClassName('');
        $this->illegalClassName(NULL);
        $this->illegalClassName(23);
        $this->illegalClassName(TRUE);
        $this->illegalClassName(FALSE);
    }

    /**
     * NOT COMPLETE!! SHOULT TEST ALL NON_ALNUM CHARACHTERS.
     */
    public function testValidateClassName() {
        $this->illegalClassName('\\a\\b\\c');
        $this->illegalClassName('a\\\\b\\c');
        $this->illegalClassName('');
        $this->illegalClassName(NULL);
        $this->illegalClassName(23);
        $this->illegalClassName(TRUE);
        $this->illegalClassName(FALSE);
        $this->illegalClassName(')');
        $this->illegalClassName('a)a');
        $this->illegalClassName('a((');
        $this->illegalClassName('/aa');
        $this->illegalClassName('a.b');
        $this->illegalClassName('a#b');
        $this->illegalClassName('a?b');
        $this->illegalClassName('a&b');
        $this->illegalClassName('a%b');
        $this->illegalClassName('a$b');
        Classes::validateClassName('a\\b\\c');
    }

    private function illegalFileName($fileName) {
        try {
            Classes::nameOfClassInFile($fileName);
            $this->fail('Invalid class name accepted, ' . $fileName);
        } catch (\InvalidArgumentException $iae) {
            return;
        }
    }

    private function complFileName($classPart) {
        return Classes::getClassPathRoot() . DIRECTORY_SEPARATOR . $classPart;
    }

}
