Advertisement
UniQuet0p1

Untitled

Oct 10th, 2021
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.97 KB | None | 0 0
  1. <?php
  2.  
  3. list ($phpMajorVersion, $phpMinorVersion) = explode('.', PHP_VERSION);
  4.  
  5. if (intval($phpMajorVersion) < 7
  6. || intval($phpMajorVersion) === 7 && intval($phpMinorVersion) < 4) {
  7.  
  8. die('This framework requires Php version 7.4 or greater. '.
  9. "Found Php version " . PHP_VERSION . '.' . PHP_EOL);
  10. }
  11.  
  12. require_once 'runner.php';
  13. require_once 'util.php';
  14. require_once 'domain.php';
  15. require_once 'internals.php';
  16. require_once 'constants.php';
  17.  
  18. include_once __DIR__ . '/simpletest/user_agent.php';
  19.  
  20. require_once 'autoload.php';
  21.  
  22. use stf\matcher\ContainsMatcher;
  23. use stf\matcher\AbstractMatcher;
  24. use stf\matcher\ContainsStringMatcher;
  25. use stf\matcher\ContainsNotStringMatcher;
  26. use function stf\getGlobals;
  27. use function stf\getFormSet;
  28.  
  29. function assertThrows($function): void {
  30. try {
  31. $function();
  32. } catch (Throwable $t) {
  33. return;
  34. }
  35.  
  36. throw new stf\FrameworkException(ERROR_C01, "Expected to throw but did not");
  37. }
  38.  
  39. function fail($code, $message): void {
  40. throw new stf\FrameworkException($code, $message);
  41. }
  42.  
  43. function assertThat($actual, stf\matcher\AbstractMatcher $matcher, $message = null): void {
  44. if ($matcher->matches($actual)) {
  45. return;
  46. }
  47.  
  48. if ($message) {
  49. throw new stf\FrameworkException(ERROR_C01, $message);
  50. }
  51.  
  52. $error = $matcher->getError($actual);
  53.  
  54. throw new stf\FrameworkException($error->getCode(), $error->getMessage());
  55. }
  56.  
  57. function disableAutomaticRedirects() : void {
  58. getGlobals()->maxRedirectCount = 0;
  59. }
  60.  
  61. function setBaseUrl(string $url) : void {
  62. getGlobals()->baseUrl = new stf\browser\Url($url);
  63. getGlobals()->currentUrl = new stf\browser\Url($url);
  64. }
  65.  
  66. function setLogRequests(bool $flag) : void {
  67. getGlobals()->logRequests = $flag;
  68. }
  69.  
  70. function setLogPostParameters(bool $flag) : void {
  71. getGlobals()->logPostParameters = $flag;
  72. }
  73.  
  74. function setPrintStackTrace(bool $flag) : void {
  75. getGlobals()->printStackTrace = $flag;
  76. }
  77.  
  78. function setPrintPageSourceOnError(bool $flag) : void {
  79. getGlobals()->printPageSourceOnError = $flag;
  80. }
  81.  
  82. function getResponseCode() : int {
  83. return getGlobals()->responseCode;
  84. }
  85.  
  86. function getCurrentUrl() : string {
  87. return getGlobals()->currentUrl->asString();
  88. }
  89.  
  90. function printPageSource() : void {
  91. print getGlobals()->page->getSource() . PHP_EOL;
  92. }
  93.  
  94. function printPageText() : void {
  95. print getPageText() . PHP_EOL;
  96. }
  97.  
  98. function getPageText() : string {
  99. return getGlobals()->page->getText();
  100. }
  101.  
  102. function getPageSource() : string {
  103. return getGlobals()->page->getSource();
  104. }
  105.  
  106. function assertPageContainsLinkWithId($linkId) : void {
  107. $link = getGlobals()->page->getLinkById($linkId);
  108.  
  109. if ($link === null) {
  110. fail(ERROR_W03,
  111. sprintf("Current page does not contain link with id '%s'.", $linkId));
  112. }
  113. }
  114.  
  115. function assertPageContainsTextFieldWithName($name) : void {
  116. if (getFormSet()->getTextFieldByName($name) !== null) {
  117. return;
  118. }
  119.  
  120. fail(ERROR_W13,
  121. sprintf("Current page does not contain text field with name '%s'.", $name));
  122. }
  123.  
  124. function assertPageContainsRadioWithName($name) : void {
  125. if (getFormSet()->getRadioByName($name) !== null) {
  126. return;
  127. }
  128.  
  129. fail(ERROR_W14,
  130. sprintf("Current page does not contain radio with name '%s'.", $name));
  131. }
  132.  
  133. function assertPageContainsSelectWithName($name) : void {
  134. if (getFormSet()->getSelectByName($name) !== null) {
  135. return;
  136. }
  137.  
  138. fail(ERROR_W16,
  139. sprintf("Current page does not contain select with name '%s'.", $name));
  140. }
  141.  
  142. function assertPageContainsFieldWithName($name) : void {
  143. if (getFormSet()->getFieldByName($name) !== null) {
  144. return;
  145. }
  146.  
  147. fail(ERROR_W05,
  148. sprintf("Current page does not contain field with name '%s'.", $name));
  149. }
  150.  
  151. function assertPageDoesNotContainFieldWithName($name) : void {
  152. if (getFormSet()->getFieldByName($name) === null) {
  153. return;
  154. }
  155.  
  156. fail(ERROR_W18,
  157. sprintf("Current page should not contain field with name '%s'.", $name));
  158. }
  159.  
  160. function assertPageDoesNotContainButtonWithName($name) : void {
  161. if (getFormSet()->getButtonByName($name) === null) {
  162. return;
  163. }
  164.  
  165. fail(ERROR_W19,
  166. sprintf("Current page should not contain button with name '%s'.", $name));
  167. }
  168.  
  169. function assertPageContainsCheckboxWithName($name) : void {
  170. if (getFormSet()->getCheckboxByName($name) !== null) {
  171. return;
  172. }
  173.  
  174. fail(ERROR_W15,
  175. sprintf("Current page does not contain checkbox with name '%s'.", $name));
  176. }
  177.  
  178. function assertPageContainsButtonWithName($name) : void {
  179. if (getFormSet()->getButtonByName($name) !== null) {
  180. return;
  181. }
  182.  
  183. fail(ERROR_W06,
  184. sprintf("Current page does not contain button with name '%s'.",
  185. $name));
  186. }
  187.  
  188. function assertPageContainsLinkWithText($text) : void {
  189. $link = getGlobals()->page->getLinkByText($text);
  190.  
  191. if ($link === null) {
  192. fail(ERROR_W03,
  193. sprintf("Current page does not contain link with text '%s'.", $text));
  194. }
  195. }
  196.  
  197. function assertPageContainsElementWithId($id) : void {
  198. $element = stf\getElementWithId($id);
  199.  
  200. if ($element) {
  201. return;
  202. }
  203.  
  204. fail(ERROR_W08,
  205. sprintf("Current page does not contain element with id '%s'.", $id));
  206. }
  207.  
  208. function assertPageDoesNotContainElementWithId($id) : void {
  209. $element = stf\getElementWithId($id);
  210.  
  211. if (!$element) {
  212. return;
  213. }
  214.  
  215. fail(ERROR_W09,
  216. sprintf("Current page should not contain element with id '%s'.", $id));
  217. }
  218.  
  219. function assertFrontControllerLink(string $id) : void {
  220. assertPageContainsLinkWithId($id);
  221.  
  222. $link = getGlobals()->page->getLinkById($id)->getHref();
  223.  
  224. $pattern = '/^(index\.php)?\??[-=&\w]*$/';
  225.  
  226. if (!preg_match($pattern, $link)) {
  227. $message = 'Front Controller pattern expects all links '
  228. . 'to be in ?key1=value1&key2=... format. But this link was: ' . $link;
  229.  
  230. fail(ERROR_W20, $message);
  231. }
  232. }
  233.  
  234. function assertPageContainsText($textToBeFound) : void {
  235. $pageText = getGlobals()->page->getText();
  236.  
  237. if (strpos($pageText, $textToBeFound) !== false) {
  238. return;
  239. }
  240.  
  241. fail(ERROR_H04, sprintf("Did not find text '%s' on the current page.",
  242. $textToBeFound));
  243. }
  244.  
  245. function assertNoOutput() : void {
  246. $source = getGlobals()->page->getSource();
  247.  
  248. if (preg_match('/^\s*$/', $source)) {
  249. return;
  250. }
  251.  
  252. fail(ERROR_W21, sprintf(
  253. "Should not print any output along with redirect header " .
  254. "but the output was: %s", $source));
  255. }
  256.  
  257. function assertCurrentUrl($expected) : void {
  258. $actual = getGlobals()->currentUrl->asString();
  259.  
  260. if ($actual !== $expected) {
  261. fail(ERROR_H03, sprintf("Expected url to be '%s' but was '%s'",
  262. $expected, $actual));
  263. }
  264. }
  265.  
  266. function clickLinkWithText($text) : void {
  267. assertPageContainsLinkWithText($text);
  268.  
  269. $link = getGlobals()->page->getLinkByText($text);
  270.  
  271. stf\navigateTo($link->getHref());
  272. }
  273.  
  274. function getHrefFromLinkWithText(string $text) : string {
  275. assertPageContainsLinkWithText($text);
  276.  
  277. return getGlobals()->page->getLinkByText($text)->getHref();
  278. }
  279.  
  280. function getTextFromLinkWithId(string $id) : string {
  281. assertPageContainsLinkWithId($id);
  282.  
  283. return getGlobals()->page->getLinkById($id)->getText();
  284. }
  285.  
  286. function clickLinkWithId($linkId) : void {
  287. assertPageContainsLinkWithId($linkId);
  288.  
  289. $link = getGlobals()->page->getLinkById($linkId);
  290.  
  291. navigateTo($link->getHref());
  292. }
  293.  
  294. function navigateTo(string $url) {
  295. stf\navigateTo($url);
  296. }
  297.  
  298. function clickButton(string $buttonName, ?string $buttonValue = null) {
  299. assertPageContainsButtonWithName($buttonName);
  300.  
  301. stf\submitFormByButtonPress($buttonName, $buttonValue);
  302. }
  303.  
  304. function setTextFieldValue(string $fieldName, string $value) {
  305. assertPageContainsTextFieldWithName($fieldName);
  306.  
  307. getFormSet()->getTextFieldByName($fieldName)->setValue($value);
  308. }
  309.  
  310. function forceFieldValue(string $fieldName, string $value) {
  311. assertPageContainsFieldWithName($fieldName);
  312.  
  313. $form = getFormSet()->findFormContainingField($fieldName);
  314.  
  315. $form->deleteFieldByName($fieldName);
  316.  
  317. $form->addTextField($fieldName, $value);
  318. }
  319.  
  320. function selectOptionWithText(string $fieldName, string $text) {
  321. assertPageContainsSelectWithName($fieldName);
  322.  
  323. $select = stf\getFormSet()->getSelectByName($fieldName);
  324.  
  325. if ($select->hasOptionWithLabel($text)) {
  326. $select->selectOptionWithText($text);
  327. } else {
  328. fail(ERROR_W12, sprintf("select with name '%s' does not have option '%s'",
  329. $fieldName, $text));
  330. }
  331.  
  332. getFormSet()->getSelectByName($fieldName)->selectOptionWithText($text);
  333. }
  334.  
  335. function setCheckboxValue(string $fieldName, bool $value) {
  336. assertPageContainsCheckboxWithName($fieldName);
  337.  
  338. getFormSet()->getCheckboxByName($fieldName)->check($value);
  339. }
  340.  
  341. function setRadioFieldValue(string $fieldName, string $value) {
  342. assertPageContainsRadioWithName($fieldName);
  343.  
  344. $field = getFormSet()->getRadioByName($fieldName);
  345.  
  346. if ($field->hasOption($value)) {
  347. $field->selectOption($value);
  348. } else {
  349. fail(ERROR_W11, sprintf("radio with name '%s' does not have option '%s'",
  350. $fieldName, $value));
  351. }
  352.  
  353. }
  354.  
  355. function getFieldValue(string $fieldName) {
  356. assertPageContainsFieldWithName($fieldName);
  357.  
  358. $field = getFormSet()->getFieldByName($fieldName);
  359.  
  360. return $field instanceof stf\browser\page\Checkbox
  361. ? $field->isChecked()
  362. : $field->getValue();
  363. }
  364.  
  365. function getButtonLabel(string $buttonName) : string {
  366. assertPageContainsButtonWithName($buttonName);
  367.  
  368. return getFormSet()->getButtonByName($buttonName)->getLabel();
  369. }
  370.  
  371. function getSelectedOptionText(string $fieldName) : string {
  372. assertPageContainsSelectWithName($fieldName);
  373.  
  374. $select = getFormSet()->getSelectByName($fieldName);
  375.  
  376. return $select->getSelectedOptionText();
  377. }
  378.  
  379. function deleteSessionCookie() : void {
  380. getGlobals()->httpClient->deleteCookie(session_name());
  381. }
  382.  
  383. function is($value) : stf\matcher\AbstractMatcher {
  384. return new stf\matcher\IsMatcher($value);
  385. }
  386.  
  387. function contains(array $needleArray) : AbstractMatcher {
  388. return new ContainsMatcher($needleArray);
  389. }
  390.  
  391. function containsString(string $needle) : AbstractMatcher {
  392. return new ContainsStringMatcher($needle);
  393. }
  394.  
  395. function doesNotContainString(string $needle) : AbstractMatcher {
  396. return new ContainsNotStringMatcher($needle);
  397. }
  398.  
  399. function containsStringOnce(string $value) : stf\matcher\AbstractMatcher {
  400. return new stf\matcher\ContainsStringOnceMatcher($value);
  401. }
  402.  
  403. function containsInAnyOrder(array $value) : stf\matcher\AbstractMatcher {
  404. return new stf\matcher\ContainsInAnyOrderMatcher($value);
  405. }
  406.  
  407. function isAnyOf(...$values) : stf\matcher\AbstractMatcher {
  408. return new stf\matcher\ContainsAnyMatcher($values);
  409. }
  410.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement