Skip to content

Commit 150ac5d

Browse files
authored
Add collectors unit tests for route and session (barryvdh#1642)
* Add collectors unit tests for route and session * Change test method attribute syntax
1 parent de8016c commit 150ac5d

File tree

8 files changed

+200
-13
lines changed

8 files changed

+200
-13
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
composer.phar
55
composer.lock
66
.DS_Store
7-
.phpunit.result.cache
7+
.phpunit*
88
/tests/Browser

src/DataCollector/RouteCollector.php

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
use Closure;
66
use DebugBar\DataCollector\DataCollector;
77
use DebugBar\DataCollector\Renderable;
8-
use Illuminate\Http\Request;
9-
use Illuminate\Routing\Route;
108
use Illuminate\Routing\Router;
119
use Illuminate\Support\Facades\Config;
1210

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?php
2+
3+
namespace Barryvdh\Debugbar\Tests\DataCollector;
4+
5+
use Barryvdh\Debugbar\Tests\TestCase;
6+
use DebugBar\DataCollector\DataCollector;
7+
8+
class RouteCollectorTest extends TestCase
9+
{
10+
/** @var \Barryvdh\Debugbar\DataCollector\RouteCollector $collector */
11+
private DataCollector $routeCollector;
12+
protected function setUp(): void
13+
{
14+
parent::setUp();
15+
debugbar()->boot();
16+
17+
$this->routeCollector = debugbar()->getCollector('route');
18+
}
19+
20+
public function testItCollectsRouteUri()
21+
{
22+
$this->get('web/html');
23+
$this->assertSame('GET web/html', $this->routeCollector->collect()['uri']);
24+
25+
$this->call('POST', 'web/mw');
26+
$this->assertSame('POST web/mw', $this->routeCollector->collect()['uri']);
27+
}
28+
29+
/**
30+
* @dataProvider controllerData
31+
*/
32+
public function testItCollectsWithControllerHandler($controller, $file)
33+
{
34+
$this->get('web/show');
35+
36+
$collected = $this->routeCollector->collect();
37+
38+
$this->assertNotEmpty($collected);
39+
$this->assertArrayHasKey('file', $collected);
40+
$this->assertArrayHasKey('controller', $collected);
41+
$this->assertStringContainsString($file, $collected['file']);
42+
$this->assertStringContainsString($controller, $collected['controller']);
43+
}
44+
45+
/**
46+
* @dataProvider viewComponentData
47+
*/
48+
public function testItCollectsWithViewComponentHandler($controller, $file)
49+
{
50+
$this->get('web/view');
51+
52+
$collected = $this->routeCollector->collect();
53+
54+
$this->assertStringContainsString($file, $collected['file']);
55+
$this->assertStringContainsString($controller, $collected['controller']);
56+
}
57+
58+
/**
59+
* @dataProvider closureData
60+
*/
61+
public function testItCollectsWithClosureHandler($file)
62+
{
63+
$this->get('web/html');
64+
65+
$collected = $this->routeCollector->collect();
66+
67+
$this->assertNotEmpty($collected);
68+
$this->assertArrayHasKey('uses', $collected);
69+
$this->assertArrayHasKey('file', $collected);
70+
$this->assertStringContainsString($file, $collected['uses']);
71+
$this->assertStringContainsString($file, $collected['file']);
72+
}
73+
74+
public function testItCollectsMiddleware()
75+
{
76+
$this->call('POST', 'web/mw');
77+
78+
$collected = $this->routeCollector->collect();
79+
80+
$this->assertNotEmpty($collected);
81+
$this->assertArrayHasKey('middleware', $collected);
82+
$this->assertStringContainsString('MockMiddleware', $collected['middleware']);
83+
}
84+
85+
public static function controllerData()
86+
{
87+
$filePath = urlencode(str_replace('\\', '/', realpath(__DIR__ . '/../Mocks/MockController.php')));
88+
return [['MockController@show',
89+
sprintf('phpstorm://open?file=%s', $filePath)
90+
]];
91+
}
92+
93+
public static function viewComponentData()
94+
{
95+
$filePath = urlencode(str_replace('\\', '/', realpath(__DIR__ . '/../Mocks/MockViewComponent.php')));
96+
return [['MockViewComponent@render',
97+
sprintf('phpstorm://open?file=%s', $filePath)
98+
]];
99+
}
100+
101+
public static function closureData()
102+
{
103+
return [['TestCase.php']];
104+
}
105+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Barryvdh\Debugbar\Tests\DataCollector;
4+
5+
use Barryvdh\Debugbar\Tests\TestCase;
6+
use Barryvdh\Debugbar\DataCollector\SessionCollector;
7+
use Illuminate\Session\SessionManager;
8+
9+
class SessionCollectorTest extends TestCase
10+
{
11+
protected function getEnvironmentSetUp($app)
12+
{
13+
$app['config']->set('debugbar.options.session.hiddens', ['secret']);
14+
parent::getEnvironmentSetUp($app);
15+
}
16+
17+
public function testItCollectsSessionVariables()
18+
{
19+
/** @var \Barryvdh\Debugbar\DataCollector\SessionCollector $collector */
20+
$collector = new SessionCollector(
21+
$this->app->make(SessionManager::class),
22+
$this->app['config']->get('debugbar.options.session.hiddens', [])
23+
);
24+
25+
$this->assertEmpty($collector->collect());
26+
27+
$this->withSession(['testVariable' => 1, 'secret' => 'testSecret'])->get('/');
28+
29+
$collected = $collector->collect();
30+
31+
$this->assertNotEmpty($collected);
32+
$this->assertArrayHasKey('secret', $collected);
33+
$this->assertArrayHasKey('testVariable', $collected);
34+
$this->assertEquals($collected['secret'], '******');
35+
$this->assertEquals($collected['testVariable'], 1);
36+
37+
$this->flushSession();
38+
$this->assertCount(0, $collector->collect());
39+
}
40+
}

tests/Mocks/MockController.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Barryvdh\Debugbar\Tests\Mocks;
4+
5+
use Illuminate\Routing\Controller;
6+
7+
class MockController extends Controller
8+
{
9+
public function show()
10+
{
11+
return view('dashboard');
12+
}
13+
}

tests/Mocks/MockMiddleware.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Barryvdh\Debugbar\Tests\Mocks;
4+
5+
use Closure;
6+
7+
class MockMiddleware
8+
{
9+
public function handle($request, Closure $next)
10+
{
11+
return $next($request);
12+
}
13+
}

tests/Mocks/MockViewComponent.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Barryvdh\Debugbar\Tests\Mocks;
4+
5+
use Illuminate\View\InvokableComponentVariable;
6+
7+
class MockViewComponent extends InvokableComponentVariable
8+
{
9+
public function render()
10+
{
11+
return view('dashboard');
12+
}
13+
}

tests/TestCase.php

+15-10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
use Barryvdh\Debugbar\ServiceProvider;
77
use Illuminate\Routing\Router;
88
use Orchestra\Testbench\TestCase as Orchestra;
9+
use Barryvdh\Debugbar\Tests\Mocks\MockController;
10+
use Barryvdh\Debugbar\Tests\Mocks\MockViewComponent;
11+
use Barryvdh\Debugbar\Tests\Mocks\MockMiddleware;
912

1013
class TestCase extends Orchestra
1114
{
@@ -55,17 +58,19 @@ protected function getEnvironmentSetUp($app)
5558
*/
5659
protected function addWebRoutes(Router $router)
5760
{
58-
$router->get('web/plain', [
59-
'uses' => function () {
60-
return 'PONG';
61-
}
62-
]);
61+
$router->get('web/plain', function () {
62+
return 'PONG';
63+
});
6364

64-
$router->get('web/html', [
65-
'uses' => function () {
66-
return '<html><head></head><body>Pong</body></html>';
67-
}
68-
]);
65+
$router->get('web/html', function () {
66+
return '<html><head></head><body>Pong</body></html>';
67+
});
68+
69+
$router->get('web/show', [ MockController::class, 'show' ]);
70+
71+
$router->get('web/view', MockViewComponent::class);
72+
73+
$router->post('web/mw')->middleware(MockMiddleware::class);
6974
}
7075

7176
/**

0 commit comments

Comments
 (0)