|
| 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 | +namespace Symfony\UX\Turbo\Tests\Request; |
| 13 | + |
| 14 | +use Symfony\Bundle\FrameworkBundle\KernelBrowser; |
| 15 | +use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
| 16 | +use Symfony\UX\Turbo\TurboBundle; |
| 17 | + |
| 18 | +/** |
| 19 | + * Tests the Turbo request listener. |
| 20 | + * |
| 21 | + * @author Alexander Hofbauer <a.hofbauer@fify.at> |
| 22 | + */ |
| 23 | +class RequestListenerTest extends WebTestCase |
| 24 | +{ |
| 25 | + public function testAddsTurboRequestFormat(): void |
| 26 | + { |
| 27 | + $client = static::createClient(server: [ |
| 28 | + 'HTTP_ACCEPT' => 'text/vnd.turbo-stream.html, text/html, application/xhtml+xml', |
| 29 | + ]); |
| 30 | + |
| 31 | + // simulate worker mode |
| 32 | + $client->disableReboot(); |
| 33 | + |
| 34 | + // request twice to test if listener is always called |
| 35 | + $this->assertPreferredFormat(); |
| 36 | + $this->assertPreferredFormat(); |
| 37 | + } |
| 38 | + |
| 39 | + private function assertPreferredFormat(): void |
| 40 | + { |
| 41 | + /** @var KernelBrowser $client */ |
| 42 | + $client = static::getClient(); |
| 43 | + |
| 44 | + $client->request('POST', '/turboRequest'); |
| 45 | + |
| 46 | + $response = $client->getResponse()->getContent(); |
| 47 | + |
| 48 | + $expectedJson = json_encode([ |
| 49 | + 'preferred_format' => TurboBundle::STREAM_FORMAT, |
| 50 | + ]); |
| 51 | + |
| 52 | + $this->assertJsonStringEqualsJsonString($expectedJson ?: '', $response ?: ''); |
| 53 | + } |
| 54 | +} |
0 commit comments