]>
Commit | Line | Data |
---|---|---|
3b815d2d J |
1 | <?php |
2 | ||
23634d5d | 3 | namespace Tests\Wallabag\CoreBundle; |
3b815d2d J |
4 | |
5 | use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | |
fdc90ceb | 6 | use Symfony\Component\BrowserKit\Cookie; |
3b815d2d | 7 | |
769e19dc | 8 | abstract class WallabagCoreTestCase extends WebTestCase |
3b815d2d J |
9 | { |
10 | private $client = null; | |
11 | ||
12 | public function getClient() | |
13 | { | |
14 | return $this->client; | |
15 | } | |
16 | ||
17 | public function setUp() | |
18 | { | |
d5027625 JB |
19 | parent::setUp(); |
20 | ||
3b815d2d J |
21 | $this->client = static::createClient(); |
22 | } | |
23 | ||
fdc90ceb JB |
24 | /** |
25 | * Login a user without making a HTTP request. | |
26 | * If we make a HTTP request we lose ability to mock service in the container. | |
27 | * | |
28 | * @param string $username User to log in | |
29 | */ | |
eb3bd7ef | 30 | public function logInAs($username) |
fdc90ceb JB |
31 | { |
32 | $container = $this->client->getContainer(); | |
33 | $session = $container->get('session'); | |
34 | ||
35 | $userManager = $container->get('fos_user.user_manager'); | |
36 | $loginManager = $container->get('fos_user.security.login_manager'); | |
37 | $firewallName = $container->getParameter('fos_user.firewall_name'); | |
38 | ||
39 | $user = $userManager->findUserBy(array('username' => $username)); | |
40 | $loginManager->loginUser($firewallName, $user); | |
41 | ||
42 | $session->set('_security_'.$firewallName, serialize($container->get('security.token_storage')->getToken())); | |
43 | $session->save(); | |
44 | ||
45 | $cookie = new Cookie($session->getName(), $session->getId()); | |
46 | $this->client->getCookieJar()->set($cookie); | |
47 | } | |
48 | ||
49 | /** | |
50 | * Instead of `logInAs` this method use a HTTP request to log in the user. | |
51 | * Could be better for some tests. | |
52 | * | |
53 | * @param string $username User to log in | |
54 | */ | |
55 | public function logInAsUsingHttp($username) | |
3b815d2d J |
56 | { |
57 | $crawler = $this->client->request('GET', '/login'); | |
58 | $form = $crawler->filter('button[type=submit]')->form(); | |
4094ea47 | 59 | $data = [ |
eb3bd7ef | 60 | '_username' => $username, |
d9085c63 | 61 | '_password' => 'mypassword', |
4094ea47 | 62 | ]; |
3b815d2d J |
63 | |
64 | $this->client->submit($form, $data); | |
65 | } | |
78833672 JB |
66 | |
67 | /** | |
68 | * Return the user id of the logged in user. | |
69 | * You should be sure that you called `logInAs` before. | |
70 | * | |
71 | * @return int | |
72 | */ | |
73 | public function getLoggedInUserId() | |
74 | { | |
75 | $token = static::$kernel->getContainer()->get('security.token_storage')->getToken(); | |
76 | ||
77 | if (null !== $token) { | |
78 | return $token->getUser()->getId(); | |
79 | } | |
80 | ||
81 | throw new \RuntimeException('No logged in User.'); | |
82 | } | |
0e0102b6 JB |
83 | |
84 | /** | |
85 | * Check if Redis is installed. | |
084fb0d3 | 86 | * If not, mark test as skip. |
0e0102b6 JB |
87 | */ |
88 | protected function checkRedis() | |
89 | { | |
90 | try { | |
91 | $this->client->getContainer()->get('wallabag_core.redis.client')->connect(); | |
92 | } catch (\Exception $e) { | |
93 | $this->markTestSkipped( | |
94 | 'Redis is not installed/activated' | |
95 | ); | |
96 | } | |
97 | } | |
3b815d2d | 98 | } |