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