|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests; |
| 4 | + |
| 5 | +use Codeception\Module\Symfony\BrowserAssertionsTrait; |
| 6 | +use Symfony\Bundle\FrameworkBundle\KernelBrowser; |
| 7 | +use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; |
| 8 | +use Symfony\Component\BrowserKit\Cookie; |
| 9 | + |
| 10 | +class BrowserAssertionsTest extends KernelTestCase |
| 11 | +{ |
| 12 | + use BrowserAssertionsTrait; |
| 13 | + |
| 14 | + private KernelBrowser $client; |
| 15 | + |
| 16 | + protected function setUp(): void |
| 17 | + { |
| 18 | + self::bootKernel(); |
| 19 | + $this->client = new KernelBrowser(self::$kernel); |
| 20 | + $this->client->getCookieJar()->set(new Cookie('browser_cookie', 'value')); |
| 21 | + } |
| 22 | + |
| 23 | + protected function tearDown(): void |
| 24 | + { |
| 25 | + parent::tearDown(); |
| 26 | + restore_exception_handler(); |
| 27 | + } |
| 28 | + |
| 29 | + protected function getClient(): KernelBrowser |
| 30 | + { |
| 31 | + return $this->client; |
| 32 | + } |
| 33 | + |
| 34 | + protected static function getKernelClass(): string |
| 35 | + { |
| 36 | + return \TestKernel::class; |
| 37 | + } |
| 38 | + |
| 39 | + public function testBrowserAssertions(): void |
| 40 | + { |
| 41 | + $this->client->request('GET', '/sample'); |
| 42 | + |
| 43 | + $this->assertBrowserHasCookie('browser_cookie'); |
| 44 | + $this->assertBrowserCookieValueSame('browser_cookie', 'value'); |
| 45 | + $this->assertBrowserNotHasCookie('missing_cookie'); |
| 46 | + |
| 47 | + $this->assertRequestAttributeValueSame('foo', 'bar'); |
| 48 | + |
| 49 | + $this->assertResponseHasCookie('response_cookie'); |
| 50 | + $this->assertResponseCookieValueSame('response_cookie', 'yum'); |
| 51 | + $this->assertResponseNotHasCookie('other_cookie'); |
| 52 | + |
| 53 | + $this->assertResponseHasHeader('X-Test'); |
| 54 | + $this->assertResponseHeaderSame('X-Test', '1'); |
| 55 | + $this->assertResponseHeaderNotSame('X-Test', '2'); |
| 56 | + $this->assertResponseNotHasHeader('X-None'); |
| 57 | + |
| 58 | + $this->assertResponseFormatSame('html'); |
| 59 | + $this->assertResponseIsSuccessful(); |
| 60 | + $this->assertResponseStatusCodeSame(200); |
| 61 | + $this->assertRouteSame('sample'); |
| 62 | + |
| 63 | + $this->seePageIsAvailable('/sample'); |
| 64 | + $this->seePageRedirectsTo('/redirect', '/sample'); |
| 65 | + |
| 66 | + $this->client->request('GET', '/unprocessable'); |
| 67 | + $this->assertResponseIsUnprocessable(); |
| 68 | + } |
| 69 | +} |
0 commit comments