|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespaceSymfony\Bundle\FrameworkBundle\Test; |
| 13 | + |
| 14 | +usePHPUnit\Framework\Constraint\LogicalAnd; |
| 15 | +usePHPUnit\Framework\Constraint\LogicalNot; |
| 16 | +useSymfony\Bundle\FrameworkBundle\KernelBrowser; |
| 17 | +useSymfony\Component\BrowserKit\Test\ConstraintasBrowserKitConstraint; |
| 18 | +useSymfony\Component\DomCrawler\Crawler; |
| 19 | +useSymfony\Component\DomCrawler\Test\ConstraintasDomCrawlerConstraint; |
| 20 | +useSymfony\Component\HttpFoundation\Request; |
| 21 | +useSymfony\Component\HttpFoundation\Response; |
| 22 | +useSymfony\Component\HttpFoundation\Test\ConstraintasResponseConstraint; |
| 23 | + |
| 24 | +/** |
| 25 | + * Ideas borrowed from Laravel Dusk's assertions. |
| 26 | + * |
| 27 | + * @see https://laravel.com/docs/5.7/dusk#available-assertions |
| 28 | + */ |
| 29 | +trait WebTestAssertions |
| 30 | +{ |
| 31 | +publicstaticfunctionassertResponseIsSuccessful(string$message =''):void |
| 32 | + { |
| 33 | +self::assertThat(static::getResponse(),newResponseConstraint\ResponseIsSuccessful(),$message); |
| 34 | + } |
| 35 | + |
| 36 | +publicstaticfunctionassertResponseStatusCodeSame(int$expectedCode,string$message =''):void |
| 37 | + { |
| 38 | +self::assertThat(static::getResponse(),newResponseConstraint\ResponseStatusCodeSame($expectedCode),$message); |
| 39 | + } |
| 40 | + |
| 41 | +publicstaticfunctionassertResponseRedirects(string$expectedLocation =null,int$expectedCode =null,string$message =''):void |
| 42 | + { |
| 43 | +$constraint =newResponseConstraint\ResponseIsRedirected(); |
| 44 | +if ($expectedLocation) { |
| 45 | +$constraint = LogicalAnd::fromConstraints($constraint,newResponseConstraint\ResponseHeaderSame('Location',$expectedLocation)); |
| 46 | + } |
| 47 | +if ($expectedCode) { |
| 48 | +$constraint = LogicalAnd::fromConstraints($constraint,newResponseConstraint\ResponseStatusCodeSame($expectedCode)); |
| 49 | + } |
| 50 | + |
| 51 | +self::assertThat(static::getResponse(),$constraint,$message); |
| 52 | + } |
| 53 | + |
| 54 | +publicstaticfunctionassertResponseHasHeader(string$headerName,string$message =''):void |
| 55 | + { |
| 56 | +self::assertThat(static::getResponse(),newResponseConstraint\ResponseHasHeader($headerName),$message); |
| 57 | + } |
| 58 | + |
| 59 | +publicstaticfunctionassertResponseNotHasHeader(string$headerName,string$message =''):void |
| 60 | + { |
| 61 | +self::assertThat(static::getResponse(),newLogicalNot(newResponseConstraint\ResponseHasHeader($headerName)),$message); |
| 62 | + } |
| 63 | + |
| 64 | +publicstaticfunctionassertResponseHeaderSame(string$headerName,string$expectedValue,string$message =''):void |
| 65 | + { |
| 66 | +self::assertThat(static::getResponse(),newResponseConstraint\ResponseHeaderSame($headerName,$expectedValue),$message); |
| 67 | + } |
| 68 | + |
| 69 | +publicstaticfunctionassertResponseHeaderNotSame(string$headerName,string$expectedValue,string$message =''):void |
| 70 | + { |
| 71 | +self::assertThat(static::getResponse(),newLogicalNot(newResponseConstraint\ResponseHeaderSame($headerName,$expectedValue)),$message); |
| 72 | + } |
| 73 | + |
| 74 | +publicstaticfunctionassertResponseHasCookie(string$name,string$path ='/',string$domain =null,string$message =''):void |
| 75 | + { |
| 76 | +self::assertThat(static::getResponse(),newResponseConstraint\ResponseHasCookie($name,$path,$domain),$message); |
| 77 | + } |
| 78 | + |
| 79 | +publicstaticfunctionassertResponseNotHasCookie(string$name,string$path ='/',string$domain =null,string$message =''):void |
| 80 | + { |
| 81 | +self::assertThat(static::getResponse(),newLogicalNot(newResponseConstraint\ResponseHasCookie($name,$path,$domain)),$message); |
| 82 | + } |
| 83 | + |
| 84 | +publicstaticfunctionassertResponseCookieValueSame(string$name,string$expectedValue,string$path ='/',string$domain =null,string$message =''):void |
| 85 | + { |
| 86 | +self::assertThat(static::getResponse(), LogicalAnd::fromConstraints( |
| 87 | +newResponseConstraint\ResponseHasCookie($name,$path,$domain), |
| 88 | +newResponseConstraint\ResponseCookieValueSame($name,$expectedValue,$path,$domain) |
| 89 | + ),$message); |
| 90 | + } |
| 91 | + |
| 92 | +publicstaticfunctionassertBrowserHasCookie(string$name,string$path ='/',string$domain =null,string$message =''):void |
| 93 | + { |
| 94 | +self::assertThat(static::getClient(),newBrowserKitConstraint\BrowserHasCookie($name,$path,$domain),$message); |
| 95 | + } |
| 96 | + |
| 97 | +publicstaticfunctionassertBrowserNotHasCookie(string$name,string$path ='/',string$domain =null,string$message =''):void |
| 98 | + { |
| 99 | +self::assertThat(static::getClient(),newLogicalNot(newBrowserKitConstraint\BrowserHasCookie($name,$path,$domain)),$message); |
| 100 | + } |
| 101 | + |
| 102 | +publicstaticfunctionassertBrowserCookieValueSame(string$name,string$expectedValue,bool$raw =false,string$path ='/',string$domain =null,string$message =''):void |
| 103 | + { |
| 104 | +self::assertThat(static::getClient(), LogicalAnd::fromConstraints( |
| 105 | +newBrowserKitConstraint\BrowserHasCookie($name,$path,$domain), |
| 106 | +newBrowserKitConstraint\BrowserCookieValueSame($name,$expectedValue,$raw,$path,$domain) |
| 107 | + ),$message); |
| 108 | + } |
| 109 | + |
| 110 | +publicstaticfunctionassertSelectorExists(string$selector,string$message =''):void |
| 111 | + { |
| 112 | +self::assertThat(static::getCrawler(),newDomCrawlerConstraint\CrawlerSelectorExists($selector),$message); |
| 113 | + } |
| 114 | + |
| 115 | +publicstaticfunctionassertSelectorNotExists(string$selector,string$message =''):void |
| 116 | + { |
| 117 | +self::assertThat(static::getCrawler(),newLogicalNot(newDomCrawlerConstraint\CrawlerSelectorExists($selector)),$message); |
| 118 | + } |
| 119 | + |
| 120 | +publicstaticfunctionassertSelectorTextContains(string$selector,string$text,string$message =''):void |
| 121 | + { |
| 122 | +self::assertThat(static::getCrawler(), LogicalAnd::fromConstraints( |
| 123 | +newDomCrawlerConstraint\CrawlerSelectorExists($selector), |
| 124 | +newDomCrawlerConstraint\CrawlerSelectorTextContains($selector,$text) |
| 125 | + ),$message); |
| 126 | + } |
| 127 | + |
| 128 | +publicstaticfunctionassertSelectorTextSame(string$selector,string$text,string$message =''):void |
| 129 | + { |
| 130 | +self::assertThat(static::getCrawler(), LogicalAnd::fromConstraints( |
| 131 | +newDomCrawlerConstraint\CrawlerSelectorExists($selector), |
| 132 | +newDomCrawlerConstraint\CrawlerSelectorTextSame($selector,$text) |
| 133 | + ),$message); |
| 134 | + } |
| 135 | + |
| 136 | +publicstaticfunctionassertSelectorTextNotContains(string$selector,string$text,string$message =''):void |
| 137 | + { |
| 138 | +self::assertThat(static::getCrawler(), LogicalAnd::fromConstraints( |
| 139 | +newDomCrawlerConstraint\CrawlerSelectorExists($selector), |
| 140 | +newLogicalNot(newDomCrawlerConstraint\CrawlerSelectorTextContains($selector,$text)) |
| 141 | + ),$message); |
| 142 | + } |
| 143 | + |
| 144 | +publicstaticfunctionassertPageTitleSame(string$expectedTitle,string$message =''):void |
| 145 | + { |
| 146 | +self::assertSelectorTextSame('title',$expectedTitle,$message); |
| 147 | + } |
| 148 | + |
| 149 | +publicstaticfunctionassertPageTitleContains(string$expectedTitle,string$message =''):void |
| 150 | + { |
| 151 | +self::assertSelectorTextContains('title',$expectedTitle,$message); |
| 152 | + } |
| 153 | + |
| 154 | +publicstaticfunctionassertInputValueSame(string$fieldName,string$expectedValue,string$message =''):void |
| 155 | + { |
| 156 | +self::assertThat(static::getCrawler(), LogicalAnd::fromConstraints( |
| 157 | +newDomCrawlerConstraint\CrawlerSelectorExists("input[name=\"$fieldName\"]"), |
| 158 | +newDomCrawlerConstraint\CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]",'value',$expectedValue) |
| 159 | + ),$message); |
| 160 | + } |
| 161 | + |
| 162 | +publicstaticfunctionassertInputValueNotSame(string$fieldName,string$expectedValue,string$message =''):void |
| 163 | + { |
| 164 | +self::assertThat(static::getCrawler(), LogicalAnd::fromConstraints( |
| 165 | +newDomCrawlerConstraint\CrawlerSelectorExists("input[name=\"$fieldName\"]"), |
| 166 | +newLogicalNot(newDomCrawlerConstraint\CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]",'value',$expectedValue)) |
| 167 | + ),$message); |
| 168 | + } |
| 169 | + |
| 170 | +publicstaticfunctionassertRequestAttributeValueSame(string$name,string$expectedValue,string$message =''):void |
| 171 | + { |
| 172 | +self::assertThat(static::getRequest(),newResponseConstraint\RequestAttributeValueSame($name,$expectedValue),$message); |
| 173 | + } |
| 174 | + |
| 175 | +publicstaticfunctionassertRouteSame($expectedRoute,array$parameters = [],string$message =''):void |
| 176 | + { |
| 177 | +$constraint =newResponseConstraint\RequestAttributeValueSame('_route',$expectedRoute); |
| 178 | +$constraints = []; |
| 179 | +foreach ($parametersas$key =>$value) { |
| 180 | +$constraints[] =newResponseConstraint\RequestAttributeValueSame($key,$value); |
| 181 | + } |
| 182 | +if ($constraints) { |
| 183 | +$constraint = LogicalAnd::fromConstraints($constraint, ...$constraints); |
| 184 | + } |
| 185 | + |
| 186 | +self::assertThat(static::getRequest(),$constraint,$message); |
| 187 | + } |
| 188 | + |
| 189 | +privatestaticfunctiongetClient():KernelBrowser |
| 190 | + { |
| 191 | +if (!static::$clientinstanceof KernelBrowser) { |
| 192 | +static::fail(\sprintf('A client must be set to make assertions on it. Did you forget to call "%s::createClient"?',__CLASS__)); |
| 193 | + } |
| 194 | + |
| 195 | +returnstatic::$client; |
| 196 | + } |
| 197 | + |
| 198 | +privatestaticfunctiongetCrawler():Crawler |
| 199 | + { |
| 200 | +if (!$crawler =static::getClient()->getCrawler()) { |
| 201 | +static::fail('A client must have a crawler to make assertions. Did you forget to make an HTTP request?'); |
| 202 | + } |
| 203 | + |
| 204 | +return$crawler; |
| 205 | + } |
| 206 | + |
| 207 | +privatestaticfunctiongetResponse():Response |
| 208 | + { |
| 209 | +if (!$response =static::getClient()->getResponse()) { |
| 210 | +static::fail('A client must have an HTTP Response to make assertions. Did you forget to make an HTTP request?'); |
| 211 | + } |
| 212 | + |
| 213 | +return$response; |
| 214 | + } |
| 215 | + |
| 216 | +privatestaticfunctiongetRequest():Request |
| 217 | + { |
| 218 | +if (!$request =static::getClient()->getRequest()) { |
| 219 | +static::fail('A client must have an HTTP Request to make assertions. Did you forget to make an HTTP request?'); |
| 220 | + } |
| 221 | + |
| 222 | +return$request; |
| 223 | + } |
| 224 | +} |