1
测试代码
function it_records_last_checked()
{
$this->getWrappedObject()->setServiceLocator($this->getServiceLocator());
$this->isAvailable('google.com')->shouldReturn(false);
/** @var Url $last */
$last = $this->getLastChecked();
$last->shoudHaveType(Url::class);
$last->host->registrableDomain->shouldBeLike('google.com');
}
该规范包装了一个对象,其代码是这样的:
namespace Application\Service;
use Application\Exception\DomainInvalidException;
use Application\Model\Whois;
use Pdp\Uri\Url;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorAwareTrait;
use Application\Exception\DomainRequiredException;
class DomainService implements ServiceLocatorAwareInterface{
use ServiceLocatorAwareTrait;
/** @var Url */
protected $last_checked;
/**
* @return Url
*/
public function getLastChecked()
{
return $this->last_checked;
}
/**
* @param Url $last_checked
*/
public function setLastChecked($last_checked)
{
$this->last_checked = $last_checked;
}
/**
* Use available configuration to determine if a domain is available
* @param $domain
* @return bool
* @throws DomainRequiredException
* @throws \Exception
*/
public function isAvailable($domain)
{
if(!$domain)
throw new DomainRequiredException();
$pslManager = new \Pdp\PublicSuffixListManager();
$parser = new \Pdp\Parser($pslManager->getList());
$host = 'http://' . $domain;
if(!$parser->isSuffixValid($host))
throw new DomainInvalidException();
$this->last_checked = $parser->parseUrl($host);
$whois = new Whois($this->last_checked->host->registerableDomain);
return $whois->isAvailable();
}
}
服务设置其last_checked构件我想测试其类型例。它似乎没有返回一个包装对象,它返回实际的Pdp \ Uri \ Url实例。
写作测试中的规则是什么,以确保我们获得包装物体(主体)?
谢谢!
皮特,非常感谢您花时间详细说明了这些细节。当前的网格是我使用的几个packagist组件的功能,修改它们会产生连锁反应,但我可以看到它的好处。我想我必须评估重写这些项目的可测试性的商业案例。非常感谢! – Saeven