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