]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/ApiBundle/WallabagApiTestCase.php
Fix tests
[github/wallabag/wallabag.git] / tests / Wallabag / ApiBundle / WallabagApiTestCase.php
CommitLineData
fcb1fba5
NL
1<?php
2
23634d5d 3namespace Tests\Wallabag\ApiBundle;
fcb1fba5
NL
4
5use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6use Symfony\Component\BrowserKit\Cookie;
7
8a493541 8abstract class WallabagApiTestCase extends WebTestCase
fcb1fba5
NL
9{
10 /**
7ab5eb95 11 * @var \Symfony\Bundle\FrameworkBundle\Client
fcb1fba5
NL
12 */
13 protected $client = null;
14
4059a061
JB
15 /**
16 * @var \FOS\UserBundle\Model\UserInterface
17 */
18 protected $user;
19
fcb1fba5
NL
20 public function setUp()
21 {
22 $this->client = $this->createAuthorizedClient();
23 }
24
25 /**
7ab5eb95 26 * @return \Symfony\Bundle\FrameworkBundle\Client
fcb1fba5
NL
27 */
28 protected function createAuthorizedClient()
29 {
30 $client = static::createClient();
31 $container = $client->getContainer();
32
fcb1fba5 33 /** @var $userManager \FOS\UserBundle\Doctrine\UserManager */
115de64e 34 $userManager = $container->get('fos_user.user_manager.test');
fcb1fba5 35 /** @var $loginManager \FOS\UserBundle\Security\LoginManager */
115de64e 36 $loginManager = $container->get('fos_user.security.login_manager.test');
fcb1fba5
NL
37 $firewallName = $container->getParameter('fos_user.firewall_name');
38
4094ea47 39 $this->user = $userManager->findUserBy(['username' => 'admin']);
5709ecb3 40 $loginManager->logInUser($firewallName, $this->user);
fcb1fba5
NL
41
42 // save the login token into the session and put it in a cookie
f808b016 43 $container->get('session')->set('_security_' . $firewallName, serialize($container->get('security.token_storage')->getToken()));
fcb1fba5 44 $container->get('session')->save();
18f8f32f
JB
45
46 $session = $container->get('session');
fcb1fba5
NL
47 $client->getCookieJar()->set(new Cookie($session->getName(), $session->getId()));
48
49 return $client;
50 }
8f2038e5
JB
51
52 /**
53 * Return the ID for the user admin.
54 * Used because on heavy testing we don't want to re-create the database on each run.
55 * Which means "admin" user won't have id 1 all the time.
56 *
57 * @param string $username
58 *
59 * @return int
60 */
61 public function getUserId($username = 'admin')
62 {
63 return $this->client
64 ->getContainer()
65 ->get('doctrine.orm.entity_manager')
66 ->getRepository('WallabagUserBundle:User')
67 ->findOneByUserName($username)
68 ->getId();
69 }
fcb1fba5 70}