web_tester.php 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532
  1. <?php
  2. /**
  3. * Base include file for SimpleTest.
  4. * @package SimpleTest
  5. * @subpackage WebTester
  6. * @version $Id: web_tester.php 2013 2011-04-29 09:29:45Z pp11 $
  7. */
  8. /**#@+
  9. * include other SimpleTest class files
  10. */
  11. require_once(dirname(__FILE__) . '/test_case.php');
  12. require_once(dirname(__FILE__) . '/browser.php');
  13. require_once(dirname(__FILE__) . '/page.php');
  14. require_once(dirname(__FILE__) . '/expectation.php');
  15. /**#@-*/
  16. /**
  17. * Test for an HTML widget value match.
  18. * @package SimpleTest
  19. * @subpackage WebTester
  20. */
  21. class FieldExpectation extends SimpleExpectation {
  22. private $value;
  23. /**
  24. * Sets the field value to compare against.
  25. * @param mixed $value Test value to match. Can be an
  26. * expectation for say pattern matching.
  27. * @param string $message Optiona message override. Can use %s as
  28. * a placeholder for the original message.
  29. * @access public
  30. */
  31. function __construct($value, $message = '%s') {
  32. parent::__construct($message);
  33. if (is_array($value)) {
  34. sort($value);
  35. }
  36. $this->value = $value;
  37. }
  38. /**
  39. * Tests the expectation. True if it matches
  40. * a string value or an array value in any order.
  41. * @param mixed $compare Comparison value. False for
  42. * an unset field.
  43. * @return boolean True if correct.
  44. * @access public
  45. */
  46. function test($compare) {
  47. if ($this->value === false) {
  48. return ($compare === false);
  49. }
  50. if ($this->isSingle($this->value)) {
  51. return $this->testSingle($compare);
  52. }
  53. if (is_array($this->value)) {
  54. return $this->testMultiple($compare);
  55. }
  56. return false;
  57. }
  58. /**
  59. * Tests for valid field comparisons with a single option.
  60. * @param mixed $value Value to type check.
  61. * @return boolean True if integer, string or float.
  62. * @access private
  63. */
  64. protected function isSingle($value) {
  65. return is_string($value) || is_integer($value) || is_float($value);
  66. }
  67. /**
  68. * String comparison for simple field with a single option.
  69. * @param mixed $compare String to test against.
  70. * @returns boolean True if matching.
  71. * @access private
  72. */
  73. protected function testSingle($compare) {
  74. if (is_array($compare) && count($compare) == 1) {
  75. $compare = $compare[0];
  76. }
  77. if (! $this->isSingle($compare)) {
  78. return false;
  79. }
  80. return ($this->value == $compare);
  81. }
  82. /**
  83. * List comparison for multivalue field.
  84. * @param mixed $compare List in any order to test against.
  85. * @returns boolean True if matching.
  86. * @access private
  87. */
  88. protected function testMultiple($compare) {
  89. if (is_string($compare)) {
  90. $compare = array($compare);
  91. }
  92. if (! is_array($compare)) {
  93. return false;
  94. }
  95. sort($compare);
  96. return ($this->value === $compare);
  97. }
  98. /**
  99. * Returns a human readable test message.
  100. * @param mixed $compare Comparison value.
  101. * @return string Description of success
  102. * or failure.
  103. * @access public
  104. */
  105. function testMessage($compare) {
  106. $dumper = $this->getDumper();
  107. if (is_array($compare)) {
  108. sort($compare);
  109. }
  110. if ($this->test($compare)) {
  111. return "Field expectation [" . $dumper->describeValue($this->value) . "]";
  112. } else {
  113. return "Field expectation [" . $dumper->describeValue($this->value) .
  114. "] fails with [" .
  115. $dumper->describeValue($compare) . "] " .
  116. $dumper->describeDifference($this->value, $compare);
  117. }
  118. }
  119. }
  120. /**
  121. * Test for a specific HTTP header within a header block.
  122. * @package SimpleTest
  123. * @subpackage WebTester
  124. */
  125. class HttpHeaderExpectation extends SimpleExpectation {
  126. private $expected_header;
  127. private $expected_value;
  128. /**
  129. * Sets the field and value to compare against.
  130. * @param string $header Case insenstive trimmed header name.
  131. * @param mixed $value Optional value to compare. If not
  132. * given then any value will match. If
  133. * an expectation object then that will
  134. * be used instead.
  135. * @param string $message Optiona message override. Can use %s as
  136. * a placeholder for the original message.
  137. */
  138. function __construct($header, $value = false, $message = '%s') {
  139. parent::__construct($message);
  140. $this->expected_header = $this->normaliseHeader($header);
  141. $this->expected_value = $value;
  142. }
  143. /**
  144. * Accessor for aggregated object.
  145. * @return mixed Expectation set in constructor.
  146. * @access protected
  147. */
  148. protected function getExpectation() {
  149. return $this->expected_value;
  150. }
  151. /**
  152. * Removes whitespace at ends and case variations.
  153. * @param string $header Name of header.
  154. * @param string Trimmed and lowecased header
  155. * name.
  156. * @access private
  157. */
  158. protected function normaliseHeader($header) {
  159. return strtolower(trim($header));
  160. }
  161. /**
  162. * Tests the expectation. True if it matches
  163. * a string value or an array value in any order.
  164. * @param mixed $compare Raw header block to search.
  165. * @return boolean True if header present.
  166. * @access public
  167. */
  168. function test($compare) {
  169. return is_string($this->findHeader($compare));
  170. }
  171. /**
  172. * Searches the incoming result. Will extract the matching
  173. * line as text.
  174. * @param mixed $compare Raw header block to search.
  175. * @return string Matching header line.
  176. * @access protected
  177. */
  178. protected function findHeader($compare) {
  179. $lines = explode("\r\n", $compare);
  180. foreach ($lines as $line) {
  181. if ($this->testHeaderLine($line)) {
  182. return $line;
  183. }
  184. }
  185. return false;
  186. }
  187. /**
  188. * Compares a single header line against the expectation.
  189. * @param string $line A single line to compare.
  190. * @return boolean True if matched.
  191. * @access private
  192. */
  193. protected function testHeaderLine($line) {
  194. if (count($parsed = explode(':', $line, 2)) < 2) {
  195. return false;
  196. }
  197. list($header, $value) = $parsed;
  198. if ($this->normaliseHeader($header) != $this->expected_header) {
  199. return false;
  200. }
  201. return $this->testHeaderValue($value, $this->expected_value);
  202. }
  203. /**
  204. * Tests the value part of the header.
  205. * @param string $value Value to test.
  206. * @param mixed $expected Value to test against.
  207. * @return boolean True if matched.
  208. * @access protected
  209. */
  210. protected function testHeaderValue($value, $expected) {
  211. if ($expected === false) {
  212. return true;
  213. }
  214. if (SimpleExpectation::isExpectation($expected)) {
  215. return $expected->test(trim($value));
  216. }
  217. return (trim($value) == trim($expected));
  218. }
  219. /**
  220. * Returns a human readable test message.
  221. * @param mixed $compare Raw header block to search.
  222. * @return string Description of success
  223. * or failure.
  224. * @access public
  225. */
  226. function testMessage($compare) {
  227. if (SimpleExpectation::isExpectation($this->expected_value)) {
  228. $message = $this->expected_value->overlayMessage($compare, $this->getDumper());
  229. } else {
  230. $message = $this->expected_header .
  231. ($this->expected_value ? ': ' . $this->expected_value : '');
  232. }
  233. if (is_string($line = $this->findHeader($compare))) {
  234. return "Searching for header [$message] found [$line]";
  235. } else {
  236. return "Failed to find header [$message]";
  237. }
  238. }
  239. }
  240. /**
  241. * Test for a specific HTTP header within a header block that
  242. * should not be found.
  243. * @package SimpleTest
  244. * @subpackage WebTester
  245. */
  246. class NoHttpHeaderExpectation extends HttpHeaderExpectation {
  247. private $expected_header;
  248. private $expected_value;
  249. /**
  250. * Sets the field and value to compare against.
  251. * @param string $unwanted Case insenstive trimmed header name.
  252. * @param string $message Optiona message override. Can use %s as
  253. * a placeholder for the original message.
  254. */
  255. function __construct($unwanted, $message = '%s') {
  256. parent::__construct($unwanted, false, $message);
  257. }
  258. /**
  259. * Tests that the unwanted header is not found.
  260. * @param mixed $compare Raw header block to search.
  261. * @return boolean True if header present.
  262. * @access public
  263. */
  264. function test($compare) {
  265. return ($this->findHeader($compare) === false);
  266. }
  267. /**
  268. * Returns a human readable test message.
  269. * @param mixed $compare Raw header block to search.
  270. * @return string Description of success
  271. * or failure.
  272. * @access public
  273. */
  274. function testMessage($compare) {
  275. $expectation = $this->getExpectation();
  276. if (is_string($line = $this->findHeader($compare))) {
  277. return "Found unwanted header [$expectation] with [$line]";
  278. } else {
  279. return "Did not find unwanted header [$expectation]";
  280. }
  281. }
  282. }
  283. /**
  284. * Test for a text substring.
  285. * @package SimpleTest
  286. * @subpackage UnitTester
  287. */
  288. class TextExpectation extends SimpleExpectation {
  289. private $substring;
  290. /**
  291. * Sets the value to compare against.
  292. * @param string $substring Text to search for.
  293. * @param string $message Customised message on failure.
  294. * @access public
  295. */
  296. function __construct($substring, $message = '%s') {
  297. parent::__construct($message);
  298. $this->substring = $substring;
  299. }
  300. /**
  301. * Accessor for the substring.
  302. * @return string Text to match.
  303. * @access protected
  304. */
  305. protected function getSubstring() {
  306. return $this->substring;
  307. }
  308. /**
  309. * Tests the expectation. True if the text contains the
  310. * substring.
  311. * @param string $compare Comparison value.
  312. * @return boolean True if correct.
  313. * @access public
  314. */
  315. function test($compare) {
  316. return (strpos($compare, $this->substring) !== false);
  317. }
  318. /**
  319. * Returns a human readable test message.
  320. * @param mixed $compare Comparison value.
  321. * @return string Description of success
  322. * or failure.
  323. * @access public
  324. */
  325. function testMessage($compare) {
  326. if ($this->test($compare)) {
  327. return $this->describeTextMatch($this->getSubstring(), $compare);
  328. } else {
  329. $dumper = $this->getDumper();
  330. return "Text [" . $this->getSubstring() .
  331. "] not detected in [" .
  332. $dumper->describeValue($compare) . "]";
  333. }
  334. }
  335. /**
  336. * Describes a pattern match including the string
  337. * found and it's position.
  338. * @param string $substring Text to search for.
  339. * @param string $subject Subject to search.
  340. * @access protected
  341. */
  342. protected function describeTextMatch($substring, $subject) {
  343. $position = strpos($subject, $substring);
  344. $dumper = $this->getDumper();
  345. return "Text [$substring] detected at character [$position] in [" .
  346. $dumper->describeValue($subject) . "] in region [" .
  347. $dumper->clipString($subject, 100, $position) . "]";
  348. }
  349. }
  350. /**
  351. * Fail if a substring is detected within the
  352. * comparison text.
  353. * @package SimpleTest
  354. * @subpackage UnitTester
  355. */
  356. class NoTextExpectation extends TextExpectation {
  357. /**
  358. * Sets the reject pattern
  359. * @param string $substring Text to search for.
  360. * @param string $message Customised message on failure.
  361. * @access public
  362. */
  363. function __construct($substring, $message = '%s') {
  364. parent::__construct($substring, $message);
  365. }
  366. /**
  367. * Tests the expectation. False if the substring appears
  368. * in the text.
  369. * @param string $compare Comparison value.
  370. * @return boolean True if correct.
  371. * @access public
  372. */
  373. function test($compare) {
  374. return ! parent::test($compare);
  375. }
  376. /**
  377. * Returns a human readable test message.
  378. * @param string $compare Comparison value.
  379. * @return string Description of success
  380. * or failure.
  381. * @access public
  382. */
  383. function testMessage($compare) {
  384. if ($this->test($compare)) {
  385. $dumper = $this->getDumper();
  386. return "Text [" . $this->getSubstring() .
  387. "] not detected in [" .
  388. $dumper->describeValue($compare) . "]";
  389. } else {
  390. return $this->describeTextMatch($this->getSubstring(), $compare);
  391. }
  392. }
  393. }
  394. /**
  395. * Test case for testing of web pages. Allows
  396. * fetching of pages, parsing of HTML and
  397. * submitting forms.
  398. * @package SimpleTest
  399. * @subpackage WebTester
  400. */
  401. class WebTestCase extends SimpleTestCase {
  402. private $browser;
  403. private $ignore_errors = false;
  404. /**
  405. * Creates an empty test case. Should be subclassed
  406. * with test methods for a functional test case.
  407. * @param string $label Name of test case. Will use
  408. * the class name if none specified.
  409. * @access public
  410. */
  411. function __construct($label = false) {
  412. parent::__construct($label);
  413. }
  414. /**
  415. * Announces the start of the test.
  416. * @param string $method Test method just started.
  417. * @access public
  418. */
  419. function before($method) {
  420. parent::before($method);
  421. $this->setBrowser($this->createBrowser());
  422. }
  423. /**
  424. * Announces the end of the test. Includes private clean up.
  425. * @param string $method Test method just finished.
  426. * @access public
  427. */
  428. function after($method) {
  429. $this->unsetBrowser();
  430. parent::after($method);
  431. }
  432. /**
  433. * Gets a current browser reference for setting
  434. * special expectations or for detailed
  435. * examination of page fetches.
  436. * @return SimpleBrowser Current test browser object.
  437. * @access public
  438. */
  439. function getBrowser() {
  440. return $this->browser;
  441. }
  442. /**
  443. * Gets a current browser reference for setting
  444. * special expectations or for detailed
  445. * examination of page fetches.
  446. * @param SimpleBrowser $browser New test browser object.
  447. * @access public
  448. */
  449. function setBrowser($browser) {
  450. return $this->browser = $browser;
  451. }
  452. /**
  453. * Sets the HTML parser to use within this browser.
  454. * @param object The parser, one of SimplePHPPageBuilder or
  455. * SimpleTidyPageBuilder.
  456. */
  457. function setParser($parser) {
  458. $this->browser->setParser($parser);
  459. }
  460. /**
  461. * Clears the current browser reference to help the
  462. * PHP garbage collector.
  463. * @access public
  464. */
  465. function unsetBrowser() {
  466. unset($this->browser);
  467. }
  468. /**
  469. * Creates a new default web browser object.
  470. * Will be cleared at the end of the test method.
  471. * @return TestBrowser New browser.
  472. * @access public
  473. */
  474. function createBrowser() {
  475. return new SimpleBrowser();
  476. }
  477. /**
  478. * Gets the last response error.
  479. * @return string Last low level HTTP error.
  480. * @access public
  481. */
  482. function getTransportError() {
  483. return $this->browser->getTransportError();
  484. }
  485. /**
  486. * Accessor for the currently selected URL.
  487. * @return string Current location or false if
  488. * no page yet fetched.
  489. * @access public
  490. */
  491. function getUrl() {
  492. return $this->browser->getUrl();
  493. }
  494. /**
  495. * Dumps the current request for debugging.
  496. * @access public
  497. */
  498. function showRequest() {
  499. $this->dump($this->browser->getRequest());
  500. }
  501. /**
  502. * Dumps the current HTTP headers for debugging.
  503. * @access public
  504. */
  505. function showHeaders() {
  506. $this->dump($this->browser->getHeaders());
  507. }
  508. /**
  509. * Dumps the current HTML source for debugging.
  510. * @access public
  511. */
  512. function showSource() {
  513. $this->dump($this->browser->getContent());
  514. }
  515. /**
  516. * Dumps the visible text only for debugging.
  517. * @access public
  518. */
  519. function showText() {
  520. $this->dump(wordwrap($this->browser->getContentAsText(), 80));
  521. }
  522. /**
  523. * Simulates the closing and reopening of the browser.
  524. * Temporary cookies will be discarded and timed
  525. * cookies will be expired if later than the
  526. * specified time.
  527. * @param string/integer $date Time when session restarted.
  528. * If ommitted then all persistent
  529. * cookies are kept. Time is either
  530. * Cookie format string or timestamp.
  531. * @access public
  532. */
  533. function restart($date = false) {
  534. if ($date === false) {
  535. $date = time();
  536. }
  537. $this->browser->restart($date);
  538. }
  539. /**
  540. * Moves cookie expiry times back into the past.
  541. * Useful for testing timeouts and expiries.
  542. * @param integer $interval Amount to age in seconds.
  543. * @access public
  544. */
  545. function ageCookies($interval) {
  546. $this->browser->ageCookies($interval);
  547. }
  548. /**
  549. * Disables frames support. Frames will not be fetched
  550. * and the frameset page will be used instead.
  551. * @access public
  552. */
  553. function ignoreFrames() {
  554. $this->browser->ignoreFrames();
  555. }
  556. /**
  557. * Switches off cookie sending and recieving.
  558. * @access public
  559. */
  560. function ignoreCookies() {
  561. $this->browser->ignoreCookies();
  562. }
  563. /**
  564. * Skips errors for the next request only. You might
  565. * want to confirm that a page is unreachable for
  566. * example.
  567. * @access public
  568. */
  569. function ignoreErrors() {
  570. $this->ignore_errors = true;
  571. }
  572. /**
  573. * Issues a fail if there is a transport error anywhere
  574. * in the current frameset. Only one such error is
  575. * reported.
  576. * @param string/boolean $result HTML or failure.
  577. * @return string/boolean $result Passes through result.
  578. * @access private
  579. */
  580. protected function failOnError($result) {
  581. if (! $this->ignore_errors) {
  582. if ($error = $this->browser->getTransportError()) {
  583. $this->fail($error);
  584. }
  585. }
  586. $this->ignore_errors = false;
  587. return $result;
  588. }
  589. /**
  590. * Adds a header to every fetch.
  591. * @param string $header Header line to add to every
  592. * request until cleared.
  593. * @access public
  594. */
  595. function addHeader($header) {
  596. $this->browser->addHeader($header);
  597. }
  598. /**
  599. * Sets the maximum number of redirects before
  600. * the web page is loaded regardless.
  601. * @param integer $max Maximum hops.
  602. * @access public
  603. */
  604. function setMaximumRedirects($max) {
  605. if (! $this->browser) {
  606. trigger_error(
  607. 'Can only set maximum redirects in a test method, setUp() or tearDown()');
  608. }
  609. $this->browser->setMaximumRedirects($max);
  610. }
  611. /**
  612. * Sets the socket timeout for opening a connection and
  613. * receiving at least one byte of information.
  614. * @param integer $timeout Maximum time in seconds.
  615. * @access public
  616. */
  617. function setConnectionTimeout($timeout) {
  618. $this->browser->setConnectionTimeout($timeout);
  619. }
  620. /**
  621. * Sets proxy to use on all requests for when
  622. * testing from behind a firewall. Set URL
  623. * to false to disable.
  624. * @param string $proxy Proxy URL.
  625. * @param string $username Proxy username for authentication.
  626. * @param string $password Proxy password for authentication.
  627. * @access public
  628. */
  629. function useProxy($proxy, $username = false, $password = false) {
  630. $this->browser->useProxy($proxy, $username, $password);
  631. }
  632. /**
  633. * Fetches a page into the page buffer. If
  634. * there is no base for the URL then the
  635. * current base URL is used. After the fetch
  636. * the base URL reflects the new location.
  637. * @param string $url URL to fetch.
  638. * @param hash $parameters Optional additional GET data.
  639. * @return boolean/string Raw page on success.
  640. * @access public
  641. */
  642. function get($url, $parameters = false) {
  643. return $this->failOnError($this->browser->get($url, $parameters));
  644. }
  645. /**
  646. * Fetches a page by POST into the page buffer.
  647. * If there is no base for the URL then the
  648. * current base URL is used. After the fetch
  649. * the base URL reflects the new location.
  650. * @param string $url URL to fetch.
  651. * @param mixed $parameters Optional POST parameters or content body to send
  652. * @param string $content_type Content type of provided body
  653. * @return boolean/string Raw page on success.
  654. * @access public
  655. */
  656. function post($url, $parameters = false, $content_type = false) {
  657. return $this->failOnError($this->browser->post($url, $parameters, $content_type));
  658. }
  659. /**
  660. * Fetches a page by PUT into the page buffer.
  661. * If there is no base for the URL then the
  662. * current base URL is used. After the fetch
  663. * the base URL reflects the new location.
  664. * @param string $url URL to fetch.
  665. * @param mixed $body Optional content body to send
  666. * @param string $content_type Content type of provided body
  667. * @return boolean/string Raw page on success.
  668. * @access public
  669. */
  670. function put($url, $body = false, $content_type = false) {
  671. return $this->failOnError($this->browser->put($url, $body, $content_type));
  672. }
  673. /**
  674. * Fetches a page by a DELETE request
  675. * @param string $url URL to fetch.
  676. * @param hash $parameters Optional additional parameters.
  677. * @return boolean/string Raw page on success.
  678. * @access public
  679. */
  680. function delete($url, $parameters = false) {
  681. return $this->failOnError($this->browser->delete($url, $parameters));
  682. }
  683. /**
  684. * Does a HTTP HEAD fetch, fetching only the page
  685. * headers. The current base URL is unchanged by this.
  686. * @param string $url URL to fetch.
  687. * @param hash $parameters Optional additional GET data.
  688. * @return boolean True on success.
  689. * @access public
  690. */
  691. function head($url, $parameters = false) {
  692. return $this->failOnError($this->browser->head($url, $parameters));
  693. }
  694. /**
  695. * Equivalent to hitting the retry button on the
  696. * browser. Will attempt to repeat the page fetch.
  697. * @return boolean True if fetch succeeded.
  698. * @access public
  699. */
  700. function retry() {
  701. return $this->failOnError($this->browser->retry());
  702. }
  703. /**
  704. * Equivalent to hitting the back button on the
  705. * browser.
  706. * @return boolean True if history entry and
  707. * fetch succeeded.
  708. * @access public
  709. */
  710. function back() {
  711. return $this->failOnError($this->browser->back());
  712. }
  713. /**
  714. * Equivalent to hitting the forward button on the
  715. * browser.
  716. * @return boolean True if history entry and
  717. * fetch succeeded.
  718. * @access public
  719. */
  720. function forward() {
  721. return $this->failOnError($this->browser->forward());
  722. }
  723. /**
  724. * Retries a request after setting the authentication
  725. * for the current realm.
  726. * @param string $username Username for realm.
  727. * @param string $password Password for realm.
  728. * @return boolean/string HTML on successful fetch. Note
  729. * that authentication may still have
  730. * failed.
  731. * @access public
  732. */
  733. function authenticate($username, $password) {
  734. return $this->failOnError(
  735. $this->browser->authenticate($username, $password));
  736. }
  737. /**
  738. * Gets the cookie value for the current browser context.
  739. * @param string $name Name of cookie.
  740. * @return string Value of cookie or false if unset.
  741. * @access public
  742. */
  743. function getCookie($name) {
  744. return $this->browser->getCurrentCookieValue($name);
  745. }
  746. /**
  747. * Sets a cookie in the current browser.
  748. * @param string $name Name of cookie.
  749. * @param string $value Cookie value.
  750. * @param string $host Host upon which the cookie is valid.
  751. * @param string $path Cookie path if not host wide.
  752. * @param string $expiry Expiry date.
  753. * @access public
  754. */
  755. function setCookie($name, $value, $host = false, $path = '/', $expiry = false) {
  756. $this->browser->setCookie($name, $value, $host, $path, $expiry);
  757. }
  758. /**
  759. * Accessor for current frame focus. Will be
  760. * false if no frame has focus.
  761. * @return integer/string/boolean Label if any, otherwise
  762. * the position in the frameset
  763. * or false if none.
  764. * @access public
  765. */
  766. function getFrameFocus() {
  767. return $this->browser->getFrameFocus();
  768. }
  769. /**
  770. * Sets the focus by index. The integer index starts from 1.
  771. * @param integer $choice Chosen frame.
  772. * @return boolean True if frame exists.
  773. * @access public
  774. */
  775. function setFrameFocusByIndex($choice) {
  776. return $this->browser->setFrameFocusByIndex($choice);
  777. }
  778. /**
  779. * Sets the focus by name.
  780. * @param string $name Chosen frame.
  781. * @return boolean True if frame exists.
  782. * @access public
  783. */
  784. function setFrameFocus($name) {
  785. return $this->browser->setFrameFocus($name);
  786. }
  787. /**
  788. * Clears the frame focus. All frames will be searched
  789. * for content.
  790. * @access public
  791. */
  792. function clearFrameFocus() {
  793. return $this->browser->clearFrameFocus();
  794. }
  795. /**
  796. * Clicks a visible text item. Will first try buttons,
  797. * then links and then images.
  798. * @param string $label Visible text or alt text.
  799. * @return string/boolean Raw page or false.
  800. * @access public
  801. */
  802. function click($label) {
  803. return $this->failOnError($this->browser->click($label));
  804. }
  805. /**
  806. * Checks for a click target.
  807. * @param string $label Visible text or alt text.
  808. * @return boolean True if click target.
  809. * @access public
  810. */
  811. function assertClickable($label, $message = '%s') {
  812. return $this->assertTrue(
  813. $this->browser->isClickable($label),
  814. sprintf($message, "Click target [$label] should exist"));
  815. }
  816. /**
  817. * Clicks the submit button by label. The owning
  818. * form will be submitted by this.
  819. * @param string $label Button label. An unlabeled
  820. * button can be triggered by 'Submit'.
  821. * @param hash $additional Additional form values.
  822. * @return boolean/string Page on success, else false.
  823. * @access public
  824. */
  825. function clickSubmit($label = 'Submit', $additional = false) {
  826. return $this->failOnError(
  827. $this->browser->clickSubmit($label, $additional));
  828. }
  829. /**
  830. * Clicks the submit button by name attribute. The owning
  831. * form will be submitted by this.
  832. * @param string $name Name attribute of button.
  833. * @param hash $additional Additional form values.
  834. * @return boolean/string Page on success.
  835. * @access public
  836. */
  837. function clickSubmitByName($name, $additional = false) {
  838. return $this->failOnError(
  839. $this->browser->clickSubmitByName($name, $additional));
  840. }
  841. /**
  842. * Clicks the submit button by ID attribute. The owning
  843. * form will be submitted by this.
  844. * @param string $id ID attribute of button.
  845. * @param hash $additional Additional form values.
  846. * @return boolean/string Page on success.
  847. * @access public
  848. */
  849. function clickSubmitById($id, $additional = false) {
  850. return $this->failOnError(
  851. $this->browser->clickSubmitById($id, $additional));
  852. }
  853. /**
  854. * Checks for a valid button label.
  855. * @param string $label Visible text.
  856. * @return boolean True if click target.
  857. * @access public
  858. */
  859. function assertSubmit($label, $message = '%s') {
  860. return $this->assertTrue(
  861. $this->browser->isSubmit($label),
  862. sprintf($message, "Submit button [$label] should exist"));
  863. }
  864. /**
  865. * Clicks the submit image by some kind of label. Usually
  866. * the alt tag or the nearest equivalent. The owning
  867. * form will be submitted by this. Clicking outside of
  868. * the boundary of the coordinates will result in
  869. * a failure.
  870. * @param string $label Alt attribute of button.
  871. * @param integer $x X-coordinate of imaginary click.
  872. * @param integer $y Y-coordinate of imaginary click.
  873. * @param hash $additional Additional form values.
  874. * @return boolean/string Page on success.
  875. * @access public
  876. */
  877. function clickImage($label, $x = 1, $y = 1, $additional = false) {
  878. return $this->failOnError(
  879. $this->browser->clickImage($label, $x, $y, $additional));
  880. }
  881. /**
  882. * Clicks the submit image by the name. Usually
  883. * the alt tag or the nearest equivalent. The owning
  884. * form will be submitted by this. Clicking outside of
  885. * the boundary of the coordinates will result in
  886. * a failure.
  887. * @param string $name Name attribute of button.
  888. * @param integer $x X-coordinate of imaginary click.
  889. * @param integer $y Y-coordinate of imaginary click.
  890. * @param hash $additional Additional form values.
  891. * @return boolean/string Page on success.
  892. * @access public
  893. */
  894. function clickImageByName($name, $x = 1, $y = 1, $additional = false) {
  895. return $this->failOnError(
  896. $this->browser->clickImageByName($name, $x, $y, $additional));
  897. }
  898. /**
  899. * Clicks the submit image by ID attribute. The owning
  900. * form will be submitted by this. Clicking outside of
  901. * the boundary of the coordinates will result in
  902. * a failure.
  903. * @param integer/string $id ID attribute of button.
  904. * @param integer $x X-coordinate of imaginary click.
  905. * @param integer $y Y-coordinate of imaginary click.
  906. * @param hash $additional Additional form values.
  907. * @return boolean/string Page on success.
  908. * @access public
  909. */
  910. function clickImageById($id, $x = 1, $y = 1, $additional = false) {
  911. return $this->failOnError(
  912. $this->browser->clickImageById($id, $x, $y, $additional));
  913. }
  914. /**
  915. * Checks for a valid image with atht alt text or title.
  916. * @param string $label Visible text.
  917. * @return boolean True if click target.
  918. * @access public
  919. */
  920. function assertImage($label, $message = '%s') {
  921. return $this->assertTrue(
  922. $this->browser->isImage($label),
  923. sprintf($message, "Image with text [$label] should exist"));
  924. }
  925. /**
  926. * Submits a form by the ID.
  927. * @param string $id Form ID. No button information
  928. * is submitted this way.
  929. * @return boolean/string Page on success.
  930. * @access public
  931. */
  932. function submitFormById($id, $additional = false) {
  933. return $this->failOnError($this->browser->submitFormById($id, $additional));
  934. }
  935. /**
  936. * Follows a link by name. Will click the first link
  937. * found with this link text by default, or a later
  938. * one if an index is given. Match is case insensitive
  939. * with normalised space.
  940. * @param string $label Text between the anchor tags.
  941. * @param integer $index Link position counting from zero.
  942. * @return boolean/string Page on success.
  943. * @access public
  944. */
  945. function clickLink($label, $index = 0) {
  946. return $this->failOnError($this->browser->clickLink($label, $index));
  947. }
  948. /**
  949. * Follows a link by id attribute.
  950. * @param string $id ID attribute value.
  951. * @return boolean/string Page on success.
  952. * @access public
  953. */
  954. function clickLinkById($id) {
  955. return $this->failOnError($this->browser->clickLinkById($id));
  956. }
  957. /**
  958. * Tests for the presence of a link label. Match is
  959. * case insensitive with normalised space.
  960. * @param string $label Text between the anchor tags.
  961. * @param mixed $expected Expected URL or expectation object.
  962. * @param string $message Message to display. Default
  963. * can be embedded with %s.
  964. * @return boolean True if link present.
  965. * @access public
  966. */
  967. function assertLink($label, $expected = true, $message = '%s') {
  968. $url = $this->browser->getLink($label);
  969. if ($expected === true || ($expected !== true && $url === false)) {
  970. return $this->assertTrue($url !== false, sprintf($message, "Link [$label] should exist"));
  971. }
  972. if (! SimpleExpectation::isExpectation($expected)) {
  973. $expected = new IdenticalExpectation($expected);
  974. }
  975. return $this->assert($expected, $url->asString(), sprintf($message, "Link [$label] should match"));
  976. }
  977. /**
  978. * Tests for the non-presence of a link label. Match is
  979. * case insensitive with normalised space.
  980. * @param string/integer $label Text between the anchor tags
  981. * or ID attribute.
  982. * @param string $message Message to display. Default
  983. * can be embedded with %s.
  984. * @return boolean True if link missing.
  985. * @access public
  986. */
  987. function assertNoLink($label, $message = '%s') {
  988. return $this->assertTrue(
  989. $this->browser->getLink($label) === false,
  990. sprintf($message, "Link [$label] should not exist"));
  991. }
  992. /**
  993. * Tests for the presence of a link id attribute.
  994. * @param string $id Id attribute value.
  995. * @param mixed $expected Expected URL or expectation object.
  996. * @param string $message Message to display. Default
  997. * can be embedded with %s.
  998. * @return boolean True if link present.
  999. * @access public
  1000. */
  1001. function assertLinkById($id, $expected = true, $message = '%s') {
  1002. $url = $this->browser->getLinkById($id);
  1003. if ($expected === true) {
  1004. return $this->assertTrue($url !== false, sprintf($message, "Link ID [$id] should exist"));
  1005. }
  1006. if (! SimpleExpectation::isExpectation($expected)) {
  1007. $expected = new IdenticalExpectation($expected);
  1008. }
  1009. return $this->assert($expected, $url->asString(), sprintf($message, "Link ID [$id] should match"));
  1010. }
  1011. /**
  1012. * Tests for the non-presence of a link label. Match is
  1013. * case insensitive with normalised space.
  1014. * @param string $id Id attribute value.
  1015. * @param string $message Message to display. Default
  1016. * can be embedded with %s.
  1017. * @return boolean True if link missing.
  1018. * @access public
  1019. */
  1020. function assertNoLinkById($id, $message = '%s') {
  1021. return $this->assertTrue(
  1022. $this->browser->getLinkById($id) === false,
  1023. sprintf($message, "Link ID [$id] should not exist"));
  1024. }
  1025. /**
  1026. * Sets all form fields with that label, or name if there
  1027. * is no label attached.
  1028. * @param string $name Name of field in forms.
  1029. * @param string $value New value of field.
  1030. * @return boolean True if field exists, otherwise false.
  1031. * @access public
  1032. */
  1033. function setField($label, $value, $position=false) {
  1034. return $this->browser->setField($label, $value, $position);
  1035. }
  1036. /**
  1037. * Sets all form fields with that name.
  1038. * @param string $name Name of field in forms.
  1039. * @param string $value New value of field.
  1040. * @return boolean True if field exists, otherwise false.
  1041. * @access public
  1042. */
  1043. function setFieldByName($name, $value, $position=false) {
  1044. return $this->browser->setFieldByName($name, $value, $position);
  1045. }
  1046. /**
  1047. * Sets all form fields with that id.
  1048. * @param string/integer $id Id of field in forms.
  1049. * @param string $value New value of field.
  1050. * @return boolean True if field exists, otherwise false.
  1051. * @access public
  1052. */
  1053. function setFieldById($id, $value) {
  1054. return $this->browser->setFieldById($id, $value);
  1055. }
  1056. /**
  1057. * Confirms that the form element is currently set
  1058. * to the expected value. A missing form will always
  1059. * fail. If no value is given then only the existence
  1060. * of the field is checked.
  1061. * @param string $name Name of field in forms.
  1062. * @param mixed $expected Expected string/array value or
  1063. * false for unset fields.
  1064. * @param string $message Message to display. Default
  1065. * can be embedded with %s.
  1066. * @return boolean True if pass.
  1067. * @access public
  1068. */
  1069. function assertField($label, $expected = true, $message = '%s') {
  1070. $value = $this->browser->getField($label);
  1071. return $this->assertFieldValue($label, $value, $expected, $message);
  1072. }
  1073. /**
  1074. * Confirms that the form element is currently set
  1075. * to the expected value. A missing form element will always
  1076. * fail. If no value is given then only the existence
  1077. * of the field is checked.
  1078. * @param string $name Name of field in forms.
  1079. * @param mixed $expected Expected string/array value or
  1080. * false for unset fields.
  1081. * @param string $message Message to display. Default
  1082. * can be embedded with %s.
  1083. * @return boolean True if pass.
  1084. * @access public
  1085. */
  1086. function assertFieldByName($name, $expected = true, $message = '%s') {
  1087. $value = $this->browser->getFieldByName($name);
  1088. return $this->assertFieldValue($name, $value, $expected, $message);
  1089. }
  1090. /**
  1091. * Confirms that the form element is currently set
  1092. * to the expected value. A missing form will always
  1093. * fail. If no ID is given then only the existence
  1094. * of the field is checked.
  1095. * @param string/integer $id Name of field in forms.
  1096. * @param mixed $expected Expected string/array value or
  1097. * false for unset fields.
  1098. * @param string $message Message to display. Default
  1099. * can be embedded with %s.
  1100. * @return boolean True if pass.
  1101. * @access public
  1102. */
  1103. function assertFieldById($id, $expected = true, $message = '%s') {
  1104. $value = $this->browser->getFieldById($id);
  1105. return $this->assertFieldValue($id, $value, $expected, $message);
  1106. }
  1107. /**
  1108. * Tests the field value against the expectation.
  1109. * @param string $identifier Name, ID or label.
  1110. * @param mixed $value Current field value.
  1111. * @param mixed $expected Expected value to match.
  1112. * @param string $message Failure message.
  1113. * @return boolean True if pass
  1114. * @access protected
  1115. */
  1116. protected function assertFieldValue($identifier, $value, $expected, $message) {
  1117. if ($expected === true) {
  1118. return $this->assertTrue(
  1119. isset($value),
  1120. sprintf($message, "Field [$identifier] should exist"));
  1121. }
  1122. if (! SimpleExpectation::isExpectation($expected)) {
  1123. $identifier = str_replace('%', '%%', $identifier);
  1124. $expected = new FieldExpectation(
  1125. $expected,
  1126. "Field [$identifier] should match with [%s]");
  1127. }
  1128. return $this->assert($expected, $value, $message);
  1129. }
  1130. /**
  1131. * Checks the response code against a list
  1132. * of possible values.
  1133. * @param array $responses Possible responses for a pass.
  1134. * @param string $message Message to display. Default
  1135. * can be embedded with %s.
  1136. * @return boolean True if pass.
  1137. * @access public
  1138. */
  1139. function assertResponse($responses, $message = '%s') {
  1140. $responses = (is_array($responses) ? $responses : array($responses));
  1141. $code = $this->browser->getResponseCode();
  1142. $message = sprintf($message, "Expecting response in [" .
  1143. implode(", ", $responses) . "] got [$code]");
  1144. return $this->assertTrue(in_array($code, $responses), $message);
  1145. }
  1146. /**
  1147. * Checks the mime type against a list
  1148. * of possible values.
  1149. * @param array $types Possible mime types for a pass.
  1150. * @param string $message Message to display.
  1151. * @return boolean True if pass.
  1152. * @access public
  1153. */
  1154. function assertMime($types, $message = '%s') {
  1155. $types = (is_array($types) ? $types : array($types));
  1156. $type = $this->browser->getMimeType();
  1157. $message = sprintf($message, "Expecting mime type in [" .
  1158. implode(", ", $types) . "] got [$type]");
  1159. return $this->assertTrue(in_array($type, $types), $message);
  1160. }
  1161. /**
  1162. * Attempt to match the authentication type within
  1163. * the security realm we are currently matching.
  1164. * @param string $authentication Usually basic.
  1165. * @param string $message Message to display.
  1166. * @return boolean True if pass.
  1167. * @access public
  1168. */
  1169. function assertAuthentication($authentication = false, $message = '%s') {
  1170. if (! $authentication) {
  1171. $message = sprintf($message, "Expected any authentication type, got [" .
  1172. $this->browser->getAuthentication() . "]");
  1173. return $this->assertTrue(
  1174. $this->browser->getAuthentication(),
  1175. $message);
  1176. } else {
  1177. $message = sprintf($message, "Expected authentication [$authentication] got [" .
  1178. $this->browser->getAuthentication() . "]");
  1179. return $this->assertTrue(
  1180. strtolower($this->browser->getAuthentication()) == strtolower($authentication),
  1181. $message);
  1182. }
  1183. }
  1184. /**
  1185. * Checks that no authentication is necessary to view
  1186. * the desired page.
  1187. * @param string $message Message to display.
  1188. * @return boolean True if pass.
  1189. * @access public
  1190. */
  1191. function assertNoAuthentication($message = '%s') {
  1192. $message = sprintf($message, "Expected no authentication type, got [" .
  1193. $this->browser->getAuthentication() . "]");
  1194. return $this->assertFalse($this->browser->getAuthentication(), $message);
  1195. }
  1196. /**
  1197. * Attempts to match the current security realm.
  1198. * @param string $realm Name of security realm.
  1199. * @param string $message Message to display.
  1200. * @return boolean True if pass.
  1201. * @access public
  1202. */
  1203. function assertRealm($realm, $message = '%s') {
  1204. if (! SimpleExpectation::isExpectation($realm)) {
  1205. $realm = new EqualExpectation($realm);
  1206. }
  1207. return $this->assert(
  1208. $realm,
  1209. $this->browser->getRealm(),
  1210. "Expected realm -> $message");
  1211. }
  1212. /**
  1213. * Checks each header line for the required value. If no
  1214. * value is given then only an existence check is made.
  1215. * @param string $header Case insensitive header name.
  1216. * @param mixed $value Case sensitive trimmed string to
  1217. * match against. An expectation object
  1218. * can be used for pattern matching.
  1219. * @return boolean True if pass.
  1220. * @access public
  1221. */
  1222. function assertHeader($header, $value = false, $message = '%s') {
  1223. return $this->assert(
  1224. new HttpHeaderExpectation($header, $value),
  1225. $this->browser->getHeaders(),
  1226. $message);
  1227. }
  1228. /**
  1229. * Confirms that the header type has not been received.
  1230. * Only the landing page is checked. If you want to check
  1231. * redirect pages, then you should limit redirects so
  1232. * as to capture the page you want.
  1233. * @param string $header Case insensitive header name.
  1234. * @return boolean True if pass.
  1235. * @access public
  1236. */
  1237. function assertNoHeader($header, $message = '%s') {
  1238. return $this->assert(
  1239. new NoHttpHeaderExpectation($header),
  1240. $this->browser->getHeaders(),
  1241. $message);
  1242. }
  1243. /**
  1244. * Tests the text between the title tags.
  1245. * @param string/SimpleExpectation $title Expected title.
  1246. * @param string $message Message to display.
  1247. * @return boolean True if pass.
  1248. * @access public
  1249. */
  1250. function assertTitle($title = false, $message = '%s') {
  1251. if (! SimpleExpectation::isExpectation($title)) {
  1252. $title = new EqualExpectation($title);
  1253. }
  1254. return $this->assert($title, $this->browser->getTitle(), $message);
  1255. }
  1256. /**
  1257. * Will trigger a pass if the text is found in the plain
  1258. * text form of the page.
  1259. * @param string $text Text to look for.
  1260. * @param string $message Message to display.
  1261. * @return boolean True if pass.
  1262. * @access public
  1263. */
  1264. function assertText($text, $message = '%s') {
  1265. return $this->assert(
  1266. new TextExpectation($text),
  1267. $this->browser->getContentAsText(),
  1268. $message);
  1269. }
  1270. /**
  1271. * Will trigger a pass if the text is not found in the plain
  1272. * text form of the page.
  1273. * @param string $text Text to look for.
  1274. * @param string $message Message to display.
  1275. * @return boolean True if pass.
  1276. * @access public
  1277. */
  1278. function assertNoText($text, $message = '%s') {
  1279. return $this->assert(
  1280. new NoTextExpectation($text),
  1281. $this->browser->getContentAsText(),
  1282. $message);
  1283. }
  1284. /**
  1285. * Will trigger a pass if the Perl regex pattern
  1286. * is found in the raw content.
  1287. * @param string $pattern Perl regex to look for including
  1288. * the regex delimiters.
  1289. * @param string $message Message to display.
  1290. * @return boolean True if pass.
  1291. * @access public
  1292. */
  1293. function assertPattern($pattern, $message = '%s') {
  1294. return $this->assert(
  1295. new PatternExpectation($pattern),
  1296. $this->browser->getContent(),
  1297. $message);
  1298. }
  1299. /**
  1300. * Will trigger a pass if the perl regex pattern
  1301. * is not present in raw content.
  1302. * @param string $pattern Perl regex to look for including
  1303. * the regex delimiters.
  1304. * @param string $message Message to display.
  1305. * @return boolean True if pass.
  1306. * @access public
  1307. */
  1308. function assertNoPattern($pattern, $message = '%s') {
  1309. return $this->assert(
  1310. new NoPatternExpectation($pattern),
  1311. $this->browser->getContent(),
  1312. $message);
  1313. }
  1314. /**
  1315. * Checks that a cookie is set for the current page
  1316. * and optionally checks the value.
  1317. * @param string $name Name of cookie to test.
  1318. * @param string $expected Expected value as a string or
  1319. * false if any value will do.
  1320. * @param string $message Message to display.
  1321. * @return boolean True if pass.
  1322. * @access public
  1323. */
  1324. function assertCookie($name, $expected = false, $message = '%s') {
  1325. $value = $this->getCookie($name);
  1326. if (! $expected) {
  1327. return $this->assertTrue(
  1328. $value,
  1329. sprintf($message, "Expecting cookie [$name]"));
  1330. }
  1331. if (! SimpleExpectation::isExpectation($expected)) {
  1332. $expected = new EqualExpectation($expected);
  1333. }
  1334. return $this->assert($expected, $value, "Expecting cookie [$name] -> $message");
  1335. }
  1336. /**
  1337. * Checks that no cookie is present or that it has
  1338. * been successfully cleared.
  1339. * @param string $name Name of cookie to test.
  1340. * @param string $message Message to display.
  1341. * @return boolean True if pass.
  1342. * @access public
  1343. */
  1344. function assertNoCookie($name, $message = '%s') {
  1345. return $this->assertTrue(
  1346. $this->getCookie($name) === null or $this->getCookie($name) === false,
  1347. sprintf($message, "Not expecting cookie [$name]"));
  1348. }
  1349. /**
  1350. * Called from within the test methods to register
  1351. * passes and failures.
  1352. * @param boolean $result Pass on true.
  1353. * @param string $message Message to display describing
  1354. * the test state.
  1355. * @return boolean True on pass
  1356. * @access public
  1357. */
  1358. function assertTrue($result, $message = '%s') {
  1359. return $this->assert(new TrueExpectation(), $result, $message);
  1360. }
  1361. /**
  1362. * Will be true on false and vice versa. False
  1363. * is the PHP definition of false, so that null,
  1364. * empty strings, zero and an empty array all count
  1365. * as false.
  1366. * @param boolean $result Pass on false.
  1367. * @param string $message Message to display.
  1368. * @return boolean True on pass
  1369. * @access public
  1370. */
  1371. function assertFalse($result, $message = '%s') {
  1372. return $this->assert(new FalseExpectation(), $result, $message);
  1373. }
  1374. /**
  1375. * Will trigger a pass if the two parameters have
  1376. * the same value only. Otherwise a fail. This
  1377. * is for testing hand extracted text, etc.
  1378. * @param mixed $first Value to compare.
  1379. * @param mixed $second Value to compare.
  1380. * @param string $message Message to display.
  1381. * @return boolean True on pass
  1382. * @access public
  1383. */
  1384. function assertEqual($first, $second, $message = '%s') {
  1385. return $this->assert(
  1386. new EqualExpectation($first),
  1387. $second,
  1388. $message);
  1389. }
  1390. /**
  1391. * Will trigger a pass if the two parameters have
  1392. * a different value. Otherwise a fail. This
  1393. * is for testing hand extracted text, etc.
  1394. * @param mixed $first Value to compare.
  1395. * @param mixed $second Value to compare.
  1396. * @param string $message Message to display.
  1397. * @return boolean True on pass
  1398. * @access public
  1399. */
  1400. function assertNotEqual($first, $second, $message = '%s') {
  1401. return $this->assert(
  1402. new NotEqualExpectation($first),
  1403. $second,
  1404. $message);
  1405. }
  1406. /**
  1407. * Uses a stack trace to find the line of an assertion.
  1408. * @return string Line number of first assert*
  1409. * method embedded in format string.
  1410. * @access public
  1411. */
  1412. function getAssertionLine() {
  1413. $trace = new SimpleStackTrace(array('assert', 'click', 'pass', 'fail'));
  1414. return $trace->traceMethod();
  1415. }
  1416. }
  1417. ?>