mardi 5 mai 2015

First time unit testing

This is the first time I'm getting into PHPUnit (unit tests), and I've already written some tests for my constructor, but how would I go about properly testing the following methods?

Can somebody give me an example so I could get an idea how to start with it? I've read about mocks and stubs, but I don't think I fully grasp it yet, because I still have no idea what would be the proper way of writing tests for this.

class Autoloader
{
    private $mappings = [];

    //__construct

    public function mapPrefix($prefix, array $baseDirs, $prepend = false)
    {
        if (!isset($this->mappings[$prefix])) {
            $this->mappings[$prefix] = [];
        }

        if ($prepend) {
            $baseDirs = array_reverse($baseDirs);

            foreach ($baseDirs as $baseDir) {
                array_unshift($this->mappings[$prefix], $baseDir);
            }

            return;
        }

        $this->mappings[$prefix] = array_merge($this->mappings[$prefix], $baseDirs);
    }

    private function loadClassFile($className)
    {
        if (!$filePath = $this->findClassFile($className)) {
            return false;
       }

        require_once $filePath;

        return true;
    }

    //etc.
}

Aucun commentaire:

Enregistrer un commentaire