/
PommGuardServiceProviderTest.php
83 lines (61 loc) · 2.62 KB
/
PommGuardServiceProviderTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace GHub\Silex\PommGuard\Tests;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use GHub\Silex\PommGuard\PommGuardServiceProvider;
use GHub\Silex\Pomm\PommServiceProvider;
/**
* PommGuardServiceProvider test cases.
*
* @Grégoire Hubert <hubert.greg@gmail.com>
*/
class PommGuardServiceProviderTest extends \PHPUnit_Framework_TestCase
{
public function testRegister()
{
$app = new Application();
$app->register(new PommServiceProvider(), array(
'pomm.databases' => array('default' => array('dsn' => 'pgsql://test/test', 'name' => 'test'))
));
$app->register(new PommGuardServiceProvider());
$app['session.storage'] = $app->share(function () use ($app) {
return new MockArraySessionStorage();
});
$app->get('/login', function() use ($app) {
return 'Login page';
});
$app->get('/logout', function() use ($app) {
$app['session']->authenticate(false);
return 'Logged out successfully.';
});
$app->get('/protected', function() use ($app) {
return 'Protected page.';
})->middleware($app['pomm_guard.must_be_authenticated']);
$app->get('/not-protected', function() use ($app) {
return 'Not protected page.';
})->middleware($app['pomm_guard.must_not_be_authenticated']);
$app->get('/authenticate', function () use ($app) {
$app['session']->authenticate(true);
return 'Logged in successfully.';
});
$request = Request::create('/protected');
$response = $app->handle($request);
$this->assertTrue($response->isRedirect('/login'));
$request = Request::create('/not-protected');
$response = $app->handle($request);
$this->assertEquals('Not protected page.', $response->getContent());
$request = Request::create('/authenticate');
$response = $app->handle($request);
$this->assertEquals('Logged in successfully.', $response->getContent());
$request = Request::create('/protected');
$response = $app->handle($request);
$this->assertEquals('Protected page.', $response->getContent());
$request = Request::create('/not-protected');
$response = $app->handle($request);
$this->assertTrue($response->isRedirect('/logout'));
$request = Request::create('/logout');
$response = $app->handle($request);
$this->assertEquals('Logged out successfully.', $response->getContent());
}
}