質問

I'm trying to create a unit test for a Factory class, but PHPUnit is returning me a fatal error on the creation of the Product.

Fatal error: Class 'JsonStorage' not found

Files structure

-\
    -libs
    -My
        -Storage
                -IStorage.php
                -IStorageFactory.php
                -StorageFactory.php
                -JsonStorage.php
                -FileStorage.php
    -tests
        -StorageFactoryTest.php

This is the

Creator

use \tests;
namespace My\Storage;

class StorageFactory implements IStorageFactory
{
    public static function build($type)
    {
        $classname = "Storage";

        if (is_string($type)) {
            switch($type = ucfirst(strtolower(trim($type)))) {
                case "Json":    // Fall-through
                case "File":
                    $classname = $type . $classname;
                    break;
                default:
                    throw new \Exception("Storage type not recognized or not yet implemented: please provide a valid type");
            }

            if (class_exists($classname)) {
                try {
                    return new $classname;
                } catch (Exception $e) {
                    throw new \Exception("Cannot create Storage object: please provide a valid type");
                }
            } else {
                throw new \Exception("Class not recognized");

        }

Product of the factory

can be

namespace My\Storage;

class FileStorage implements IStorage
{
    private $filepath;
    private $data;
    private $handle = null;

    public function __construct($filepath = null)
    {
        if (isset($filepath)) {
            $this->setPath($filepath);
        }
    }

    //....
}

or (the case I'm testing)

namespace My\Storage;

class JsonStorage extends FileStorage
{
    // .... additional implementations
}

and I have a

Testing class

    use \tests;
    namespace My\Form\Storage;

    require_once _DIR_ . "\..\My\Form\Storage\IStorage.php";
    require_once .... <all the other classes>.....
    require_once

class StorageFactoryTest extends \PHPUnit_Framework_TestCase
{

    public function testCanBuildJSON()
    {
        $s = StorageFactory::build("JSON");

        $this->assertInstanceOf($s, "IStorage");  // Or better directly 'JsonStorage'?
    }
}

Maybe is something wrong with the namespaces, but I cannot understand why the JsonStorage class is not found.

役に立ちましたか?

解決

The solution was easy (but took me a lot..)

I checked that the class was declared with get_declared_classes(). Then I read that assertInstanceOf needs a full qualified namespace if executed inside a namespace.

So I just changed the

Creator

$classname = __NAMESPACE__."\\".$classname;

if (class_exists($classname)) {
    try {
        return new $classname;
    } //...

and the

Test

public function testCanBuildJSON()
{
    $s = StorageFactory::build("JSON");

    $this->assertInstanceOf(__NAMESPACE__."\\JsonStorage", $s);
}

Hope it could help someone.

PS - Feel free to add details or suggestions for better practices

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top