Location: PHPKode > projects > GnomePHP > peec-GnomePHP-b5a360b/gnomephp/testsuite/TestBrowser.php
<?php
namespace gnomephp\testsuite;
/**
 * Virtual browser that has test mechanisms.
 * 
 * @author peec
 *
 */
class TestBrowser implements CaseInterface{
	
	/**
	 * Url of what page we are at.
	 * @var string
	 */
	private $url;
	
	/**
	 * Result tests.
	 * @var array
	 */
	private $result=array();
	
	/**
	 * Content of this webpage.
	 * @var string
	 */
	private $content = false;
	
	/**
	 * Curl response array.
	 * @var array
	 */
	private $response = null;
	

	
	/**
	 * Dom Document of the content parsed.
	 * @var \DomDocument
	 */
	private $document = null;
	
	
	/**
	 * Starts a new web page based on url.
	 * Test variables will not be reset but all content and response variables will be reset.
	 * 
	 * @param string $url Full URL  to where we want to go ( including http://host )
	 */
	public function __construct($url){
		$this->url = $url;
		
		$this->content = @file_get_contents($url);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $this->content = curl_exec($ch);
		$this->response = curl_getinfo($ch);
			
		if ($this->content){
			$this->document = new \DomDocument('1.0', 'utf-8');
			$this->document->preserveWhiteSpace = false;
			
			$this->document->loadHTML($this->content);

		}
		curl_close($ch);
		
	}
	
	/**
	 * Gets the url we are working with.
	 */
	public function getName(){
		return $this->url;
	}
	
	
	/**
	 * (non-PHPdoc)
	 * @see gnomephp\testsuite.CaseInterface::getValue()
	 */
	public function getValue(){
	
	}
	

	/**
	 * Checks if response code(s) are equal to $allowResponses
	 * @param mixed $allowedResponses eg. 200 or array(200, 300, 400)
	 */
	public function assertResponse($allowedResponses){
		if ($this->response){
			$this->result[] = array(__METHOD__, (
					is_array($allowedResponses) ? in_array($this->response['http_code'], $allowedResponses)
				 	: $allowedResponses == $this->response['http_code'] 
				 )
			);
		}else{
			$this->result[] = array(__METHOD__, false);
		}
		return $this;
	}
	
	
	
	/**
	 * Matches if the sites title element value is exact match to $title
	 * @param string $title Title to match it to.
	 */
	public function assertTitle($title){
		if ($this->document)$elems = $this->document->getElementsByTagName('title');
		if(isset($elems))$t = $elems->item(0);
		$this->result[] = array(__METHOD__,isset($t) && $t->nodeValue == $title);
		return $this;
	}
	
	/**
	 * Clicks on a link and refills the current browser with new values.
	 * Notice if it does not detect protocol / host sign ( X://x ) it will
	 * prepend $protocol://$host to $href
	 * @param string $linkName
	 */
	public function click($linkName, $protocol = 'http', $host='localhost'){
		$nodes = $this->document->getElementsByTagName('a');
		foreach($nodes as $link){
			if ($linkName == $link->nodeValue){
				$href = $link->getAttribute('href');
				if (!strstr($href, '://')){
					$href = $protocol . '://' . $host . $href; 
				}
				$this->__construct($href);
				return $this;
			}
		}
		
		return $this;
	}
	
	
	/**
	 * Matches if we find the text $text
	 * @param string $text Passes test if we find this text on the page.
	 */
	public function assertText($text){
		$this->result[] = array(__METHOD__, strstr($this->content, $text));
		return $this;
	}
	
	
	/**
	 * Validates all the asserts.
	 * Returns false if one of the asserts failed.
	 */
	public function validate(){
		foreach($this->result as $res){
			if (!$res[1])return false;
		}
		return true;
	}
	/**
	 * Gets all the failed assertions.
	 */
	public function getFailedAsserts(){
		$array = array();
		foreach($this->result as $res){
			if (!$res[1])$array[] = $res;
		}
		return $array;
	}
}
Return current item: GnomePHP