AssertHtmlTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. namespace Cake\Test\Fixture;
  3. use Cake\TestSuite\TestCase;
  4. use PHPUnit\Framework\ExpectationFailedException;
  5. /**
  6. * This class helps in indirectly testing the functionality of TestCase::assertHtml
  7. */
  8. class AssertHtmlTest extends TestCase
  9. {
  10. /**
  11. * Test whitespace after HTML tags
  12. *
  13. * @return
  14. */
  15. public function testAssertHtmlWhitespaceAfter()
  16. {
  17. $input = <<<HTML
  18. <div class="wrapper">
  19. <h4 class="widget-title">Popular tags
  20. <i class="i-icon"></i>
  21. </h4>
  22. </div>
  23. HTML;
  24. $pattern = [
  25. 'div' => ['class' => 'wrapper'],
  26. 'h4' => ['class' => 'widget-title'], 'Popular tags',
  27. 'i' => ['class' => 'i-icon'], '/i',
  28. '/h4',
  29. '/div',
  30. ];
  31. $this->assertHtml($pattern, $input);
  32. }
  33. /**
  34. * Test whitespace inside HTML tags
  35. *
  36. * @return void
  37. */
  38. public function testAssertHtmlInnerWhitespace()
  39. {
  40. $input = <<<HTML
  41. <div class="widget">
  42. <div class="widget-content">
  43. A custom widget
  44. </div>
  45. </div>
  46. HTML;
  47. $expected = [
  48. ['div' => ['class' => 'widget']],
  49. ['div' => ['class' => 'widget-content']],
  50. 'A custom widget',
  51. '/div',
  52. '/div',
  53. ];
  54. $this->assertHtml($expected, $input);
  55. }
  56. /**
  57. * test assertHtml works with single and double quotes
  58. *
  59. * @return void
  60. */
  61. public function testAssertHtmlQuoting()
  62. {
  63. $input = '<a href="/test.html" class="active">My link</a>';
  64. $pattern = [
  65. 'a' => ['href' => '/test.html', 'class' => 'active'],
  66. 'My link',
  67. '/a',
  68. ];
  69. $this->assertHtml($pattern, $input);
  70. $input = "<a href='/test.html' class='active'>My link</a>";
  71. $pattern = [
  72. 'a' => ['href' => '/test.html', 'class' => 'active'],
  73. 'My link',
  74. '/a',
  75. ];
  76. $this->assertHtml($pattern, $input);
  77. $input = "<a href='/test.html' class='active'>My link</a>";
  78. $pattern = [
  79. 'a' => ['href' => 'preg:/.*\.html/', 'class' => 'active'],
  80. 'My link',
  81. '/a',
  82. ];
  83. $this->assertHtml($pattern, $input);
  84. $input = '<span><strong>Text</strong></span>';
  85. $pattern = [
  86. '<span',
  87. '<strong',
  88. 'Text',
  89. '/strong',
  90. '/span',
  91. ];
  92. $this->assertHtml($pattern, $input);
  93. $input = "<span class='active'><strong>Text</strong></span>";
  94. $pattern = [
  95. 'span' => ['class'],
  96. '<strong',
  97. 'Text',
  98. '/strong',
  99. '/span',
  100. ];
  101. $this->assertHtml($pattern, $input);
  102. }
  103. /**
  104. * Test that assertHtml runs quickly.
  105. *
  106. * @return void
  107. */
  108. public function testAssertHtmlRuntimeComplexity()
  109. {
  110. $pattern = [
  111. 'div' => [
  112. 'attr1' => 'val1',
  113. 'attr2' => 'val2',
  114. 'attr3' => 'val3',
  115. 'attr4' => 'val4',
  116. 'attr5' => 'val5',
  117. 'attr6' => 'val6',
  118. 'attr7' => 'val7',
  119. 'attr8' => 'val8',
  120. ],
  121. 'My div',
  122. '/div',
  123. ];
  124. $input = '<div attr8="val8" attr6="val6" attr4="val4" attr2="val2"' .
  125. ' attr1="val1" attr3="val3" attr5="val5" attr7="val7" />' .
  126. 'My div' .
  127. '</div>';
  128. $this->assertHtml($pattern, $input);
  129. }
  130. /**
  131. * test that assertHtml knows how to handle correct quoting.
  132. *
  133. * @return void
  134. */
  135. public function testAssertHtmlQuotes()
  136. {
  137. $input = '<a href="/test.html" class="active">My link</a>';
  138. $pattern = [
  139. 'a' => ['href' => '/test.html', 'class' => 'active'],
  140. 'My link',
  141. '/a',
  142. ];
  143. $this->assertHtml($pattern, $input);
  144. $input = "<a href='/test.html' class='active'>My link</a>";
  145. $pattern = [
  146. 'a' => ['href' => '/test.html', 'class' => 'active'],
  147. 'My link',
  148. '/a',
  149. ];
  150. $this->assertHtml($pattern, $input);
  151. $input = "<a href='/test.html' class='active'>My link</a>";
  152. $pattern = [
  153. 'a' => ['href' => 'preg:/.*\.html/', 'class' => 'active'],
  154. 'My link',
  155. '/a',
  156. ];
  157. $this->assertHtml($pattern, $input);
  158. }
  159. /**
  160. * testNumericValuesInExpectationForAssertHtml
  161. *
  162. * @return void
  163. */
  164. public function testNumericValuesInExpectationForAssertHtml()
  165. {
  166. $value = 220985;
  167. $input = '<p><strong>' . $value . '</strong></p>';
  168. $pattern = [
  169. '<p',
  170. '<strong',
  171. $value,
  172. '/strong',
  173. '/p',
  174. ];
  175. $this->assertHtml($pattern, $input);
  176. $input = '<p><strong>' . $value . '</strong></p><p><strong>' . $value . '</strong></p>';
  177. $pattern = [
  178. '<p',
  179. '<strong',
  180. $value,
  181. '/strong',
  182. '/p',
  183. '<p',
  184. '<strong',
  185. $value,
  186. '/strong',
  187. '/p',
  188. ];
  189. $this->assertHtml($pattern, $input);
  190. $input = '<p><strong>' . $value . '</strong></p><p id="' . $value . '"><strong>' . $value . '</strong></p>';
  191. $pattern = [
  192. '<p',
  193. '<strong',
  194. $value,
  195. '/strong',
  196. '/p',
  197. 'p' => ['id' => $value],
  198. '<strong',
  199. $value,
  200. '/strong',
  201. '/p',
  202. ];
  203. $this->assertHtml($pattern, $input);
  204. }
  205. /**
  206. * test assertions fail when attributes are wrong.
  207. *
  208. * @return void
  209. */
  210. public function testBadAssertHtmlInvalidAttribute()
  211. {
  212. $input = '<a href="/test.html" class="active">My link</a>';
  213. $pattern = [
  214. 'a' => ['hRef' => '/test.html', 'clAss' => 'active'],
  215. 'My link2',
  216. '/a',
  217. ];
  218. try {
  219. $this->assertHtml($pattern, $input);
  220. $this->fail('Assertion should fail');
  221. } catch (ExpectationFailedException $e) {
  222. $this->assertContains(
  223. 'Attribute did not match. Was expecting Attribute "clAss" == "active"',
  224. $e->getMessage()
  225. );
  226. }
  227. }
  228. /**
  229. * test assertion failure on incomplete HTML
  230. *
  231. * @return void
  232. */
  233. public function testBadAssertHtmlMissingTags()
  234. {
  235. $input = '<a href="/test.html" class="active">My link</a>';
  236. $pattern = [
  237. '<a' => ['href' => '/test.html', 'class' => 'active'],
  238. 'My link',
  239. '/a',
  240. ];
  241. try {
  242. $this->assertHtml($pattern, $input);
  243. } catch (ExpectationFailedException $e) {
  244. $this->assertContains(
  245. 'Item #1 / regex #0 failed: Open <a tag',
  246. $e->getMessage()
  247. );
  248. }
  249. }
  250. }