]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/AnnotationBundle/WallabagAnnotationTestCase.php
Fold migrations & fixtures command on Travis
[github/wallabag/wallabag.git] / tests / Wallabag / AnnotationBundle / WallabagAnnotationTestCase.php
1 <?php
2
3 namespace Tests\Wallabag\AnnotationBundle;
4
5 use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6 use Symfony\Component\BrowserKit\Cookie;
7
8 abstract class WallabagAnnotationTestCase extends WebTestCase
9 {
10 /**
11 * @var \Symfony\Bundle\FrameworkBundle\Client
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();
29 $data = [
30 '_username' => $username,
31 '_password' => 'mypassword',
32 ];
33
34 $this->client->submit($form, $data);
35 }
36
37 /**
38 * @return \Symfony\Bundle\FrameworkBundle\Client
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
51 $this->user = $userManager->findUserBy(['username' => 'admin']);
52 $loginManager->logInUser($firewallName, $this->user);
53
54 // save the login token into the session and put it in a cookie
55 $container->get('session')->set('_security_'.$firewallName, serialize($container->get('security.token_storage')->getToken()));
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 }