PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Foundation/Testing/Constraints/PageConstraint.php

https://gitlab.com/mdabutaleb/bitm-laravel-1
PHP | 124 lines | 57 code | 16 blank | 51 comment | 5 complexity | 29afaea13876d05f8a4e20dbc67c2961 MD5 | raw file
  1. <?php
  2. namespace Illuminate\Foundation\Testing\Constraints;
  3. use PHPUnit_Framework_Constraint;
  4. use Symfony\Component\DomCrawler\Crawler;
  5. use SebastianBergmann\Comparator\ComparisonFailure;
  6. use PHPUnit_Framework_ExpectationFailedException as FailedExpection;
  7. abstract class PageConstraint extends PHPUnit_Framework_Constraint
  8. {
  9. /**
  10. * Make sure we obtain the HTML from the crawler or the response.
  11. *
  12. * @param \Symfony\Component\DomCrawler\Crawler|string $crawler
  13. * @return string
  14. */
  15. protected function html($crawler)
  16. {
  17. return is_object($crawler) ? $crawler->html() : $crawler;
  18. }
  19. /**
  20. * Make sure we obtain the HTML from the crawler or the response.
  21. *
  22. * @param \Symfony\Component\DomCrawler\Crawler|string $crawler
  23. * @return string
  24. */
  25. protected function text($crawler)
  26. {
  27. return is_object($crawler) ? $crawler->text() : strip_tags($crawler);
  28. }
  29. /**
  30. * Create a crawler instance if the given value is not already a Crawler.
  31. *
  32. * @param \Symfony\Component\DomCrawler\Crawler|string $crawler
  33. * @return \Symfony\Component\DomCrawler\Crawler
  34. */
  35. protected function crawler($crawler)
  36. {
  37. return is_object($crawler) ? $crawler : new Crawler($crawler);
  38. }
  39. /**
  40. * Get the escaped text pattern for the constraint.
  41. *
  42. * @param string $text
  43. * @return string
  44. */
  45. protected function getEscapedPattern($text)
  46. {
  47. $rawPattern = preg_quote($text, '/');
  48. $escapedPattern = preg_quote(e($text), '/');
  49. return $rawPattern == $escapedPattern
  50. ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
  51. }
  52. /**
  53. * Throw an exception for the given comparison and test description.
  54. *
  55. * @param \Symfony\Component\DomCrawler\Crawler|string $crawler
  56. * @param string $description
  57. * @param \SebastianBergmann\Comparator\ComparisonFailure|null $comparisonFailure
  58. * @return void
  59. *
  60. * @throws \PHPUnit_Framework_ExpectationFailedException
  61. */
  62. protected function fail($crawler, $description, ComparisonFailure $comparisonFailure = null)
  63. {
  64. $html = $this->html($crawler);
  65. $failureDescription = sprintf(
  66. "%s\n\n\nFailed asserting that %s",
  67. $html, $this->getFailureDescription()
  68. );
  69. if (! empty($description)) {
  70. $failureDescription .= ": {$description}";
  71. }
  72. if (trim($html) != '') {
  73. $failureDescription .= '. Please check the content above.';
  74. } else {
  75. $failureDescription .= '. The response is empty.';
  76. }
  77. throw new FailedExpection($failureDescription, $comparisonFailure);
  78. }
  79. /**
  80. * Get the description of the failure.
  81. *
  82. * @return string
  83. */
  84. protected function getFailureDescription()
  85. {
  86. return 'the page contains '.$this->toString();
  87. }
  88. /**
  89. * Returns the reversed description of the failure.
  90. *
  91. * @return string
  92. */
  93. protected function getReverseFailureDescription()
  94. {
  95. return 'the page does not contain '.$this->toString();
  96. }
  97. /**
  98. * Get a string representation of the object.
  99. *
  100. * Placeholder method to avoid forcing definition of this method.
  101. *
  102. * @return string
  103. */
  104. public function toString()
  105. {
  106. return '';
  107. }
  108. }