# Autoloader unit test - first attempt

I am learning TDD and I want to test my AutoLoader class, what needs to do the next job:

• do a check is the namespace is string
• do a check, is the path to directory parameter is a string
• do a check is the path to directory what I want to load is exists, directory and readable.

There will be more jobs, but I am newbee, so I need your opinion about my test:

<?php

/**
* @var string
*/
protected $namespace; /** * @var string */ protected$path;

public function __construct($namespace,$pathToClassesDirectory = '') {
if (!is_string($namespace)) { throw new \Exception('Namespace must be string'); } if (!is_string($pathToClassesDirectory)) {
throw new \Exception('Path to directory parameter should be string');
}
if (!file_exists($pathToClassesDirectory) || !is_dir($pathToClassesDirectory) || !is_readable($pathToClassesDirectory)) { throw new \Exception('Path to directory is not accessible to read: "' .$pathToClassesDirectory . '"');
}
$this->namespace =$namespace;
$this->path =$pathToClassesDirectory;
}

}


The class:

    <?php

use PHPUnit\Framework\TestCase;

/**
* @author vaso
*/

protected $Autoloader; public function setUp() {$this->Autoloader = new Myframework\Application\Autoloader\Autoloader(TEST_NAMESPACE, BASE_PATH);
}

public function tearDown() {
unset($this->Autoloader); } public function testConstructorCall() { new Myframework\Application\Autoloader\Autoloader('nameSpaceAsString', BASE_PATH); } public function testConstructorCallNameSpaceIsNotStringException() {$this->expectException('Exception');
}

public function testConstructorCallPathIsNotStringException() {
$this->expectException('Exception');$notStringForPath = new stdClass();
new Myframework\Application\Autoloader\Autoloader(TEST_NAMESPACE, $notStringForPath); } public function testIsDirectoryExistsException() {$this->expectException('Exception');
}

}


Test passed, and my methods are works, I am happy. But I doubt this is not how TDD works even if I follow every requirements.

My questions are:

• Is it ok to use constants in test what comes from bootstrap.php to define path?
• All of the test ok, but is this the right way to test a constructor? I feel no for some reason, if its ok, please tell me, if not, please tell me why.

I found a lot of resources about TDD, videos, books, but testing the constructor is not in it.

• Not sure I am following. Where does loading actually happen? Also, I would think that in this day and age, if one were to build an autoloader, they would do son in PSR-4 compliant fashion. See PSR-4 examples implementations here: github.com/php-fig/fig-standards/blob/master/accepted/… I guess I don't understand the need to "roll your own" autoloader at this point when this is territory that is very well covered in PHP. – Mike Brant Mar 6 '17 at 22:43
• Load is not implemented yet, and I am loading as PSR suggested in other projects. That is a unit, what I can test by unit test. Yes, I am know unit test is for units, but if I want to be sure that my code is coveraged I should write tests for the constructor too. – vaso123 Mar 7 '17 at 8:55