]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/AnnotationBundle/WallabagAnnotationTestCase.php
Merge pull request #3597 from wallabag/fix-tests
[github/wallabag/wallabag.git] / tests / Wallabag / AnnotationBundle / WallabagAnnotationTestCase.php
CommitLineData
f38e03dc
TC
1<?php
2
23634d5d 3namespace Tests\Wallabag\AnnotationBundle;
f38e03dc
TC
4
5use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6use Symfony\Component\BrowserKit\Cookie;
7
4dc87223 8abstract class WallabagAnnotationTestCase extends WebTestCase
f38e03dc
TC
9{
10 /**
0c271b9e 11 * @var \Symfony\Bundle\FrameworkBundle\Client
f38e03dc
TC
12 */
13 protected $client = null;
14
15 /**
16 * @var \FOS\UserBundle\Model\UserInterface
17 */
18 protected $user;
19
20 public function setUp()
21 {
22 $this->client = $this->createAuthorizedClient();
23 }
24
25 public function logInAs($username)
26 {
27 $crawler = $this->client->request('GET', '/login');
28 $form = $crawler->filter('button[type=submit]')->form();
4094ea47 29 $data = [
f38e03dc
TC
30 '_username' => $username,
31 '_password' => 'mypassword',
4094ea47 32 ];
f38e03dc
TC
33
34 $this->client->submit($form, $data);
35 }
36
37 /**
0c271b9e 38 * @return \Symfony\Bundle\FrameworkBundle\Client
f38e03dc
TC
39 */
40 protected function createAuthorizedClient()
41 {
42 $client = static::createClient();
43 $container = $client->getContainer();
44
45 /** @var $userManager \FOS\UserBundle\Doctrine\UserManager */
46 $userManager = $container->get('fos_user.user_manager');
47 /** @var $loginManager \FOS\UserBundle\Security\LoginManager */
48 $loginManager = $container->get('fos_user.security.login_manager');
49 $firewallName = $container->getParameter('fos_user.firewall_name');
50
4094ea47 51 $this->user = $userManager->findUserBy(['username' => 'admin']);
0c271b9e 52 $loginManager->logInUser($firewallName, $this->user);
f38e03dc
TC
53
54 // save the login token into the session and put it in a cookie
f808b016 55 $container->get('session')->set('_security_' . $firewallName, serialize($container->get('security.token_storage')->getToken()));
f38e03dc
TC
56 $container->get('session')->save();
57
58 $session = $container->get('session');
59 $client->getCookieJar()->set(new Cookie($session->getName(), $session->getId()));
60
61 return $client;
62 }
63}