Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 620d9dc

Browse files
committed
Testing rendered result
1 parent df8e92e commit 620d9dc

File tree

1 file changed

+20
-38
lines changed

1 file changed

+20
-38
lines changed

src/Symfony/Bundle/WebProfilerBundle/Tests/Controller/RouterControllerTest.php

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,56 +11,38 @@
1111

1212
namespace Symfony\Bundle\WebProfilerBundle\Tests\Controller;
1313

14+
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
15+
use Symfony\Bundle\FrameworkBundle\Routing\Router;
1416
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
15-
use Symfony\Bundle\WebProfilerBundle\Controller\RouterController;
16-
use Symfony\Component\HttpFoundation\Request;
17-
use Symfony\Component\HttpFoundation\Response;
18-
use Symfony\Component\HttpKernel\DataCollector\RequestDataCollector;
19-
use Symfony\Component\Routing\Matcher\UrlMatcherInterface;
20-
use Symfony\Component\Routing\RequestContext;
17+
use Symfony\Bundle\WebProfilerBundle\Tests\Functional\WebProfilerBundleKernel;
18+
use Symfony\Component\DomCrawler\Crawler;
2119
use Symfony\Component\Routing\Route;
22-
use Symfony\Component\Routing\RouteCollection;
23-
use Twig\Environment;
2420

2521
class RouterControllerTest extends WebTestCase
2622
{
2723
public function testFalseNegativeTrace()
2824
{
29-
$routes = new RouteCollection();
30-
$routes->add('route1', new Route('/foo/bar:123/baz'));
25+
$path = '/foo/bar:123/baz';
3126

32-
$request = Request::create('https://example.com/foo/bar:123/baz');
33-
$response = new Response;
27+
$kernel = new WebProfilerBundleKernel();
28+
$client = new KernelBrowser($kernel);
29+
$client->disableReboot();
30+
$client->getKernel()->boot();
3431

35-
$requestDataCollector = new RequestDataCollector();
36-
$requestDataCollector->collect($request, $response);
37-
$requestDataCollector->lateCollect();
32+
/** @var Router $router */
33+
$router = $client->getContainer()->get('router');
34+
$router->getRouteCollection()->add('route1', new Route($path));
3835

39-
$matcher = $this->createMock(UrlMatcherInterface::class);
36+
$client->request('GET', $path);
4037

41-
$matcher
42-
->method('getContext')
43-
->willReturn(new RequestContext());
38+
$crawler = $client->request('GET', '/_profiler/latest?panel=router&type=request');
4439

45-
$controller = $this->getMockBuilder(RouterController::class)
46-
->setConstructorArgs([
47-
'profiler' => null,
48-
'twig' => $this->createMock(Environment::class),
49-
'matcher' => $matcher,
50-
'routes' => $routes
51-
])
52-
->onlyMethods(['panelAction'])
53-
->getMock();
40+
$matchedRouteCell = $crawler
41+
->filter('#router-logs .status-success td')
42+
->reduce(function (Crawler $td) use ($path): bool {
43+
return $td->innerText() === $path;
44+
});
5445

55-
$method = new \ReflectionMethod(RouterController::class, 'getTraces');
56-
$method->setAccessible(true);
57-
58-
$traces = $method->invoke($controller, $requestDataCollector, 'GET');
59-
60-
$levels = array_map(static function (array $trace): int {
61-
return $trace['level'];
62-
}, $traces);
63-
64-
$this->assertContains(2, $levels);
46+
$this->assertSame(1, $matchedRouteCell->count());
6547
}
6648
}

0 commit comments

Comments
 (0)