开发者

PHPUnit: Test an an factory class with a dependency

I've got following factory class which has a dependency $client. I moved the dependency to the factory class as it first occurred in Service_Service1 class so I could test the service class. But how can I test the factory class?

class Factory implements Service
{

    public static function factory($service)
    {
        $config = Zend_Registry::get('config');

        switch ($service) {
            case Service::Service1:
                $client = new Zend_Soap_Client(
                    $config->url,
                    array(
                        'encoding' => 'UTF开发者_开发问答-8'
                    )
                );
                $pickupPoint = new Service_Service1($client);
                break;
        }
    }
}


I am not sure what the question is, but if it is how to stub the hardcoded dependencies, have a look at this article by the author of PHPUnit:

  • http://sebastian-bergmann.de/archives/885-Stubbing-Hard-Coded-Dependencies.html

Your best would be to inject instances of those classes instead of hardcoding them. As an alternative you could make their classnames properties in the Factory, so you can exchange what it will instantiate.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜