PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/yousafsyed/easternglamor
PHP | 436 lines | 224 code | 39 blank | 173 comment | 34 complexity | d12b23a3678c4b791fc3d5b14acd2a61 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_DomQuery41 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. * NOTE 2:
  206. * Interface changed again in PHPUnit 4.1.0 because of refactoring to SebastianBergmann\Comparator
  207. */
  208. public function fail($other, $description, \SebastianBergmann\Comparator\ComparisonFailure $cannot_be_used = NULL)
  209. {
  210. #require_once 'Zend/Test/PHPUnit/Constraint/Exception.php';
  211. switch ($this->_assertType) {
  212. case self::ASSERT_CONTENT_CONTAINS:
  213. $failure = 'Failed asserting node denoted by %s CONTAINS content "%s"';
  214. if ($this->_negate) {
  215. $failure = 'Failed asserting node DENOTED BY %s DOES NOT CONTAIN content "%s"';
  216. }
  217. $failure = sprintf($failure, $other, $this->_content);
  218. break;
  219. case self::ASSERT_CONTENT_REGEX:
  220. $failure = 'Failed asserting node denoted by %s CONTAINS content MATCHING "%s"';
  221. if ($this->_negate) {
  222. $failure = 'Failed asserting node DENOTED BY %s DOES NOT CONTAIN content MATCHING "%s"';
  223. }
  224. $failure = sprintf($failure, $other, $this->_content);
  225. break;
  226. case self::ASSERT_CONTENT_COUNT:
  227. $failure = 'Failed asserting node DENOTED BY %s OCCURS EXACTLY %d times';
  228. if ($this->_negate) {
  229. $failure = 'Failed asserting node DENOTED BY %s DOES NOT OCCUR EXACTLY %d times';
  230. }
  231. $failure = sprintf($failure, $other, $this->_content);
  232. break;
  233. case self::ASSERT_CONTENT_COUNT_MIN:
  234. $failure = 'Failed asserting node DENOTED BY %s OCCURS AT LEAST %d times';
  235. $failure = sprintf($failure, $other, $this->_content);
  236. break;
  237. case self::ASSERT_CONTENT_COUNT_MAX:
  238. $failure = 'Failed asserting node DENOTED BY %s OCCURS AT MOST %d times';
  239. $failure = sprintf($failure, $other, $this->_content);
  240. break;
  241. case self::ASSERT_QUERY:
  242. default:
  243. $failure = 'Failed asserting node DENOTED BY %s EXISTS';
  244. if ($this->_negate) {
  245. $failure = 'Failed asserting node DENOTED BY %s DOES NOT EXIST';
  246. }
  247. $failure = sprintf($failure, $other);
  248. break;
  249. }
  250. if (!empty($description)) {
  251. $failure = $description . "\n" . $failure;
  252. }
  253. throw new Zend_Test_PHPUnit_Constraint_Exception($failure);
  254. }
  255. /**
  256. * Complete implementation
  257. *
  258. * @return string
  259. */
  260. public function toString()
  261. {
  262. return '';
  263. }
  264. /**
  265. * Register XPath namespaces
  266. *
  267. * @param array $xpathNamespaces
  268. * @return void
  269. */
  270. public function registerXpathNamespaces($xpathNamespaces)
  271. {
  272. $this->_xpathNamespaces = $xpathNamespaces;
  273. }
  274. /**
  275. * Check to see if content is matched in selected nodes
  276. *
  277. * @param Zend_Dom_Query_Result $result
  278. * @param string $match Content to match
  279. * @return bool
  280. */
  281. protected function _matchContent($result, $match)
  282. {
  283. $match = (string) $match;
  284. if (0 == count($result)) {
  285. return false;
  286. }
  287. foreach ($result as $node) {
  288. $content = $this->_getNodeContent($node);
  289. if (strstr($content, $match)) {
  290. return true;
  291. }
  292. }
  293. return false;
  294. }
  295. /**
  296. * Check to see if content is NOT matched in selected nodes
  297. *
  298. * @param Zend_Dom_Query_Result $result
  299. * @param string $match
  300. * @return bool
  301. */
  302. protected function _notMatchContent($result, $match)
  303. {
  304. if (0 == count($result)) {
  305. return true;
  306. }
  307. foreach ($result as $node) {
  308. $content = $this->_getNodeContent($node);
  309. if (strstr($content, $match)) {
  310. return false;
  311. }
  312. }
  313. return true;
  314. }
  315. /**
  316. * Check to see if content is matched by regex in selected nodes
  317. *
  318. * @param Zend_Dom_Query_Result $result
  319. * @param string $pattern
  320. * @return bool
  321. */
  322. protected function _regexContent($result, $pattern)
  323. {
  324. if (0 == count($result)) {
  325. return false;
  326. }
  327. foreach ($result as $node) {
  328. $content = $this->_getNodeContent($node);
  329. if (preg_match($pattern, $content)) {
  330. return true;
  331. }
  332. }
  333. return false;
  334. }
  335. /**
  336. * Check to see if content is NOT matched by regex in selected nodes
  337. *
  338. * @param Zend_Dom_Query_Result $result
  339. * @param string $pattern
  340. * @return bool
  341. */
  342. protected function _notRegexContent($result, $pattern)
  343. {
  344. if (0 == count($result)) {
  345. return true;
  346. }
  347. foreach ($result as $node) {
  348. $content = $this->_getNodeContent($node);
  349. if (preg_match($pattern, $content)) {
  350. return false;
  351. }
  352. }
  353. return true;
  354. }
  355. /**
  356. * Determine if content count matches criteria
  357. *
  358. * @param Zend_Dom_Query_Result $result
  359. * @param int $test Value against which to test
  360. * @param string $type assertion type
  361. * @return boolean
  362. */
  363. protected function _countContent($result, $test, $type)
  364. {
  365. $count = count($result);
  366. switch ($type) {
  367. case self::ASSERT_CONTENT_COUNT:
  368. return ($this->_negate)
  369. ? ($test != $count)
  370. : ($test == $count);
  371. case self::ASSERT_CONTENT_COUNT_MIN:
  372. return ($count >= $test);
  373. case self::ASSERT_CONTENT_COUNT_MAX:
  374. return ($count <= $test);
  375. default:
  376. return false;
  377. }
  378. }
  379. /**
  380. * Get node content, minus node markup tags
  381. *
  382. * @param DOMNode $node
  383. * @return string
  384. */
  385. protected function _getNodeContent(DOMNode $node)
  386. {
  387. if ($node instanceof DOMAttr) {
  388. return $node->value;
  389. } else {
  390. $doc = $node->ownerDocument;
  391. $content = $doc->saveXML($node);
  392. $tag = $node->nodeName;
  393. $regex = '|</?' . $tag . '[^>]*>|';
  394. return preg_replace($regex, '', $content);
  395. }
  396. }
  397. }