]> BookStack Code Mirror - bookstack/blob - tests/TestResponse.php
Update passwords.php
[bookstack] / tests / TestResponse.php
1 <?php namespace Tests;
2
3 use \Illuminate\Foundation\Testing\TestResponse as BaseTestResponse;
4 use Symfony\Component\DomCrawler\Crawler;
5 use PHPUnit\Framework\Assert as PHPUnit;
6
7 /**
8  * Class TestResponse
9  * Custom extension of the default Laravel TestResponse class.
10  * @package Tests
11  */
12 class TestResponse extends BaseTestResponse {
13
14     protected $crawlerInstance;
15
16     /**
17      * Get the DOM Crawler for the response content.
18      * @return Crawler
19      */
20     protected function crawler()
21     {
22         if (!is_object($this->crawlerInstance)) {
23             $this->crawlerInstance = new Crawler($this->getContent());
24         }
25         return $this->crawlerInstance;
26     }
27
28     /**
29      * Assert the response contains the specified element.
30      * @param string $selector
31      * @return $this
32      */
33     public function assertElementExists(string $selector)
34     {
35         $elements = $this->crawler()->filter($selector);
36         PHPUnit::assertTrue(
37             $elements->count() > 0,
38             'Unable to find element matching the selector: '.PHP_EOL.PHP_EOL.
39             "[{$selector}]".PHP_EOL.PHP_EOL.
40             'within'.PHP_EOL.PHP_EOL.
41             "[{$this->getContent()}]."
42         );
43         return $this;
44     }
45
46     /**
47      * Assert the response does not contain the specified element.
48      * @param string $selector
49      * @return $this
50      */
51     public function assertElementNotExists(string $selector)
52     {
53         $elements = $this->crawler()->filter($selector);
54         PHPUnit::assertTrue(
55             $elements->count() === 0,
56             'Found elements matching the selector: '.PHP_EOL.PHP_EOL.
57             "[{$selector}]".PHP_EOL.PHP_EOL.
58             'within'.PHP_EOL.PHP_EOL.
59             "[{$this->getContent()}]."
60         );
61         return $this;
62     }
63
64     /**
65      * Assert the response includes a specific element containing the given text.
66      * @param string $selector
67      * @param string $text
68      * @return $this
69      */
70     public function assertElementContains(string $selector, string $text)
71     {
72         $elements = $this->crawler()->filter($selector);
73         $matched = false;
74         $pattern = $this->getEscapedPattern($text);
75         foreach ($elements as $element) {
76             $element = new Crawler($element);
77             if (preg_match("/$pattern/i", $element->html())) {
78                 $matched = true;
79                 break;
80             }
81         }
82
83         PHPUnit::assertTrue(
84             $matched,
85             'Unable to find element of selector: '.PHP_EOL.PHP_EOL.
86             "[{$selector}]".PHP_EOL.PHP_EOL.
87             'containing text'.PHP_EOL.PHP_EOL.
88             "[{$text}]".PHP_EOL.PHP_EOL.
89             'within'.PHP_EOL.PHP_EOL.
90             "[{$this->getContent()}]."
91         );
92
93         return $this;
94     }
95
96     /**
97      * Assert the response does not include a specific element containing the given text.
98      * @param string $selector
99      * @param string $text
100      * @return $this
101      */
102     public function assertElementNotContains(string $selector, string $text)
103     {
104         $elements = $this->crawler()->filter($selector);
105         $matched = false;
106         $pattern = $this->getEscapedPattern($text);
107         foreach ($elements as $element) {
108             $element = new Crawler($element);
109             if (preg_match("/$pattern/i", $element->html())) {
110                 $matched = true;
111                 break;
112             }
113         }
114
115         PHPUnit::assertTrue(
116             !$matched,
117             'Found element of selector: '.PHP_EOL.PHP_EOL.
118             "[{$selector}]".PHP_EOL.PHP_EOL.
119             'containing text'.PHP_EOL.PHP_EOL.
120             "[{$text}]".PHP_EOL.PHP_EOL.
121             'within'.PHP_EOL.PHP_EOL.
122             "[{$this->getContent()}]."
123         );
124
125         return $this;
126     }
127
128     /**
129      * Get the escaped text pattern for the constraint.
130      * @param  string  $text
131      * @return string
132      */
133     protected function getEscapedPattern($text)
134     {
135         $rawPattern = preg_quote($text, '/');
136         $escapedPattern = preg_quote(e($text), '/');
137         return $rawPattern == $escapedPattern
138             ? $rawPattern : "({$rawPattern}|{$escapedPattern})";
139     }
140
141 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.