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