PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/magento/zendframework1/library/Zend/Test/PHPUnit/Constraint/DomQuery37.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 434 lines | 224 code | 39 blank | 171 comment | 34 complexity | 568b6a541ada491923196c2b04e05e02 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Test
  17. * @subpackage PHPUnit
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /** @see Zend_Dom_Query */
  23. #require_once 'Zend/Dom/Query.php';
  24. /**
  25. * Zend_Dom_Query-based PHPUnit Constraint
  26. *
  27. * @uses PHPUnit_Framework_Constraint
  28. * @category Zend
  29. * @package Zend_Test
  30. * @subpackage PHPUnit
  31. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Test_PHPUnit_Constraint_DomQuery37 extends PHPUnit_Framework_Constraint
  35. {
  36. /**#@+
  37. * Assertion type constants
  38. */
  39. const ASSERT_QUERY = 'assertQuery';
  40. const ASSERT_CONTENT_CONTAINS = 'assertQueryContentContains';
  41. const ASSERT_CONTENT_REGEX = 'assertQueryContentRegex';
  42. const ASSERT_CONTENT_COUNT = 'assertQueryCount';
  43. const ASSERT_CONTENT_COUNT_MIN= 'assertQueryCountMin';
  44. const ASSERT_CONTENT_COUNT_MAX= 'assertQueryCountMax';
  45. /**#@-*/
  46. /**
  47. * Current assertion type
  48. * @var string
  49. */
  50. protected $_assertType = null;
  51. /**
  52. * Available assertion types
  53. * @var array
  54. */
  55. protected $_assertTypes = array(
  56. self::ASSERT_QUERY,
  57. self::ASSERT_CONTENT_CONTAINS,
  58. self::ASSERT_CONTENT_REGEX,
  59. self::ASSERT_CONTENT_COUNT,
  60. self::ASSERT_CONTENT_COUNT_MIN,
  61. self::ASSERT_CONTENT_COUNT_MAX,
  62. );
  63. /**
  64. * Content being matched
  65. * @var string
  66. */
  67. protected $_content = null;
  68. /**
  69. * Whether or not assertion is negated
  70. * @var bool
  71. */
  72. protected $_negate = false;
  73. /**
  74. * CSS selector or XPath path to select against
  75. * @var string
  76. */
  77. protected $_path = null;
  78. /**
  79. * Whether or not to use XPath when querying
  80. * @var bool
  81. */
  82. protected $_useXpath = false;
  83. /**
  84. * XPath namespaces
  85. * @var array
  86. */
  87. protected $_xpathNamespaces = array();
  88. /**
  89. * Constructor; setup constraint state
  90. *
  91. * @param string $path CSS selector path
  92. * @return void
  93. */
  94. public function __construct($path)
  95. {
  96. $this->_path = $path;
  97. }
  98. /**
  99. * Indicate negative match
  100. *
  101. * @param bool $flag
  102. * @return void
  103. */
  104. public function setNegate($flag = true)
  105. {
  106. $this->_negate = $flag;
  107. }
  108. /**
  109. * Whether or not path is a straight XPath expression
  110. *
  111. * @param bool $flag
  112. * @return Zend_Test_PHPUnit_Constraint_DomQuery
  113. */
  114. public function setUseXpath($flag = true)
  115. {
  116. $this->_useXpath = (bool) $flag;
  117. return $this;
  118. }
  119. /**
  120. * Evaluate an object to see if it fits the constraints
  121. *
  122. * @param string Response content to be matched against (haystack)
  123. * @param null|string Assertion type
  124. * @param string (optional) String to match (needle), may be required depending on assertion type
  125. * @return bool
  126. *
  127. * NOTE:
  128. * Drastic changes up to PHPUnit 3.5.15 this was:
  129. * public function evaluate($other, $assertType = null)
  130. * In PHPUnit 3.6.0 they changed the interface into this:
  131. * public function evaluate($other, $description = '', $returnResult = FALSE)
  132. * We use the new interface for PHP-strict checking, but emulate the old one
  133. */
  134. public function evaluate($content, $assertType = '', $match = FALSE)
  135. {
  136. if (strstr($assertType, 'Not')) {
  137. $this->setNegate(true);
  138. $assertType = str_replace('Not', '', $assertType);
  139. }
  140. if (strstr($assertType, 'Xpath')) {
  141. $this->setUseXpath(true);
  142. $assertType = str_replace('Xpath', 'Query', $assertType);
  143. }
  144. if (!in_array($assertType, $this->_assertTypes)) {
  145. #require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  146. throw new Zend_Test_PHPUnit_Constraint_Exception(sprintf('Invalid assertion type "%s" provided to %s constraint', $assertType, __CLASS__));
  147. }
  148. $this->_assertType = $assertType;
  149. $method = $this->_useXpath ? 'queryXpath' : 'query';
  150. $domQuery = new Zend_Dom_Query($content);
  151. $domQuery->registerXpathNamespaces($this->_xpathNamespaces);
  152. $result = $domQuery->$method($this->_path);
  153. switch ($assertType) {
  154. case self::ASSERT_CONTENT_CONTAINS:
  155. if (!$match) {
  156. #require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  157. throw new Zend_Test_PHPUnit_Constraint_Exception('No content provided against which to match');
  158. }
  159. $this->_content = $match;
  160. return ($this->_negate)
  161. ? $this->_notMatchContent($result, $match)
  162. : $this->_matchContent($result, $match);
  163. case self::ASSERT_CONTENT_REGEX:
  164. if (!$match) {
  165. #require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  166. throw new Zend_Test_PHPUnit_Constraint_Exception('No pattern provided against which to match');
  167. }
  168. $this->_content = $match;
  169. return ($this->_negate)
  170. ? $this->_notRegexContent($result, $match)
  171. : $this->_regexContent($result, $match);
  172. case self::ASSERT_CONTENT_COUNT:
  173. case self::ASSERT_CONTENT_COUNT_MIN:
  174. case self::ASSERT_CONTENT_COUNT_MAX:
  175. if ($match === false) {
  176. #require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  177. throw new Zend_Test_PHPUnit_Constraint_Exception('No count provided against which to compare');
  178. }
  179. $this->_content = $match;
  180. return $this->_countContent($result, $match, $assertType);
  181. case self::ASSERT_QUERY:
  182. default:
  183. if ($this->_negate) {
  184. return (0 == count($result));
  185. } else {
  186. return (0 != count($result));
  187. }
  188. }
  189. }
  190. /**
  191. * Report Failure
  192. *
  193. * @see PHPUnit_Framework_Constraint for implementation details
  194. * @param mixed CSS selector path
  195. * @param string Failure description
  196. * @param object Cannot be used, null
  197. * @return void
  198. * @throws PHPUnit_Framework_ExpectationFailedException
  199. * NOTE:
  200. * Drastic changes up to PHPUnit 3.5.15 this was:
  201. * public function fail($other, $description, $not = false)
  202. * In PHPUnit 3.6.0 they changed the interface into this:
  203. * protected function fail($other, $description, PHPUnit_Framework_ComparisonFailure $comparisonFailure = NULL)
  204. * We use the new interface for PHP-strict checking
  205. */
  206. public function fail($other, $description, PHPUnit_Framework_ComparisonFailure $cannot_be_used = NULL)
  207. {
  208. #require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  209. switch ($this->_assertType) {
  210. case self::ASSERT_CONTENT_CONTAINS:
  211. $failure = 'Failed asserting node denoted by %s CONTAINS content "%s"';
  212. if ($this->_negate) {
  213. $failure = 'Failed asserting node DENOTED BY %s DOES NOT CONTAIN content "%s"';
  214. }
  215. $failure = sprintf($failure, $other, $this->_content);
  216. break;
  217. case self::ASSERT_CONTENT_REGEX:
  218. $failure = 'Failed asserting node denoted by %s CONTAINS content MATCHING "%s"';
  219. if ($this->_negate) {
  220. $failure = 'Failed asserting node DENOTED BY %s DOES NOT CONTAIN content MATCHING "%s"';
  221. }
  222. $failure = sprintf($failure, $other, $this->_content);
  223. break;
  224. case self::ASSERT_CONTENT_COUNT:
  225. $failure = 'Failed asserting node DENOTED BY %s OCCURS EXACTLY %d times';
  226. if ($this->_negate) {
  227. $failure = 'Failed asserting node DENOTED BY %s DOES NOT OCCUR EXACTLY %d times';
  228. }
  229. $failure = sprintf($failure, $other, $this->_content);
  230. break;
  231. case self::ASSERT_CONTENT_COUNT_MIN:
  232. $failure = 'Failed asserting node DENOTED BY %s OCCURS AT LEAST %d times';
  233. $failure = sprintf($failure, $other, $this->_content);
  234. break;
  235. case self::ASSERT_CONTENT_COUNT_MAX:
  236. $failure = 'Failed asserting node DENOTED BY %s OCCURS AT MOST %d times';
  237. $failure = sprintf($failure, $other, $this->_content);
  238. break;
  239. case self::ASSERT_QUERY:
  240. default:
  241. $failure = 'Failed asserting node DENOTED BY %s EXISTS';
  242. if ($this->_negate) {
  243. $failure = 'Failed asserting node DENOTED BY %s DOES NOT EXIST';
  244. }
  245. $failure = sprintf($failure, $other);
  246. break;
  247. }
  248. if (!empty($description)) {
  249. $failure = $description . "\n" . $failure;
  250. }
  251. throw new Zend_Test_PHPUnit_Constraint_Exception($failure);
  252. }
  253. /**
  254. * Complete implementation
  255. *
  256. * @return string
  257. */
  258. public function toString()
  259. {
  260. return '';
  261. }
  262. /**
  263. * Register XPath namespaces
  264. *
  265. * @param array $xpathNamespaces
  266. * @return void
  267. */
  268. public function registerXpathNamespaces($xpathNamespaces)
  269. {
  270. $this->_xpathNamespaces = $xpathNamespaces;
  271. }
  272. /**
  273. * Check to see if content is matched in selected nodes
  274. *
  275. * @param Zend_Dom_Query_Result $result
  276. * @param string $match Content to match
  277. * @return bool
  278. */
  279. protected function _matchContent($result, $match)
  280. {
  281. $match = (string) $match;
  282. if (0 == count($result)) {
  283. return false;
  284. }
  285. foreach ($result as $node) {
  286. $content = $this->_getNodeContent($node);
  287. if (strstr($content, $match)) {
  288. return true;
  289. }
  290. }
  291. return false;
  292. }
  293. /**
  294. * Check to see if content is NOT matched in selected nodes
  295. *
  296. * @param Zend_Dom_Query_Result $result
  297. * @param string $match
  298. * @return bool
  299. */
  300. protected function _notMatchContent($result, $match)
  301. {
  302. if (0 == count($result)) {
  303. return true;
  304. }
  305. foreach ($result as $node) {
  306. $content = $this->_getNodeContent($node);
  307. if (strstr($content, $match)) {
  308. return false;
  309. }
  310. }
  311. return true;
  312. }
  313. /**
  314. * Check to see if content is matched by regex in selected nodes
  315. *
  316. * @param Zend_Dom_Query_Result $result
  317. * @param string $pattern
  318. * @return bool
  319. */
  320. protected function _regexContent($result, $pattern)
  321. {
  322. if (0 == count($result)) {
  323. return false;
  324. }
  325. foreach ($result as $node) {
  326. $content = $this->_getNodeContent($node);
  327. if (preg_match($pattern, $content)) {
  328. return true;
  329. }
  330. }
  331. return false;
  332. }
  333. /**
  334. * Check to see if content is NOT matched by regex in selected nodes
  335. *
  336. * @param Zend_Dom_Query_Result $result
  337. * @param string $pattern
  338. * @return bool
  339. */
  340. protected function _notRegexContent($result, $pattern)
  341. {
  342. if (0 == count($result)) {
  343. return true;
  344. }
  345. foreach ($result as $node) {
  346. $content = $this->_getNodeContent($node);
  347. if (preg_match($pattern, $content)) {
  348. return false;
  349. }
  350. }
  351. return true;
  352. }
  353. /**
  354. * Determine if content count matches criteria
  355. *
  356. * @param Zend_Dom_Query_Result $result
  357. * @param int $test Value against which to test
  358. * @param string $type assertion type
  359. * @return boolean
  360. */
  361. protected function _countContent($result, $test, $type)
  362. {
  363. $count = count($result);
  364. switch ($type) {
  365. case self::ASSERT_CONTENT_COUNT:
  366. return ($this->_negate)
  367. ? ($test != $count)
  368. : ($test == $count);
  369. case self::ASSERT_CONTENT_COUNT_MIN:
  370. return ($count >= $test);
  371. case self::ASSERT_CONTENT_COUNT_MAX:
  372. return ($count <= $test);
  373. default:
  374. return false;
  375. }
  376. }
  377. /**
  378. * Get node content, minus node markup tags
  379. *
  380. * @param DOMNode $node
  381. * @return string
  382. */
  383. protected function _getNodeContent(DOMNode $node)
  384. {
  385. if ($node instanceof DOMAttr) {
  386. return $node->value;
  387. } else {
  388. $doc = $node->ownerDocument;
  389. $content = $doc->saveXML($node);
  390. $tag = $node->nodeName;
  391. $regex = '|</?' . $tag . '[^>]*>|';
  392. return preg_replace($regex, '', $content);
  393. }
  394. }
  395. }