|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Doctrine\Bundle\DoctrineBundle\Tests\ArgumentResolver; |
| 6 | + |
| 7 | +use Doctrine\Bundle\DoctrineBundle\Tests\ArgumentResolver\Fixtures\EntityValueResolverFunctionalKernel; |
| 8 | +use Doctrine\Bundle\DoctrineBundle\Tests\ArgumentResolver\Fixtures\Post; |
| 9 | +use Doctrine\ORM\EntityManagerInterface; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use Symfony\Component\HttpFoundation\Request; |
| 12 | + |
| 13 | +use function interface_exists; |
| 14 | +use function json_decode; |
| 15 | + |
| 16 | +/** |
| 17 | + * Regression coverage for the /posts/{post} + Post $post happy path resolved by |
| 18 | + * Symfony\Bridge\Doctrine\ArgumentResolver\EntityValueResolver. The tag priority |
| 19 | + * configured in config/orm.php (and the priorities of FrameworkBundle's own |
| 20 | + * value resolvers) must stay compatible so that the entity argument is still |
| 21 | + * populated from the route placeholder. |
| 22 | + */ |
| 23 | +class EntityValueResolverFunctionalTest extends TestCase |
| 24 | +{ |
| 25 | + public function testEntityArgumentResolvedFromRoutePlaceholder(): void |
| 26 | + { |
| 27 | + if (! interface_exists(EntityManagerInterface::class)) { |
| 28 | + self::markTestSkipped('This test requires ORM'); |
| 29 | + } |
| 30 | + |
| 31 | + $kernel = new EntityValueResolverFunctionalKernel(); |
| 32 | + $kernel->boot(); |
| 33 | + |
| 34 | + try { |
| 35 | + $container = $kernel->getContainer(); |
| 36 | + $em = $container->get('doctrine.orm.default_entity_manager'); |
| 37 | + |
| 38 | + // SchemaTool::createSchema triggers Schema::createTable, deprecated in |
| 39 | + // DBAL 4 (https://github.com/doctrine/dbal/pull/7373); the DBAL 4 fluent |
| 40 | + // replacement (Table::editor() / Column::editor()) is unavailable on |
| 41 | + // DBAL 3 which this branch must still support, so a raw CREATE TABLE is |
| 42 | + // emitted directly through the connection. The test kernel is SQLite- |
| 43 | + // only, so a SQLite literal is safe here. |
| 44 | + $em->getConnection()->executeStatement( |
| 45 | + 'CREATE TABLE posts (id INTEGER NOT NULL, title VARCHAR(255) NOT NULL, PRIMARY KEY(id))', |
| 46 | + ); |
| 47 | + |
| 48 | + $post = new Post('Hello world'); |
| 49 | + $em->persist($post); |
| 50 | + $em->flush(); |
| 51 | + $em->clear(); |
| 52 | + |
| 53 | + $response = $kernel->handle(Request::create('/posts/' . $post->id, 'GET')); |
| 54 | + |
| 55 | + self::assertSame(200, $response->getStatusCode()); |
| 56 | + self::assertSame( |
| 57 | + ['id' => $post->id, 'title' => 'Hello world'], |
| 58 | + json_decode((string) $response->getContent(), true), |
| 59 | + ); |
| 60 | + } finally { |
| 61 | + $kernel->shutdown(); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments