For example:
Test code
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');
}
The spec wraps an object whose code is this:
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();
}
}
The service sets its last_checked member whose type I want to test for example. It seems that it doesn't return a wrapped object, it returns the actual Pdp\Uri\Url instance.
What's the rule in writing tests, to ensure that we get wrapped objects back (Subject)?
Thanks!