]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/CoreBundle/WallabagCoreTestCase.php
Merge pull request #3716 from wallabag/csfixer
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / WallabagCoreTestCase.php
CommitLineData
3b815d2d
J
1<?php
2
23634d5d 3namespace Tests\Wallabag\CoreBundle;
3b815d2d 4
7ab5eb95 5use Symfony\Bundle\FrameworkBundle\Client;
6use Symfony\Bundle\FrameworkBundle\Console\Application;
3b815d2d 7use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
fdc90ceb 8use Symfony\Component\BrowserKit\Cookie;
7ab5eb95 9use Symfony\Component\Console\Input\ArrayInput;
10use Symfony\Component\Console\Output\NullOutput;
11use Wallabag\CoreBundle\Entity\Config;
12use Wallabag\UserBundle\Entity\User;
3b815d2d 13
769e19dc 14abstract class WallabagCoreTestCase extends WebTestCase
3b815d2d 15{
7ab5eb95 16 /**
17 * @var Client|null
18 */
3b815d2d
J
19 private $client = null;
20
3b815d2d
J
21 public function setUp()
22 {
d5027625
JB
23 parent::setUp();
24
3b815d2d
J
25 $this->client = static::createClient();
26 }
27
300f293c
KD
28 public function getNewClient()
29 {
30 return $this->client = static::createClient();
31 }
32
f808b016
JB
33 public function getClient()
34 {
35 return $this->client;
36 }
37
7ab5eb95 38 public function resetDatabase(Client $client)
39 {
40 $application = new Application($client->getKernel());
41 $application->setAutoExit(false);
42
43 $application->run(new ArrayInput([
44 'command' => 'doctrine:schema:drop',
45 '--no-interaction' => true,
46 '--force' => true,
47 '--env' => 'test',
48 ]), new NullOutput());
49
50 $application->run(new ArrayInput([
51 'command' => 'doctrine:schema:create',
52 '--no-interaction' => true,
53 '--env' => 'test',
54 ]), new NullOutput());
55
56 $application->run(new ArrayInput([
57 'command' => 'doctrine:fixtures:load',
58 '--no-interaction' => true,
59 '--env' => 'test',
60 ]), new NullOutput());
61
62 /*
63 * Recreate client to avoid error:
64 *
65 * [Doctrine\DBAL\ConnectionException]
66 * Transaction commit failed because the transaction has been marked for rollback only.
67 */
68 $this->client = static::createClient();
69 }
70
71 public function getEntityManager()
72 {
73 return $this->client->getContainer()->get('doctrine.orm.entity_manager');
74 }
75
fdc90ceb
JB
76 /**
77 * Login a user without making a HTTP request.
78 * If we make a HTTP request we lose ability to mock service in the container.
79 *
80 * @param string $username User to log in
81 */
eb3bd7ef 82 public function logInAs($username)
fdc90ceb
JB
83 {
84 $container = $this->client->getContainer();
85 $session = $container->get('session');
86
87 $userManager = $container->get('fos_user.user_manager');
88 $loginManager = $container->get('fos_user.security.login_manager');
89 $firewallName = $container->getParameter('fos_user.firewall_name');
90
f808b016 91 $user = $userManager->findUserBy(['username' => $username]);
7ab5eb95 92 $loginManager->logInUser($firewallName, $user);
fdc90ceb 93
f808b016 94 $session->set('_security_' . $firewallName, serialize($container->get('security.token_storage')->getToken()));
fdc90ceb
JB
95 $session->save();
96
97 $cookie = new Cookie($session->getName(), $session->getId());
98 $this->client->getCookieJar()->set($cookie);
99 }
100
101 /**
102 * Instead of `logInAs` this method use a HTTP request to log in the user.
103 * Could be better for some tests.
104 *
105 * @param string $username User to log in
106 */
107 public function logInAsUsingHttp($username)
3b815d2d
J
108 {
109 $crawler = $this->client->request('GET', '/login');
110 $form = $crawler->filter('button[type=submit]')->form();
4094ea47 111 $data = [
eb3bd7ef 112 '_username' => $username,
d9085c63 113 '_password' => 'mypassword',
4094ea47 114 ];
3b815d2d
J
115
116 $this->client->submit($form, $data);
117 }
78833672
JB
118
119 /**
7ab5eb95 120 * Return the user of the logged in user.
78833672
JB
121 * You should be sure that you called `logInAs` before.
122 *
7ab5eb95 123 * @return User
78833672 124 */
7ab5eb95 125 public function getLoggedInUser()
78833672
JB
126 {
127 $token = static::$kernel->getContainer()->get('security.token_storage')->getToken();
128
129 if (null !== $token) {
7ab5eb95 130 return $token->getUser();
78833672
JB
131 }
132
133 throw new \RuntimeException('No logged in User.');
134 }
0e0102b6 135
7ab5eb95 136 /**
137 * Return the user id of the logged in user.
138 * You should be sure that you called `logInAs` before.
139 *
140 * @return int
141 */
142 public function getLoggedInUserId()
143 {
144 return $this->getLoggedInUser()->getId();
145 }
146
147 public function useTheme($theme)
148 {
149 $config = $this->getEntityManager()->getRepository(Config::class)->findOneByUser($this->getLoggedInUser());
150 $config->setTheme($theme);
151 $this->getEntityManager()->persist($config);
152 $this->getEntityManager()->flush();
153 }
154
0e0102b6
JB
155 /**
156 * Check if Redis is installed.
084fb0d3 157 * If not, mark test as skip.
0e0102b6
JB
158 */
159 protected function checkRedis()
160 {
161 try {
162 $this->client->getContainer()->get('wallabag_core.redis.client')->connect();
163 } catch (\Exception $e) {
a0a4ce31 164 $this->markTestSkipped('Redis is not installed/activated');
0e0102b6
JB
165 }
166 }
3b815d2d 167}