]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/ApiBundle/WallabagApiTestCase.php
a0f2f889513e6f58a481ce01935db65ed11d2118
[github/wallabag/wallabag.git] / tests / Wallabag / ApiBundle / WallabagApiTestCase.php
1 <?php
2
3 namespace Tests\Wallabag\ApiBundle;
4
5 use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
6 use Symfony\Component\BrowserKit\Cookie;
7
8 abstract class WallabagApiTestCase 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 /**
26 * @return \Symfony\Bundle\FrameworkBundle\Client
27 */
28 protected function createAuthorizedClient()
29 {
30 $client = static::createClient();
31 $container = $client->getContainer();
32
33 /** @var $userManager \FOS\UserBundle\Doctrine\UserManager */
34 $userManager = $container->get('fos_user.user_manager.test');
35 /** @var $loginManager \FOS\UserBundle\Security\LoginManager */
36 $loginManager = $container->get('fos_user.security.login_manager.test');
37 $firewallName = $container->getParameter('fos_user.firewall_name');
38
39 $this->user = $userManager->findUserBy(['username' => 'admin']);
40 $loginManager->logInUser($firewallName, $this->user);
41
42 // save the login token into the session and put it in a cookie
43 $container->get('session')->set('_security_' . $firewallName, serialize($container->get('security.token_storage')->getToken()));
44 $container->get('session')->save();
45
46 $session = $container->get('session');
47 $client->getCookieJar()->set(new Cookie($session->getName(), $session->getId()));
48
49 return $client;
50 }
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 }
70 }